-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLLMUtilities.py
481 lines (386 loc) · 15.9 KB
/
LLMUtilities.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# Repository: https://github.com/levitation-opensource/bioblue
import os
import time
import tenacity
import tiktoken
import traceback
import httpcore
import httpx
import json
import json_tricks
from openai import OpenAI
from anthropic import Anthropic
from Utilities import Timer, wait_for_enter
import configparser
import ast
config_path = r"config.ini"
config = configparser.ConfigParser()
config.read_file(open(config_path))
model_name = ast.literal_eval(config.get('Model params', 'name'))
# Initialize the appropriate client based on the model name
if model_name.lower().startswith('claude'):
from anthropic import Anthropic
claude_client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
print("Initialized Claude client")
elif model_name.lower().startswith('gpt'):
from openai import OpenAI
openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
print("Initialized OpenAI client")
else:
print(f"Unsupported model: {model_name}")
## https://platform.openai.com/docs/guides/rate-limits/error-mitigation
# TODO: config parameter for max attempt number
@tenacity.retry(
wait=tenacity.wait_random_exponential(min=1, max=60),
stop=tenacity.stop_after_attempt(10),
) # TODO: config parameters
def completion_with_backoff(
gpt_timeout, **kwargs
): # TODO: ensure that only HTTP 429 is handled here
# return openai.ChatCompletion.create(**kwargs)
attempt_number = completion_with_backoff.retry.statistics["attempt_number"]
max_attempt_number = completion_with_backoff.retry.stop.max_attempt_number
timeout_multiplier = 2 ** (attempt_number - 1) # increase timeout exponentially
try:
timeout = gpt_timeout * timeout_multiplier
# print(f"Sending OpenAI API request... Using timeout: {timeout} seconds")
# TODO!!! support for other LLM API-s
is_claude = model_name.startswith('claude-')
if is_claude:
messages = kwargs.pop('messages', [])
system_message = next((msg['content'] for msg in messages if msg['role'] == 'system'), None)
# Build the messages for Claude
claude_messages = []
claude_messages = [msg for msg in messages if msg['role'] != 'system']
response = claude_client.messages.create(
model=kwargs['model'],
system=system_message,
messages=claude_messages,
max_tokens=kwargs.get('max_tokens', 1024),
temperature=kwargs.get('temperature', 0.5)
)
return (response.content[0].text, response.stop_reason, response.usage.input_tokens, response.usage.output_tokens)
else:
# TODO!!! support for local LLM-s
#
# set openai internal max_retries to 1 so that we can log errors to console
openai_response = openai_client.with_options(
timeout=gpt_timeout, max_retries=1
).with_raw_response.chat.completions.create(**kwargs)
# print("Done OpenAI API request.")
openai_response = json_tricks.loads(
openai_response.content.decode("utf-8", "ignore")
)
if openai_response.get("error"):
if (
openai_response["error"]["code"] == 502
or openai_response["error"]["code"] == 503
): # Bad gateway or Service Unavailable
raise httpcore.NetworkError(openai_response["error"]["message"])
else:
raise Exception(
str(openai_response["error"]["code"])
+ " : "
+ openai_response["error"]["message"]
) # TODO: use a more specific exception type
# NB! this line may also throw an exception if the OpenAI announces that it is overloaded # TODO: do not retry for all error messages
response_content = openai_response["choices"][0]["message"]["content"]
finish_reason = openai_response["choices"][0]["finish_reason"]
return (response_content, finish_reason, None, None) # TODO: input_tokens, output_tokens
except Exception as ex:
t = type(
ex
)
if (
t is httpcore.ReadTimeout or t is httpx.ReadTimeout
): # both exception types have occurred
if attempt_number < max_attempt_number:
print("Read timeout, retrying...")
else:
print("Read timeout, giving up")
elif t is httpcore.NetworkError:
if attempt_number < max_attempt_number:
print("Network error, retrying...")
else:
print("Network error, giving up")
elif t is json.decoder.JSONDecodeError:
if attempt_number < max_attempt_number:
print("Response format error, retrying...")
else:
print("Response format error, giving up")
else: # / if (t ishttpcore.ReadTimeout
msg = f"{str(ex)}\n{traceback.format_exc()}"
print(msg)
if attempt_number < max_attempt_number:
wait_for_enter("Press any key to retry")
else:
print("Giving up")
# / if (t ishttpcore.ReadTimeout
raise
# / except Exception as ex:
# / def completion_with_backoff(gpt_timeout, **kwargs):
def get_encoding_for_model(model):
try:
encoding = tiktoken.encoding_for_model(model)
except KeyError:
print("Warning: model not found. Using cl100k_base encoding.")
encoding = tiktoken.get_encoding("cl100k_base")
return encoding
# / def get_encoding_for_model(model):
# https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
def num_tokens_from_messages(messages, model, encoding=None):
"""Return the number of tokens used by a list of messages."""
is_claude = model_name.startswith('claude-')
if is_claude:
return 0 # TODO
else: # OpenAI
if encoding is None:
encoding = get_encoding_for_model(model)
if model in {
"gpt-3.5-turbo-0125",
"gpt-3.5-turbo-0613",
"gpt-3.5-turbo-16k-0613",
"gpt-4-0314",
"gpt-4-0613",
"gpt-4-32k-0314",
"gpt-4-32k-0613",
"gpt-4o-mini-2024-07-18",
"gpt-4o-2024-08-06",
}:
tokens_per_message = 3
tokens_per_name = 1
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 "gpt-3.5-turbo-16k" in model: # roland
# print("Warning: gpt-3.5-turbo-16k may update over time. Returning num tokens assuming gpt-3.5-turbo-16k-0613.")
return num_tokens_from_messages(
messages, model="gpt-3.5-turbo-16k-0613", encoding=encoding
)
elif "gpt-3.5-turbo" in model:
# print("Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0613.")
return num_tokens_from_messages(
messages, model="gpt-3.5-turbo-0613", encoding=encoding
)
elif "gpt-4-32k" in model: # roland
# print("Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-32k-0613.")
return num_tokens_from_messages(
messages, model="gpt-4-32k-0613", encoding=encoding
)
elif "gpt-4o-mini" in model:
# print("Warning: gpt-4o-mini may update over time. Returning num tokens assuming gpt-4o-mini-2024-07-18.")
return num_tokens_from_messages(
messages, model="gpt-4o-mini-2024-07-18", encoding=encoding
)
elif "gpt-4o" in model:
# print("Warning: gpt-4o and gpt-4o-mini may update over time. Returning num tokens assuming gpt-4o-2024-08-06.")
return num_tokens_from_messages(
messages, model="gpt-4o-2024-08-06", encoding=encoding
)
elif "gpt-4" in model:
# print("Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.")
return num_tokens_from_messages(messages, model="gpt-4-0613", encoding=encoding)
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."""
# )
print(f"num_tokens_from_messages() is not implemented for model {model}")
# just take some conservative assumptions here
tokens_per_message = 4
tokens_per_name = 1
num_tokens = 0
for message in messages:
num_tokens += tokens_per_message
for key, value in message.items():
if key == "weight":
continue
num_tokens += len(encoding.encode(value))
if key == "name":
num_tokens += tokens_per_name
# / for key, value in message.items():
# / for message in messages:
num_tokens += 3 # every reply is primed with <|start|>assistant<|message|>
return num_tokens
# / def num_tokens_from_messages(messages, model, encoding=None):
def get_max_tokens_for_model(model_name):
# TODO: config
is_claude = model_name.startswith('claude-')
if is_claude:
# Adding Claude model token limits
claude_limits = {
# https://aws.amazon.com/bedrock/claude/
# TODO: check whether the listing below is complete
'claude-3-5-sonnet-latest': 200000,
'claude-3-5-haiku-latest': 200000,
'claude-3-5-sonnet-20241022': 200000,
'claude-3-5-haiku-20241022': 200000,
'claude-3-opus-latest': 200000,
'claude-3-sonnet-latest': 200000,
'claude-3-haiku-latest': 200000,
'claude-3-opus-20240229': 200000,
'claude-3-sonnet-20240229': 200000,
'claude-3-haiku-20240307': 200000,
'claude-2.1': 200000,
'claude-2.0': 100000,
'claude-instant': 100000,
}
if model_name in claude_limits:
max_tokens = claude_limits[model_name]
else:
assert False # you probably have to add your model name to above list
max_tokens = 100000 # 4096
# OpenAI models # TODO: refactor to use dictionary like claude's branch uses
elif model_name == "o1": # https://platform.openai.com/docs/models/#o1
max_tokens = 200000
elif model_name == "o1-2024-12-17": # https://platform.openai.com/docs/models/#o1
max_tokens = 200000
elif model_name == "o1-mini": # https://platform.openai.com/docs/models/#o1
max_tokens = 128000
elif (
model_name == "o1-mini-2024-09-12"
): # https://platform.openai.com/docs/models/#o1
max_tokens = 128000
elif model_name == "o1-preview": # https://platform.openai.com/docs/models/#o1
max_tokens = 128000
elif (
model_name == "o1-preview-2024-09-12"
): # https://platform.openai.com/docs/models/#o1
max_tokens = 128000
elif (
model_name == "gpt-4o-mini"
): # https://platform.openai.com/docs/models/gpt-4o-mini
max_tokens = 128000
elif (
model_name == "gpt-4o-mini-2024-07-18"
): # https://platform.openai.com/docs/models/gpt-4o-mini
max_tokens = 128000
elif model_name == "gpt-4o": # https://platform.openai.com/docs/models/gpt-4o
max_tokens = 128000
elif (
model_name == "gpt-4o-2024-05-13"
): # https://platform.openai.com/docs/models/gpt-4o
max_tokens = 128000
elif (
model_name == "gpt-4o-2024-08-06"
): # https://platform.openai.com/docs/models/gpt-4o
max_tokens = 128000
elif (
model_name == "gpt-4o-2024-11-20"
): # https://platform.openai.com/docs/models/gpt-4o
max_tokens = 128000
elif (
model_name == "chatgpt-4o-latest"
): # https://platform.openai.com/docs/models/gpt-4o
max_tokens = 128000
elif model_name == "gpt-4-turbo": # https://platform.openai.com/docs/models/gpt-4
max_tokens = 128000
elif (
model_name == "gpt-4-turbo-2024-04-09"
): # https://platform.openai.com/docs/models/gpt-4
max_tokens = 128000
elif (
model_name == "gpt-4-turbo-preview"
): # https://platform.openai.com/docs/models/gpt-4
max_tokens = 128000
elif (
model_name == "gpt-4-0125-preview"
): # https://platform.openai.com/docs/models/gpt-4
max_tokens = 128000
elif (
model_name == "gpt-4-1106-preview"
): # https://platform.openai.com/docs/models/gpt-4
max_tokens = 128000
elif model_name == "gpt-4-32k": # https://platform.openai.com/docs/models/gpt-4
max_tokens = 32768
elif (
model_name == "gpt-3.5-turbo-16k"
): # https://platform.openai.com/docs/models/gpt-3-5
max_tokens = 16384
elif model_name == "gpt-4": # https://platform.openai.com/docs/models/gpt-4
max_tokens = 8192
elif model_name == "gpt-4-0314": # https://platform.openai.com/docs/models/gpt-4
max_tokens = 8192
elif model_name == "gpt-4-0613": # https://platform.openai.com/docs/models/gpt-4
max_tokens = 8192
elif (
model_name == "gpt-3.5-turbo-0125"
): # https://platform.openai.com/docs/models/gpt-3-5-turbo
max_tokens = 16385
elif (
model_name == "gpt-3.5-turbo"
): # https://platform.openai.com/docs/models/gpt-3-5-turbo
max_tokens = 16385
elif (
model_name == "gpt-3.5-turbo-1106"
): # https://platform.openai.com/docs/models/gpt-3-5-turbo
max_tokens = 16385
elif (
model_name == "gpt-3.5-turbo-instruct"
): # https://platform.openai.com/docs/models/gpt-3-5-turbo
max_tokens = 4096
else:
max_tokens = 128000
return max_tokens
# / def get_max_tokens_for_model(model_name):
# TODO: caching support
def run_llm_completion(
model_name, gpt_timeout, messages, temperature=0, max_output_tokens=100
):
is_claude = model_name.startswith('claude-')
if is_claude:
num_input_tokens = 0 # TODO
else:
num_input_tokens = num_tokens_from_messages(
messages, model_name
) # TODO: a more precise token count is already provided by OpenAI, no need to recalculate it here
max_tokens = get_max_tokens_for_model(model_name)
print(f"num_input_tokens: {num_input_tokens} max_tokens: {max_tokens}")
time_start = time.time()
(response_content, finish_reason, input_tokens, output_tokens) = completion_with_backoff(
gpt_timeout,
model=model_name,
messages=messages,
n=1,
stream=False,
temperature=temperature, # 1, 0 means deterministic output # TODO: increase in case of sampling the GPT multiple times per same text
top_p=1,
max_tokens=max_output_tokens,
presence_penalty=0,
frequency_penalty=0,
# logit_bias = None,
)
time_elapsed = time.time() - time_start
too_long = finish_reason == "length" if not is_claude else finish_reason == "max_tokens"
assert not too_long
output_message = {"role": "assistant", "content": response_content}
if is_claude:
num_output_tokens = output_tokens
num_total_tokens = input_tokens + output_tokens
else:
# TODO: use input_tokens, output_tokens variables
num_output_tokens = num_tokens_from_messages(
[output_message], model_name
) # TODO: a more precise token count is already provided by OpenAI, no need to recalculate it here
num_total_tokens = num_input_tokens + num_output_tokens
print(
f"num_total_tokens: {num_total_tokens} num_output_tokens: {num_output_tokens} max_tokens: {max_tokens} performance: {(num_output_tokens / time_elapsed)} output_tokens/sec"
)
return response_content, output_message
# / def run_llm_completion(model_name, gpt_timeout, messages, temperature = 0, sample_index = 0):
def extract_int_from_text(text):
result = int(''.join(c for c in text if c.isdigit() or c == "-"))
return result
def format_float(value):
if abs(value) < 1e-3: # TODO: tune/config
value = 0
# format to have three numbers in total, regardless whether they are before or after comma
text = "{0:.3f}".format(float(value)) # TODO: tune/config
if text == "0.000" or text == "-0.000":
text = "0.0"
return text