Skip to content

Commit

Permalink
fix(anthropic): add instrumentation for Anthropic tool calling (alter…
Browse files Browse the repository at this point in the history
…native to #1372) (#2150)
  • Loading branch information
dinmukhamedm authored Oct 16, 2024
1 parent 5e709c3 commit 8606235
Show file tree
Hide file tree
Showing 5 changed files with 1,152 additions and 670 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,55 @@ async def _aset_input_attributes(span, kwargs):
span, f"{SpanAttributes.LLM_PROMPTS}.{i}.role", message.get("role")
)

if kwargs.get("tools") is not None:
for i, tool in enumerate(kwargs.get("tools")):
prefix = f"{SpanAttributes.LLM_REQUEST_FUNCTIONS}.{i}"
set_span_attribute(span, f"{prefix}.name", tool.get("name"))
set_span_attribute(span, f"{prefix}.description", tool.get("description"))
input_schema = tool.get("input_schema")
if input_schema is not None:
set_span_attribute(span, f"{prefix}.input_schema", json.dumps(input_schema))


def _set_span_completions(span, response):
index = 0
prefix = f"{SpanAttributes.LLM_COMPLETIONS}.{index}"
set_span_attribute(span, f"{prefix}.finish_reason", response.get("stop_reason"))
if response.get("role"):
set_span_attribute(span, f"{prefix}.role", response.get("role"))

if response.get("completion"):
set_span_attribute(span, f"{prefix}.content", response.get("completion"))
elif response.get("content"):
for i, content in enumerate(response.get("content")):
set_span_attribute(
span,
f"{SpanAttributes.LLM_COMPLETIONS}.{i}.content",
content.text,
)
tool_call_index = 0
text = ""
for content in response.get("content"):
content_block_type = content.type
# usually, Antrhopic responds with just one text block,
# but the API allows for multiple text blocks, so concatenate them
if content_block_type == "text":
text += content.text
elif content_block_type == "tool_use":
content = dict(content)
set_span_attribute(
span,
f"{prefix}.tool_calls.{tool_call_index}.id",
content.get("id"),
)
set_span_attribute(
span,
f"{prefix}.tool_calls.{tool_call_index}.name",
content.get("name"),
)
tool_arguments = content.get("input")
if tool_arguments is not None:
set_span_attribute(
span,
f"{prefix}.tool_calls.{tool_call_index}.arguments",
json.dumps(tool_arguments),
)
tool_call_index += 1
set_span_attribute(span, f"{prefix}.content", text)


@dont_throw
Expand Down
Loading

0 comments on commit 8606235

Please sign in to comment.