-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathinference_hosted.py
51 lines (46 loc) · 1.8 KB
/
inference_hosted.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
import openai
import json
openai.api_key = "EMPTY"
openai.api_base = "http://luigi.millennium.berkeley.edu:8000/v1"
# Example dummy function hard coded to return the same weather
# In production, this could be your backend API or an external API
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 run_conversation():
# Step 1: send the conversation and available functions to GPT
messages = [{"role": "user", "content": "What's the weather like in the two cities of Boston and San Francisco?"}]
functions = [
{
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
}
]
completion = openai.ChatCompletion.create(
model='gorilla-openfunctions-v2',
messages=messages,
functions=functions,
function_call="auto", # auto is default, but we'll be explicit
)
print("--------------------")
print(f"Function call strings(s): {completion.choices[0].message.content}")
print("--------------------")
print(f"OpenAI compatible `function_call`: {completion.choices[0].message.function_call}")
run_conversation()