forked from Arize-ai/openinference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_completions_streaming_with_function_calling.py
53 lines (49 loc) · 1.98 KB
/
chat_completions_streaming_with_function_calling.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
from mistralai.client import MistralClient
from mistralai.models.chat_completion import ChatMessage, ToolChoice
from openinference.instrumentation.mistralai import MistralAIInstrumentor
from opentelemetry import trace as trace_api
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
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()))
trace_api.set_tracer_provider(tracer_provider)
MistralAIInstrumentor().instrument()
if __name__ == "__main__":
client = MistralClient()
response_stream = client.chat_stream(
model="mistral-large-latest",
tool_choice=ToolChoice.any,
tools=[
{
"type": "function",
"function": {
"name": "get_weather",
"description": "finds the weather for a given city",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city to find the weather for, e.g. 'London'",
}
},
"required": ["city"],
},
},
},
],
messages=[
ChatMessage(
content="What's the weather like in San Francisco?",
role="user",
)
],
)
tool_call_arguments = ""
for chunk in response_stream:
if (tool_calls := chunk.choices[0].delta.tool_calls) is not None:
tool_call = tool_calls[0]
print(tool_call)