Skip to content

Commit

Permalink
feat: Updated semantic conventions based on otel community (#884)
Browse files Browse the repository at this point in the history
Co-authored-by: Nir Gazit <nirga@users.noreply.github.com>
  • Loading branch information
gyliu513 and nirga authored Apr 29, 2024
1 parent e2a09ae commit 23eba2a
Show file tree
Hide file tree
Showing 69 changed files with 454 additions and 435 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ def _set_input_attributes(span, kwargs):
set_span_attribute(
span, SpanAttributes.LLM_REQUEST_MAX_TOKENS, kwargs.get("max_tokens_to_sample")
)
set_span_attribute(span, SpanAttributes.LLM_TEMPERATURE, kwargs.get("temperature"))
set_span_attribute(span, SpanAttributes.LLM_TOP_P, kwargs.get("top_p"))
set_span_attribute(span, SpanAttributes.LLM_REQUEST_TEMPERATURE, kwargs.get("temperature"))
set_span_attribute(span, SpanAttributes.LLM_REQUEST_TOP_P, kwargs.get("top_p"))
set_span_attribute(
span, SpanAttributes.LLM_FREQUENCY_PENALTY, kwargs.get("frequency_penalty")
)
Expand Down Expand Up @@ -394,7 +394,7 @@ def _calculate_metrics_attributes(response):
if not isinstance(response, dict):
response = response.__dict__
return {
"llm.response.model": response.get("model"),
"gen_ai.response.model": response.get("model"),
}


Expand All @@ -420,7 +420,7 @@ def _wrap(
name,
kind=SpanKind.CLIENT,
attributes={
SpanAttributes.LLM_VENDOR: "Anthropic",
SpanAttributes.LLM_SYSTEM: "Anthropic",
SpanAttributes.LLM_REQUEST_TYPE: LLMRequestTypeValues.COMPLETION.value,
},
)
Expand Down Expand Up @@ -516,7 +516,7 @@ async def _awrap(
name,
kind=SpanKind.CLIENT,
attributes={
SpanAttributes.LLM_VENDOR: "Anthropic",
SpanAttributes.LLM_SYSTEM: "Anthropic",
SpanAttributes.LLM_REQUEST_TYPE: LLMRequestTypeValues.COMPLETION.value,
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def build_from_streaming_response(
_process_response_item(item, complete_response)

metric_attributes = {
"llm.response.model": complete_response.get("model"),
"gen_ai.response.model": complete_response.get("model"),
}

if duration_histogram:
Expand Down Expand Up @@ -205,7 +205,7 @@ async def abuild_from_streaming_response(
_process_response_item(item, complete_response)

metric_attributes = {
"llm.response.model": complete_response.get("model"),
"gen_ai.response.model": complete_response.get("model"),
}

if duration_histogram:
Expand Down
8 changes: 4 additions & 4 deletions packages/opentelemetry-instrumentation-anthropic/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ python = ">=3.9,<4"
opentelemetry-api = "^1.24.0"
opentelemetry-instrumentation = "^0.45b0"
opentelemetry-semantic-conventions = "^0.45b0"
opentelemetry-semantic-conventions-ai = "0.1.1"
opentelemetry-semantic-conventions-ai = "0.2.0"

[tool.poetry.group.dev.dependencies]
autopep8 = "2.1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ def test_anthropic_completion(exporter, reader):

anthropic_span = spans[0]
assert (
anthropic_span.attributes["llm.prompts.0.user"]
anthropic_span.attributes["gen_ai.prompt.0.user"]
== f"{HUMAN_PROMPT}\nHello world\n{AI_PROMPT}"
)
assert anthropic_span.attributes.get("llm.completions.0.content")
assert anthropic_span.attributes.get("gen_ai.completion.0.content")

metrics_data = reader.get_metrics_data()
resource_metrics = metrics_data.resource_metrics
Expand All @@ -51,7 +51,7 @@ def test_anthropic_completion(exporter, reader):
"prompt",
]
assert (
data_point.attributes["llm.response.model"]
data_point.attributes["gen_ai.response.model"]
== "claude-instant-1.2"
)
assert data_point.value > 0
Expand All @@ -61,7 +61,7 @@ def test_anthropic_completion(exporter, reader):
for data_point in metric.data.data_points:
assert data_point.value >= 1
assert (
data_point.attributes["llm.response.model"]
data_point.attributes["gen_ai.response.model"]
== "claude-instant-1.2"
)

Expand All @@ -74,7 +74,7 @@ def test_anthropic_completion(exporter, reader):
data_point.sum > 0 for data_point in metric.data.data_points
)
assert all(
data_point.attributes.get("llm.response.model")
data_point.attributes.get("gen_ai.response.model")
== "claude-instant-1.2"
or data_point.attributes.get("error.type") == "TypeError"
for data_point in metric.data.data_points
Expand Down Expand Up @@ -117,18 +117,18 @@ def test_anthropic_message_create(exporter, reader):

anthropic_span = spans[0]
assert (
anthropic_span.attributes["llm.prompts.0.content"]
anthropic_span.attributes["gen_ai.prompt.0.content"]
== "Tell me a joke about OpenTelemetry"
)
assert (anthropic_span.attributes["llm.prompts.0.role"]) == "user"
assert (anthropic_span.attributes["gen_ai.prompt.0.role"]) == "user"
assert (
anthropic_span.attributes.get("llm.completions.0.content")
anthropic_span.attributes.get("gen_ai.completion.0.content")
== response.content[0].text
)
assert anthropic_span.attributes["llm.usage.prompt_tokens"] == 8
assert anthropic_span.attributes["gen_ai.usage.prompt_tokens"] == 8
assert (
anthropic_span.attributes["llm.usage.completion_tokens"]
+ anthropic_span.attributes["llm.usage.prompt_tokens"]
anthropic_span.attributes["gen_ai.usage.completion_tokens"]
+ anthropic_span.attributes["gen_ai.usage.prompt_tokens"]
== anthropic_span.attributes["llm.usage.total_tokens"]
)

Expand All @@ -152,7 +152,7 @@ def test_anthropic_message_create(exporter, reader):
"prompt",
]
assert (
data_point.attributes["llm.response.model"]
data_point.attributes["gen_ai.response.model"]
== "claude-3-opus-20240229"
)
assert data_point.value > 0
Expand All @@ -162,7 +162,7 @@ def test_anthropic_message_create(exporter, reader):
for data_point in metric.data.data_points:
assert data_point.value >= 1
assert (
data_point.attributes["llm.response.model"]
data_point.attributes["gen_ai.response.model"]
== "claude-3-opus-20240229"
)

Expand All @@ -175,7 +175,7 @@ def test_anthropic_message_create(exporter, reader):
data_point.sum > 0 for data_point in metric.data.data_points
)
assert all(
data_point.attributes.get("llm.response.model")
data_point.attributes.get("gen_ai.response.model")
== "claude-3-opus-20240229"
or data_point.attributes.get("error.type") == "TypeError"
for data_point in metric.data.data_points
Expand Down Expand Up @@ -225,7 +225,7 @@ def test_anthropic_multi_modal(exporter):
"anthropic.completion",
]
anthropic_span = spans[0]
assert anthropic_span.attributes["llm.prompts.0.content"] == json.dumps(
assert anthropic_span.attributes["gen_ai.prompt.0.content"] == json.dumps(
[
{"type": "text", "text": "What do you see?"},
{
Expand All @@ -238,15 +238,15 @@ def test_anthropic_multi_modal(exporter):
},
]
)
assert (anthropic_span.attributes["llm.prompts.0.role"]) == "user"
assert (anthropic_span.attributes["gen_ai.prompt.0.role"]) == "user"
assert (
anthropic_span.attributes.get("llm.completions.0.content")
anthropic_span.attributes.get("gen_ai.completion.0.content")
== response.content[0].text
)
assert anthropic_span.attributes["llm.usage.prompt_tokens"] == 1381
assert anthropic_span.attributes["gen_ai.usage.prompt_tokens"] == 1381
assert (
anthropic_span.attributes["llm.usage.completion_tokens"]
+ anthropic_span.attributes["llm.usage.prompt_tokens"]
anthropic_span.attributes["gen_ai.usage.completion_tokens"]
+ anthropic_span.attributes["gen_ai.usage.prompt_tokens"]
== anthropic_span.attributes["llm.usage.total_tokens"]
)

Expand Down Expand Up @@ -277,17 +277,17 @@ def test_anthropic_message_streaming(exporter, reader):
]
anthropic_span = spans[0]
assert (
anthropic_span.attributes["llm.prompts.0.content"]
anthropic_span.attributes["gen_ai.prompt.0.content"]
== "Tell me a joke about OpenTelemetry"
)
assert (anthropic_span.attributes["llm.prompts.0.role"]) == "user"
assert (anthropic_span.attributes["gen_ai.prompt.0.role"]) == "user"
assert (
anthropic_span.attributes.get("llm.completions.0.content") == response_content
anthropic_span.attributes.get("gen_ai.completion.0.content") == response_content
)
assert anthropic_span.attributes["llm.usage.prompt_tokens"] == 8
assert anthropic_span.attributes["gen_ai.usage.prompt_tokens"] == 8
assert (
anthropic_span.attributes["llm.usage.completion_tokens"]
+ anthropic_span.attributes["llm.usage.prompt_tokens"]
anthropic_span.attributes["gen_ai.usage.completion_tokens"]
+ anthropic_span.attributes["gen_ai.usage.prompt_tokens"]
== anthropic_span.attributes["llm.usage.total_tokens"]
)

Expand All @@ -311,7 +311,7 @@ def test_anthropic_message_streaming(exporter, reader):
"prompt",
]
assert (
data_point.attributes["llm.response.model"]
data_point.attributes["gen_ai.response.model"]
== "claude-3-haiku-20240307"
)
assert data_point.value > 0
Expand All @@ -321,7 +321,7 @@ def test_anthropic_message_streaming(exporter, reader):
for data_point in metric.data.data_points:
assert data_point.value >= 1
assert (
data_point.attributes["llm.response.model"]
data_point.attributes["gen_ai.response.model"]
== "claude-3-haiku-20240307"
)

Expand All @@ -334,7 +334,7 @@ def test_anthropic_message_streaming(exporter, reader):
data_point.sum > 0 for data_point in metric.data.data_points
)
assert all(
data_point.attributes.get("llm.response.model")
data_point.attributes.get("gen_ai.response.model")
== "claude-3-haiku-20240307"
or data_point.attributes.get("error.type") == "TypeError"
for data_point in metric.data.data_points
Expand Down Expand Up @@ -372,18 +372,18 @@ async def test_async_anthropic_message_create(exporter, reader):
]
anthropic_span = spans[0]
assert (
anthropic_span.attributes["llm.prompts.0.content"]
anthropic_span.attributes["gen_ai.prompt.0.content"]
== "Tell me a joke about OpenTelemetry"
)
assert (anthropic_span.attributes["llm.prompts.0.role"]) == "user"
assert (anthropic_span.attributes["gen_ai.prompt.0.role"]) == "user"
assert (
anthropic_span.attributes.get("llm.completions.0.content")
anthropic_span.attributes.get("gen_ai.completion.0.content")
== response.content[0].text
)
assert anthropic_span.attributes["llm.usage.prompt_tokens"] == 8
assert anthropic_span.attributes["gen_ai.usage.prompt_tokens"] == 8
assert (
anthropic_span.attributes["llm.usage.completion_tokens"]
+ anthropic_span.attributes["llm.usage.prompt_tokens"]
anthropic_span.attributes["gen_ai.usage.completion_tokens"]
+ anthropic_span.attributes["gen_ai.usage.prompt_tokens"]
== anthropic_span.attributes["llm.usage.total_tokens"]
)

