import copyimport requests class BoltThinkingBudgetClient: def __init__(self, base_url, api_key="EMPTY"): self.base_url = base_url.rstrip("/") def _post(self, payload): response = requests.post( f"{self.base_url}/chat/completions", json=payload, timeout=300, ) response.raise_for_status() return response.json() def chat_completion( self, model, messages, max_thinking_budget=512, max_tokens=4096, tools=None, temperature=1.0, top_p=0.95, seed=None, ): if max_tokens <= max_thinking_budget: raise ValueError("max_tokens must be larger than max_thinking_budget") original_messages = copy.deepcopy(messages) sampling = {"temperature": temperature, "top_p": top_p} if seed is not None: sampling["seed"] = seed first_payload = { "model": model, "skip_special_tokens": False, "messages": original_messages, "max_tokens": max_thinking_budget, "chat_template_kwargs": {"enable_thinking": True}, **sampling, } if tools is not None: first_payload["tools"] = tools first_payload["tool_choice"] = "auto" first = self._post(first_payload) choice = first["choices"][0] message = choice["message"] reasoning = message.get("reasoning_content") or message.get("reasoning") or "" partial_answer = message.get("content") or "" tool_calls = message.get("tool_calls") or [] if choice.get("finish_reason") == "stop" and (partial_answer or tool_calls): return { "reasoning_content": reasoning, "content": partial_answer, "tool_calls": tool_calls, "finish_reason": choice.get("finish_reason"), "budget_forced": False, } used_tokens = first.get("usage", {}).get("completion_tokens", max_thinking_budget) remaining_tokens = max_tokens - used_tokens if remaining_tokens <= 0: raise ValueError("Increase max_tokens or lower max_thinking_budget") continuation_messages = copy.deepcopy(original_messages) continuation_messages.append( { "role": "assistant", "reasoning_content": reasoning, "content": partial_answer, } ) second_payload = { "model": model, "skip_special_tokens": False, "messages": continuation_messages, "max_tokens": remaining_tokens, "add_generation_prompt": False, "continue_final_message": True, "chat_template_kwargs": { "enable_thinking": True, "preserve_thinking": True, }, **sampling, } if tools is not None: second_payload["tools"] = tools second_payload["tool_choice"] = "auto" second = self._post(second_payload) second_choice = second["choices"][0] second_message = second_choice["message"] return { "reasoning_content": reasoning, "content": partial_answer + (second_message.get("content") or ""), "tool_calls": second_message.get("tool_calls") or [], "finish_reason": second_choice.get("finish_reason"), "budget_forced": True, }