Skip to content

Commit 83d2163

Browse files
authored
Merge pull request #296 from Scale3-Labs/obinna/S3EN-2344-instrument-mistral
Obinna/s3 en 2344 instrument mistral
2 parents 0707958 + 55645e8 commit 83d2163

File tree

17 files changed

+388
-4
lines changed

17 files changed

+388
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ Langtrace automatically captures traces from the following vendors:
246246
| Groq | LLM | :x: | :white_check_mark: |
247247
| Perplexity | LLM | :white_check_mark: | :white_check_mark: |
248248
| Gemini | LLM | :x: | :white_check_mark: |
249+
| Mistral | LLM | :x: | :white_check_mark: |
249250
| Langchain | Framework | :x: | :white_check_mark: |
250251
| LlamaIndex | Framework | :white_check_mark: | :white_check_mark: |
251252
| Langgraph | Framework | :x: | :white_check_mark: |

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ classifiers=[
1818
"Operating System :: OS Independent",
1919
]
2020
dependencies = [
21-
'trace-attributes==7.0.3',
21+
'trace-attributes==7.0.4',
2222
'opentelemetry-api>=1.25.0',
2323
'opentelemetry-sdk>=1.25.0',
2424
'opentelemetry-instrumentation>=0.47b0',
@@ -52,7 +52,8 @@ dev = [
5252
"ollama",
5353
"groq",
5454
"google-generativeai",
55-
"google-cloud-aiplatform"
55+
"google-cloud-aiplatform",
56+
"mistralai"
5657
]
5758

5859
test = [
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import asyncio
2+
from examples.mistral_example.complete import chat_complete
3+
from examples.mistral_example.complete_async import complete_async
4+
from examples.mistral_example.embeddings import embeddings_create
5+
from langtrace_python_sdk import langtrace, with_langtrace_root_span
6+
7+
langtrace.init()
8+
9+
class MistralRunner:
10+
@with_langtrace_root_span("Mistral")
11+
def run(self):
12+
chat_complete()
13+
asyncio.run(complete_async())
14+
embeddings_create()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import os
2+
from langtrace_python_sdk import with_langtrace_root_span
3+
from mistralai import Mistral
4+
5+
@with_langtrace_root_span("chat_complete")
6+
def chat_complete():
7+
model = "mistral-large-latest"
8+
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
9+
chat_response = client.chat.complete(
10+
model= model,
11+
messages = [
12+
{
13+
"role": "user",
14+
"content": "I need 10 cocktail recipes with tequila other than the classics like margarita, tequila"
15+
},
16+
]
17+
)
18+
print(chat_response.choices[0].message.content)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os
2+
from langtrace_python_sdk import with_langtrace_root_span
3+
from mistralai import Mistral
4+
5+
@with_langtrace_root_span("chat_complete_async")
6+
async def complete_async():
7+
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
8+
res = await client.chat.complete_async(model="mistral-small-latest", messages=[
9+
{
10+
"content": "Which locations should I visit when I travel to New york? Answer in one short sentence.",
11+
"role": "user",
12+
},
13+
])
14+
if res is not None:
15+
# handle response
16+
print(res.choices[0].message.content)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
from langtrace_python_sdk import with_langtrace_root_span
3+
from mistralai import Mistral
4+
5+
6+
@with_langtrace_root_span("create_embeddings")
7+
def embeddings_create():
8+
model = "mistral-embed"
9+
10+
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
11+
12+
embeddings_batch_response = client.embeddings.create(
13+
model=model,
14+
inputs=["Embed this sentence.", "As well as this one."],
15+
)
16+
17+
print(embeddings_batch_response.data[0].embedding)

src/langtrace_python_sdk/constants/instrumentation/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"OLLAMA": "Ollama",
3030
"VERTEXAI": "VertexAI",
3131
"GEMINI": "Gemini",
32+
"MISTRAL": "Mistral",
3233
}
3334

3435
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY = "langtrace_additional_attributes"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from langtrace.trace_attributes import MistralMethods
2+
3+
APIS = {
4+
"CHAT_COMPLETE": {
5+
"METHOD": MistralMethods.CHAT_COMPLETE.value,
6+
"ENDPOINT": "/v1/chat/completions",
7+
},
8+
"ASYNC_CHAT_COMPLETE": {
9+
"METHOD": MistralMethods.ASYNC_CHAT_COMPLETE.value,
10+
"ENDPOINT": "/v1/chat/completions",
11+
},
12+
"CHAT_STREAM": {
13+
"METHOD": MistralMethods.CHAT_STREAM.value,
14+
"ENDPOINT": "/v1/chat/completions",
15+
},
16+
"EMBEDDINGS_CREATE": {
17+
"METHOD": MistralMethods.EMBEDDINGS_CREATE.value,
18+
"ENDPOINT": "/v1/embeddings",
19+
},
20+
"ASYNC_EMBEDDINGS_CREATE": {
21+
"METHOD": MistralMethods.ASYNC_EMBEDDINGS_CREATE.value,
22+
"ENDPOINT": "/v1/embeddings",
23+
},
24+
}

src/langtrace_python_sdk/instrumentation/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from .dspy import DspyInstrumentation
1717
from .vertexai import VertexAIInstrumentation
1818
from .gemini import GeminiInstrumentation
19+
from .mistral import MistralInstrumentation
1920

2021
__all__ = [
2122
"AnthropicInstrumentation",
@@ -36,4 +37,5 @@
3637
"DspyInstrumentation",
3738
"VertexAIInstrumentation",
3839
"GeminiInstrumentation",
40+
"MistralInstrumentation",
3941
]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .instrumentation import MistralInstrumentation
2+
3+
__all__ = ["MistralInstrumentation"]

0 commit comments

Comments
 (0)