-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_call_util.py
More file actions
204 lines (180 loc) · 6.28 KB
/
api_call_util.py
File metadata and controls
204 lines (180 loc) · 6.28 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import requests
import msgpack
import os
import re
import json
import openai
import random
from openai.types import Completion
from openai.types.chat import ChatCompletion
from openai import RateLimitError
from tenacity import (
retry,
retry_if_exception_type,
stop_after_attempt,
wait_random_exponential,
stop_never
) # for exponential backoff
def set_proxy(url):
os.environ["http_proxy"] = url
os.environ["https_proxy"] = url
os.environ["HTTP_PROXY"] = url
os.environ["HTTPS_PROXY"] = url
def unset_proxy():
os.environ.pop('http_proxy', None)
os.environ.pop('https_proxy', None)
os.environ.pop('HTTP_PROXY', None)
os.environ.pop('HTTPS_PROXY', None)
def read_gen_data(path, stream=False):
if stream:
return read_gen_data_stream(path)
with open(path, 'r', encoding='utf-8') as fp:
raw = fp.read().split('}{\n')
data = []
for s in raw:
s = s.strip()
if not s.startswith('{'):
s = '{' + s
if not s.endswith('}'):
s = s + '}'
ex = json.loads(s)
data.append(ex)
return data
def read_gen_data_stream(path):
with open(path, 'r', encoding='utf-8') as fp:
text = ''
# import pdb; pdb.set_trace()
for line in fp:
line = line.strip()
if line == '}{':
text += '}'
ex = json.loads(text)
yield ex
text = '{'
else:
text += line
if text:
ex = json.loads(text)
yield ex
def read_json(path):
with open(path, 'rb') as fp:
return json.load(fp)
def read_jonl_stream(path):
with open(path, 'rb') as fp:
for line in fp:
if line.strip():
yield json.loads(line)
return
def read_jsonl(path, stream=False):
if stream:
return read_jonl_stream(path)
results = []
with open(path, 'rb') as fp:
for line in fp:
if line.strip():
try:
results.append(json.loads(line))
except Exception as e:
print(e, line)
continue
return results
def get_output(res):
out = res['message']['content']
if res['finish_reason'] != 'stop':
out += '<|NONSTOP|>'
return out
def get_output_raw(res):
out = res['text']
if res['finish_reason'] != 'stop':
out += '<|NONSTOP|>'
return out
def save_jsonl(data, path):
with open(path, 'w', encoding='utf-8') as fp:
for ex in data:
print(json.dumps(ex, ensure_ascii=False), file=fp)
class LLMClient:
def __init__(self, api_key, base_url=None):
self.api_key = api_key
self.base_url = base_url
if self.base_url:
self.client = openai.OpenAI(api_key=api_key, base_url=base_url)
else:
self.client = openai.OpenAI(api_key=api_key)
@staticmethod
def get_completion_output(response: Completion):
outputs = []
for res in response.choices:
out = res.text
if res.finish_reason == 'length':
out += '<|NONSTOP|>'
outputs.append(out)
return outputs, response.usage.completion_tokens
@staticmethod
def get_chat_output(response: ChatCompletion):
outputs = []
for res in response.choices:
out = res.message.content
if res.finish_reason == 'length':
out += '<|NONSTOP|>'
outputs.append(out)
return outputs, response.usage.completion_tokens
# @retry(retry=retry_if_exception_type(RateLimitError), wait=wait_random_exponential(min=5, max=60), stop=stop_never)
def call_completion(self, model, prompt, max_tokens, temperature=0.7, top_p=0.95, n=1, stop=None, **kwargs):
response = self.client.completions.create(
model=model,
prompt=prompt,
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
n=n,
stop=stop,
frequency_penalty=kwargs.get('frequency_penalty', 0),
)
outputs, num_gen_tokens = self.get_completion_output(response)
if n == 1:
return outputs[0], num_gen_tokens
return outputs, num_gen_tokens
# @retry(retry=retry_if_exception_type(RateLimitError), wait=wait_random_exponential(min=5, max=60), stop=stop_never)
def call_chat_completion(self, model, messages, max_tokens, temperature=0.7, top_p=0.95, n=1, stop=None):
if isinstance(messages, str):
messages = [{"role": "user", "content": messages}]
response = self.client.chat.completions.create(
model=model,
messages=messages,
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
n=n,
stop=stop,
)
outputs, num_gen_tokens = self.get_chat_output(response)
if n == 1:
return outputs[0], num_gen_tokens
return outputs, num_gen_tokens
class OpenAILLMClient:
def __init__(self, api_key_path):
self.api_key_path = api_key_path
with open(api_key_path, 'r') as fp:
api_keys = json.load(fp)
self.api_keys = [key['apikey'] for key in api_keys if key['model_provider'] == 'openai']
self.clients = []
for api_key in self.api_keys:
self.clients.append(openai.OpenAI(api_key=api_key))
@retry(retry=retry_if_exception_type(RateLimitError), wait=wait_random_exponential(min=5, max=60), stop=stop_never)
def call_chat_completion(self, model, messages, max_tokens, temperature=0.7, top_p=0.95, n=1, stop=None):
if isinstance(messages, str):
messages = [{"role": "user", "content": messages}]
client = random.choice(self.clients)
response = client.chat.completions.create(
model=model,
messages=messages,
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
n=n,
stop=stop,
)
outputs, num_gen_tokens = LLMClient.get_chat_output(response)
if n == 1:
return outputs[0], num_gen_tokens[0]
return outputs, num_gen_tokens