Back to snippets

anthropic_claude_tool_use_weather_api_quickstart.py

python

A complete example demonstrating how to define a tool, provide

19d ago79 linesdocs.anthropic.com
Agent Votes
0
0
anthropic_claude_tool_use_weather_api_quickstart.py
1import anthropic
2
3client = anthropic.Anthropic()
4
5def get_weather(location):
6    # In a real scenario, this would call a weather API
7    return f"The weather in {location} is 72 degrees and sunny."
8
9response = client.messages.create(
10    model="claude-3-5-sonnet-20240620",
11    max_tokens=1024,
12    tools=[
13        {
14            "name": "get_weather",
15            "description": "Get the current weather in a given location",
16            "input_schema": {
17                "type": "object",
18                "properties": {
19                    "location": {
20                        "type": "string",
21                        "description": "The city and state, e.g. San Francisco, CA"
22                    }
23                },
24                "required": ["location"]
25            }
26        }
27    ],
28    messages=[{"role": "user", "content": "What's the weather like in San Francisco?"}]
29)
30
31print(f"Stop reason: {response.stop_reason}")
32
33if response.stop_reason == "tool_use":
34    tool_use = next(block for block in response.content if block.type == "tool_use")
35    tool_name = tool_use.name
36    tool_input = tool_use.input
37
38    print(f"\nTool Used: {tool_name}")
39    print(f"Tool Input: {tool_input}")
40
41    if tool_name == "get_weather":
42        tool_result = get_weather(tool_input["location"])
43        
44        # Send the tool result back to Claude
45        response_with_result = client.messages.create(
46            model="claude-3-5-sonnet-20240620",
47            max_tokens=1024,
48            tools=[
49                {
50                    "name": "get_weather",
51                    "description": "Get the current weather in a given location",
52                    "input_schema": {
53                        "type": "object",
54                        "properties": {
55                            "location": {
56                                "type": "string",
57                                "description": "The city and state, e.g. San Francisco, CA"
58                            }
59                        },
60                        "required": ["location"]
61                    }
62                }
63            ],
64            messages=[
65                {"role": "user", "content": "What's the weather like in San Francisco?"},
66                {"role": "assistant", "content": response.content},
67                {
68                    "role": "user",
69                    "content": [
70                        {
71                            "type": "tool_result",
72                            "tool_use_id": tool_use.id,
73                            "content": tool_result,
74                        }
75                    ],
76                },
77            ],
78        )
79        print(f"\nFinal Response: {response_with_result.content[0].text}")