-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions_args.py
More file actions
185 lines (160 loc) · 5.86 KB
/
Copy pathfunctions_args.py
File metadata and controls
185 lines (160 loc) · 5.86 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""
use optional : function parameter in chat completion API
enable models to generate function arguments based on spec provided
**chat completion API , will not invoke or execute the methods.
request :
{
"model" : "gpt-3.5-turbo", required
"messages": [] ,** required
"functions" : [{
"name" : "name of function to be called" # required,
"description" : "what does the functio do" , # optional but useful
}] # list of functions , model may generate JSON parameters for
}
refer example :
https://platform.openai.com/docs/guides/gpt/function-calling
"""
# imports
import openai
from openai.error import OpenAIError
import os
from dotenv import load_dotenv
from tenacity import retry, wait_random_exponential, stop_after_attempt
from termcolor import colored
import json
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
GPT_MODEL = "gpt-3.5-turbo"
functions_spec = [
{
"name": "get_current_weather",
"description": "get current weather",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The name of city or town , e.g : New Delhi",
},
"format": {
"type": "string",
"enum": ["celcius", "fahrenheit"],
"description": "Temparature unit to use. Infer based on location",
},
},
"required": ["location", "format"],
},
},
{
"name": "get_n_day_weather_forecast",
"description": "Get an N-day weather forecast",
"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.",
},
"num_days": {
"type": "integer",
"description": "The number of days to forecast",
},
},
"required": ["location", "format", "num_days"],
},
},
]
def pretty_print_conversation(messages):
role_to_color = {
"system": "red",
"user": "green",
"assistant": "blue",
"function": "magenta",
}
for message in messages:
if message["role"] == "system":
print(
colored(
f"system: {message['content']}\n", role_to_color[message["role"]]
)
)
elif message["role"] == "user":
print(
colored(f"user: {message['content']}\n", role_to_color[message["role"]])
)
elif message["role"] == "assistant" and message.get("function_call"):
print(
colored(
f"assistant: {message['function_call']}\n",
role_to_color[message["role"]],
)
)
elif message["role"] == "assistant" and not message.get("function_call"):
print(
colored(
f"assistant: {message['content']}\n", role_to_color[message["role"]]
)
)
elif message["role"] == "function":
print(
colored(
f"function ({message['name']}): {message['content']}\n",
role_to_color[message["role"]],
)
)
# call completions
@retry(wait=wait_random_exponential(multiplier=2, max=40), stop=stop_after_attempt(3))
def call_chat_completion(
message_array, max_tokens, functions=None, function_call=None, model=GPT_MODEL
):
custom_args = {"model": model, "messages": message_array, "max_tokens": max_tokens}
if functions is not None:
custom_args.update({"functions": functions})
if function_call is not None:
custom_args.update(
{"function_call": function_call}
) # if functions is present default 'auto'
# print(custom_args)
try:
response = openai.ChatCompletion.create(**custom_args)
print("token used is : ", response.usage.total_tokens)
# print("full API response : ", response)
return response.choices[0].message
except OpenAIError as e:
print("OpenAIError caught : ", e._message)
# create functions to be supplied to "functions" parameter to openai API
# To describe a function that accepts no parameters, provide the value {"type": "object", "properties": {}}.
system_message = {
"role": "system",
"content": "Don't make assumptions about values to provide to function , ask clarifying quetions",
}
user_message = {"role": "user", "content": "What is the weather like today ?"}
messages = [system_message, user_message]
# call the API with functions
response_message = call_chat_completion(messages, 250, functions=functions_spec)
# check tokens without functions
# call_chat_completion(messages, 250)
messages.append(response_message)
# pretty_print_conversation(messages)
# provide your inputs
input_message = {"role": "user", "content": "I'm in New Delhi"}
messages.append(input_message)
message_response = call_chat_completion(messages, 100, functions=functions_spec)
print(message_response)
print(message_response.get("function_call").get("name"))
messages.append(message_response)
# pretty_print_conversation(messages)
function_args_json = json.loads(
message_response.function_call.arguments
) # a dictionary
"""
model can generate function arguments
by directly using user input or use user input
to generate argument from previous knowledge
** note : functions use tokens as well , same as prompts
"""