-
Notifications
You must be signed in to change notification settings - Fork 674
feat(mistral): Add integration with Chat.complete and Chat.complete_async patches
#7528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
eba2bae
feat(mistral): Add integration with Chat.complete and Chat.complete_a…
alexander-alderman-webb 4b57f72
.
alexander-alderman-webb 08b3eb0
.
alexander-alderman-webb e5c7c9a
.
alexander-alderman-webb cb02dfa
merge master and address comments
alexander-alderman-webb 4e2b954
.
alexander-alderman-webb 4b631ad
.
alexander-alderman-webb 9cc9bef
take out of auto-enabling
alexander-alderman-webb e21572c
handle None Model
alexander-alderman-webb ae22b23
generate GH workflows
alexander-alderman-webb 868ea47
raise min version to 2.0.5
alexander-alderman-webb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,6 +92,7 @@ | |
| "google_genai", | ||
| "huggingface_hub", | ||
| "litellm", | ||
| "mistral", | ||
| "openai-base", | ||
| "openai-notiktoken", | ||
| ], | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| from functools import wraps | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import sentry_sdk | ||
| from sentry_sdk.ai.utils import ( | ||
| get_start_span_function, | ||
| ) | ||
| from sentry_sdk.consts import OP, SPANDATA | ||
| from sentry_sdk.integrations import DidNotEnable, Integration | ||
| from sentry_sdk.tracing_utils import ( | ||
| has_span_streaming_enabled, | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Any, Callable | ||
|
|
||
| try: | ||
| from mistralai.client.chat import Chat | ||
| except ImportError: | ||
| raise DidNotEnable("mistralai not installed") | ||
|
|
||
|
|
||
| class MistralIntegration(Integration): | ||
| identifier = "mistral" | ||
| origin = f"auto.ai.{identifier}" | ||
|
|
||
| @staticmethod | ||
| def setup_once() -> None: | ||
| Chat.complete = _wrap_complete(Chat.complete) # type: ignore[method-assign] | ||
|
|
||
| Chat.complete_async = _wrap_complete_async(Chat.complete_async) # type: ignore[method-assign] | ||
|
|
||
|
|
||
| def _wrap_complete(f: "Callable[..., Any]") -> "Callable[..., Any]": | ||
| @wraps(f) | ||
| def wrap_complete(self: "Chat", *args: "Any", **kwargs: "Any") -> "Any": | ||
| client = sentry_sdk.get_client() | ||
| integration = client.get_integration(MistralIntegration) | ||
| if integration is None or kwargs.get("stream"): | ||
| return f(self, *args, **kwargs) | ||
|
|
||
| model = kwargs.get("model") | ||
|
|
||
| if has_span_streaming_enabled(client.options): | ||
| span = sentry_sdk.traces.start_span( | ||
| name=f"chat {model}" if model is not None else "chat", | ||
| attributes={ | ||
| "sentry.op": OP.GEN_AI_CHAT, | ||
| "sentry.origin": MistralIntegration.origin, | ||
| SPANDATA.GEN_AI_PROVIDER_NAME: "mistral", | ||
| SPANDATA.GEN_AI_OPERATION_NAME: "chat", | ||
| }, | ||
| ) | ||
|
|
||
| set_on_span = span.set_attribute | ||
| else: | ||
| span = get_start_span_function()( | ||
| op=OP.GEN_AI_CHAT, | ||
| name=f"chat {model}" if model is not None else "chat", | ||
| origin=MistralIntegration.origin, | ||
| ) | ||
| span.set_data(SPANDATA.GEN_AI_PROVIDER_NAME, "mistral") | ||
| span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "chat") | ||
|
|
||
| set_on_span = span.set_data | ||
|
|
||
| with span: | ||
| if model is not None: | ||
| set_on_span(SPANDATA.GEN_AI_REQUEST_MODEL, model) | ||
|
|
||
| set_on_span(SPANDATA.GEN_AI_RESPONSE_STREAMING, False) | ||
| return f(self, *args, **kwargs) | ||
|
|
||
| return wrap_complete | ||
|
|
||
|
|
||
| def _wrap_complete_async(f: "Callable[..., Any]") -> "Callable[..., Any]": | ||
| @wraps(f) | ||
| async def wrap_complete_async(self: "Chat", *args: "Any", **kwargs: "Any") -> "Any": | ||
| client = sentry_sdk.get_client() | ||
| integration = client.get_integration(MistralIntegration) | ||
| if integration is None or kwargs.get("stream"): | ||
| return await f(self, *args, **kwargs) | ||
|
|
||
| model = kwargs.get("model") | ||
|
|
||
| if has_span_streaming_enabled(client.options): | ||
| span = sentry_sdk.traces.start_span( | ||
| name=f"chat {model}" if model is not None else "chat", | ||
| attributes={ | ||
| "sentry.op": OP.GEN_AI_CHAT, | ||
| "sentry.origin": MistralIntegration.origin, | ||
| SPANDATA.GEN_AI_PROVIDER_NAME: "mistral", | ||
| SPANDATA.GEN_AI_OPERATION_NAME: "chat", | ||
| }, | ||
| ) | ||
|
|
||
| set_on_span = span.set_attribute | ||
| else: | ||
| span = get_start_span_function()( | ||
| op=OP.GEN_AI_CHAT, | ||
| name=f"chat {model}" if model is not None else "chat", | ||
| origin=MistralIntegration.origin, | ||
| ) | ||
| span.set_data(SPANDATA.GEN_AI_PROVIDER_NAME, "mistral") | ||
| span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "chat") | ||
|
|
||
| set_on_span = span.set_data | ||
|
|
||
| with span: | ||
| if model is not None: | ||
| set_on_span(SPANDATA.GEN_AI_REQUEST_MODEL, model) | ||
|
|
||
| set_on_span(SPANDATA.GEN_AI_RESPONSE_STREAMING, False) | ||
| return await f(self, *args, **kwargs) | ||
|
|
||
| return wrap_complete_async | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import pytest | ||
|
|
||
| pytest.importorskip("mistralai") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
| from mistralai.client import Mistral | ||
| from mistralai.client.chat import Chat | ||
| from mistralai.client.models import ( | ||
| AssistantMessage, | ||
| ChatCompletionChoice, | ||
| ChatCompletionResponse, | ||
| UsageInfo, | ||
| ) | ||
|
|
||
| import sentry_sdk | ||
| from sentry_sdk.consts import OP, SPANDATA | ||
| from sentry_sdk.integrations.mistral import MistralIntegration | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mistral_response(): | ||
| return ChatCompletionResponse( | ||
| id="chat-id", | ||
| object="chat.completion", | ||
| model="mistral-medium-3-5", | ||
| created=10000000, | ||
| usage=UsageInfo( | ||
| prompt_tokens=10, | ||
| completion_tokens=20, | ||
| total_tokens=30, | ||
| ), | ||
| choices=[ | ||
| ChatCompletionChoice( | ||
| index=0, | ||
| finish_reason="stop", | ||
| message=AssistantMessage(content="Hello, how can I help you?"), | ||
| ) | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("span_streaming", [True, False]) | ||
| @pytest.mark.parametrize("stream_gen_ai_spans", [True, False]) | ||
| def test_nonstreaming_chat( | ||
| sentry_init, | ||
| capture_items, | ||
| get_model_response, | ||
| mistral_response, | ||
| stream_gen_ai_spans, | ||
| span_streaming, | ||
| ): | ||
| sentry_init( | ||
| integrations=[MistralIntegration()], | ||
| traces_sample_rate=1.0, | ||
| stream_gen_ai_spans=stream_gen_ai_spans, | ||
| trace_lifecycle="stream" if span_streaming else "static", | ||
| ) | ||
|
|
||
| client = Mistral(api_key="z") | ||
|
|
||
| model_response = get_model_response( | ||
| mistral_response, | ||
| serialize_pydantic=True, | ||
| ) | ||
|
|
||
| if span_streaming or stream_gen_ai_spans: | ||
| items = capture_items("span") | ||
|
|
||
| with mock.patch.object( | ||
| Chat, | ||
| "do_request", | ||
| return_value=model_response, | ||
| ), sentry_sdk.start_transaction(name="mistral"): | ||
| client.chat.complete( | ||
| model="mistral-medium-latest", | ||
| messages=[ | ||
| {"role": "user", "content": "What is the best French cheese?"} | ||
| ], | ||
| ) | ||
|
|
||
| sentry_sdk.flush() | ||
| spans = [item.payload for item in items] | ||
| (span,) = ( | ||
| span | ||
| for span in spans | ||
| if span["attributes"].get("sentry.op") == OP.GEN_AI_CHAT | ||
| ) | ||
|
|
||
| assert span["name"] == "chat mistral-medium-latest" | ||
| assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "mistral" | ||
| assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" | ||
|
|
||
| assert ( | ||
| span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "mistral-medium-latest" | ||
| ) | ||
| assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False | ||
| else: | ||
| items = capture_items("transaction") | ||
|
|
||
| with mock.patch.object( | ||
| Chat, | ||
| "do_request", | ||
| return_value=model_response, | ||
| ), sentry_sdk.start_transaction(name="mistral"): | ||
| client.chat.complete( | ||
| model="open-mistral", | ||
| messages=[{"role": "user", "content": "Hello, Mistral"}], | ||
| ) | ||
|
|
||
| (transaction,) = [item.payload for item in items] | ||
| (span,) = transaction["spans"] | ||
|
|
||
| assert span["description"] == "chat open-mistral" | ||
| assert span["data"][SPANDATA.GEN_AI_PROVIDER_NAME] == "mistral" | ||
| assert span["data"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" | ||
|
|
||
| assert span["data"][SPANDATA.GEN_AI_REQUEST_MODEL] == "open-mistral" | ||
| assert span["data"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.parametrize("span_streaming", [True, False]) | ||
| @pytest.mark.parametrize("stream_gen_ai_spans", [True, False]) | ||
| async def test_nonstreaming_chat_async( | ||
| sentry_init, | ||
| capture_items, | ||
| get_model_response, | ||
| mistral_response, | ||
| stream_gen_ai_spans, | ||
| span_streaming, | ||
| ): | ||
| sentry_init( | ||
| integrations=[MistralIntegration()], | ||
| traces_sample_rate=1.0, | ||
| stream_gen_ai_spans=stream_gen_ai_spans, | ||
| trace_lifecycle="stream" if span_streaming else "static", | ||
| ) | ||
|
|
||
| client = Mistral(api_key="z") | ||
|
|
||
| model_response = get_model_response( | ||
| mistral_response, | ||
| serialize_pydantic=True, | ||
| ) | ||
|
|
||
| if span_streaming or stream_gen_ai_spans: | ||
| items = capture_items("span") | ||
|
|
||
| with mock.patch.object( | ||
| Chat, | ||
| "do_request_async", | ||
| return_value=model_response, | ||
| ), sentry_sdk.start_transaction(name="mistral"): | ||
| await client.chat.complete_async( | ||
| model="mistral-medium-latest", | ||
| messages=[ | ||
| {"role": "user", "content": "What is the best French cheese?"} | ||
| ], | ||
| ) | ||
|
|
||
| sentry_sdk.flush() | ||
| spans = [item.payload for item in items] | ||
| (span,) = ( | ||
| span | ||
| for span in spans | ||
| if span["attributes"].get("sentry.op") == OP.GEN_AI_CHAT | ||
| ) | ||
|
|
||
| assert span["name"] == "chat mistral-medium-latest" | ||
| assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "mistral" | ||
| assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" | ||
|
|
||
| assert ( | ||
| span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "mistral-medium-latest" | ||
| ) | ||
| assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False | ||
| else: | ||
| items = capture_items("transaction") | ||
|
|
||
| with mock.patch.object( | ||
| Chat, | ||
| "do_request_async", | ||
| return_value=model_response, | ||
| ), sentry_sdk.start_transaction(name="mistral"): | ||
| await client.chat.complete_async( | ||
| model="mistral-medium-latest", | ||
| messages=[{"role": "user", "content": "Hello, Mistral"}], | ||
| ) | ||
|
|
||
| (transaction,) = [item.payload for item in items] | ||
| (span,) = transaction["spans"] | ||
|
|
||
| assert span["description"] == "chat mistral-medium-latest" | ||
| assert span["data"][SPANDATA.GEN_AI_PROVIDER_NAME] == "mistral" | ||
| assert span["data"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" | ||
|
|
||
| assert span["data"][SPANDATA.GEN_AI_REQUEST_MODEL] == "mistral-medium-latest" | ||
| assert span["data"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.