Expand All @@ -407,7 +407,7 @@ async def test_async_anthropic_message_create(exporter, reader):
"prompt",
]
assert (
data_point.attributes["llm.response.model"]
data_point.attributes["gen_ai.response.model"]
== "claude-3-opus-20240229"
)
assert data_point.value > 0
Expand All @@ -417,7 +417,7 @@ async def test_async_anthropic_message_create(exporter, reader):
for data_point in metric.data.data_points:
assert data_point.value >= 1
assert (
data_point.attributes["llm.response.model"]
data_point.attributes["gen_ai.response.model"]
== "claude-3-opus-20240229"
)

Expand All @@ -430,7 +430,7 @@ async def test_async_anthropic_message_create(exporter, reader):
data_point.sum > 0 for data_point in metric.data.data_points
)
assert all(
data_point.attributes.get("llm.response.model")
data_point.attributes.get("gen_ai.response.model")
== "claude-3-opus-20240229"
or data_point.attributes.get("error.type") == "TypeError"
for data_point in metric.data.data_points
Expand Down Expand Up @@ -474,17 +474,17 @@ async def test_async_anthropic_message_streaming(exporter, reader):
]
anthropic_span = spans[0]
assert (
anthropic_span.attributes["llm.prompts.0.content"]
anthropic_span.attributes["gen_ai.prompt.0.content"]
== "Tell me a joke about OpenTelemetry"
)
assert (anthropic_span.attributes["llm.prompts.0.role"]) == "user"
assert (anthropic_span.attributes["gen_ai.prompt.0.role"]) == "user"
assert (
anthropic_span.attributes.get("llm.completions.0.content") == response_content
anthropic_span.attributes.get("gen_ai.completion.0.content") == response_content
)
assert anthropic_span.attributes["llm.usage.prompt_tokens"] == 8
assert anthropic_span.attributes["gen_ai.usage.prompt_tokens"] == 8
assert (
anthropic_span.attributes["llm.usage.completion_tokens"]
+ anthropic_span.attributes["llm.usage.prompt_tokens"]
anthropic_span.attributes["gen_ai.usage.completion_tokens"]
+ anthropic_span.attributes["gen_ai.usage.prompt_tokens"]
== anthropic_span.attributes["llm.usage.total_tokens"]
)

Expand All @@ -508,7 +508,7 @@ async def test_async_anthropic_message_streaming(exporter, reader):
"prompt",
]
assert (
data_point.attributes["llm.response.model"]
data_point.attributes["gen_ai.response.model"]
== "claude-3-haiku-20240307"
)
assert data_point.value > 0
Expand All @@ -518,7 +518,7 @@ async def test_async_anthropic_message_streaming(exporter, reader):
for data_point in metric.data.data_points:
assert data_point.value >= 1
assert (
data_point.attributes["llm.response.model"]
data_point.attributes["gen_ai.response.model"]
== "claude-3-haiku-20240307"
)

Expand All @@ -531,7 +531,7 @@ async def test_async_anthropic_message_streaming(exporter, reader):
data_point.sum > 0 for data_point in metric.data.data_points
)
assert all(
data_point.attributes.get("llm.response.model")
data_point.attributes.get("gen_ai.response.model")
== "claude-3-haiku-20240307"
or data_point.attributes.get("error.type") == "TypeError"
for data_point in metric.data.data_points
Expand Down
Loading

0 comments on commit 23eba2a

Please sign in to comment.