2828
2929DEEPSEEK_CHAT_TEMPLATE_NO_THINK = "{% if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif %}{% set ns = namespace(is_first=false, is_tool=false, is_output_first=true, system_prompt='') %}{%- for message in messages %}{%- if message['role'] == 'system' %}{% set ns.system_prompt = message['content'] %}{%- endif %}{%- endfor %}{{bos_token}}{{ns.system_prompt}}{%- for message in messages %}{%- if message['role'] == 'user' %}{%- set ns.is_tool = false -%}{{'<|User|>' + message['content']}}{%- endif %}{%- if message['role'] == 'assistant' and message['content'] is none %}{%- set ns.is_tool = false -%}{%- for tool in message['tool_calls']%}{%- if not ns.is_first %}{{'<|Assistant|><|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\\ n' + '```json' + '\\ n' + tool['function']['arguments'] + '\\ n' + '```' + '<|tool▁call▁end|>'}}{%- set ns.is_first = true -%}{%- else %}{{'\\ n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\\ n' + '```json' + '\\ n' + tool['function']['arguments'] + '\\ n' + '```' + '<|tool▁call▁end|>'}}{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}{%- endif %}{%- endfor %}{%- endif %}{%- if message['role'] == 'assistant' and message['content'] is not none %}{%- if ns.is_tool %}{{'<|tool▁outputs▁end|>' + message['content'] + '<|end▁of▁sentence|>'}}{%- set ns.is_tool = false -%}{%- else %}{% set content = message['content'] %}{% if '</think>' in content %}{% set content = content.split('</think>')[-1] %}{% endif %}{{'<|Assistant|>' + content + '<|end▁of▁sentence|>'}}{%- endif %}{%- endif %}{%- if message['role'] == 'tool' %}{%- set ns.is_tool = true -%}{%- if ns.is_output_first %}{{'<|tool▁outputs▁begin|><|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}{%- set ns.is_output_first = false %}{%- else %}{{'\\ n<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}{%- endif %}{%- endif %}{%- endfor -%}{% if ns.is_tool %}{{'<|tool▁outputs▁end|>'}}{% endif %}{% if add_generation_prompt and not ns.is_tool %}{{'<|Assistant|><think>\\ n</think>\\ n'}}{% endif %}"
3030
31+ CLAUDE_MODELS = [
32+ "claude-3.7-sonnet" ,
33+ "claude-4-sonnet" ,
34+ "claude-4-opus" ,
35+ "claude-sonnet-4.5" ,
36+ "claude-haiku-4.5" ,
37+ "claude-opus-4.5" ,
38+ "claude-opus-4.6" ,
39+ "claude-sonnet-4.6" ,
40+ ]
41+
42+ OPENAI_MODELS = [
43+ "o1" ,
44+ "o1-mini" ,
45+ "o1-preview" ,
46+ "o3-mini" ,
47+ "o4-mini" ,
48+ "o3" ,
49+ "gpt-5.1" ,
50+ "gpt-5.2" ,
51+ "gpt-5" ,
52+ "gpt-5-mini" ,
53+ "gpt-5-nano" ,
54+ ]
55+
56+ GEMINI_MODELS = [
57+ "gemini-2.5-pro" ,
58+ "gemini-3-pro-preview" ,
59+ ]
60+
3161
3262class ReasoningAgent (tales .Agent ):
3363
@@ -117,42 +147,25 @@ def act(self, obs, reward, done, infos):
117147 "stream" : True , # Should prevent openai.APITimeoutError
118148 }
119149 if isinstance (self .reasoning_effort , int ):
120- if self .llm in [ "claude-3.7-sonnet" ] :
150+ if self .llm in CLAUDE_MODELS :
121151 llm_kwargs ["thinking_budget" ] = self .reasoning_effort
122152 else :
123153 llm_kwargs ["max_tokens" ] = self .reasoning_effort
124154
125- elif self .llm in [
126- "o1" ,
127- "o1-preview" ,
128- "o3-mini" ,
129- "o4-mini" ,
130- "o3" ,
131- "gpt-5" ,
132- "gpt-5-mini" ,
133- "gpt-5-nano" ,
134- ]:
155+ elif self .llm in OPENAI_MODELS :
135156 llm_kwargs ["reasoning_effort" ] = self .reasoning_effort
136157
137- if self .llm in [
138- "o1" ,
139- "o1-mini" ,
140- "o1-preview" ,
141- "o3-mini" ,
142- "o4-mini" ,
143- "o3" ,
144- "claude-3.7-sonnet" ,
145- "gpt-5" ,
146- "gpt-5-mini" ,
147- "gpt-5-nano" ,
148- ]:
158+ elif self .llm in CLAUDE_MODELS :
159+ llm_kwargs ["thinking_effort" ] = self .reasoning_effort
160+
161+ if self .llm in OPENAI_MODELS + CLAUDE_MODELS :
149162 # For these models, we cannot set the temperature.
150163 llm_kwargs .pop ("temperature" )
151164
152165 if self .llm in ["o3-mini" ]:
153166 llm_kwargs .pop ("stream" )
154167
155- if self .llm in [ "claude-3.7-sonnet" ] :
168+ if self .llm in CLAUDE_MODELS :
156169 llm_kwargs ["thinking" ] = 1
157170 llm_kwargs .pop ("seed" )
158171
@@ -238,7 +251,7 @@ def act(self, obs, reward, done, infos):
238251 # Extract the action part from the response.
239252 action = action [reasoning_end :].strip ()
240253
241- elif self .llm in [ "claude-3.7-sonnet" ] :
254+ elif self .llm in CLAUDE_MODELS :
242255 # Extract the thinking part from the response JSON.
243256 thinking = "" .join (
244257 [item .get ("thinking" , "" ) for item in response .json ()["content" ]]
@@ -253,24 +266,14 @@ def act(self, obs, reward, done, infos):
253266 "response" : response_text ,
254267 }
255268
256- if self .llm in [ "gemini-2.5-pro-preview-03-25" , "gemini-2.5-pro-preview-05-06" ] :
269+ if self .llm in GEMINI_MODELS :
257270 stats ["nb_tokens_prompt" ] = response .usage ().input
258271 stats ["nb_tokens_thinking" ] = response .usage ().details .get (
259272 "thoughtsTokenCount" , 0
260273 )
261274 stats ["nb_tokens_response" ] = response .usage ().output
262275
263- elif self .llm in [
264- "o1" ,
265- "o1-mini" ,
266- "o1-preview" ,
267- "o3-mini" ,
268- "o4-mini" ,
269- "o3" ,
270- "gpt-5" ,
271- "gpt-5-mini" ,
272- "gpt-5-nano" ,
273- ]:
276+ elif self .llm in OPENAI_MODELS :
274277 # stats["nb_tokens_prompt"] = self.token_counter(messages=messages),
275278 # stats["nb_tokens_response"] = self.token_counter(text=response_text)
276279 stats ["nb_tokens_prompt" ] = response .usage ().input
@@ -281,8 +284,17 @@ def act(self, obs, reward, done, infos):
281284 "completion_tokens_details"
282285 ]["reasoning_tokens" ]
283286
287+ elif self .llm in CLAUDE_MODELS :
288+ stats ["nb_tokens_prompt" ] = self .token_counter (messages = messages )
289+ stats ["nb_tokens_response" ] = self .token_counter (text = response_text )
290+ stats ["nb_tokens_thinking" ] = 0
291+ if thinking :
292+ stats ["nb_tokens_thinking" ] = (
293+ response .usage ().output - self .token_counter (text = response_text )
294+ )
295+
284296 else :
285- stats ["nb_tokens_prompt" ] = ( self .token_counter (messages = messages ), )
297+ stats ["nb_tokens_prompt" ] = self .token_counter (messages = messages )
286298 stats ["nb_tokens_thinking" ] = (
287299 self .token_counter (text = thinking ) if thinking else 0
288300 )
0 commit comments