-
Notifications
You must be signed in to change notification settings - Fork 678
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(anthropic): add instrumentation for Anthropic tool calling #1372
Conversation
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.
Thanks for this @peachypeachyy! Can you also add instrumentation for the tool use that anthropic returns?
@nirga The response of tool_use as the finish reason is already captured here: When you say instrumentation of tool_use, do you mean setting the response attributes of the Chain of Thought? i.e setting response attributes to something like this:
|
@nirga if I have to set the response attributes as mentioned above? Which spans do I use? I ask because the only response attribute within the opentelemetry-semconv-ai package I have is I do see
|
@peachypeachyy this should be similar to OpenAI. You're right btw, we're missing these from the semantic conventions. |
@nirga done, I have captured spans for tool_use in response as well, I have used SpanAttributes.LLM_COMPLETIONS as that is what was used in the OpenAI code. |
if response.get("stop_reason"): | ||
set_span_attribute(span, f"{SpanAttributes.LLM_COMPLETIONS}.stop_reason", response.get("stop_reason")) | ||
|
||
if response.get("content"): |
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.
Hey @peachypeachyy this is already happening in _set_span_completions
. What I meant is editing the code there to specify the tool selection (if a tool was selected) - and following the similar conventions we have in the OpenAI instrumentation
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.
Hey @nirga let me step you through my thought process.
Following is the request being sent:
response = client.messages.create(
model="claude-3-opus-20240229",
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 City, NY?"
}
]
)
The corresponding formatted response is:
{'content': [TextBlock(text='<thinking>\nThe get_weather function is relevant to answer this question, as it provides the current weather for a given location.\n\nIt requires the "location" parameter, which in this case is provided by the user as "New York City, NY". \n\nThe "unit" parameter is optional. The user did not specify a preference, so we can use the default.\n\nSince all required parameters are available, we can proceed with calling the get_weather function to get the current weather in New York City.\n</thinking>', type='text'),
ToolUseBlock(id='toolu_013gp9gPBaYhPVLMeq4N34yU', input={'location': 'New York City, NY'}, name='get_weather', type='tool_use')],
'id': 'msg_01H62BKGLJBD8zstmvW21Cp4',
'model': 'claude-3-opus-20240229',
'role': 'assistant',
'stop_reason': 'tool_use',
'stop_sequence': None,
'type': 'message',
'usage': Usage(input_tokens=748, output_tokens=165)}
In this case, even though we have 2 functions passed in tools, get_weather
and get_time
, claude's opus determined to use get_weather as seen in the ToolUseBlock
.
I have captured this tool get_weather
which is being used within ToolUseBlock
using f"{SpanAttributes.LLM_COMPLETIONS}.{i}.name"
.
Now, which tool has to be used is governed by the parameter tool_choice which needs to be set in the request. I have not set it in this example, by default the value is auto
. Do you need me to set spans for this within the request? something like f"{SpanAttributes.LLM_REQUEST_FUNCTIONS}.{i}.tool_choice == auto
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.
@peachypeachyy can you look at set_span_completions
? It's also setting the same attributes you're setting here. You should only add the tool choice logging, similar to OpenAI
@nirga @peachypeachyy What's the status of the work here? If this is stale, we are happy to contribute from the newest main branch. |
@dinmukhamedm happy for a contribution! It's a small one and I think @peachypeachyy is no longer working on it |
Awesome, opened #2150. |
Fixes #879
feat(instrumentation): ...
orfix(instrumentation): ...
.