PhilSpiel commited on
Commit
d27b8a1
·
1 Parent(s): cc8b45f

Rename install.py to app.py

Browse files
Files changed (2) hide show
  1. app.py +80 -0
  2. install.py +0 -1
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pip install openai
2
+ pip install json
3
+
4
+ import openai
5
+ import json
6
+
7
+ # Load your API key from an environment variable or secret management service
8
+ openai.api_key = "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"
9
+
10
+ # Example dummy function hard coded to return the same weather
11
+ # In production, this could be your backend API or an external API
12
+ def get_current_weather(location, unit="fahrenheit"):
13
+ """Get the current weather in a given location"""
14
+ weather_info = {
15
+ "location": location,
16
+ "temperature": "72",
17
+ "unit": unit,
18
+ "forecast": ["sunny", "windy"],
19
+ }
20
+ return json.dumps(weather_info)
21
+
22
+ def run_conversation():
23
+ # Step 1: send the conversation and available functions to GPT
24
+ messages = [{"role": "user", "content": "What's the weather like in Boston?"}]
25
+ functions = [
26
+ {
27
+ "name": "get_current_weather",
28
+ "description": "Get the current weather in a given location",
29
+ "parameters": {
30
+ "type": "object",
31
+ "properties": {
32
+ "location": {
33
+ "type": "string",
34
+ "description": "The city and state, e.g. San Francisco, CA",
35
+ },
36
+ "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
37
+ },
38
+ "required": ["location"],
39
+ },
40
+ }
41
+ ]
42
+ response = openai.ChatCompletion.create(
43
+ model="gpt-3.5-turbo",
44
+ messages=messages,
45
+ functions=functions,
46
+ function_call="auto", # auto is default, but we'll be explicit
47
+ )
48
+ response_message = response["choices"][0]["message"]
49
+
50
+ # Step 2: check if GPT wanted to call a function
51
+ if response_message.get("function_call"):
52
+ # Step 3: call the function
53
+ # Note: the JSON response may not always be valid; be sure to handle errors
54
+ available_functions = {
55
+ "get_current_weather": get_current_weather,
56
+ } # only one function in this example, but you can have multiple
57
+ function_name = response_message["function_call"]["name"]
58
+ function_to_call = available_functions[function_name]
59
+ function_args = json.loads(response_message["function_call"]["arguments"])
60
+ function_response = function_to_call(
61
+ location=function_args.get("location"),
62
+ unit=function_args.get("unit"),
63
+ )
64
+
65
+ # Step 4: send the info on the function call and function response to GPT
66
+ messages.append(response_message) # extend conversation with assistant's reply
67
+ messages.append(
68
+ {
69
+ "role": "function",
70
+ "name": function_name,
71
+ "content": function_response,
72
+ }
73
+ ) # extend conversation with function response
74
+ second_response = openai.ChatCompletion.create(
75
+ model="gpt-3.5-turbo",
76
+ messages=messages,
77
+ ) # get a new response from GPT where it can see the function response
78
+ return second_response
79
+
80
+ print(run_conversation())
install.py DELETED
@@ -1 +0,0 @@
1
- pip install openai