-
Notifications
You must be signed in to change notification settings - Fork 39
/
tool_calling.py
75 lines (70 loc) · 2.71 KB
/
tool_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import json
from haystack import Pipeline
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from openinference.instrumentation.haystack import HaystackInstrumentor
from openinference.instrumentation.openai import OpenAIInstrumentor
endpoint = "http://127.0.0.1:6006/v1/traces"
tracer_provider = trace_sdk.TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
HaystackInstrumentor().instrument(tracer_provider=tracer_provider)
OpenAIInstrumentor().instrument(tracer_provider=tracer_provider)
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"format": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": (
"The temperature unit to use. Infer this from the users location."
),
},
},
"required": ["location", "format"],
},
},
}
]
messages = [ChatMessage.from_user("What's the weather like in Berlin?")]
llm = OpenAIChatGenerator(model="gpt-4o")
pipe = Pipeline()
pipe.add_component("llm", llm)
response = pipe.run({"llm": {"messages": messages, "generation_kwargs": {"tools": tools}}})
response_msg = response["llm"]["replies"][0]
messages.append(response_msg)
weather_response = [
{
"id": "response_uhGNifLfopt5JrCUxXw1L3zo",
"status": "success",
"function": {
"name": "get_current_weather",
"arguments": {"location": "Berlin", "format": "celsius"},
},
"data": {
"location": "Berlin",
"temperature": 18,
"weather_condition": "Partly Cloudy",
"humidity": "60%",
"wind_speed": "15 km/h",
"observation_time": "2024-03-05T14:00:00Z",
},
}
]
messages.append(
ChatMessage.from_function(content=json.dumps(weather_response), name="get_current_weather")
)
response = pipe.run({"llm": {"messages": messages, "generation_kwargs": {"tools": tools}}})
print(f"{response["llm"]["replies"][0]=}")