-
Notifications
You must be signed in to change notification settings - Fork 9
/
simple_example.py
51 lines (37 loc) · 1.38 KB
/
simple_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
import json
import os
from openai import OpenAI
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
from openai_functools import openai_function
@openai_function
def get_current_weather(location, unit="fahrenheit"):
weather_info = {
"location": location,
"temperature": "72",
"unit": unit,
"forecast": ["sunny", "windy"],
}
return json.dumps(weather_info)
def run_conversation():
messages = [{"role": "user", "content": "What's the weather like in London?"}]
response = client.chat.completions.create(
model="gpt-3.5-turbo-0613",
messages=messages,
functions=[get_current_weather.openai_metadata],
function_call="auto",
)
response_message = response.choices[0].message
if response_message.get("function_call"):
function_name = response_message["function_call"]["name"]
function_args = json.loads(response_message["function_call"]["arguments"])
function_response = get_current_weather(**function_args)
messages.append(
{"role": "function", "name": function_name, "content": function_response}
)
messages.append(response_message)
second_response = client.chat.completions.create(
model="gpt-3.5-turbo-0613", messages=messages
)
return second_response
if __name__ == "__main__":
print(run_conversation())