Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.
TypeScript agent-harnessagent-harness-engineeringai-engineeringai-observabilityamd-gpuclaude-codeclickhousecodexcoding-agentcoding-agentscursorgenaigpu-monitoringllm-observabilityllmopsnvidia-smiobservabilityopen-sourceopenaiopentelemetry
add test
patcher9e7e4ec7490b9
diff --git a/assets/pricing.json b/assets/pricing.jsonindex 8b863222..4645e400 100644--- a/assets/pricing.json+++ b/assets/pricing.json@@ -72,7 +72,14 @@},"audio": {"tts-1" : 0.015,- "tts-1-hd": 0.030+ "tts-1-hd": 0.030,+ "eleven_multilingual_v2": 0.24,+ "eleven_multilingual_v1": 0.24,+ "eleven_monolingual_v1": 0.24,+ "eleven_english_v1": 0.24,+ "eleven_turbo_v2": 0.24,+ "eleven_english_sts_v2": 0.24,+ "eleven_multilingual_sts_v2": 0.24},"chat": {"gpt-3.5-turbo": {diff --git a/sdk/python/src/openlit/__init__.py b/sdk/python/src/openlit/__init__.pyindex ddf649f3..2c224f16 100644--- a/sdk/python/src/openlit/__init__.py+++ b/sdk/python/src/openlit/__init__.py@@ -31,6 +31,7 @@ from openlit.instrumentation.vertexai import VertexAIInstrumentorfrom openlit.instrumentation.groq import GroqInstrumentorfrom openlit.instrumentation.ollama import OllamaInstrumentorfrom openlit.instrumentation.gpt4all import GPT4AllInstrumentor+from openlit.instrumentation.elevenlabs import ElevenLabsInstrumentorfrom openlit.instrumentation.langchain import LangChainInstrumentorfrom openlit.instrumentation.llamaindex import LlamaIndexInstrumentorfrom openlit.instrumentation.haystack import HaystackInstrumentor@@ -192,6 +193,7 @@ def init(environment="default", application_name="default", tracer=None, otlp_en"groq": "groq","ollama": "ollama","gpt4all": "gpt4all",+ "elevenlabs": "elevenlabs","langchain": "langchain","llama_index": "llama_index","haystack": "haystack",@@ -267,6 +269,7 @@ def init(environment="default", application_name="default", tracer=None, otlp_en"groq": GroqInstrumentor(),"ollama": OllamaInstrumentor(),"gpt4all": GPT4AllInstrumentor(),+ "elevenlabs": ElevenLabsInstrumentor(),"langchain": LangChainInstrumentor(),"llama_index": LlamaIndexInstrumentor(),"haystack": HaystackInstrumentor(),diff --git a/sdk/python/src/openlit/instrumentation/elevenlabs/__init__.py b/sdk/python/src/openlit/instrumentation/elevenlabs/__init__.pynew file mode 100644index 00000000..bd8e449c--- /dev/null+++ b/sdk/python/src/openlit/instrumentation/elevenlabs/__init__.py@@ -0,0 +1,55 @@+# pylint: disable=useless-return, bad-staticmethod-argument, disable=duplicate-code+"""Initializer of Auto Instrumentation of ElevenLabs Functions"""++from typing import Collection+import importlib.metadata+from opentelemetry.instrumentation.instrumentor import BaseInstrumentor+from wrapt import wrap_function_wrapper++from openlit.instrumentation.elevenlabs.elevenlabs import (+ generate+)+from openlit.instrumentation.elevenlabs.async_elevenlabs import (+ async_generate+)++_instruments = ("elevenlabs >= 1.4.0",)++class ElevenLabsInstrumentor(BaseInstrumentor):+ """+ An instrumentor for ElevenLabs's client library.+ """++ def instrumentation_dependencies(self) -> Collection[str]:+ return _instruments++ def _instrument(self, **kwargs):+ 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", {})+ trace_content = kwargs.get("trace_content", False)+ disable_metrics = kwargs.get("disable_metrics")+ version = importlib.metadata.version("elevenlabs")++ # sync generate+ wrap_function_wrapper(+ "elevenlabs.client",+ "ElevenLabs.generate",+ generate("elevenlabs.generate", version, environment, application_name,+ tracer, pricing_info, trace_content, metrics, disable_metrics),+ )++ # async generate+ wrap_function_wrapper(+ "elevenlabs.client",+ "AsyncElevenLabs.generate",+ async_generate("elevenlabs.generate", version, environment, application_name,+ tracer, pricing_info, trace_content, metrics, disable_metrics),+ )+++ def _uninstrument(self, **kwargs):+ # Proper uninstrumentation logic to revert patched methods+ passdiff --git a/sdk/python/src/openlit/instrumentation/elevenlabs/async_elevenlabs.py b/sdk/python/src/openlit/instrumentation/elevenlabs/async_elevenlabs.pynew file mode 100644index 00000000..7975b9af--- /dev/null+++ b/sdk/python/src/openlit/instrumentation/elevenlabs/async_elevenlabs.py@@ -0,0 +1,117 @@+# pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument, possibly-used-before-assignment+"""+Module for monitoring Ollama API calls.+"""++import logging+from opentelemetry.trace import SpanKind, Status, StatusCode+from opentelemetry.sdk.resources import TELEMETRY_SDK_NAME+from openlit.__helpers import get_audio_model_cost+from openlit.__helpers import handle_exception, general_tokens+from openlit.semcov import SemanticConvetion++# Initialize logger for logging potential issues and operations+logger = logging.getLogger(__name__)++def async_generate(gen_ai_endpoint, version, environment, application_name,+ tracer, pricing_info, trace_content, metrics, disable_metrics):+ """+ Generates a telemetry wrapper for creating speech audio to collect metrics.++ Args:+ gen_ai_endpoint: Endpoint identifier for logging and tracing.+ version: Version of the monitoring package.+ environment: Deployment environment (e.g., production, staging).+ application_name: Name of the application using the ElevenLabs API.+ tracer: OpenTelemetry tracer for creating spans.+ pricing_info: Information used for calculating the cost of generating speech audio.+ trace_content: Flag indicating whether to trace the input text and generated audio.++ Returns:+ A function that wraps the speech audio creation method to add telemetry.+ """++ async def wrapper(wrapped, instance, args, kwargs):+ """+ Wraps the 'generate' API call to add telemetry.++ This collects metrics such as execution time, cost, and handles errors+ gracefully, adding details to the trace for observability.++ Args:+ wrapped: The original 'generate' method to be wrapped.+ instance: The instance of the class where the original method is defined.+ args: Positional arguments for the 'generate' method.+ kwargs: Keyword arguments for the 'generate' method.++ Returns:+ The response from the original 'generate' method.+ """++ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:+ response = await wrapped(*args, **kwargs)++ try:+ # Calculate cost of the operation+ cost = get_audio_model_cost(kwargs.get("model", "eleven_multilingual_v2"),+ pricing_info, kwargs.get("text", ""))++ # Set Span attributes+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,+ SemanticConvetion.GEN_AI_SYSTEM_ELEVENLABS)+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,+ SemanticConvetion.GEN_AI_TYPE_AUDIO)+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,+ gen_ai_endpoint)+ 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", "eleven_multilingual_v2"))+ if isinstance(kwargs.get("voice", "Rachel"), str):+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_AUDIO_VOICE,+ kwargs.get("voice", "Rachel"))+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_AUDIO_RESPONSE_FORMAT,+ kwargs.get("output_format", "mp3"))+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_AUDIO_SETTINGS,+ str(kwargs.get("voice_settings", "")))+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,+ cost)+ if trace_content:+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,+ str(kwargs.get("text", "")))++ 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_ELEVENLABS,+ SemanticConvetion.GEN_AI_ENVIRONMENT:+ environment,+ SemanticConvetion.GEN_AI_TYPE:+ SemanticConvetion.GEN_AI_TYPE_AUDIO,+ SemanticConvetion.GEN_AI_REQUEST_MODEL:+ kwargs.get("model", "eleven_multilingual_v2")+ }++ metrics["genai_requests"].add(1, attributes)+ metrics["genai_cost"].record(cost, attributes)++ # Return original response+ return response++ except Exception as e:+ handle_exception(span, e)+ logger.error("Error in trace creation: %s", e)++ # Return original response+ return response++ return wrapper\ No newline at end of filediff --git a/sdk/python/src/openlit/instrumentation/elevenlabs/elevenlabs.py b/sdk/python/src/openlit/instrumentation/elevenlabs/elevenlabs.pynew file mode 100644index 00000000..6d6470c1--- /dev/null+++ b/sdk/python/src/openlit/instrumentation/elevenlabs/elevenlabs.py@@ -0,0 +1,117 @@+# pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument, possibly-used-before-assignment+"""+Module for monitoring Ollama API calls.+"""++import logging+from opentelemetry.trace import SpanKind, Status, StatusCode+from opentelemetry.sdk.resources import TELEMETRY_SDK_NAME+from openlit.__helpers import get_audio_model_cost+from openlit.__helpers import handle_exception, general_tokens+from openlit.semcov import SemanticConvetion++# Initialize logger for logging potential issues and operations+logger = logging.getLogger(__name__)++def generate(gen_ai_endpoint, version, environment, application_name,+ tracer, pricing_info, trace_content, metrics, disable_metrics):+ """+ Generates a telemetry wrapper for creating speech audio to collect metrics.++ Args:+ gen_ai_endpoint: Endpoint identifier for logging and tracing.+ version: Version of the monitoring package.+ environment: Deployment environment (e.g., production, staging).+ application_name: Name of the application using the ElevenLabs API.+ tracer: OpenTelemetry tracer for creating spans.+ pricing_info: Information used for calculating the cost of generating speech audio.+ trace_content: Flag indicating whether to trace the input text and generated audio.++ Returns:+ A function that wraps the speech audio creation method to add telemetry.+ """++ def wrapper(wrapped, instance, args, kwargs):+ """+ Wraps the 'generate' API call to add telemetry.++ This collects metrics such as execution time, cost, and handles errors+ gracefully, adding details to the trace for observability.++ Args:+ wrapped: The original 'generate' method to be wrapped.+ instance: The instance of the class where the original method is defined.+ args: Positional arguments for the 'generate' method.+ kwargs: Keyword arguments for the 'generate' method.++ Returns:+ The response from the original 'generate' method.+ """++ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:+ response = wrapped(*args, **kwargs)++ try:+ # Calculate cost of the operation+ cost = get_audio_model_cost(kwargs.get("model", "eleven_multilingual_v2"),+ pricing_info, kwargs.get("text", ""))++ # Set Span attributes+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,+ SemanticConvetion.GEN_AI_SYSTEM_ELEVENLABS)+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,+ SemanticConvetion.GEN_AI_TYPE_AUDIO)+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,+ gen_ai_endpoint)+ 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", "eleven_multilingual_v2"))+ if isinstance(kwargs.get("voice", "Rachel"), str):+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_AUDIO_VOICE,+ kwargs.get("voice", "Rachel"))+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_AUDIO_RESPONSE_FORMAT,+ kwargs.get("output_format", "mp3"))+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_AUDIO_SETTINGS,+ str(kwargs.get("voice_settings", "")))+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,+ cost)+ if trace_content:+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,+ str(kwargs.get("text", "")))++ 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_ELEVENLABS,+ SemanticConvetion.GEN_AI_ENVIRONMENT:+ environment,+ SemanticConvetion.GEN_AI_TYPE:+ SemanticConvetion.GEN_AI_TYPE_AUDIO,+ SemanticConvetion.GEN_AI_REQUEST_MODEL:+ kwargs.get("model", "eleven_multilingual_v2")+ }++ metrics["genai_requests"].add(1, attributes)+ metrics["genai_cost"].record(cost, attributes)++ # Return original response+ return response++ except Exception as e:+ handle_exception(span, e)+ logger.error("Error in trace creation: %s", e)++ # Return original response+ return response++ return wrapper\ No newline at end of filediff --git a/sdk/python/src/openlit/semcov/__init__.py b/sdk/python/src/openlit/semcov/__init__.pyindex 683fc012..57a2b75d 100644--- a/sdk/python/src/openlit/semcov/__init__.py+++ b/sdk/python/src/openlit/semcov/__init__.py@@ -45,6 +45,7 @@ class SemanticConvetion:GEN_AI_REQUEST_AUDIO_VOICE = "gen_ai.request.audio_voice"GEN_AI_REQUEST_AUDIO_RESPONSE_FORMAT = "gen_ai.request.audio_response_format"GEN_AI_REQUEST_AUDIO_SPEED = "gen_ai.request.audio_speed"+ GEN_AI_REQUEST_AUDIO_SETTINGS = "gen_ai.request.audio_settings"GEN_AI_REQUEST_FINETUNE_STATUS = "gen_ai.request.fine_tune_status"GEN_AI_REQUEST_FINETUNE_MODEL_SUFFIX = "gen_ai.request.fine_tune_model_suffix"GEN_AI_REQUEST_FINETUNE_MODEL_EPOCHS = "gen_ai.request.fine_tune_n_epochs"@@ -97,6 +98,7 @@ class SemanticConvetion:GEN_AI_SYSTEM_GROQ = "groq"GEN_AI_SYSTEM_OLLAMA = "ollama"GEN_AI_SYSTEM_GPT4ALL = "gpt4all"+ GEN_AI_SYSTEM_ELEVENLABS = "elevenlabs"GEN_AI_SYSTEM_LANGCHAIN = "langchain"GEN_AI_SYSTEM_LLAMAINDEX = "llama_index"GEN_AI_SYSTEM_HAYSTACK = "haystack"diff --git a/sdk/python/tests/test_elevenlabs.py b/sdk/python/tests/test_elevenlabs.pynew file mode 100644index 00000000..5bf93899--- /dev/null+++ b/sdk/python/tests/test_elevenlabs.py@@ -0,0 +1,94 @@+# pylint: disable=duplicate-code, no-name-in-module+"""+This module contains tests for ElevenLabs functionality using the ElevenLabs Python library.++Tests cover various API endpoints, including chat.+These tests validate integration with OpenLIT.++Environment Variables:+ - ELEVENLABS_API_TOKEN: GroElevenLabsq API api_key for authentication.++Note: Ensure the environment is properly configured for ElevenLabs access and OpenLIT monitoring+prior to running these tests.+"""++import os+import pytest+import types+from elevenlabs.client import ElevenLabs, AsyncElevenLabs+import openlit++# Initialize synchronous ElevenLabs client+sync_client = ElevenLabs(+ api_key=os.getenv("ELEVENLABS_API_TOKEN")+)++async_client = AsyncElevenLabs(+ api_key=os.getenv("ELEVENLABS_API_TOKEN")+)++# Initialize environment and application name for OpenLIT monitoring+openlit.init(environment="openlit-testing", application_name="openlit-python-test")++def test_sync_elevenlabs_generate():+ """+ Tests synchronous generate text-to-speech with the 'eleven_multilingual_v2' model.++ Raises:+ AssertionError: If the generate text-to-speech response object is not as expected.+ """++ try:+ audio = sync_client.generate(+ text="Say LLM Monitoring",+ voice="Rachel",+ model="eleven_multilingual_v2"+ )+ assert isinstance(audio, types.GeneratorType)++ audio = sync_client.generate(+ text="Say LLM Observability!",+ voice="Rachel",+ model="eleven_multilingual_v2",+ stream=True+ )+ assert isinstance(audio, types.GeneratorType)++ # pylint: disable=broad-exception-caught+ except Exception as e:+ if "rate limit" in str(e).lower():+ print("Rate Limited:", e)+ else:+ raise++async def test_async_elevenlabs_generate():+ """+ Tests synchronous generate text-to-speech with the 'eleven_multilingual_v2' model.++ Raises:+ AssertionError: If the generate text-to-speech response object is not as expected.+ """++ try:+ audio = await async_client.generate(+ text="Say LLM Monitoring",+ voice="Rachel",+ model="eleven_multilingual_v2"+ )+ assert isinstance(audio, types.AsyncGeneratorType)++ audio = await async_client.generate(+ text="Say LLM Observability!",+ voice="Rachel",+ model="eleven_multilingual_v2",+ stream=True+ )+ assert isinstance(audio, types.AsyncGeneratorType)++ # pylint: disable=broad-exception-caught+ except Exception as e:+ if "rate limit" in str(e).lower():+ print("Rate Limited:", e)+ else:+ raise