-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathchat.py
More file actions
67 lines (56 loc) · 2.43 KB
/
chat.py
File metadata and controls
67 lines (56 loc) · 2.43 KB
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
57
58
59
60
61
62
63
64
65
66
67
"""Anthropic chat with tool calling, tracked via OpenTelemetry."""
import os
import json
import urllib.request
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.resources import Resource, SERVICE_NAME
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.anthropic import AnthropicInstrumentor
resource = Resource(attributes={SERVICE_NAME: "example-anthropic-app"})
exporter = OTLPSpanExporter(
endpoint=f"{os.environ.get('POSTHOG_HOST', 'https://us.i.posthog.com')}/i/v0/ai/otel",
headers={"Authorization": f"Bearer {os.environ['POSTHOG_API_KEY']}"},
)
provider = TracerProvider(resource=resource)
provider.add_span_processor(SimpleSpanProcessor(exporter))
trace.set_tracer_provider(provider)
AnthropicInstrumentor().instrument()
import anthropic # noqa: E402
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
tools = [
{
"name": "get_weather",
"description": "Get current weather for a location",
"input_schema": {
"type": "object",
"properties": {
"latitude": {"type": "number"},
"longitude": {"type": "number"},
"location_name": {"type": "string"},
},
"required": ["latitude", "longitude", "location_name"],
},
}
]
def get_weather(latitude: float, longitude: float, location_name: str) -> str:
url = f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}¤t=temperature_2m,relative_humidity_2m,wind_speed_10m"
with urllib.request.urlopen(url) as resp:
data = json.loads(resp.read())
current = data["current"]
return f"Weather in {location_name}: {current['temperature_2m']}°C, humidity {current['relative_humidity_2m']}%, wind {current['wind_speed_10m']} km/h"
message = client.messages.create(
model="claude-sonnet-4-5-20250929",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "What's the weather like in San Francisco?"}],
)
# Handle tool use if the model requests it.
# In production, send tool results back to the model for a final response.
for block in message.content:
if block.type == "text":
print(block.text)
elif block.type == "tool_use":
result = get_weather(**block.input)
print(result)