2
2
import asyncio
3
3
from anthropic import Anthropic , AsyncAnthropic
4
4
5
- from lunary .anthrophic import monitor
5
+ import lunary
6
+ from lunary .anthrophic import monitor , parse_message
6
7
7
8
def sync_non_streaming ():
8
9
client = Anthropic ()
@@ -228,6 +229,65 @@ async def async_tool_calls():
228
229
print (f"snapshot: { event .snapshot } " )
229
230
230
231
232
+ def reconcilliation_tool_calls ():
233
+ from anthropic import Anthropic
234
+ from anthropic .types import ToolParam , MessageParam
235
+
236
+ thread = lunary .open_thread ()
237
+ client = monitor (Anthropic ())
238
+
239
+ user_message : MessageParam = {
240
+ "role" : "user" ,
241
+ "content" : "What is the weather in San Francisco, California?" ,
242
+ }
243
+ tools : list [ToolParam ] = [
244
+ {
245
+ "name" : "get_weather" ,
246
+ "description" : "Get the weather for a specific location" ,
247
+ "input_schema" : {
248
+ "type" : "object" ,
249
+ "properties" : {"location" : {"type" : "string" }},
250
+ },
251
+ }
252
+ ]
253
+
254
+ message_id = thread .track_message (user_message )
255
+
256
+ with lunary .parent (message_id ):
257
+ message = client .messages .create (
258
+ model = "claude-3-opus-20240229" ,
259
+ max_tokens = 1024 ,
260
+ messages = [user_message ],
261
+ tools = tools ,
262
+ )
263
+ print (f"Initial response: { message .model_dump_json (indent = 2 )} " )
264
+
265
+ assert message .stop_reason == "tool_use"
266
+
267
+ tool = next (c for c in message .content if c .type == "tool_use" )
268
+ response = client .messages .create (
269
+ model = "claude-3-opus-20240229" ,
270
+ max_tokens = 1024 ,
271
+ messages = [
272
+ user_message ,
273
+ {"role" : message .role , "content" : message .content },
274
+ {
275
+ "role" : "user" ,
276
+ "content" : [
277
+ {
278
+ "type" : "tool_result" ,
279
+ "tool_use_id" : tool .id ,
280
+ "content" : [{"type" : "text" , "text" : "The weather is 73f" }],
281
+ }
282
+ ],
283
+ },
284
+ ],
285
+ tools = tools ,
286
+ )
287
+ print (f"\n Final response: { response .model_dump_json (indent = 2 )} " )
288
+
289
+
290
+
231
291
# sync_non_streaming()
232
292
# asyncio.run(async_non_streaming())
233
293
@@ -243,3 +303,5 @@ async def async_tool_calls():
243
303
244
304
# tool_calls()
245
305
# asyncio.run(async_tool_calls())
306
+
307
+ reconcilliation_tool_calls ()
0 commit comments