-
Notifications
You must be signed in to change notification settings - Fork 39
/
end_to_end_tool_use.py
116 lines (105 loc) · 3.58 KB
/
end_to_end_tool_use.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from typing import Optional
import anthropic
from anthropic.types import (
Message,
MessageParam,
TextBlock,
ToolResultBlockParam,
ToolUseBlock,
)
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from typing_extensions import assert_never
from openinference.instrumentation.anthropic import AnthropicInstrumentor
endpoint = "http://127.0.0.1:6006/v1/traces"
tracer_provider = trace_sdk.TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
AnthropicInstrumentor().instrument(tracer_provider=tracer_provider)
def _to_assistant_message_param(
message: Message,
) -> MessageParam:
content = []
for block in message.content:
if isinstance(block, TextBlock):
content.append(block)
elif isinstance(block, ToolUseBlock):
content.append(block)
else:
assert_never(block)
return MessageParam(content=content, role="assistant")
def _get_tool_use_id(message: Message) -> Optional[str]:
for block in message.content:
if isinstance(block, ToolUseBlock):
return block.id
return None
client = anthropic.Anthropic()
messages = [{"role": "user", "content": "What is the weather like in San Francisco in Fahrenheit?"}]
response = client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=1024,
tools=[
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": 'The unit of temperature, either "celsius" or "fahrenheit"',
},
},
"required": ["location"],
},
}
],
messages=messages,
)
messages.append(_to_assistant_message_param(response))
assert (tool_use_id := _get_tool_use_id(response)) is not None, "tool was not called"
messages.append(
MessageParam(
content=[
ToolResultBlockParam(
tool_use_id=tool_use_id,
content='{"weather": "sunny", "temperature": "75"}',
type="tool_result",
is_error=False,
)
],
role="user",
)
)
response = client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=1024,
tools=[
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": 'The unit of temperature, either "celsius" or "fahrenheit"',
},
},
"required": ["location"],
},
}
],
messages=messages,
)
print(f"{response=}")