Skip to content

Commit 747341e

Browse files
authored
Add OTel Tracing (#280)
1 parent c6c1f78 commit 747341e

File tree

9 files changed

+798
-67
lines changed

9 files changed

+798
-67
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import os
2+
from mistralai import Mistral
3+
4+
from mistralai.extra.run.context import RunContext
5+
import logging
6+
import time
7+
import asyncio
8+
9+
10+
MODEL = "mistral-medium-latest"
11+
12+
USER_MESSAGE = """
13+
Please make the Secret Santa for me
14+
To properly do it you need to:
15+
- Get the friend you were assigned to (using the get_secret_santa_assignment function)
16+
- Read into his gift wishlist what they would like to receive (using the get_gift_wishlist function)
17+
- Buy the gift (using the buy_gift function)
18+
- Find the best website to buy the gift using a web search
19+
- Send it to them (using the send_gift function)
20+
"""
21+
22+
23+
async def main():
24+
api_key = os.environ["MISTRAL_API_KEY"]
25+
mistral_agent_id = os.environ["MISTRAL_AGENT_ID"]
26+
client = Mistral(
27+
api_key=api_key, debug_logger=logging.getLogger("mistralai")
28+
)
29+
30+
async with RunContext(
31+
agent_id=mistral_agent_id
32+
) as run_context:
33+
run_context.register_func(get_secret_santa_assignment)
34+
run_context.register_func(get_gift_wishlist)
35+
run_context.register_func(buy_gift)
36+
run_context.register_func(send_gift)
37+
38+
await client.beta.conversations.run_async(
39+
run_ctx=run_context,
40+
inputs=USER_MESSAGE,
41+
)
42+
43+
44+
def get_secret_santa_assignment():
45+
"""Get the friend you were assigned to"""
46+
time.sleep(2)
47+
return "John Doe"
48+
49+
50+
def get_gift_wishlist(friend_name: str):
51+
"""Get the gift wishlist of the friend you were assigned to"""
52+
time.sleep(1.5)
53+
return ["Book", "Chocolate", "T-Shirt"]
54+
55+
56+
def buy_gift(gift_name: str):
57+
"""Buy the gift you want to send to your friend"""
58+
time.sleep(1.1)
59+
return f"Bought {gift_name}"
60+
61+
62+
def send_gift(friend_name: str, gift_name: str, website: str):
63+
"""Send the gift to your friend"""
64+
time.sleep(2.2)
65+
return f"Sent {gift_name} to {friend_name} bought on {website}"
66+
67+
68+
if __name__ == "__main__":
69+
asyncio.run(main())

poetry.lock

Lines changed: 186 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "mistralai"
3-
version = "1.9.11"
3+
version = "1.9.12"
44
description = "Python Client SDK for the Mistral AI API."
55
authors = [{ name = "Mistral" },]
66
readme = "README-PYPI.md"
@@ -13,6 +13,10 @@ dependencies = [
1313
"typing-inspection >=0.4.0",
1414
"pyyaml (>=6.0.2,<7.0.0)",
1515
"invoke (>=2.2.0,<3.0.0)",
16+
"opentelemetry-sdk (>=1.33.1,<2.0.0)",
17+
"opentelemetry-api (>=1.33.1,<2.0.0)",
18+
"opentelemetry-exporter-otlp-proto-http (>=1.37.0,<2.0.0)",
19+
"opentelemetry-semantic-conventions (>=0.59b0,<0.60)",
1620
]
1721

1822
[tool.poetry]

src/mistralai/_hooks/registration.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .custom_user_agent import CustomUserAgentHook
22
from .deprecation_warning import DeprecationWarningHook
3+
from .tracing import TracingHook
34
from .types import Hooks
45

56
# This file is only ever generated once on the first generation and then is free to be modified.
@@ -13,5 +14,9 @@ def init_hooks(hooks: Hooks):
1314
with an instance of a hook that implements that specific Hook interface
1415
Hooks are registered per SDK instance, and are valid for the lifetime of the SDK instance
1516
"""
17+
tracing_hook = TracingHook()
1618
hooks.register_before_request_hook(CustomUserAgentHook())
1719
hooks.register_after_success_hook(DeprecationWarningHook())
20+
hooks.register_after_success_hook(tracing_hook)
21+
hooks.register_before_request_hook(tracing_hook)
22+
hooks.register_after_error_hook(tracing_hook)

0 commit comments

Comments
 (0)