Skip to content

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

gpt4all ready

patcher906790dd39e64
diff --git a/sdk/python/pyproject.toml b/sdk/python/pyproject.toml
index 5ac8e837..4a131781 100644
--- a/sdk/python/pyproject.toml
+++ b/sdk/python/pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "openlit"
-version = "1.34.7"
+version = "1.34.8"
description = "OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications and GPUs, facilitating the integration of observability into your GenAI-driven projects"
authors = ["OpenLIT"]
license = "Apache-2.0"
diff --git a/sdk/python/src/openlit/instrumentation/gpt4all/__init__.py b/sdk/python/src/openlit/instrumentation/gpt4all/__init__.py
index 1b3021d6..0348dbd7 100644
--- a/sdk/python/src/openlit/instrumentation/gpt4all/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/gpt4all/__init__.py
@@ -1,4 +1,3 @@
-# pylint: disable=useless-return, bad-staticmethod-argument, disable=duplicate-code
"""Initializer of Auto Instrumentation of GPT4All Functions"""
from typing import Collection
@@ -14,15 +13,15 @@ _instruments = ("gpt4all >= 2.6.0",)
class GPT4AllInstrumentor(BaseInstrumentor):
"""
- An instrumentor for GPT4All's client library.
+ An instrumentor for GPT4All client library.
"""
def instrumentation_dependencies(self) -> Collection[str]:
return _instruments
def _instrument(self, **kwargs):
- application_name = kwargs.get("application_name", "default_application")
- environment = kwargs.get("environment", "default_environment")
+ application_name = kwargs.get("application_name", "default")
+ environment = kwargs.get("environment", "default")
tracer = kwargs.get("tracer")
metrics = kwargs.get("metrics_dict")
pricing_info = kwargs.get("pricing_info", {})
@@ -46,7 +45,5 @@ class GPT4AllInstrumentor(BaseInstrumentor):
tracer, pricing_info, capture_message_content, metrics, disable_metrics),
)
-
def _uninstrument(self, **kwargs):
- # Proper uninstrumentation logic to revert patched methods
pass
diff --git a/sdk/python/src/openlit/instrumentation/gpt4all/gpt4all.py b/sdk/python/src/openlit/instrumentation/gpt4all/gpt4all.py
index 31a0c874..b971bcc1 100644
--- a/sdk/python/src/openlit/instrumentation/gpt4all/gpt4all.py
+++ b/sdk/python/src/openlit/instrumentation/gpt4all/gpt4all.py
@@ -2,66 +2,47 @@
Module for monitoring GPT4All API calls.
"""
-import logging
import time
-from opentelemetry.trace import SpanKind, Status, StatusCode
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.trace import SpanKind
from openlit.__helpers import (
handle_exception,
- general_tokens,
- create_metrics_attributes,
- set_server_address_and_port,
- calculate_tbt,
- calculate_ttft
+ set_server_address_and_port
+)
+from openlit.instrumentation.gpt4all.utils import (
+ process_generate_response,
+ process_chunk,
+ process_streaming_generate_response,
+ process_embedding_response
)
from openlit.semcov import SemanticConvention
-# Initialize logger for logging potential issues and operations
-logger = logging.getLogger(__name__)
-
def generate(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics):
"""
- Generates a telemetry wrapper for chat completions to collect metrics.
-
- Args:
- version: Version of the monitoring package.
- environment: Deployment environment (e.g., production, staging).
- application_name: Name of the application using the GPT4All API.
- tracer: OpenTelemetry tracer for creating spans.
- pricing_info: Information used for calculating GPT4All usage.
- capture_message_content: Flag indicating whether to trace the actual content.
-
- Returns:
- A function that wraps the chat completions method to add telemetry.
+ Generates a telemetry wrapper for GenAI function call
"""
class TracedSyncStream:
"""
- Wrapper for streaming responses to collect metrics and trace data.
- Wraps the 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.
+ Wrapper for streaming responses to collect telemetry.
"""
+
def __init__(
self,
wrapped,
span,
+ span_name,
+ args,
kwargs,
server_address,
server_port,
request_model,
- **args,
):
self.__wrapped__ = wrapped
self._span = span
- # Placeholder for aggregating streaming response
+ self._span_name = span_name
self._llmresponse = ""
-
+ self._request_model = request_model
self._args = args
self._kwargs = kwargs
self._start_time = time.time()
@@ -71,7 +52,7 @@ def generate(version, environment, application_name,
self._tbt = 0
self._server_address = server_address
self._server_port = server_port
- self._request_model = request_model
+ self._tools = None
def __enter__(self):
self.__wrapped__.__enter__()
@@ -90,408 +71,119 @@ def generate(version, environment, application_name,
def __next__(self):
try:
chunk = self.__wrapped__.__next__()
- end_time = time.time()
- # Record the timestamp for the current chunk
- self._timestamps.append(end_time)
-
- if len(self._timestamps) == 1:
- # Calculate time to first chunk
- self._ttft = calculate_ttft(self._timestamps, self._start_time)
-
- self._llmresponse += chunk
+ process_chunk(self, chunk)
return chunk
except StopIteration:
- # Handling exception ensure LLM observability without disrupting operation
try:
- self._end_time = time.time()
-
- if len(self._timestamps) > 1:
- self._tbt = calculate_tbt(self._timestamps)
-
- prompt = self._kwargs.get("prompt") or self._args[0] or ""
-
- # Calculate tokens using input prompt and aggregated response
- input_tokens = general_tokens(prompt)
- output_tokens = general_tokens(self._llmresponse)
-
- # Set Span attributes (OTel Semconv)
- self._span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- self._span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT)
- self._span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_GPT4ALL)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- self._request_model)
- self._span.set_attribute(SemanticConvention.SERVER_PORT,
- self._server_port)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
- self._kwargs.get("repeat_penalty", 1.18))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
- self._kwargs.get("max_tokens", 200))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
- self._kwargs.get("presence_penalty", 0.0))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
- self._kwargs.get("temp", 0.7))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P,
- self._kwargs.get("top_p", 0.4))
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_K,
- self._kwargs.get("top_k", 40))
- self._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
- self._request_model)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,
- input_tokens)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS,
- output_tokens)
- self._span.set_attribute(SemanticConvention.SERVER_ADDRESS,
- self._server_address)
- if isinstance(self._llmresponse, str):
- self._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "text")
- else:
- self._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "json")
-
- # Set Span attributes (Extra)
- self._span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- self._span.set_attribute(SERVICE_NAME,
- application_name)
- self._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM,
- True)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS,
- input_tokens + output_tokens)
- self._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TBT,
- self._tbt)
- self._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT,
- self._ttft)
- self._span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
- version)
- self._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- 0)
- if capture_message_content:
- self._span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: prompt,
- },
- )
- self._span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: self._llmresponse,
- },
+ with tracer.start_as_current_span(self._span_name, kind=SpanKind.CLIENT) as self._span:
+ process_streaming_generate_response(
+ self,
+ pricing_info=pricing_info,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version
)
- self._span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
- system=SemanticConvention.GEN_AI_SYSTEM_GPT4ALL,
- request_model=self._request_model,
- server_address=self._server_address,
- server_port=self._server_port,
- response_model=self._request_model,
- )
-
- metrics["genai_client_usage_tokens"].record(
- input_tokens + output_tokens, attributes
- )
- metrics["genai_client_operation_duration"].record(
- self._end_time - self._start_time, attributes
- )
- metrics["genai_server_tbt"].record(
- self._tbt, attributes
- )
- metrics["genai_server_ttft"].record(
- self._ttft, attributes
- )
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_completion_tokens"].add(output_tokens, attributes)
- metrics["genai_prompt_tokens"].add(input_tokens, attributes)
- metrics["genai_cost"].record(0, 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, and token usage, and handles errors
- gracefully, adding details to the trace for observability.
-
- Args:
- wrapped: The original 'chat.completions' method to be wrapped.
- instance: The instance of the class where the original method is defined.
- args: Positional arguments for the 'chat.completions' method.
- kwargs: Keyword arguments for the 'chat.completions' method.
-
- Returns:
- The response from the original 'chat.completions' method.
+ Wraps the GenAI function call.
"""
# Check if streaming is enabled for the API call
streaming = kwargs.get("streaming", False)
- server_address, server_port = set_server_address_and_port(instance, "localhost", 80)
- request_model = str(instance.model.model_path).rsplit('/', maxsplit=1)[-1] or "orca-mini-3b-gguf2-q4_0.gguf"
+ server_address, server_port = set_server_address_and_port(instance, "127.0.0.1", 80)
+ request_model = str(instance.model.model_path).rsplit("/", maxsplit=1)[-1] or "orca-mini-3b-gguf2-q4_0.gguf"
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
- # pylint: disable=no-else-return
if streaming:
# Special handling for streaming response to accommodate the nature of data flow
awaited_wrapped = wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
-
- return TracedSyncStream(awaited_wrapped, span, kwargs, server_address, server_port, request_model)
+ return TracedSyncStream(awaited_wrapped, span, span_name, args, kwargs, server_address, server_port, request_model)
# Handling for non-streaming responses
else:
- with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
response = wrapped(*args, **kwargs)
- end_time = time.time()
try:
- prompt = kwargs.get("prompt") or args[0] or ""
-
- # Calculate tokens using input prompt and aggregated response
- input_tokens = general_tokens(str(prompt))
- output_tokens = general_tokens(str(response))
-
- # Set Span attributes (OTel Semconv)
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_GPT4ALL)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.SERVER_PORT,
- server_port)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
- kwargs.get("repeat_penalty", 1.18))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
- kwargs.get("max_tokens", 200))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
- kwargs.get("presence_penalty", 0.0))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
- kwargs.get("temp", 0.7))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P,
- kwargs.get("top_p", 0.4))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_K,
- kwargs.get("top_k", 40))
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,
- input_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS,
- output_tokens)
- span.set_attribute(SemanticConvention.SERVER_ADDRESS,
- server_address)
- if isinstance(response, str):
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "text")
- else:
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- "json")
-
- # Set Span attributes (Extra)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM,
- False)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS,
- input_tokens + output_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT,
- end_time - start_time)
- span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
- version)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- 0)
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: prompt,
- },
- )
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: response,
- },
- )
-
- span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
- system=SemanticConvention.GEN_AI_SYSTEM_GPT4ALL,
- request_model=request_model,
- server_address=server_address,
- server_port=server_port,
- response_model=request_model,
- )
-
- metrics["genai_client_usage_tokens"].record(
- input_tokens + output_tokens, attributes
- )
- metrics["genai_client_operation_duration"].record(
- end_time - start_time, attributes
- )
- metrics["genai_server_ttft"].record(
- end_time - start_time, attributes
- )
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_completion_tokens"].add(output_tokens, attributes)
- metrics["genai_prompt_tokens"].add(input_tokens, attributes)
- metrics["genai_cost"].record(0, attributes)
-
- # Return original response
- return response
+ response = process_generate_response(
+ response=response,
+ request_model=request_model,
+ pricing_info=pricing_info,
+ server_port=server_port,
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ span=span,
+ args=args,
+ kwargs=kwargs,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version
+ )
except Exception as e:
handle_exception(span, e)
- logger.error("Error in trace creation: %s", e)
- # Return original response
- return response
+ return response
return wrapper
def embed(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics):
"""
- Generates a telemetry wrapper for embeddings to collect metrics.
-
- Args:
- version: Version of the monitoring package.
- environment: Deployment environment (e.g., production, staging).
- application_name: Name of the application using the GPT4All API.
- tracer: OpenTelemetry tracer for creating spans.
- pricing_info: Information used for calculating GPT4All usage.
- capture_message_content: Flag indicating whether to trace the actual content.
-
- Returns:
- A function that wraps the embeddings method to add telemetry.
+ Generates a telemetry wrapper for GenAI function call
"""
def wrapper(wrapped, instance, args, kwargs):
"""
- Wraps the 'embeddings' API call to add telemetry.
-
- This collects metrics such as execution time, and token usage, and handles errors
- gracefully, adding details to the trace for observability.
-
- Args:
- wrapped: The original 'embeddings' method to be wrapped.
- instance: The instance of the class where the original method is defined.
- args: Positional arguments for the 'embeddings' method.
- kwargs: Keyword arguments for the 'embeddings' method.
-
- Returns:
- The response from the original 'embeddings' method.
+ Wraps the GenAI function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "localhost", 80)
-
- # pylint: disable=line-too-long
- request_model = str(instance.gpt4all.model.model_path).rsplit('/', maxsplit=1)[-1] or "all-MiniLM-L6-v2.gguf2.f16.gguf"
+ server_address, server_port = set_server_address_and_port(instance, "127.0.0.1", 80)
+ request_model = str(instance.gpt4all.model.model_path).rsplit("/", maxsplit=1)[-1] or "all-MiniLM-L6-v2.gguf2.f16.gguf"
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
- with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
response = wrapped(*args, **kwargs)
- end_time = time.time()
try:
- prompt = kwargs.get("prompt") or args[0] or ""
- input_tokens = general_tokens(prompt)
-
- # Set Span attributes (OTel Semconv)
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_GPT4ALL)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
- request_model)
- span.set_attribute(SemanticConvention.SERVER_ADDRESS,
- server_address)
- span.set_attribute(SemanticConvention.SERVER_PORT,
- server_port)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,
- input_tokens)
-
- # Set Span attributes (Extras)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS,
- input_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
- version)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- 0)
-
- if capture_message_content:
- span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: str(kwargs.get("input", "")),
- },
- )
-
- span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
- system=SemanticConvention.GEN_AI_SYSTEM_GPT4ALL,
- request_model=request_model,
- server_address=server_address,
- server_port=server_port,
- response_model=request_model,
- )
- metrics["genai_client_usage_tokens"].record(
- input_tokens, attributes
- )
- metrics["genai_client_operation_duration"].record(
- end_time - start_time, attributes
- )
- metrics["genai_requests"].add(1, attributes)
- metrics["genai_prompt_tokens"].add(input_tokens, attributes)
- metrics["genai_cost"].record(0, attributes)
-
-
- # Return original response
- return response
+ response = process_embedding_response(
+ response=response,
+ request_model=request_model,
+ pricing_info=pricing_info,
+ server_port=server_port,
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ span=span,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ **kwargs
+ )
except Exception as e:
handle_exception(span, e)
- logger.error("Error in trace creation: %s", e)
- # Return original response
- return response
+ return response
return wrapper
diff --git a/sdk/python/src/openlit/instrumentation/gpt4all/utils.py b/sdk/python/src/openlit/instrumentation/gpt4all/utils.py
new file mode 100644
index 00000000..095c067b
--- /dev/null
+++ b/sdk/python/src/openlit/instrumentation/gpt4all/utils.py
@@ -0,0 +1,281 @@
+"""
+GPT4All OpenTelemetry instrumentation utility functions
+"""
+import time
+
+from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.trace import Status, StatusCode
+
+from openlit.__helpers import (
+ calculate_ttft,
+ calculate_tbt,
+ general_tokens,
+ create_metrics_attributes,
+ get_chat_model_cost,
+ get_embed_model_cost,
+)
+from openlit.semcov import SemanticConvention
+
+def format_content(prompt):
+ """
+ Process a prompt to extract content.
+ """
+ return str(prompt) if prompt else ""
+
+def process_chunk(scope, chunk):
+ """
+ Process a chunk of response data and update state.
+ """
+
+ end_time = time.time()
+ # Record the timestamp for the current chunk
+ scope._timestamps.append(end_time)
+
+ if len(scope._timestamps) == 1:
+ # Calculate time to first chunk
+ scope._ttft = calculate_ttft(scope._timestamps, scope._start_time)
+
+ scope._llmresponse += chunk
+ scope._end_time = time.time()
+
+def common_span_attributes(scope, gen_ai_operation, gen_ai_system, server_address, server_port,
+ request_model, response_model, environment, application_name, is_stream, tbt, ttft, version):
+ """
+ Set common span attributes for both generate and embed operations.
+ """
+
+ scope._span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
+ scope._span.set_attribute(SemanticConvention.GEN_AI_OPERATION, gen_ai_operation)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_SYSTEM, gen_ai_system)
+ scope._span.set_attribute(SemanticConvention.SERVER_ADDRESS, server_address)
+ scope._span.set_attribute(SemanticConvention.SERVER_PORT, server_port)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, request_model)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, response_model)
+ scope._span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
+ scope._span.set_attribute(SERVICE_NAME, application_name)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, is_stream)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TBT, tbt)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT, ttft)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, version)
+
+def record_completion_metrics(metrics, gen_ai_operation, gen_ai_system, server_address, server_port,
+ request_model, response_model, environment, application_name, start_time, end_time,
+ input_tokens, output_tokens, cost, tbt=None, ttft=None):
+ """
+ Record completion-specific metrics for the operation.
+ """
+
+ attributes = create_metrics_attributes(
+ operation=gen_ai_operation,
+ system=gen_ai_system,
+ server_address=server_address,
+ server_port=server_port,
+ request_model=request_model,
+ response_model=response_model,
+ service_name=application_name,
+ deployment_environment=environment,
+ )
+ metrics["genai_client_operation_duration"].record(end_time - start_time, attributes)
+ metrics["genai_requests"].add(1, attributes)
+ metrics["genai_prompt_tokens"].add(input_tokens, attributes)
+ metrics["genai_completion_tokens"].add(output_tokens, attributes)
+ metrics["genai_client_usage_tokens"].record(input_tokens + output_tokens, attributes)
+ metrics["genai_cost"].record(cost, attributes)
+ if tbt is not None:
+ metrics["genai_server_tbt"].record(tbt, attributes)
+ if ttft is not None:
+ metrics["genai_server_ttft"].record(ttft, attributes)
+
+def record_embedding_metrics(metrics, gen_ai_operation, gen_ai_system, server_address, server_port,
+ request_model, response_model, environment, application_name, start_time, end_time,
+ input_tokens, cost):
+ """
+ Record embedding-specific metrics for the operation.
+ """
+
+ attributes = create_metrics_attributes(
+ operation=gen_ai_operation,
+ system=gen_ai_system,
+ server_address=server_address,
+ server_port=server_port,
+ request_model=request_model,
+ response_model=response_model,
+ service_name=application_name,
+ deployment_environment=environment,
+ )
+ metrics["genai_client_usage_tokens"].record(input_tokens, attributes)
+ metrics["genai_client_operation_duration"].record(end_time - start_time, attributes)
+ metrics["genai_requests"].add(1, attributes)
+ metrics["genai_prompt_tokens"].add(input_tokens, attributes)
+ metrics["genai_cost"].record(cost, attributes)
+
+def common_generate_logic(scope, pricing_info, environment, application_name, metrics,
+ capture_message_content, disable_metrics, version, is_stream):
+ """
+ Process generate request and generate Telemetry
+ """
+
+ if len(scope._timestamps) > 1:
+ scope._tbt = calculate_tbt(scope._timestamps)
+
+ prompt = format_content(scope._kwargs.get("prompt") or (scope._args[0] if scope._args else "") or "")
+ request_model = scope._request_model
+
+ # Calculate tokens using input prompt and aggregated response
+ input_tokens = general_tokens(prompt)
+ output_tokens = general_tokens(scope._llmresponse)
+
+ cost = get_chat_model_cost(request_model, pricing_info, input_tokens, output_tokens)
+
+ # Common Span Attributes
+ common_span_attributes(scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_GPT4ALL,
+ scope._server_address, scope._server_port, request_model, request_model,
+ environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+
+ # Span Attributes for Request parameters
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY, scope._kwargs.get("repeat_penalty", 1.18))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, scope._kwargs.get("max_tokens", 200))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY, scope._kwargs.get("presence_penalty", 0.0))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, scope._kwargs.get("temp", 0.7))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("top_p", 0.4))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_K, scope._kwargs.get("top_k", 40))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text" if isinstance(scope._llmresponse, str) else "json")
+
+ # Span Attributes for Cost and Tokens
+ scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens + output_tokens)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
+
+ # Span Attributes for Tools
+ if scope._tools:
+ scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, scope._tools.get("function","")).get("name","")
+ scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, str(scope._tools.get("id","")))
+ scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_ARGS, str(scope._tools.get("function","").get("arguments","")))
+
+ # Span Attributes for Content
+ if capture_message_content:
+ scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+
+ # To be removed one the change to span_attributes (from span events) is complete
+ scope._span.add_event(
+ name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
+ attributes={
+ SemanticConvention.GEN_AI_CONTENT_PROMPT: prompt,
+ },
+ )
+ scope._span.add_event(
+ name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,
+ attributes={
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION: scope._llmresponse,
+ },
+ )
+
+ scope._span.set_status(Status(StatusCode.OK))
+
+ # Metrics
+ if not disable_metrics:
+ record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_GPT4ALL,
+ scope._server_address, scope._server_port, request_model, request_model, environment,
+ application_name, scope._start_time, scope._end_time, input_tokens, output_tokens,
+ cost, scope._tbt, scope._ttft)
+
+def common_embedding_logic(scope, pricing_info, environment, application_name, metrics,
+ capture_message_content, disable_metrics, version):
+ """
+ Process embedding request and generate Telemetry
+ """
+
+ prompt = format_content(scope._kwargs.get("prompt") or "")
+ request_model = scope._request_model
+
+ input_tokens = general_tokens(prompt)
+
+ cost = get_embed_model_cost(request_model, pricing_info, input_tokens)
+
+ # Common Span Attributes
+ common_span_attributes(scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING, SemanticConvention.GEN_AI_SYSTEM_GPT4ALL,
+ scope._server_address, scope._server_port, request_model, request_model,
+ environment, application_name, False, scope._tbt, scope._ttft, version)
+
+ # Embedding-specific span attributes
+ scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
+
+ # Span Attributes for Content
+ if capture_message_content:
+ scope._span.add_event(
+ name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
+ attributes={
+ SemanticConvention.GEN_AI_CONTENT_PROMPT: str(scope._kwargs.get("input", "")),
+ },
+ )
+
+ scope._span.set_status(Status(StatusCode.OK))
+
+ # Metrics
+ if not disable_metrics:
+ record_embedding_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING, SemanticConvention.GEN_AI_SYSTEM_GPT4ALL,
+ scope._server_address, scope._server_port, request_model, request_model, environment,
+ application_name, scope._start_time, scope._end_time, input_tokens, cost)
+
+def process_streaming_generate_response(scope, pricing_info, environment, application_name, metrics,
+ capture_message_content=False, disable_metrics=False, version=""):
+ """
+ Process generate request and generate Telemetry
+ """
+ common_generate_logic(scope, pricing_info, environment, application_name, metrics,
+ capture_message_content, disable_metrics, version, is_stream=True)
+
+def process_generate_response(response, request_model, pricing_info, server_port, server_address,
+ environment, application_name, metrics, start_time, span, args, kwargs, capture_message_content=False,
+ disable_metrics=False, version="1.0.0"):
+ """
+ Process generate request and generate Telemetry
+ """
+
+ scope = type("GenericScope", (), {})()
+
+ scope._start_time = start_time
+ scope._end_time = time.time()
+ scope._span = span
+ scope._llmresponse = str(response)
+ scope._request_model = request_model
+ scope._timestamps = []
+ scope._ttft, scope._tbt = scope._end_time - scope._start_time, 0
+ scope._server_address, scope._server_port = server_address, server_port
+ scope._kwargs = kwargs
+ scope._args = args
+ scope._tools = None
+
+ common_generate_logic(scope, pricing_info, environment, application_name, metrics,
+ capture_message_content, disable_metrics, version, is_stream=False)
+
+ return response
+
+def process_embedding_response(response, request_model, pricing_info, server_port, server_address,
+ environment, application_name, metrics, start_time, span, capture_message_content=False,
+ disable_metrics=False, version="1.0.0", **kwargs):
+ """
+ Process embedding request and generate Telemetry
+ """
+
+ scope = type("GenericScope", (), {})()
+
+ scope._start_time = start_time
+ scope._end_time = time.time()
+ scope._span = span
+ scope._request_model = request_model
+ scope._timestamps = []
+ scope._ttft, scope._tbt = scope._end_time - scope._start_time, 0
+ scope._server_address, scope._server_port = server_address, server_port
+ scope._kwargs = kwargs
+
+ common_embedding_logic(scope, pricing_info, environment, application_name, metrics,
+ capture_message_content, disable_metrics, version)
+
+ return response
\ No newline at end of file
diff --git a/sdk/python/src/openlit/instrumentation/premai/__init__.py b/sdk/python/src/openlit/instrumentation/premai/__init__.py
index 2b38ef55..d3e4fb81 100644
--- a/sdk/python/src/openlit/instrumentation/premai/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/premai/__init__.py
@@ -20,8 +20,8 @@ class PremAIInstrumentor(BaseInstrumentor):
return _instruments
def _instrument(self, **kwargs):
- application_name = kwargs.get("application_name", "default_application")
- environment = kwargs.get("environment", "default_environment")
+ application_name = kwargs.get("application_name", "default")
+ environment = kwargs.get("environment", "default")
tracer = kwargs.get("tracer")
metrics = kwargs.get("metrics_dict")
pricing_info = kwargs.get("pricing_info", {})
diff --git a/sdk/python/src/openlit/instrumentation/premai/utils.py b/sdk/python/src/openlit/instrumentation/premai/utils.py
index 95a36692..4a88ca30 100644
--- a/sdk/python/src/openlit/instrumentation/premai/utils.py
+++ b/sdk/python/src/openlit/instrumentation/premai/utils.py
@@ -64,6 +64,7 @@ def process_chunk(scope, chunk):
scope._finish_reason = chunked.get("choices")[0].get("finish_reason")
scope._response_id = chunked.get("id")
scope._response_model = chunked.get("model")
+ scope._end_time = time.time()
def common_span_attributes(scope, gen_ai_operation, gen_ai_system, server_address, server_port,
request_model, response_model, environment, application_name, is_stream, tbt, ttft, version):
@@ -77,12 +78,12 @@ def common_span_attributes(scope, gen_ai_operation, gen_ai_system, server_addres
scope._span.set_attribute(SemanticConvention.SERVER_ADDRESS, server_address)
scope._span.set_attribute(SemanticConvention.SERVER_PORT, server_port)
scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, request_model)
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, scope._response_model)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, response_model)
scope._span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
scope._span.set_attribute(SERVICE_NAME, application_name)
scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, is_stream)
- scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TBT, scope._tbt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT, scope._ttft)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TBT, tbt)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT, ttft)
scope._span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, version)
def record_completion_metrics(metrics, gen_ai_operation, gen_ai_system, server_address, server_port,
diff --git a/sdk/python/src/openlit/instrumentation/reka/utils.py b/sdk/python/src/openlit/instrumentation/reka/utils.py
index 9ce9279e..0f46c7db 100644
--- a/sdk/python/src/openlit/instrumentation/reka/utils.py
+++ b/sdk/python/src/openlit/instrumentation/reka/utils.py
@@ -48,12 +48,12 @@ def common_span_attributes(scope, gen_ai_operation, gen_ai_system, server_addres
scope._span.set_attribute(SemanticConvention.SERVER_ADDRESS, server_address)
scope._span.set_attribute(SemanticConvention.SERVER_PORT, server_port)
scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, request_model)
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, scope._response_model)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, response_model)
scope._span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
scope._span.set_attribute(SERVICE_NAME, application_name)
scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, is_stream)
- scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TBT, scope._tbt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT, scope._ttft)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TBT, tbt)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT, ttft)
scope._span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, version)
def record_common_metrics(metrics, gen_ai_operation, gen_ai_system, server_address, server_port,
diff --git a/sdk/python/src/openlit/instrumentation/together/utils.py b/sdk/python/src/openlit/instrumentation/together/utils.py
index 8b213622..9c0021a7 100644
--- a/sdk/python/src/openlit/instrumentation/together/utils.py
+++ b/sdk/python/src/openlit/instrumentation/together/utils.py
@@ -80,12 +80,12 @@ def common_span_attributes(scope, gen_ai_operation, gen_ai_system, server_addres
scope._span.set_attribute(SemanticConvention.SERVER_ADDRESS, server_address)
scope._span.set_attribute(SemanticConvention.SERVER_PORT, server_port)
scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, request_model)
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, scope._response_model)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, response_model)
scope._span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
scope._span.set_attribute(SERVICE_NAME, application_name)
scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, is_stream)
- scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TBT, scope._tbt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT, scope._ttft)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TBT, tbt)
+ scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT, ttft)
scope._span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, version)
def record_common_metrics(metrics, gen_ai_operation, gen_ai_system, server_address, server_port,