from transformers import AutoTokenizerfrom vllm import LLM, SamplingParams model_id = "enver/rootformer-code-v19.2-qwen38-nwp"tok = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)llm = LLM(model=model_id, quantization="fp8", dtype="bfloat16", language_model_only=True, max_model_len=12288, trust_remote_code=True) msgs = [ {"role": "system", "content": "You are an expert Python 3 software engineer. Output clean, bug-free Python 3 code inside ```python ... ```."}, {"role": "user", "content": "Implement an O(1) LRU cache with TTL expiration in Python."},]prompt = tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True, enable_thinking=True, reasoning_effort="xhigh") # Phase 1: Bounded <think> (prevents infinite dithering truncation!)p1 = llm.generate([prompt], SamplingParams(temperature=0.0, max_tokens=2000, stop=["</think>"], include_stop_str_in_output=True))[0].outputs[0].textclosed = (p1.rsplit("</think>", 1)[0] + "</think>\n\n") if "</think>" in p1 else (p1.rstrip() + "\n\nI have finished the analysis. Emitting final clean code now.\n</think>\n\n") # Phase 2: Guaranteed clean post-</think> code emission (uses vLLM prefix cache!)code_reply = llm.generate([prompt + closed], SamplingParams(temperature=0.0, max_tokens=2048))[0].outputs[0].textprint(code_reply)