Skip to content

Commit

Permalink
cleanup setting prompt events & finish reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
alizenhom committed Sep 6, 2024
1 parent 71aaeb6 commit 8495a24
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
from opentelemetry.trace import SpanKind, Span
from opentelemetry.trace.status import Status, StatusCode
from .span_attributes import LLMSpanAttributes
from opentelemetry.semconv.attributes import (
error_attributes as ErrorAttributes,
)
from opentelemetry.semconv._incubating.attributes import (
gen_ai_attributes as GenAIAttributes,
error_attributes as ErrorAttributes,
)
from .utils import (
silently_fail,
Expand All @@ -28,8 +30,10 @@
set_span_attribute,
set_event_completion,
extract_tools_prompt,
set_event_prompt,
)
from opentelemetry.trace import Tracer
import json


def chat_completions_create(tracer: Tracer):
Expand All @@ -43,16 +47,15 @@ def traced_method(wrapped, instance, args, kwargs):
tools_prompt = extract_tools_prompt(item)
llm_prompts.append(tools_prompt if tools_prompt else item)

span_attributes = {
**get_llm_request_attributes(kwargs, prompts=llm_prompts),
}
span_attributes = {**get_llm_request_attributes(kwargs)}

attributes = LLMSpanAttributes(**span_attributes)

span_name = f"{attributes.gen_ai_operation_name} {attributes.gen_ai_request_model}"

span = tracer.start_span(name=span_name, kind=SpanKind.CLIENT)
_set_input_attributes(span, attributes)
set_event_prompt(span, json.dumps(llm_prompts))

try:
result = wrapped(*args, **kwargs)
Expand Down Expand Up @@ -112,13 +115,15 @@ def _set_response_attributes(span, result):
}
for choice in choices
]
finish_reasons = []
for choice in choices:
if choice.finish_reason:
set_span_attribute(
span,
GenAIAttributes.GEN_AI_RESPONSE_FINISH_REASONS,
choice.finish_reason,
)
finish_reasons.append(choice.finish_reason or "error")

set_span_attribute(
span,
GenAIAttributes.GEN_AI_RESPONSE_FINISH_REASONS,
finish_reasons,
)
set_event_completion(span, responses)

if getattr(result, "id", None):
Expand Down Expand Up @@ -270,14 +275,15 @@ def build_streaming_response(self, chunk):
):
content.append(tool_call.function.arguments)

finish_reasons = []
for choice in choices:
finish_reason = choice.finish_reason
if finish_reason:
set_span_attribute(
self.span,
GenAIAttributes.GEN_AI_RESPONSE_FINISH_REASONS,
finish_reason,
)
finish_reasons.append(choice.finish_reason or "error")

set_span_attribute(
self.span,
GenAIAttributes.GEN_AI_RESPONSE_FINISH_REASONS,
finish_reasons,
)
if content:
self.result_content.append(content[0])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,7 @@ def set_span_attribute(span: Span, name, value):
if non_numerical_value_is_set(value) is False:
return

if name == GenAIAttributes.GEN_AI_PROMPT:
set_event_prompt(span, value)
else:
span.set_attribute(name, value)
span.set_attribute(name, value)


def is_streaming(kwargs):
Expand All @@ -143,37 +140,17 @@ def non_numerical_value_is_set(value: Optional[Union[bool, str]]):

def get_llm_request_attributes(
kwargs,
prompts=None,
model=None,
operation_name=GenAIAttributes.GenAiOperationNameValues.CHAT.value,
):

user = kwargs.get("user")
if prompts is None:
prompts = (
[{"role": user or "user", "content": kwargs.get("prompt")}]
if "prompt" in kwargs
else None
)
top_k = (
kwargs.get("n")
or kwargs.get("k")
or kwargs.get("top_k")
or kwargs.get("top_n")
)

top_p = kwargs.get("p") or kwargs.get("top_p")

return {
GenAIAttributes.GEN_AI_OPERATION_NAME: operation_name,
GenAIAttributes.GEN_AI_SYSTEM: GenAIAttributes.GenAiSystemValues.OPENAI.value,
GenAIAttributes.GEN_AI_REQUEST_MODEL: model or kwargs.get("model"),
GenAIAttributes.GEN_AI_REQUEST_TEMPERATURE: kwargs.get("temperature"),
GenAIAttributes.GEN_AI_REQUEST_TOP_K: top_k,
GenAIAttributes.GEN_AI_PROMPT: (
json.dumps(prompts) if prompts else None
),
GenAIAttributes.GEN_AI_REQUEST_TOP_P: top_p,
GenAIAttributes.GEN_AI_REQUEST_TOP_P: kwargs.get("p")
or kwargs.get("top_p"),
GenAIAttributes.GEN_AI_REQUEST_MAX_TOKENS: kwargs.get("max_tokens"),
GenAIAttributes.GEN_AI_REQUEST_PRESENCE_PENALTY: kwargs.get(
"presence_penalty"
Expand Down

0 comments on commit 8495a24

Please sign in to comment.