1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
import json
from typing_extensions import Literal
import rich
from anthropic import Anthropic, beta_tool
client = Anthropic()
@beta_tool
def get_weather(location: str, units: Literal["c", "f"]) -> str:
"""Lookup the weather for a given city in either celsius or fahrenheit
Args:
location: The city and state, e.g. San Francisco, CA
units: Unit for the output, either 'c' for celsius or 'f' for fahrenheit
Returns:
A dictionary containing the location, temperature, and weather condition.
"""
# Simulate a weather API call
print(f"Fetching weather for {location} in {units}")
# Here you would typically make an API call to a weather service
# For demonstration, we return a mock response
if units == "c":
return json.dumps(
{
"location": location,
"temperature": "20°C",
"condition": "Sunny",
}
)
else:
return json.dumps(
{
"location": location,
"temperature": "68°F",
"condition": "Sunny",
}
)
def main() -> None:
runner = client.beta.messages.tool_runner(
max_tokens=1024,
model="claude-3-5-sonnet-latest",
# alternatively, you can use `tools=[anthropic.beta_tool(get_weather)]`
tools=[get_weather],
messages=[{"role": "user", "content": "What is the weather in SF?"}],
)
for message in runner:
rich.print(message)
main()
|