Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

[Fix]: OpenAI Instrumentation to use class instead of Generator (#503)

Patchereec91112d8ec
diff --git a/sdk/python/pyproject.toml b/sdk/python/pyproject.toml
index cc1a9f0b..6fdec26c 100644
--- a/sdk/python/pyproject.toml
+++ b/sdk/python/pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "openlit"
-version = "1.29.3"
+version = "1.29.4"
description = "OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications and GPUs, facilitating the integration of observability into your GenAI-driven projects"
authors = ["OpenLIT"]
repository = "https://github.com/openlit/openlit/tree/main/openlit/python"
diff --git a/sdk/python/src/openlit/__helpers.py b/sdk/python/src/openlit/__helpers.py
index e9a26e42..bcff4e80 100644
--- a/sdk/python/src/openlit/__helpers.py
+++ b/sdk/python/src/openlit/__helpers.py
@@ -13,6 +13,20 @@ from opentelemetry.trace import Status, StatusCode
# Set up logging
logger = logging.getLogger(__name__)
+def response_as_dict(response):
+ """
+ Return parsed response as a dict
+ """
+ # pylint: disable=no-else-return
+ if isinstance(response, dict):
+ return response
+ if hasattr(response, "model_dump"):
+ return response.model_dump()
+ elif hasattr(response, "parse"):
+ return response_as_dict(response.parse())
+ else:
+ return response
+
def get_env_variable(name, arg_value, error_message):
"""
Retrieve an environment variable if the argument is not provided
diff --git a/sdk/python/src/openlit/instrumentation/openai/async_openai.py b/sdk/python/src/openlit/instrumentation/openai/async_openai.py
index e3118464..b83291a4 100644
--- a/sdk/python/src/openlit/instrumentation/openai/async_openai.py
+++ b/sdk/python/src/openlit/instrumentation/openai/async_openai.py
@@ -6,8 +6,15 @@ Module for monitoring OpenAI API calls.
import logging
from opentelemetry.trace import SpanKind, Status, StatusCode
from opentelemetry.sdk.resources import TELEMETRY_SDK_NAME
-from openlit.__helpers import get_chat_model_cost, get_embed_model_cost, get_audio_model_cost
-from openlit.__helpers import get_image_model_cost, openai_tokens, handle_exception
+from openlit.__helpers import (
+ get_chat_model_cost,
+ get_embed_model_cost,
+ get_audio_model_cost,
+ get_image_model_cost,
+ openai_tokens,
+ handle_exception,
+ response_as_dict,
+)
from openlit.semcov import SemanticConvetion
# Initialize logger for logging potential issues and operations
@@ -46,8 +53,8 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
self,
wrapped,
span,
- *args,
- **kwargs,
+ kwargs,
+ **args,
):
self.__wrapped__ = wrapped
self._span = span
@@ -68,17 +75,22 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
def __aiter__(self):
return self
+ async def __getattr__(self, name):
+ """Delegate attribute access to the wrapped object."""
+ return getattr(await self.__wrapped__, name)
+
async def __anext__(self):
try:
chunk = await self.__wrapped__.__anext__()
+ chunked = response_as_dict(chunk)
# Collect message IDs and aggregated response from events
- if len(chunk.choices) > 0:
- # pylint: disable=line-too-long
- if hasattr(chunk.choices[0], "delta") and hasattr(chunk.choices[0].delta, "content"):
- content = chunk.choices[0].delta.content
- if content:
- self._llmresponse += content
- self._response_id = chunk.id
+ if (len(chunked.get('choices')) > 0 and ('delta' in chunked.get('choices')[0] and
+ 'content' in chunked.get('choices')[0].get('delta'))):
+
+ content = chunked.get('choices')[0].get('delta').get('content')
+ if content:
+ self._llmresponse += content
+ self._response_id = chunked.get('id')
return chunk
except StopAsyncIteration:
# Handling exception ensure observability without disrupting operation
@@ -226,7 +238,7 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
awaited_wrapped = await wrapped(*args, **kwargs)
span = tracer.start_span(gen_ai_endpoint, kind=SpanKind.CLIENT)
- return TracedAsyncStream(awaited_wrapped, span)
+ return TracedAsyncStream(awaited_wrapped, span, kwargs)
# Handling for non-streaming responses
else:
@@ -234,6 +246,8 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
response = await wrapped(*args, **kwargs)
+ response_dict = response_as_dict(response)
+
try:
# Format 'messages' into a single string
message_prompt = kwargs.get("messages", "")
@@ -263,7 +277,7 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
gen_ai_endpoint)
span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
- response.id)
+ response_dict.get("id"))
span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
environment)
span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
@@ -294,23 +308,21 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
},
)
- span.set_status(Status(StatusCode.OK))
-
# Set span attributes when tools is not passed to the function call
if "tools" not in kwargs:
# Calculate cost of the operation
cost = get_chat_model_cost(kwargs.get("model", "gpt-3.5-turbo"),
- pricing_info, response.usage.prompt_tokens,
- response.usage.completion_tokens)
+ pricing_info, response_dict.get('usage', {}).get('prompt_tokens', None),
+ response_dict.get('usage', {}).get('completion_tokens', None))
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
- response.usage.prompt_tokens)
+ response_dict.get('usage', {}).get('prompt_tokens', None))
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
- response.usage.completion_tokens)
+ response_dict.get('usage', {}).get('completion_tokens', None))
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
- response.usage.total_tokens)
+ response_dict.get('usage', {}).get('total_tokens', None))
span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,
- [response.choices[0].finish_reason])
+ [response_dict.get('choices', [])[0].get('finish_reason', None)])
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
cost)
@@ -320,7 +332,7 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
span.add_event(
name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
attributes={
- SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response.choices[0].message.content,
+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response_dict.get('choices', [])[0].get("message").get("content"),
},
)
@@ -332,7 +344,7 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
span.add_event(
name=attribute_name,
attributes={
- SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response.choices[i].message.content,
+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response_dict.get('choices')[i].get("message").get("content"),
},
)
i += 1
@@ -344,9 +356,8 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
elif "tools" in kwargs:
# Calculate cost of the operation
cost = get_chat_model_cost(kwargs.get("model", "gpt-3.5-turbo"),
- pricing_info, response.usage.prompt_tokens,
- response.usage.completion_tokens)
-
+ pricing_info, response_dict.get('usage').get('prompt_tokens'),
+ response_dict.get('usage').get('completion_tokens'))
span.add_event(
name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
attributes={
@@ -354,11 +365,11 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
},
)
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
- response.usage.prompt_tokens)
+ response_dict.get('usage').get('prompt_tokens'))
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
- response.usage.completion_tokens)
+ response_dict.get('usage').get('completion_tokens'))
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
- response.usage.total_tokens)
+ response_dict.get('usage').get('total_tokens'))
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
cost)
@@ -381,9 +392,9 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
}
metrics["genai_requests"].add(1, attributes)
- metrics["genai_total_tokens"].add(response.usage.total_tokens, attributes)
- metrics["genai_completion_tokens"].add(response.usage.completion_tokens, attributes)
- metrics["genai_prompt_tokens"].add(response.usage.prompt_tokens, attributes)
+ metrics["genai_total_tokens"].add(response_dict.get('usage').get('total_tokens'), attributes)
+ metrics["genai_completion_tokens"].add(response_dict.get('usage').get('completion_tokens'), attributes)
+ metrics["genai_prompt_tokens"].add(response_dict.get('usage').get('prompt_tokens'), attributes)
metrics["genai_cost"].record(cost, attributes)
# Return original response
@@ -548,13 +559,14 @@ def async_finetune(gen_ai_endpoint, version, environment, application_name,
with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
response = await wrapped(*args, **kwargs)
+ # Handling exception ensure observability without disrupting operation
try:
# Set Span attributes
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
SemanticConvetion.GEN_AI_SYSTEM_OPENAI)
span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
- "fine_tuning")
+ SemanticConvetion.GEN_AI_TYPE_FINETUNING)
span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
gen_ai_endpoint)
span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
@@ -585,7 +597,22 @@ def async_finetune(gen_ai_endpoint, version, environment, application_name,
span.set_status(Status(StatusCode.OK))
if disable_metrics is False:
- metrics["genai_requests"].add(1)
+ attributes = {
+ TELEMETRY_SDK_NAME:
+ "openlit",
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
+ application_name,
+ SemanticConvetion.GEN_AI_SYSTEM:
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI,
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
+ environment,
+ SemanticConvetion.GEN_AI_TYPE:
+ SemanticConvetion.GEN_AI_TYPE_FINETUNING,
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
+ kwargs.get("model", "gpt-3.5-turbo")
+ }
+
+ metrics["genai_requests"].add(1, attributes)
# Return original response
return response
diff --git a/sdk/python/src/openlit/instrumentation/openai/openai.py b/sdk/python/src/openlit/instrumentation/openai/openai.py
index 3ee03f6b..09241126 100644
--- a/sdk/python/src/openlit/instrumentation/openai/openai.py
+++ b/sdk/python/src/openlit/instrumentation/openai/openai.py
@@ -6,8 +6,15 @@ Module for monitoring OpenAI API calls.
import logging
from opentelemetry.trace import SpanKind, Status, StatusCode
from opentelemetry.sdk.resources import TELEMETRY_SDK_NAME
-from openlit.__helpers import get_chat_model_cost, get_embed_model_cost, get_audio_model_cost
-from openlit.__helpers import get_image_model_cost, openai_tokens, handle_exception
+from openlit.__helpers import (
+ get_chat_model_cost,
+ get_embed_model_cost,
+ get_audio_model_cost,
+ get_image_model_cost,
+ openai_tokens,
+ handle_exception,
+ response_as_dict,
+)
from openlit.semcov import SemanticConvetion
# Initialize logger for logging potential issues and operations
@@ -31,10 +38,184 @@ def chat_completions(gen_ai_endpoint, version, environment, application_name,
A function that wraps the chat completions method to add telemetry.
"""
+ class TracedSyncStream:
+ """
+ Wrapper for streaming responses to collect metrics and trace data.
+ Wraps the 'openai.AsyncStream' response to collect message IDs and aggregated response.
+
+ This class implements the '__aiter__' and '__anext__' methods that
+ handle asynchronous streaming responses.
+
+ This class also implements '__aenter__' and '__aexit__' methods that
+ handle asynchronous context management protocol.
+ """
+ def __init__(
+ self,
+ wrapped,
+ span,
+ kwargs,
+ **args,
+ ):
+ self.__wrapped__ = wrapped
+ self._span = span
+ # Placeholder for aggregating streaming response
+ self._llmresponse = ""
+ self._response_id = ""
+
+ self._args = args
+ self._kwargs = kwargs
+
+ def __enter__(self):
+ self.__wrapped__.__enter__()
+ return self
+
+ def __exit__(self, exc_type, exc_value, traceback):
+ self.__wrapped__.__exit__(exc_type, exc_value, traceback)
+
+ def __iter__(self):
+ return self
+
+ def __getattr__(self, name):
+ """Delegate attribute access to the wrapped object."""
+ return getattr(self.__wrapped__, name)
+
+ def __next__(self):
+ try:
+ chunk = self.__wrapped__.__next__()
+ chunked = response_as_dict(chunk)
+ # Collect message IDs and aggregated response from events
+ if (len(chunked.get('choices')) > 0 and ('delta' in chunked.get('choices')[0] and
+ 'content' in chunked.get('choices')[0].get('delta'))):
+
+ content = chunked.get('choices')[0].get('delta').get('content')
+ if content:
+ self._llmresponse += content
+ self._response_id = chunked.get('id')
+ return chunk
+ except StopIteration:
+ # Handling exception ensure observability without disrupting operation
+ try:
+ # Format 'messages' into a single string
+ message_prompt = self._kwargs.get("messages", "")
+ formatted_messages = []
+ for message in message_prompt:
+ role = message["role"]
+ content = message["content"]
+
+ if isinstance(content, list):
+ content_str = ", ".join(
+ # pylint: disable=line-too-long
+ f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
+ if "type" in item else f'text: {item["text"]}'
+ for item in content
+ )
+ formatted_messages.append(f"{role}: {content_str}")
+ else:
+ formatted_messages.append(f"{role}: {content}")
+ prompt = "\n".join(formatted_messages)
+
+ # Calculate tokens using input prompt and aggregated response
+ prompt_tokens = openai_tokens(prompt,
+ self._kwargs.get("model", "gpt-3.5-turbo"))
+ completion_tokens = openai_tokens(self._llmresponse,
+ self._kwargs.get("model", "gpt-3.5-turbo"))
+
+ # Calculate cost of the operation
+ cost = get_chat_model_cost(self._kwargs.get("model", "gpt-3.5-turbo"),
+ pricing_info, prompt_tokens,
+ completion_tokens)
+
+ # Set Span attributes
+ self._span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
+ self._span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
+ SemanticConvetion.GEN_AI_TYPE_CHAT)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
+ gen_ai_endpoint)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
+ self._response_id)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
+ environment)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
+ application_name)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
+ self._kwargs.get("model", "gpt-3.5-turbo"))
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
+ self._kwargs.get("user", ""))
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,
+ self._kwargs.get("top_p", 1.0))
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS,
+ self._kwargs.get("max_tokens", -1))
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
+ self._kwargs.get("temperature", 1.0))
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_PRESENCE_PENALTY,
+ self._kwargs.get("presence_penalty", 0.0))
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_FREQUENCY_PENALTY,
+ self._kwargs.get("frequency_penalty", 0.0))
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_SEED,
+ self._kwargs.get("seed", ""))
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
+ True)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
+ prompt_tokens)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
+ completion_tokens)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
+ prompt_tokens + completion_tokens)
+ self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
+ cost)
+ if trace_content:
+ self._span.add_event(
+ name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
+ attributes={
+ SemanticConvetion.GEN_AI_CONTENT_PROMPT: prompt,
+ },
+ )
+ self._span.add_event(
+ name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
+ attributes={
+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: self._llmresponse,
+ },
+ )
+
+ self._span.set_status(Status(StatusCode.OK))
+
+ if disable_metrics is False:
+ attributes = {
+ TELEMETRY_SDK_NAME:
+ "openlit",
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
+ application_name,
+ SemanticConvetion.GEN_AI_SYSTEM:
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI,
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
+ environment,
+ SemanticConvetion.GEN_AI_TYPE:
+ SemanticConvetion.GEN_AI_TYPE_CHAT,
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
+ self._kwargs.get("model", "gpt-3.5-turbo")
+ }
+
+ metrics["genai_requests"].add(1, attributes)
+ metrics["genai_total_tokens"].add(
+ prompt_tokens + completion_tokens, attributes
+ )
+ metrics["genai_completion_tokens"].add(completion_tokens, attributes)
+ metrics["genai_prompt_tokens"].add(prompt_tokens, attributes)
+ metrics["genai_cost"].record(cost, attributes)
+
+ except Exception as e:
+ handle_exception(self._span, e)
+ logger.error("Error in trace creation: %s", e)
+ finally:
+ self._span.end()
+ raise
+
def wrapper(wrapped, instance, args, kwargs):
"""
Wraps the 'chat.completions' API call to add telemetry.
-
+
This collects metrics such as execution time, cost, and token usage, and handles errors
gracefully, adding details to the trace for observability.
@@ -54,141 +235,10 @@ def chat_completions(gen_ai_endpoint, version, environment, application_name,
# pylint: disable=no-else-return
if streaming:
# Special handling for streaming response to accommodate the nature of data flow
- def stream_generator():
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
- # Placeholder for aggregating streaming response
- llmresponse = ""
-
-
- # Loop through streaming events capturing relevant details
- for chunk in wrapped(*args, **kwargs):
- # Collect message IDs and aggregated response from events
- if len(chunk.choices) > 0:
- # pylint: disable=line-too-long
- if hasattr(chunk.choices[0], "delta") and hasattr(chunk.choices[0].delta, "content"):
- content = chunk.choices[0].delta.content
- if content:
- llmresponse += content
- yield chunk
- response_id = chunk.id
-
- # Handling exception ensure observability without disrupting operation
- try:
- # Format 'messages' into a single string
- message_prompt = kwargs.get("messages", "")
- formatted_messages = []
- for message in message_prompt:
- role = message["role"]
- content = message["content"]
-
- if isinstance(content, list):
- content_str = ", ".join(
- # pylint: disable=line-too-long
- f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
- if "type" in item else f'text: {item["text"]}'
- for item in content
- )
- formatted_messages.append(f"{role}: {content_str}")
- else:
- formatted_messages.append(f"{role}: {content}")
- prompt = "\n".join(formatted_messages)
-
- # Calculate tokens using input prompt and aggregated response
- prompt_tokens = openai_tokens(prompt,
- kwargs.get("model", "gpt-3.5-turbo"))
- completion_tokens = openai_tokens(llmresponse,
- kwargs.get("model", "gpt-3.5-turbo"))
-
- # Calculate cost of the operation
- cost = get_chat_model_cost(kwargs.get("model", "gpt-3.5-turbo"),
- pricing_info, prompt_tokens,
- completion_tokens)
-
- # Set Span attributes
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
- SemanticConvetion.GEN_AI_SYSTEM_OPENAI)
- span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
- SemanticConvetion.GEN_AI_TYPE_CHAT)
- span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
- gen_ai_endpoint)
- span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
- response_id)
- span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
- environment)
- span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
- application_name)
- span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
- kwargs.get("model", "gpt-3.5-turbo"))
- span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
- kwargs.get("user", ""))
- span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,
- kwargs.get("top_p", 1.0))
- span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS,
- kwargs.get("max_tokens", -1))
- span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
- kwargs.get("temperature", 1.0))
- span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_PRESENCE_PENALTY,
- kwargs.get("presence_penalty", 0.0))
- span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_FREQUENCY_PENALTY,
- kwargs.get("frequency_penalty", 0.0))
- span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_SEED,
- kwargs.get("seed", ""))
- span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
- True)
- span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
- prompt_tokens)
- span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
- completion_tokens)
- span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
- prompt_tokens + completion_tokens)
- span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
- cost)
- if trace_content:
- span.add_event(
- name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvetion.GEN_AI_CONTENT_PROMPT: prompt,
- },
- )
- span.add_event(
- name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
- attributes={
- SemanticConvetion.GEN_AI_CONTENT_COMPLETION: llmresponse,
- },
- )
-
- span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = {
- TELEMETRY_SDK_NAME:
- "openlit",
- SemanticConvetion.GEN_AI_APPLICATION_NAME:
- application_name,
- SemanticConvetion.GEN_AI_SYSTEM:
- SemanticConvetion.GEN_AI_SYSTEM_OPENAI,
- SemanticConvetion.GEN_AI_ENVIRONMENT:
- environment,
- SemanticConvetion.GEN_AI_TYPE:
- SemanticConvetion.GEN_AI_TYPE_CHAT,
- SemanticConvetion.GEN_AI_REQUEST_MODEL:
- kwargs.get("model", "gpt-3.5-turbo")
- }
-
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_total_tokens"].add(
- prompt_tokens + completion_tokens, attributes
- )
- metrics["genai_completion_tokens"].add(completion_tokens, attributes)
- metrics["genai_prompt_tokens"].add(prompt_tokens, attributes)
- metrics["genai_cost"].record(cost, attributes)
+ awaited_wrapped = wrapped(*args, **kwargs)
+ span = tracer.start_span(gen_ai_endpoint, kind=SpanKind.CLIENT)
- except Exception as e:
- handle_exception(span, e)
- logger.error("Error in trace creation: %s", e)
-
- return stream_generator()
+ return TracedSyncStream(awaited_wrapped, span, kwargs)
# Handling for non-streaming responses
else:
@@ -196,6 +246,8 @@ def chat_completions(gen_ai_endpoint, version, environment, application_name,
with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
response = wrapped(*args, **kwargs)
+ response_dict = response_as_dict(response)
+
try:
# Format 'messages' into a single string
message_prompt = kwargs.get("messages", "")
@@ -225,7 +277,7 @@ def chat_completions(gen_ai_endpoint, version, environment, application_name,
span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
gen_ai_endpoint)
span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
- response.id)
+ response_dict.get("id"))
span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
environment)
span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
@@ -260,17 +312,17 @@ def chat_completions(gen_ai_endpoint, version, environment, application_name,
if "tools" not in kwargs:
# Calculate cost of the operation
cost = get_chat_model_cost(kwargs.get("model", "gpt-3.5-turbo"),
- pricing_info, response.usage.prompt_tokens,
- response.usage.completion_tokens)
+ pricing_info, response_dict.get('usage', {}).get('prompt_tokens', None),
+ response_dict.get('usage', {}).get('completion_tokens', None))
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
- response.usage.prompt_tokens)
+ response_dict.get('usage', {}).get('prompt_tokens', None))
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
- response.usage.completion_tokens)
+ response_dict.get('usage', {}).get('completion_tokens', None))
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
- response.usage.total_tokens)
+ response_dict.get('usage', {}).get('total_tokens', None))
span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,
- [response.choices[0].finish_reason])
+ [response_dict.get('choices', [])[0].get('finish_reason', None)])
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
cost)
@@ -280,7 +332,7 @@ def chat_completions(gen_ai_endpoint, version, environment, application_name,
span.add_event(
name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
attributes={
- SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response.choices[0].message.content,
+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response_dict.get('choices', [])[0].get("message").get("content"),
},
)
@@ -292,7 +344,7 @@ def chat_completions(gen_ai_endpoint, version, environment, application_name,
span.add_event(
name=attribute_name,
attributes={
- SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response.choices[i].message.content,
+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response_dict.get('choices')[i].get("message").get("content"),
},
)
i += 1
@@ -304,8 +356,8 @@ def chat_completions(gen_ai_endpoint, version, environment, application_name,
elif "tools" in kwargs:
# Calculate cost of the operation
cost = get_chat_model_cost(kwargs.get("model", "gpt-3.5-turbo"),
- pricing_info, response.usage.prompt_tokens,
- response.usage.completion_tokens)
+ pricing_info, response_dict.get('usage').get('prompt_tokens'),
+ response_dict.get('usage').get('completion_tokens'))
span.add_event(
name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
attributes={
@@ -313,11 +365,11 @@ def chat_completions(gen_ai_endpoint, version, environment, application_name,
},
)
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
- response.usage.prompt_tokens)
+ response_dict.get('usage').get('prompt_tokens'))
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
- response.usage.completion_tokens)
+ response_dict.get('usage').get('completion_tokens'))
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
- response.usage.total_tokens)
+ response_dict.get('usage').get('total_tokens'))
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
cost)
@@ -340,9 +392,9 @@ def chat_completions(gen_ai_endpoint, version, environment, application_name,
}
metrics["genai_requests"].add(1, attributes)
- metrics["genai_total_tokens"].add(response.usage.total_tokens, attributes)
- metrics["genai_completion_tokens"].add(response.usage.completion_tokens, attributes)
- metrics["genai_prompt_tokens"].add(response.usage.prompt_tokens, attributes)
+ metrics["genai_total_tokens"].add(response_dict.get('usage').get('total_tokens'), attributes)
+ metrics["genai_completion_tokens"].add(response_dict.get('usage').get('completion_tokens'), attributes)
+ metrics["genai_prompt_tokens"].add(response_dict.get('usage').get('prompt_tokens'), attributes)
metrics["genai_cost"].record(cost, attributes)
# Return original response