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
[Feat]: Update Opentelemetry instrumentation for Mistral AI to support `mistralai` Python SDK `1.0.0` (#376)
patcher96bae431fa3d8
diff --git a/docs/latest/integrations/mistral.mdx b/docs/latest/integrations/mistral.mdxindex f5e32d6e..2eb2dce5 100644--- a/docs/latest/integrations/mistral.mdx+++ b/docs/latest/integrations/mistral.mdx@@ -12,7 +12,7 @@ OpenLIT uses OpenTelemetry Auto-Instrumentation to help you monitor LLM applicatAuto-instrumentation means you don't have to set up monitoring manually for different LLMs, frameworks, or databases. By simply adding OpenLIT in your application, all the necessary monitoring configurations are automatically set up.The integration is compatible with-- Mistral Python SDK client `>= 0.1.0`+- Mistral Python SDK client `>= 1.0.0`## Get Starteddiff --git a/sdk/python/pyproject.toml b/sdk/python/pyproject.tomlindex b8800799..172ad053 100644--- a/sdk/python/pyproject.toml+++ b/sdk/python/pyproject.toml@@ -1,12 +1,12 @@[tool.poetry]name = "openlit"-version = "1.18.1"-description = "OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications, facilitating the integration of observability into your GenAI-driven projects"+version = "1.18.2"+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"readme = "README.md"homepage = "https://github.com/openlit/openlit/tree/main/openlit/python"-keywords = ["OpenTelemetry", "otel", "otlp","llm", "tracing", "openai", "anthropic", "claude", "cohere", "llm monitoring", "observability", "monitoring", "gpt", "Generative AI", "chatGPT"]+keywords = ["OpenTelemetry", "otel", "otlp","llm", "tracing", "openai", "anthropic", "claude", "cohere", "llm monitoring", "observability", "monitoring", "gpt", "Generative AI", "chatGPT", "gpu"][tool.poetry.dependencies]python = "^3.7.1"diff --git a/sdk/python/src/openlit/instrumentation/mistral/__init__.py b/sdk/python/src/openlit/instrumentation/mistral/__init__.pyindex 9027d21d..123f6e4b 100644--- a/sdk/python/src/openlit/instrumentation/mistral/__init__.py+++ b/sdk/python/src/openlit/instrumentation/mistral/__init__.py@@ -9,10 +9,10 @@ from openlit.instrumentation.mistral.mistral import chat, chat_stream, embeddingfrom openlit.instrumentation.mistral.async_mistral import async_chat, async_chat_streamfrom openlit.instrumentation.mistral.async_mistral import async_embeddings-_instruments = ("mistralai >= 0.1.0",)+_instruments = ("mistralai >= 1.0.0",)class MistralInstrumentor(BaseInstrumentor):- """An instrumentor for Azure Mistral's client library."""+ """An instrumentor for Mistral's client library."""def instrumentation_dependencies(self) -> Collection[str]:return _instruments@@ -27,50 +27,50 @@ class MistralInstrumentor(BaseInstrumentor):disable_metrics = kwargs.get("disable_metrics")version = importlib.metadata.version("mistralai")- #sync+ # syncwrap_function_wrapper(- "mistralai.client",- "MistralClient.chat",+ "mistralai.chat",+ "Chat.complete",chat("mistral.chat", version, environment, application_name,tracer, pricing_info, trace_content, metrics, disable_metrics),)- #sync+ # syncwrap_function_wrapper(- "mistralai.client",- "MistralClient.chat_stream",+ "mistralai.chat",+ "Chat.stream",chat_stream("mistral.chat", version, environment, application_name,tracer, pricing_info, trace_content, metrics, disable_metrics),)- #sync+ # syncwrap_function_wrapper(- "mistralai.client",- "MistralClient.embeddings",+ "mistralai.embeddings",+ "Embeddings.create",embeddings("mistral.embeddings", version, environment, application_name,tracer, pricing_info, trace_content, metrics, disable_metrics),)# Asyncwrap_function_wrapper(- "mistralai.async_client",- "MistralAsyncClient.chat",+ "mistralai.chat",+ "Chat.complete_async",async_chat("mistral.chat", version, environment, application_name,tracer, pricing_info, trace_content, metrics, disable_metrics),)- #sync+ # Asyncwrap_function_wrapper(- "mistralai.async_client",- "MistralAsyncClient.chat_stream",+ "mistralai.chat",+ "Chat.stream_async",async_chat_stream("mistral.chat", version, environment, application_name,tracer, pricing_info, trace_content, metrics, disable_metrics),)#syncwrap_function_wrapper(- "mistralai.async_client",- "MistralAsyncClient.embeddings",+ "mistralai.embeddings",+ "Embeddings.create_async",async_embeddings("mistral.embeddings", version, environment, application_name,tracer, pricing_info, trace_content, metrics, disable_metrics),)diff --git a/sdk/python/src/openlit/instrumentation/mistral/async_mistral.py b/sdk/python/src/openlit/instrumentation/mistral/async_mistral.pyindex 9603711e..2673123a 100644--- a/sdk/python/src/openlit/instrumentation/mistral/async_mistral.py+++ b/sdk/python/src/openlit/instrumentation/mistral/async_mistral.py@@ -56,8 +56,8 @@ def async_chat(gen_ai_endpoint, version, environment, application_name,message_prompt = kwargs.get('messages', "")formatted_messages = []for message in message_prompt:- role = message.role- content = message.content+ role = message["role"]+ content = message["content"]if isinstance(content, list):content_str = ", ".join(@@ -207,14 +207,14 @@ def async_chat_stream(gen_ai_endpoint, version, environment, application_name,llmresponse = ""# Loop through streaming events capturing relevant details- async for event in wrapped(*args, **kwargs):- response_id = event.id- llmresponse += event.choices[0].delta.content- if event.usage is not None:- prompt_tokens = event.usage.prompt_tokens- completion_tokens = event.usage.completion_tokens- total_tokens = event.usage.total_tokens- finish_reason = event.choices[0].finish_reason+ async for event in await wrapped(*args, **kwargs):+ response_id = event.data.id+ llmresponse += event.data.choices[0].delta.content+ if event.data.usage is not None:+ prompt_tokens = event.data.usage.prompt_tokens+ completion_tokens = event.data.usage.completion_tokens+ total_tokens = event.data.usage.total_tokens+ finish_reason = event.data.choices[0].finish_reasonyield event# Handling exception ensure observability without disrupting operation@@ -223,8 +223,8 @@ def async_chat_stream(gen_ai_endpoint, version, environment, application_name,message_prompt = kwargs.get('messages', "")formatted_messages = []for message in message_prompt:- role = message.role- content = message.content+ role = message["role"]+ content = message["content"]if isinstance(content, list):content_str = ", ".join(@@ -364,7 +364,7 @@ def async_embeddings(gen_ai_endpoint, version, environment, application_name,try:# Get prompt from kwargs and store as a single string- prompt = ', '.join(kwargs.get('input', []))+ prompt = ', '.join(kwargs.get('inputs', []))# Calculate cost of the operationcost = get_embed_model_cost(kwargs.get('model', "mistral-embed"),diff --git a/sdk/python/src/openlit/instrumentation/mistral/mistral.py b/sdk/python/src/openlit/instrumentation/mistral/mistral.pyindex 983d63eb..d00410ee 100644--- a/sdk/python/src/openlit/instrumentation/mistral/mistral.py+++ b/sdk/python/src/openlit/instrumentation/mistral/mistral.py@@ -55,8 +55,8 @@ def chat(gen_ai_endpoint, version, environment, application_name,message_prompt = kwargs.get('messages', "")formatted_messages = []for message in message_prompt:- role = message.role- content = message.content+ role = message["role"]+ content = message["content"]if isinstance(content, list):content_str = ", ".join(@@ -207,13 +207,13 @@ def chat_stream(gen_ai_endpoint, version, environment, application_name,# Loop through streaming events capturing relevant detailsfor event in wrapped(*args, **kwargs):- response_id = event.id- llmresponse += event.choices[0].delta.content- if event.usage is not None:- prompt_tokens = event.usage.prompt_tokens- completion_tokens = event.usage.completion_tokens- total_tokens = event.usage.total_tokens- finish_reason = event.choices[0].finish_reason+ response_id = event.data.id+ llmresponse += event.data.choices[0].delta.content+ if event.data.usage is not None:+ prompt_tokens = event.data.usage.prompt_tokens+ completion_tokens = event.data.usage.completion_tokens+ total_tokens = event.data.usage.total_tokens+ finish_reason = event.data.choices[0].finish_reasonyield event# Handling exception ensure observability without disrupting operation@@ -222,8 +222,8 @@ def chat_stream(gen_ai_endpoint, version, environment, application_name,message_prompt = kwargs.get('messages', "")formatted_messages = []for message in message_prompt:- role = message.role- content = message.content+ role = message["role"]+ content = message["content"]if isinstance(content, list):content_str = ", ".join(@@ -363,7 +363,7 @@ def embeddings(gen_ai_endpoint, version, environment, application_name,try:# Get prompt from kwargs and store as a single string- prompt = ', '.join(kwargs.get('input', []))+ prompt = ', '.join(kwargs.get('inputs', []))# Calculate cost of the operationcost = get_embed_model_cost(kwargs.get('model', "mistral-embed"),diff --git a/sdk/python/tests/requirements.txt b/sdk/python/tests/requirements.txtindex d208e8bb..93d77488 100644--- a/sdk/python/tests/requirements.txt+++ b/sdk/python/tests/requirements.txt@@ -9,7 +9,7 @@ pylint>=3.0.2cohere>=5.0.0openai>=1.1.1anthropic>=0.21.0-mistralai>=0.1.0+mistralai>=1.0.0transformers>=4.39.3boto3>=1.34.93botocore>=1.34.93diff --git a/sdk/python/tests/test_mistral.py.hold b/sdk/python/tests/test_mistral.pysimilarity index 72%rename from sdk/python/tests/test_mistral.py.holdrename to sdk/python/tests/test_mistral.pyindex 0dfd7976..efe0141d 100644--- a/sdk/python/tests/test_mistral.py.hold+++ b/sdk/python/tests/test_mistral.py@@ -14,18 +14,11 @@ prior to running these tests.import osimport pytest-from mistralai.client import MistralClient-from mistralai.async_client import MistralAsyncClient-from mistralai.models.chat_completion import ChatMessage+from mistralai import Mistralimport openlit# Initialize synchronous Mistral client-sync_client = MistralClient(- api_key=os.getenv("MISTRAL_API_TOKEN")-)--# Initialize asynchronous Mistral client-async_client = MistralAsyncClient(+client = Mistral(api_key=os.getenv("MISTRAL_API_TOKEN"))@@ -41,10 +34,13 @@ def test_sync_mistral_chat():"""messages = [- ChatMessage(role="user", content="What is the best French cheese?")+ {+ "role": "user",+ "content": "sync: What is LLM Observability?",+ },]- message = sync_client.chat(+ message = client.chat.complete(model="open-mistral-7b",messages=messages,max_tokens=1,@@ -59,9 +55,9 @@ def test_sync_mistral_embeddings():AssertionError: If the embedding response object is not as expected."""- response = sync_client.embeddings(+ response = client.embeddings.create(model="mistral-embed",- input=["Embed this sentence.", "As well as this one."],+ inputs=["Embed this sentence.", "OpenTelemetry LLM Observability"],)assert response.object == 'list'@@ -76,10 +72,13 @@ async def test_async_mistral():# Tests synchronous chat with the 'open-mistral-7b' model.messages = [- ChatMessage(role="user", content="What is the best French cheese?")+ {+ "role": "user",+ "content": "sync: What is LLM Observability?",+ },]- message = await async_client.chat(+ message = await client.chat.complete_async(model="open-mistral-7b",messages=messages,max_tokens=1,@@ -87,8 +86,8 @@ async def test_async_mistral():assert message.object == 'chat.completion'# Tests asynchronous embedding creation with the 'mistral-embed' model.- response = await async_client.embeddings(+ response = await client.embeddings.create_async(model="mistral-embed",- input=["Embed this sentence.", "As well as this one."],+ inputs=["Embed this sentence.", "Monitor LLM Applications"],)assert response.object == 'list'