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 , deprecated
15
15
27
27
RetryPromptPart ,
28
28
SystemPromptPart ,
29
29
TextPart ,
30
+ ThinkingPart ,
30
31
ToolCallPart ,
31
32
ToolReturnPart ,
32
33
UserPromptPart ,
@@ -256,13 +257,14 @@ async def _messages_create(
256
257
257
258
try :
258
259
return await self .client .messages .create (
259
- max_tokens = model_settings .get ('max_tokens' , 1024 ),
260
+ max_tokens = model_settings .get ('max_tokens' , 2048 ),
260
261
system = system_prompt or NOT_GIVEN ,
261
262
messages = anthropic_messages ,
262
263
model = self ._model_name ,
263
264
tools = tools or NOT_GIVEN ,
264
265
tool_choice = tool_choice or NOT_GIVEN ,
265
266
stream = stream ,
267
+ thinking = {'budget_tokens' : 1024 , 'type' : 'enabled' },
266
268
temperature = model_settings .get ('temperature' , NOT_GIVEN ),
267
269
top_p = model_settings .get ('top_p' , NOT_GIVEN ),
268
270
timeout = model_settings .get ('timeout' , NOT_GIVEN ),
@@ -279,6 +281,8 @@ def _process_response(self, response: AnthropicMessage) -> ModelResponse:
279
281
for item in response .content :
280
282
if isinstance (item , TextBlock ):
281
283
items .append (TextPart (content = item .text ))
284
+ elif isinstance (item , ThinkingBlock ):
285
+ items .append (ThinkingPart (content = item .thinking , signature = item .signature ))
282
286
else :
283
287
assert isinstance (item , ToolUseBlock ), 'unexpected item type'
284
288
items .append (
@@ -345,10 +349,17 @@ async def _map_message(self, messages: list[ModelMessage]) -> tuple[str, list[Me
345
349
user_content_params .append (retry_param )
346
350
anthropic_messages .append (MessageParam (role = 'user' , content = user_content_params ))
347
351
elif isinstance (m , ModelResponse ):
348
- assistant_content_params : list [TextBlockParam | ToolUseBlockParam ] = []
352
+ assistant_content_params : list [TextBlockParam | ToolUseBlockParam | ThinkingBlockParam ] = []
349
353
for response_part in m .parts :
350
354
if isinstance (response_part , TextPart ):
351
355
assistant_content_params .append (TextBlockParam (text = response_part .content , type = 'text' ))
356
+ elif isinstance (response_part , ThinkingPart ):
357
+ assert response_part .signature is not None , 'Thinking part must have a signature'
358
+ assistant_content_params .append (
359
+ ThinkingBlockParam (
360
+ thinking = response_part .content , signature = response_part .signature , type = 'thinking'
361
+ )
362
+ )
352
363
else :
353
364
tool_use_block_param = ToolUseBlockParam (
354
365
id = _guard_tool_call_id (t = response_part , model_source = 'Anthropic' ),
0 commit comments