-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Tool calling for Anthropic instrumentor #939
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
...instrumentation/openinference-instrumentation-anthropic/examples/multiple_tool_calling.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import anthropic | ||
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 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) | ||
|
||
client = anthropic.Anthropic() | ||
|
||
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"], | ||
}, | ||
}, | ||
{ | ||
"name": "get_time", | ||
"description": "Get the current time in a given time zone", | ||
"input_schema": { | ||
"type": "object", | ||
"properties": { | ||
"timezone": { | ||
"type": "string", | ||
"description": "The IANA time zone name, e.g. America/Los_Angeles", | ||
} | ||
}, | ||
"required": ["timezone"], | ||
}, | ||
}, | ||
], | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "What is the weather like right now in New York?" | ||
" Also what time is it there? Use necessary tools simultaneously.", | ||
} | ||
], | ||
) | ||
print(response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...pic/cassettes/test_instrumentor/test_anthropic_instrumentation_multiple_tool_calling.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
interactions: | ||
- request: | ||
body: '{"max_tokens": 1024, "messages": [{"role": "user", "content": "What is | ||
the weather like right now in New York? Also what time is it there? Use necessary | ||
tools simultaneously."}], "model": "claude-3-5-sonnet-20240620", "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"]}}, {"name": | ||
"get_time", "description": "Get the current time in a given time zone", "input_schema": | ||
{"type": "object", "properties": {"timezone": {"type": "string", "description": | ||
"The IANA time zone name, e.g. America/Los_Angeles"}}, "required": ["timezone"]}}]}' | ||
headers: {} | ||
method: POST | ||
uri: https://api.anthropic.com/v1/messages | ||
response: | ||
body: | ||
string: '{"id":"msg_01CMa5RpucSF21EeGUgkbucN","type":"message","role":"assistant","model":"claude-3-5-sonnet-20240620","content":[{"type":"text","text":"Certainly! | ||
I''ll use the available tools to get the weather in New York and the current | ||
time there simultaneously. Let me fetch that information for you."},{"type":"tool_use","id":"toolu_01NDhzJaEt48dbFQgTnNb8uv","name":"get_weather","input":{"location":"New | ||
York, NY","unit":"celsius"}},{"type":"tool_use","id":"toolu_015oXjg7raTjag52Qc6h6e3J","name":"get_time","input":{"timezone":"America/New_York"}}],"stop_reason":"tool_use","stop_sequence":null,"usage":{"input_tokens":518,"output_tokens":144}}' | ||
headers: {} | ||
status: | ||
code: 200 | ||
message: OK | ||
version: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL. will use this for my future tests. thanks!