-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmistral_basic.py
More file actions
45 lines (32 loc) · 1.13 KB
/
Copy pathmistral_basic.py
File metadata and controls
45 lines (32 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""Smallest possible ``@protect`` usage with raw Mistral.
The first ``@protect`` call lazily creates the runtime and patches
httpx so the Mistral SDK fires ``track_llm`` events automatically.
NullRun's URL-keyed extractor reads the Mistral response body and
pulls out ``prompt_tokens`` / ``completion_tokens``.
Run:
pip install nullrun mistralai
export NULLRUN_API_KEY=nr_live_...
export MISTRAL_API_KEY=...
python examples/mistral_basic.py
"""
from __future__ import annotations
from _env import load_env
load_env() # populate os.environ from examples/.env (no-op if absent)
import os
from mistralai import Mistral
import nullrun
from nullrun import protect, shutdown
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
@protect
def answer(prompt: str) -> str:
response = client.chat.complete(
model="mistral-small-latest",
messages=[{"role": "user", "content": prompt}],
)
return response.choices[0].message.content or ""
if __name__ == "__main__":
try:
with nullrun.handle():
print(answer("In one sentence, what does NullRun do?"))
finally:
shutdown()