Debian Codegraph @

examples/tools_stream.py

raw
1 import asyncio
2
3 from anthropic import AsyncAnthropic
4
5 client = AsyncAnthropic()
6
7
8 async def main() -> None:
9 async with client.messages.stream(
10 max_tokens=1024,
11 model="claude-sonnet-5-5",
12 tools=[
13 {
14 "name": "get_weather",
15 "description": "Get the weather at a specific location",
16 "input_schema": {
17 "type": "object",
18 "properties": {
19 "location": {"type": "string", "description": "The city and state, e.g. San Francisco, CA"},
20 "unit": {
21 "type": "string",
22 "enum": ["celsius", "fahrenheit"],
23 "description": "Unit for the output",
24 },
25 },
26 "required": ["location"],
27 },
28 }
29 ],
30 messages=[{"role": "user", "content": "What is the weather in SF?"}],
31 ) as stream:
32 async for event in stream:
33 if event.type == "input_json":
34 print(f"delta: {repr(event.partial_json)}")
35 print(f"snapshot: {event.snapshot}")
36
37 print()
38
39
40 asyncio.run(main())