-
Notifications
You must be signed in to change notification settings - Fork 32
/
chat_completions_async.py
57 lines (49 loc) · 1.8 KB
/
chat_completions_async.py
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
46
47
48
49
50
51
52
53
54
55
56
57
import asyncio
from mistralai import Mistral
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from openinference.instrumentation import using_attributes
from openinference.instrumentation.mistralai import MistralAIInstrumentor
endpoint = "http://127.0.0.1:6006/v1/traces"
tracer_provider = trace_sdk.TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
MistralAIInstrumentor().instrument(tracer_provider=tracer_provider)
async def chat_completions_async():
client = Mistral(
api_key="redacted",
)
with using_attributes(
session_id="my-test-session",
user_id="my-test-user",
metadata={
"test-int": 1,
"test-str": "string",
"test-list": [1, 2, 3],
"test-dict": {
"key-1": "val-1",
"key-2": "val-2",
},
},
tags=["tag-1", "tag-2"],
prompt_template="Who won the soccer match in {city} on {date}",
prompt_template_version="v1.0",
prompt_template_variables={
"city": "Johannesburg",
"date": "July 11th",
},
):
res = await client.chat.complete_async(
model="mistral-small-latest",
messages=[
{
"content": "Who won the World Cup in 2018?",
"role": "user",
},
],
)
if res is not None:
print(res.choices[0].message.content)
if __name__ == "__main__":
asyncio.run(chat_completions_async())