-
Notifications
You must be signed in to change notification settings - Fork 9
/
orchestrator_example.py
56 lines (44 loc) · 1.64 KB
/
orchestrator_example.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
import json
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
from openai_functools import FunctionsOrchestrator
def get_current_weather(location, unit="fahrenheit"):
"""Get the current weather in a given location"""
weather_info = {
"location": location,
"temperature": "72",
"unit": unit,
"forecast": ["sunny", "windy"],
}
return json.dumps(weather_info)
def get_weather_next_day(location, unit="fahrenheit"):
"""Get the weather forecast for the next day in a given location"""
weather_info = {
"location": location,
"temperature": "72",
"unit": unit,
"forecast": ["sunny", "windy"],
}
return json.dumps(weather_info)
orchestrator = FunctionsOrchestrator()
orchestrator.register_all([get_current_weather, get_weather_next_day])
if __name__ == "__main__":
response = client.chat.completions.create(
model="gpt-3.5-turbo-0613",
messages=[{"role": "user", "content": "What's the weather like in Boston?"}],
functions=orchestrator.create_function_descriptions(),
function_call="auto",
)
# Call the function that is specified in the response
print(orchestrator.call_function(response))
response = client.chat.completions.create(
model="gpt-3.5-turbo-1106",
messages=[
{"role": "user", "content": "What's the weather like in Boston tomorrow?"}
],
functions=orchestrator.create_function_descriptions(),
function_call="auto",
)
# Call the function that is specified in the response
print(orchestrator.call_function(response))