Skip to content
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 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import opentelemetry.context as context_api
from opentelemetry import trace as trace_api

from anthropic.types import TextBlock, ToolUseBlock
from openinference.instrumentation import get_attributes_from_context, safe_json_dumps
from openinference.semconv.trace import (
DocumentAttributes,
Expand Down Expand Up @@ -182,8 +183,7 @@ def __call__(
span.set_status(trace_api.StatusCode.OK)
span.set_attributes(
{
f"{LLM_OUTPUT_MESSAGES}.0.{MESSAGE_CONTENT}": response.content[0].text,
f"{LLM_OUTPUT_MESSAGES}.0.{MESSAGE_ROLE}": response.role,
**dict(_get_output_messages(response)),
LLM_TOKEN_COUNT_PROMPT: response.usage.input_tokens,
LLM_TOKEN_COUNT_COMPLETION: response.usage.output_tokens,
OUTPUT_VALUE: response.model_dump_json(),
Expand Down Expand Up @@ -241,8 +241,7 @@ async def __call__(
span.set_status(trace_api.StatusCode.OK)
span.set_attributes(
{
f"{LLM_OUTPUT_MESSAGES}.0.{MESSAGE_CONTENT}": response.content[0].text,
f"{LLM_OUTPUT_MESSAGES}.0.{MESSAGE_ROLE}": response.role,
**dict(_get_output_messages(response)),
LLM_TOKEN_COUNT_PROMPT: response.usage.input_tokens,
LLM_TOKEN_COUNT_COMPLETION: response.usage.output_tokens,
OUTPUT_VALUE: response.model_dump_json(),
Expand All @@ -269,13 +268,6 @@ def _get_input_messages(messages: List[Dict[str, str]]) -> Any:
yield f"{LLM_INPUT_MESSAGES}.{i}.{MESSAGE_ROLE}", role


def _get_output_message(response: Any) -> Any:
if response.content:
yield f"{LLM_OUTPUT_MESSAGES}.0.{MESSAGE_CONTENT}", response.content[0].text
if response.role:
yield f"{LLM_OUTPUT_MESSAGES}.0.{MESSAGE_ROLE}", response.role


def _get_invocation_parameters(kwargs: Mapping[str, Any]) -> Any:
"""
Extracts the invocation parameters from the call
Expand All @@ -287,6 +279,26 @@ def _get_invocation_parameters(kwargs: Mapping[str, Any]) -> Any:
return invocation_parameters


def _get_output_messages(response: Any) -> Any:
"""
Extracts the tool call information from the response
"""
for i in range(len(response.content)):
block = response.content[i]
yield f"{LLM_OUTPUT_MESSAGES}.{i}.{MESSAGE_ROLE}", response.role
if isinstance(block, ToolUseBlock):
yield (
f"{LLM_OUTPUT_MESSAGES}.{i}.{MESSAGE_TOOL_CALLS}.0.{TOOL_CALL_FUNCTION_NAME}",
block.name,
)
yield (
f"{LLM_OUTPUT_MESSAGES}.{i}.{MESSAGE_TOOL_CALLS}.0.{TOOL_CALL_FUNCTION_ARGUMENTS_JSON}",
safe_json_dumps(block.input),
)
if isinstance(block, TextBlock):
yield f"{LLM_OUTPUT_MESSAGES}.{i}.{MESSAGE_CONTENT}", block.text


def _validate_invocation_parameter(parameter: Any) -> bool:
"""
Validates the invocation parameters
Expand Down Expand Up @@ -339,6 +351,7 @@ def _validate_invocation_parameter(parameter: Any) -> bool:
LLM_TOKEN_COUNT_PROMPT = SpanAttributes.LLM_TOKEN_COUNT_PROMPT
LLM_TOKEN_COUNT_TOTAL = SpanAttributes.LLM_TOKEN_COUNT_TOTAL
MESSAGE_CONTENT = MessageAttributes.MESSAGE_CONTENT
MESSAGE_CONTENTS = MessageAttributes.MESSAGE_CONTENTS
MESSAGE_FUNCTION_CALL_ARGUMENTS_JSON = MessageAttributes.MESSAGE_FUNCTION_CALL_ARGUMENTS_JSON
MESSAGE_FUNCTION_CALL_NAME = MessageAttributes.MESSAGE_FUNCTION_CALL_NAME
MESSAGE_ROLE = MessageAttributes.MESSAGE_ROLE
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,108 @@ async def test_anthropic_instrumentation_async_messages(
assert not attributes


@pytest.mark.vcr(
Copy link
Contributor

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!

decode_compressed_response=True,
before_record_request=remove_all_vcr_request_headers,
before_record_response=remove_all_vcr_response_headers,
)
def test_anthropic_instrumentation_multiple_tool_calling(
tracer_provider: TracerProvider,
in_memory_span_exporter: InMemorySpanExporter,
setup_anthropic_instrumentation: Any,
) -> None:
client = anthropic.Anthropic(api_key="fake")

input_message = (
"What is the weather like right now in New York?"
" Also what time is it there? Use necessary tools simultaneously."
)

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": input_message}],
)

spans = in_memory_span_exporter.get_finished_spans()

assert spans[0].name == "Messages"
attributes = dict(spans[0].attributes or {})

assert isinstance(attributes.pop(LLM_MODEL_NAME), str)
assert attributes.pop(f"{LLM_INPUT_MESSAGES}.0.{MESSAGE_CONTENT}") == input_message
assert attributes.pop(f"{LLM_INPUT_MESSAGES}.0.{MESSAGE_ROLE}") == "user"
assert isinstance(attributes.pop(LLM_INVOCATION_PARAMETERS), str)
assert isinstance(attributes.pop(INPUT_VALUE), str)
assert attributes.pop(INPUT_MIME_TYPE) == JSON
assert attributes.pop(f"{LLM_OUTPUT_MESSAGES}.0.{MESSAGE_ROLE}") == "assistant"
assert isinstance(attributes.pop(f"{LLM_OUTPUT_MESSAGES}.0.{MESSAGE_CONTENT}"), str)
assert attributes.pop(f"{LLM_OUTPUT_MESSAGES}.1.{MESSAGE_ROLE}") == "assistant"
assert (
attributes.pop(f"{LLM_OUTPUT_MESSAGES}.1.{MESSAGE_TOOL_CALLS}.0.{TOOL_CALL_FUNCTION_NAME}")
== "get_weather"
)
assert isinstance(
attributes.pop(
f"{LLM_OUTPUT_MESSAGES}.1.{MESSAGE_TOOL_CALLS}.0.{TOOL_CALL_FUNCTION_ARGUMENTS_JSON}"
),
str,
)
assert attributes.pop(f"{LLM_OUTPUT_MESSAGES}.2.{MESSAGE_ROLE}") == "assistant"
assert (
attributes.pop(f"{LLM_OUTPUT_MESSAGES}.2.{MESSAGE_TOOL_CALLS}.0.{TOOL_CALL_FUNCTION_NAME}")
== "get_time"
)
assert isinstance(
attributes.pop(
f"{LLM_OUTPUT_MESSAGES}.2.{MESSAGE_TOOL_CALLS}.0.{TOOL_CALL_FUNCTION_ARGUMENTS_JSON}"
),
str,
)
assert isinstance(attributes.pop(LLM_TOKEN_COUNT_PROMPT), int)
assert isinstance(attributes.pop(LLM_TOKEN_COUNT_COMPLETION), int)
assert isinstance(attributes.pop(OUTPUT_VALUE), str)
assert isinstance(attributes.pop(OUTPUT_MIME_TYPE), str)
assert attributes.pop(OPENINFERENCE_SPAN_KIND) == "LLM"
assert not attributes


@pytest.mark.vcr(
decode_compressed_response=True,
before_record_request=remove_all_vcr_request_headers,
Expand Down
Loading