forked from Leirunlin/Text-level-Graph-Attack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
288 lines (247 loc) · 9.97 KB
/
api.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
from yaml import load
import openai
import math
from tqdm import tqdm
import asyncio
import langchain
from langchain.cache import SQLiteCache
import os.path as osp
import json
langchain.llm_cache = SQLiteCache(database_path="./openai_io/db/.langchain.db")
import os
import logging
from async_api import process_api_requests_from_file
import tiktoken
import pickle
import json
import google.generativeai as palm
import torch
import time
def print_str_to_file(str, filename):
with open(filename, 'w') as f:
f.write(str)
def persist_cache_to_disk(filename):
def decorator(original_func):
try:
cache = pickle.load(open(filename, 'rb'))
except (IOError, ValueError):
cache = {}
def new_func(*args, **kwargs):
str_repr = json.dumps([args, kwargs], sort_keys=True)
if str_repr not in cache:
cache[str_repr] = original_func(*args, **kwargs)
pickle.dump(cache, open(filename, "wb"))
return cache[str_repr]
return new_func
return decorator
def load_yaml_file(filename = 'config.yaml'):
with open(filename, 'r') as stream:
data = load(stream=stream, Loader=Loader)
return data
def get_embedding(text, model="text-embedding-ada-002"):
text = text.replace("\n", " ")
res = openai.Embedding.create(input = [text], model=model)['data'][0]['embedding']
return res
def openai_ada_api(input_list, model_name = 'text-embedding-ada-002', max_len = 8190, max_batch = 1024):
if len(input_list) < max_batch:
input_list = [x[:max_len] for x in input_list]
res = openai.Embedding.create(input = input_list, model=model_name)['data']
res = [x['embedding'] for x in res]
return res
else:
input_list = [x[:max_len] for x in input_list]
total_res = []
total_batch_num = math.ceil(len(input_list) / max_batch)
for i in tqdm(range(total_batch_num)):
sub_input_list = input_list[i * max_batch: (i + 1) * max_batch]
res = openai.Embedding.create(input = sub_input_list, model=model_name)['data']
res = [x['embedding'] for x in res]
total_res.extend(res)
return total_res
def openai_text_davinci_003(prompt, api_key):
response = openai.Completion.create(
model='text-davinci-003',
prompt=prompt,
temperature=0,
max_tokens=1500,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
api_key=api_key
)
return response['choices'][0]['text']
def openai_text_api(input_text, api_key, model_name = "gpt-3.5-turbo-1106", temperature = 0):
response = openai.ChatCompletion.create(
model=model_name,
messages=[{"role": "user", "content": input_text}],
temperature=temperature,
api_key=api_key)
return response
def openai_text_api_stream(input_text, api_key, model_name = "gpt-3.5-turbo-1106", temperature = 0.7):
print(api_key)
try:
print(model_name)
response = openai.ChatCompletion.create(
model=model_name,
messages=[{"role": "user", "content": input_text}],
temperature=temperature,
api_key=api_key,
stream=True,
)
completion = {'role': '', 'content': ''}
for event in response:
if event['choices'][0]['finish_reason'] == 'stop':
print(f'{completion}')
break
for delta_k, delta_v in event['choices'][0]['delta'].items():
print(f'{delta_k} = {delta_v}')
completion[delta_k] += delta_v
return completion['content']
except Exception as err:
print(err)
return str(err)
@persist_cache_to_disk("./ogb/res_chat.pkl")
def openai_text_api_list(input_texts):
out = []
for x in tqdm(input_texts):
resp = openai_text_api(x)
out.append(resp)
return out
async def chat_generate(agent, instruction, message):
if instruction:
res = await agent([instruction, message])
else:
res = await agent([message])
print("Generate")
return res
def generate_request_json_file_correct(texts, max_tokens = 300, filename = 'correct.jsonl'):
filename = osp.join("./ogb/data", filename)
jobs = [{"model": "gpt-3.5-turbo", "messages": [{'role': 'user', 'content': f"It seems a lot of words from the following paragraph lose some alphas in the end, can you help me correct them\n{line[:max_tokens]}"}]} for line in texts]
with open(filename, "w+") as f:
for job in jobs:
json_string = json.dumps(job)
f.write(json_string + "\n")
async def call_async_api(request_filepath, save_filepath, request_url, api_key, max_request_per_minute, max_tokens_per_minute, sp, ss):
await process_api_requests_from_file(
requests_filepath=request_filepath,
save_filepath=save_filepath,
request_url=request_url,
api_key=api_key,
max_requests_per_minute=float(max_request_per_minute),
max_tokens_per_minute=float(max_tokens_per_minute),
token_encoding_name='cl100k_base',
max_attempts=int(2),
logging_level=int(logging.INFO),
seconds_to_pause=sp,
seconds_to_sleep=ss
)
def num_tokens_from_messages(messages, model="gpt-3.5-turbo-0301"):
"""Returns the number of tokens used by a list of messages."""
try:
encoding = tiktoken.encoding_for_model(model)
except KeyError:
print("Warning: model not found. Using cl100k_base encoding.")
encoding = tiktoken.get_encoding("cl100k_base")
if model == "gpt-3.5-turbo":
print("Warning: gpt-3.5-turbo may change over time. Returning num tokens assuming gpt-3.5-turbo-0301.")
return num_tokens_from_messages(messages, model="gpt-3.5-turbo-0301")
elif model == "gpt-4":
print("Warning: gpt-4 may change over time. Returning num tokens assuming gpt-4-0314.")
return num_tokens_from_messages(messages, model="gpt-4-0314")
elif model == "gpt-3.5-turbo-0301":
tokens_per_message = 4 # every message follows <|start|>{role/name}\n{content}<|end|>\n
tokens_per_name = -1 # if there's a name, the role is omitted
elif model == "gpt-4-0314":
tokens_per_message = 3
tokens_per_name = 1
else:
raise NotImplementedError(f"""num_tokens_from_messages() is not implemented for model {model}. See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens.""")
num_tokens = 0
for message in messages:
num_tokens += tokens_per_message
for key, value in message.items():
num_tokens += len(encoding.encode(value))
if key == "name":
num_tokens += tokens_per_name
num_tokens += 3 # every reply is primed with <|start|>assistant<|message|>
return num_tokens
def num_tokens_from_string(string: str, model = "text-davinci-003") -> int:
"""Returns the number of tokens in a text string."""
encoding = tiktoken.encoding_for_model(model)
num_tokens = len(encoding.encode(string))
return num_tokens
def generate_chat_input_file(input_text, model_name = 'gpt-3.5-turbo-1106'):
jobs = []
for i, text in enumerate(input_text):
obj = {}
obj['model'] = model_name
obj['messages'] = [
{
'role': 'user',
'content': text
}
]
jobs.append(obj)
return jobs
@persist_cache_to_disk("./async_req_davinci.pkl")
def generate_davinci_003_input_file(input_text, model_name = 'text-davinci-003', max_token = 4096, temperature = 0.7, log_probs = None):
for text in input_text:
obj = {}
obj['model'] = model_name
obj['messages'] = [
{
"model": "text-davinci-003",
"prompt": text,
"max_tokens": max_token,
"temperature": temperature,
"stream": False,
"logprobs": log_probs
}
]
jobs.append(obj)
return jobs
def load_result_from_jsonline(json_file_name):
openai_result = []
with open(json_file_name, 'r') as f:
for line in f:
json_obj = json.loads(line.strip())
openai_result.append(json_obj[1]['choices'][0]['message']['content'])
return openai_result
async def async_openai_text_api(input_text, api_key, model_name = "gpt-3.5-turbo"):
response = await openai.ChatCompletion.acreate(
model=model_name,
messages=[{"role": "user", "content": input_text}],
temperature=0.7,
api_key=api_key)
return response['choices'][0]['message']['content']
def efficient_openai_text_api(input_text, filename, savepath, sp, ss, api_key="your-key", rewrite = True):
if not osp.exists(savepath) or rewrite:
jobs = generate_chat_input_file(input_text)
with open(filename, "w") as f:
for job in jobs:
json_string = json.dumps(job)
f.write(json_string + "\n")
asyncio.run(
call_async_api(
filename, save_filepath=savepath,
request_url="https://api.openai.com/v1/chat/completions",
api_key=api_key,
max_request_per_minute=1000,
max_tokens_per_minute=90000,
sp=sp,
ss=ss
)
)
openai_result = []
with open(savepath, 'r') as f:
for line in f:
json_obj = json.loads(line.strip())
idx = json_obj[-1]
if isinstance(idx, int):
openai_result.append((json_obj[1]['choices'][0]['message']['content'], idx))
else:
idx = json_obj[-2]
new_result = openai_text_api_stream(json_obj[0]['messages'][0]['content'], api_key)
openai_result.append((new_result, idx))
openai_result = sorted(openai_result, key=lambda x:x[-1])
return openai_result