-
Notifications
You must be signed in to change notification settings - Fork 16
/
openai_utils.py
198 lines (185 loc) · 7.47 KB
/
openai_utils.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
import openai
from tenacity import retry, wait_random_exponential, stop_after_attempt
import time
import os
from datetime import datetime
import tiktoken
from copy import deepcopy
import json
from config import *
from arguments import parse_args
import importlib
from termcolor import colored
enc = tiktoken.encoding_for_model("gpt-4")
args = parse_args()
output_dir = args.output_dir
if api_type == "azure":
from openai import AzureOpenAI as Client
client = Client(
api_key=api_key,
api_version=api_version,
azure_endpoint = api_base
)
else:
from openai import OpenAI as Client
client = Client(
api_key=api_key,
)
# turbo_client = Client(
# api_key=api_key,
# api_version=api_version,
# azure_endpoint = api_base
# )
class dotdict(dict):
"""dot.notation access to dictionary attributes"""
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
def call_gpt(messages, functions=None, **kwargs):
if 'model' not in kwargs:
kwargs['model'] = model_name
messages_converted = messages
for message in messages_converted:
if "tool_calls" in message:
message['function_call'] = message['tool_calls'][0]['function']
message.pop('tool_calls')
if "tool_call_id" in message:
message.pop('tool_call_id')
message['role'] = 'function'
@retry(wait=wait_random_exponential(multiplier=10, max=50), stop=stop_after_attempt(5))
def call_gpt_retry(messages, functions):
ts = time.time()
try:
response = client.chat.completions.create(
seed=123,
messages=messages,
functions=functions,
**kwargs
)
except openai.BadRequestError as e:
raise e
except openai.RateLimitError as e:
time.sleep(50)
raise e
except openai.InternalServerError as e:
raise e
except Exception as e:
raise e
t = time.time() - ts
return response, t
t_s = time.time()
try:
response, t_real = call_gpt_retry(messages_converted, functions)
# json_content = response.choices[0].message.content
# print(response.choices[0].message.function_call)
if response.choices[0].finish_reason == 'function_call':
response_json = json.loads(response.json())
tool_call = {'arguments': response_json['choices'][0]['message']['function_call']['arguments'], 'name': response_json['choices'][0]['message']['function_call']['name']}
response.choices[0].message.tool_calls = [dotdict({'id':'111', 'function':dotdict(tool_call)})]
else:
if model_name == 'gpt-4-turbo':
response.choices[0].message.tool_calls = []
# else:
# response.choices[0].message['tool_calls'] = []
if response.usage is None:
token_cnt = len(enc.encode(str(functions))) + len(enc.encode(str(messages))) + len(enc.encode(str(response.choices[0].message.content)))
response.usage = dotdict({'total_tokens': token_cnt})
else:
print(colored('tokens', 'blue'), colored(response.usage.total_tokens, 'blue'))
return response
except Exception as e:
raise e
t = time.time() - t_s
print('minus:', t, file=open(os.path.join(output_dir, "time.txt"), "a"))
return "openai error"
def call_gpt_no_func(messages):
@retry(wait=wait_random_exponential(multiplier=60, max=100), stop=stop_after_attempt(10))
def call_gpt_retry(messages):
response = client.chat.completions.create(
model=model_name,
messages=messages,
)
return response
# try:
return call_gpt_retry(messages)
#
def call_gpt_turbo(messages, functions):
functions_new = []
for function in functions:
functions_new.append({
"type": "function",
"function": function
})
# time.sleep(1)
@retry(wait=wait_random_exponential(multiplier=5, max=20), stop=stop_after_attempt(10))
def call_gpt_retry(messages, functions):
t_s = time.time()
try:
response = client.chat.completions.create(
model="gpt-4-turbo",
messages=messages,
seed=123,
# response_format={"type": "json_object"},
tools=functions,
# tool_choice="tool", # auto is default, but we'll be explicit
tool_choice="auto", # auto is default, but we'll be explicit
)
except openai.BadRequestError as e:
# raise e
return "bad request", 0
except openai.RateLimitError as e:
time.sleep(50)
raise e
except openai.InternalServerError as e:
return "internal server error", 0
t = time.time() - t_s
return response, t
t_s = time.time()
try:
# if True:
response, t_real = call_gpt_retry(messages, functions_new)
t = time.time() - t_s
print(f'{datetime.now()}', file=open(os.path.join(output_dir, "time.txt"), "a"))
if not isinstance(response, str):
print(response.usage.total_tokens, file=open(os.path.join(output_dir, "time.txt"), "a"))
print('minus:', t-t_real, file=open(os.path.join(output_dir, "time.txt"), "a"))
print('#'*100, '\n\n', messages, '\n\n', functions, '\n\n', response, file=open(os.path.join('output', "log.txt"), "a"))
return response
except Exception as e:
raise e
t = time.time() - t_s
print('minus:', t, file=open(os.path.join(output_dir, "time.txt"), "a"))
return "openai error"
# 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"""
if "tokyo" in location.lower():
return json.dumps({"location": "Tokyo", "temperature": "10", "unit": unit})
elif "san francisco" in location.lower():
return json.dumps({"location": "San Francisco", "temperature": "72", "unit": unit})
elif "paris" in location.lower():
return json.dumps({"location": "Paris", "temperature": "22", "unit": unit})
else:
return json.dumps({"location": location, "temperature": "unknown"})
if __name__ == "__main__":
messages = [{"role": "user", "content": "What's the weather like in San Francisco, Tokyo, and Paris?"}]
tools = [
{
"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"],
},
},
]
response = call_gpt(messages, tools)
print(response)