9
9
from json import JSONDecodeError , loads as json_loads
10
10
from typing import Any , Literal , Union , cast , overload
11
11
12
- from anthropic .types import DocumentBlockParam
12
+ from anthropic .types import DocumentBlockParam , ThinkingBlock , ThinkingBlockParam
13
13
from httpx import AsyncClient as AsyncHTTPClient
14
14
from typing_extensions import assert_never
15
15
27
27
RetryPromptPart ,
28
28
SystemPromptPart ,
29
29
TextPart ,
30
+ ThinkingPart ,
30
31
ToolCallPart ,
31
32
ToolReturnPart ,
32
33
UserPromptPart ,
@@ -227,13 +228,14 @@ async def _messages_create(
227
228
228
229
try :
229
230
return await self .client .messages .create (
230
- max_tokens = model_settings .get ('max_tokens' , 1024 ),
231
+ max_tokens = model_settings .get ('max_tokens' , 2048 ),
231
232
system = system_prompt or NOT_GIVEN ,
232
233
messages = anthropic_messages ,
233
234
model = self ._model_name ,
234
235
tools = tools or NOT_GIVEN ,
235
236
tool_choice = tool_choice or NOT_GIVEN ,
236
237
stream = stream ,
238
+ thinking = {'budget_tokens' : 1024 , 'type' : 'enabled' },
237
239
temperature = model_settings .get ('temperature' , NOT_GIVEN ),
238
240
top_p = model_settings .get ('top_p' , NOT_GIVEN ),
239
241
timeout = model_settings .get ('timeout' , NOT_GIVEN ),
@@ -250,6 +252,8 @@ def _process_response(self, response: AnthropicMessage) -> ModelResponse:
250
252
for item in response .content :
251
253
if isinstance (item , TextBlock ):
252
254
items .append (TextPart (content = item .text ))
255
+ elif isinstance (item , ThinkingBlock ):
256
+ items .append (ThinkingPart (content = item .thinking , signature = item .signature ))
253
257
else :
254
258
assert isinstance (item , ToolUseBlock ), 'unexpected item type'
255
259
items .append (
@@ -316,10 +320,17 @@ async def _map_message(self, messages: list[ModelMessage]) -> tuple[str, list[Me
316
320
user_content_params .append (retry_param )
317
321
anthropic_messages .append (MessageParam (role = 'user' , content = user_content_params ))
318
322
elif isinstance (m , ModelResponse ):
319
- assistant_content_params : list [TextBlockParam | ToolUseBlockParam ] = []
323
+ assistant_content_params : list [TextBlockParam | ToolUseBlockParam | ThinkingBlockParam ] = []
320
324
for response_part in m .parts :
321
325
if isinstance (response_part , TextPart ):
322
326
assistant_content_params .append (TextBlockParam (text = response_part .content , type = 'text' ))
327
+ elif isinstance (response_part , ThinkingPart ):
328
+ assert response_part .signature is not None , 'Thinking part must have a signature'
329
+ assistant_content_params .append (
330
+ ThinkingBlockParam (
331
+ thinking = response_part .content , signature = response_part .signature , type = 'thinking'
332
+ )
333
+ )
323
334
else :
324
335
tool_use_block_param = ToolUseBlockParam (
325
336
id = _guard_tool_call_id (t = response_part , model_source = 'Anthropic' ),
0 commit comments