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

fix(bedrock): Correctly set index on anthropic content attribute #2192

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Expand Up @@ -449,24 +449,43 @@ def _set_anthropic_messages_span_attributes(span, request_body, response_body, m
_record_usage_to_span(span, prompt_tokens, completion_tokens, metric_params)

if should_send_prompts():
for idx, message in enumerate(request_body.get("messages")):
message_idx_start = 0
if request_body.get("system"):
_set_span_attribute(
span, f"{SpanAttributes.LLM_PROMPTS}.0.role", "system",
)
_set_span_attribute(
span, f"{SpanAttributes.LLM_PROMPTS}.0.content", request_body.get("system"),
)
message_idx_start += 1

for idx, message in enumerate(request_body.get("messages"), message_idx_start):
_set_span_attribute(
span, f"{SpanAttributes.LLM_PROMPTS}.{idx}.role", message.get("role")
)
content = message.get("content")
if isinstance(content, str):
text = content
else:
text = content[0].get("text")
_set_span_attribute(
span,
f"{SpanAttributes.LLM_PROMPTS}.0.content",
json.dumps(message.get("content")),
f"{SpanAttributes.LLM_PROMPTS}.{idx}.content",
text,
)

_set_span_attribute(
span, f"{SpanAttributes.LLM_COMPLETIONS}.0.content", "assistant"
)
_set_span_attribute(
span,
f"{SpanAttributes.LLM_COMPLETIONS}.0.content",
json.dumps(response_body.get("content")),
)
for idx, content in enumerate(response_body.get("content")):
_set_span_attribute(
span, f"{SpanAttributes.LLM_COMPLETIONS}.{idx}.role", "assistant"
)
_set_span_attribute(
span,
f"{SpanAttributes.LLM_COMPLETIONS}.{idx}.content",
content.get("text"),
)
_set_span_attribute(
span, f"{SpanAttributes.LLM_COMPLETIONS}.{idx}.finish_reason", response_body.get("stop_reason")
)


def _count_anthropic_tokens(messages: list[str]):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,21 @@ def test_anthropic_3_completion_complex_content(test_context, brt):
"content": [
{"type": "text", "text": "Tell me a joke about opentelemetry"},
],
}
},
{
"role": "assistant",
"content": [
{"type": "text", "text": "Why did the microservice bring OpenTelemetry to the party?"}
],
},
{
"role": "user",
"content": [
{"type": "text", "text": "Because it knew how to keep track of all the span-taneous events!"}
],
},
],
"system": "You're a comedian",
"max_tokens": 200,
"temperature": 0.5,
"anthropic_version": "bedrock-2023-05-31",
Expand All @@ -81,18 +94,22 @@ def test_anthropic_3_completion_complex_content(test_context, brt):
assert all(span.name == "bedrock.completion" for span in spans)

anthropic_span = spans[0]
assert json.loads(
anthropic_span.attributes[f"{SpanAttributes.LLM_PROMPTS}.0.content"]
) == [
{"type": "text", "text": "Tell me a joke about opentelemetry"},
]

assert anthropic_span.attributes[f"{SpanAttributes.LLM_PROMPTS}.0.role"] == "system"
assert anthropic_span.attributes[f"{SpanAttributes.LLM_PROMPTS}.0.content"] == "You're a comedian"

assert anthropic_span.attributes[f"{SpanAttributes.LLM_PROMPTS}.1.role"] == "user"
assert anthropic_span.attributes[f"{SpanAttributes.LLM_PROMPTS}.1.content"] == "Tell me a joke about opentelemetry"

assert anthropic_span.attributes[f"{SpanAttributes.LLM_PROMPTS}.2.role"] == "assistant"
assert anthropic_span.attributes[f"{SpanAttributes.LLM_PROMPTS}.2.content"] == "Why did the microservice bring OpenTelemetry to the party?"

assert anthropic_span.attributes[f"{SpanAttributes.LLM_PROMPTS}.3.role"] == "user"
assert anthropic_span.attributes[f"{SpanAttributes.LLM_PROMPTS}.3.content"] == "Because it knew how to keep track of all the span-taneous events!"

assert (
json.loads(
anthropic_span.attributes.get(f"{SpanAttributes.LLM_COMPLETIONS}.0.content")
)
== completion
)
anthropic_span.attributes.get(f"{SpanAttributes.LLM_COMPLETIONS}.0.content")
) == completion[0].get("text")

assert anthropic_span.attributes.get(SpanAttributes.LLM_USAGE_PROMPT_TOKENS) == 16
assert anthropic_span.attributes.get(
Expand Down Expand Up @@ -142,20 +159,9 @@ def test_anthropic_3_completion_streaming(test_context, brt):
assert all(span.name == "bedrock.completion" for span in spans)

anthropic_span = spans[0]
assert json.loads(
anthropic_span.attributes[f"{SpanAttributes.LLM_PROMPTS}.0.content"]
) == [
{"type": "text", "text": "Tell me a joke about opentelemetry"},
]
assert anthropic_span.attributes[f"{SpanAttributes.LLM_PROMPTS}.0.content"] == "Tell me a joke about opentelemetry"

assert json.loads(
anthropic_span.attributes.get(f"{SpanAttributes.LLM_COMPLETIONS}.0.content")
) == [
{
"type": "text",
"text": completion,
}
]
assert anthropic_span.attributes.get(f"{SpanAttributes.LLM_COMPLETIONS}.0.content") == completion

assert anthropic_span.attributes.get(SpanAttributes.LLM_USAGE_PROMPT_TOKENS) == 16
assert anthropic_span.attributes.get(
Expand Down Expand Up @@ -198,16 +204,18 @@ def test_anthropic_3_completion_string_content(test_context, brt):
assert all(span.name == "bedrock.completion" for span in spans)

anthropic_span = spans[0]

for key, value in anthropic_span.attributes.items():
print(key, value)

assert (
json.loads(anthropic_span.attributes[f"{SpanAttributes.LLM_PROMPTS}.0.content"])
anthropic_span.attributes[f"{SpanAttributes.LLM_PROMPTS}.0.content"]
== "Tell me a joke about opentelemetry"
)

assert (
json.loads(
anthropic_span.attributes.get(f"{SpanAttributes.LLM_COMPLETIONS}.0.content")
)
== completion
anthropic_span.attributes.get(f"{SpanAttributes.LLM_COMPLETIONS}.0.content")
== completion[0].get("text")
)

assert anthropic_span.attributes.get(SpanAttributes.LLM_USAGE_PROMPT_TOKENS) == 16
Expand Down
Loading