@@ -88,31 +88,6 @@ def mock_anthropic_response_with_cached_tokens():
8888 )
8989
9090
91- @pytest .fixture
92- def mock_anthropic_response_with_tool_use ():
93- return Message (
94- id = "msg_123" ,
95- type = "message" ,
96- role = "assistant" ,
97- content = [
98- {"type" : "text" , "text" : "I'll help you with that." },
99- {
100- "type" : "tool_use" ,
101- "id" : "tool_1" ,
102- "name" : "get_weather" ,
103- "input" : {"location" : "New York" },
104- },
105- ],
106- model = "claude-3-opus-20240229" ,
107- usage = Usage (
108- input_tokens = 20 ,
109- output_tokens = 10 ,
110- ),
111- stop_reason = "end_turn" ,
112- stop_sequence = None ,
113- )
114-
115-
11691def test_basic_completion (mock_client , mock_anthropic_response ):
11792 with patch (
11893 "anthropic.resources.Messages.create" , return_value = mock_anthropic_response
@@ -461,20 +436,41 @@ def test_cached_tokens(mock_client, mock_anthropic_response_with_cached_tokens):
461436 assert isinstance (props ["$ai_latency" ], float )
462437
463438
464- def test_tool_use_response (mock_client , mock_anthropic_response_with_tool_use ):
439+ def test_tool_definition (mock_client , mock_anthropic_response ):
465440 with patch (
466441 "anthropic.resources.Messages.create" ,
467- return_value = mock_anthropic_response_with_tool_use ,
442+ return_value = mock_anthropic_response ,
468443 ):
469444 client = Anthropic (api_key = "test-key" , posthog_client = mock_client )
445+
446+ tools = [
447+ {
448+ "name" : "get_weather" ,
449+ "description" : "Get the current weather for a specific location" ,
450+ "input_schema" : {
451+ "type" : "object" ,
452+ "properties" : {
453+ "location" : {
454+ "type" : "string" ,
455+ "description" : "The city or location name to get weather for"
456+ }
457+ },
458+ "required" : ["location" ]
459+ }
460+ }
461+ ]
462+
470463 response = client .messages .create (
471- model = "claude-3-opus-20240229" ,
472- messages = [{"role" : "user" , "content" : "What's the weather like?" }],
464+ model = "claude-3-5-sonnet-20241022" ,
465+ max_tokens = 200 ,
466+ temperature = 0.7 ,
467+ tools = tools ,
468+ messages = [{"role" : "user" , "content" : "hey" }],
473469 posthog_distinct_id = "test-id" ,
474470 posthog_properties = {"foo" : "bar" },
475471 )
476472
477- assert response == mock_anthropic_response_with_tool_use
473+ assert response == mock_anthropic_response
478474 assert mock_client .capture .call_count == 1
479475
480476 call_args = mock_client .capture .call_args [1 ]
@@ -483,25 +479,15 @@ def test_tool_use_response(mock_client, mock_anthropic_response_with_tool_use):
483479 assert call_args ["distinct_id" ] == "test-id"
484480 assert call_args ["event" ] == "$ai_generation"
485481 assert props ["$ai_provider" ] == "anthropic"
486- assert props ["$ai_model" ] == "claude-3-opus-20240229"
487- assert props ["$ai_input" ] == [
488- {"role" : "user" , "content" : "What's the weather like?" }
489- ]
490- # Should only include text content, not tool_use content
482+ assert props ["$ai_model" ] == "claude-3-5-sonnet-20241022"
483+ assert props ["$ai_input" ] == [{"role" : "user" , "content" : "hey" }]
491484 assert props ["$ai_output_choices" ] == [
492- {"role" : "assistant" , "content" : "I'll help you with that. " }
485+ {"role" : "assistant" , "content" : "Test response " }
493486 ]
494487 assert props ["$ai_input_tokens" ] == 20
495488 assert props ["$ai_output_tokens" ] == 10
496489 assert props ["$ai_http_status" ] == 200
497490 assert props ["foo" ] == "bar"
498491 assert isinstance (props ["$ai_latency" ], float )
499- # Verify that tools are captured separately
500- assert props ["$ai_tools" ] == [
501- {
502- "type" : "tool_use" ,
503- "id" : "tool_1" ,
504- "name" : "get_weather" ,
505- "input" : {"location" : "New York" },
506- }
507- ]
492+ # Verify that tools are captured in the $ai_tools property
493+ assert props ["$ai_tools" ] == tools
0 commit comments