forked from lmarena/arena-hard-auto
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
1099 lines (919 loc) · 42.9 KB
/
utils.py
File metadata and controls
1099 lines (919 loc) · 42.9 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
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import json
import time
import yaml
import random
import requests
from typing import Optional
from glob import glob
# API setting constants
API_MAX_RETRY = 16
API_RETRY_SLEEP = 10
API_ERROR_OUTPUT = "$ERROR$"
OPENAI_MODEL_LIST = (
"gpt-3.5-turbo",
"gpt-3.5-turbo-0301",
"gpt-3.5-turbo-0613",
"gpt-3.5-turbo-0613-verbose",
"gpt-3.5-turbo-1106",
"gpt-3.5-turbo-0125",
"gpt-4",
"gpt-4-0314",
"gpt-4-0613",
"gpt-4-turbo",
"gpt-4-1106-preview",
"gpt-4-0125-preview",
)
temperature_config = {
"writing": 0.7,
"roleplay": 0.7,
"extraction": 0.0,
"math": 0.0,
"coding": 0.0,
"reasoning": 0.0,
"stem": 0.1,
"humanities": 0.1,
}
def load_questions(question_file: str):
"""Load questions from a file."""
questions = []
with open(question_file, "r") as ques_file:
for line in ques_file:
if line:
questions.append(json.loads(line))
return questions
def load_model_answers(answer_dir: str):
"""Load model answers.
The return value is a python dict of type:
Dict[model_name: str -> Dict[question_id: int -> answer: dict]]
"""
filenames = glob(os.path.join(answer_dir, "*.jsonl"))
filenames.sort()
model_answers = {}
for filename in filenames:
model_name = os.path.basename(filename)[:-6]
answer = {}
with open(filename) as fin:
for line in fin:
line = json.loads(line)
answer[line["question_id"]] = line
model_answers[model_name] = answer
return model_answers
def get_endpoint(endpoint_list):
if endpoint_list is None:
return None
assert endpoint_list is not None
# randomly pick one
api_dict = random.choices(
endpoint_list
)[0]
return api_dict
# load config args from config yaml files
def make_config(config_file: str) -> dict:
config_kwargs = {}
with open(config_file, "r") as f:
config_kwargs = yaml.load(f, Loader=yaml.SafeLoader)
return config_kwargs
import re
def remove_special_tokens(messages):
# List of special tokens to remove
special_tokens = [
r'<\|im_start\|>', r'<\|im_end\|>', # ChatML tokens
r'<s>', r'</s>', r'<\|eot_id\|>', # LLaMA tokens
r'<\|endoftext\|>', # GPT special token
]
new_messages = []
# Combine all special tokens into a single regex pattern
pattern = '|'.join(special_tokens)
for m in messages:
# Remove all instances of special tokens from the prompt
m['content'] = re.sub(pattern, '', m['content'])
# Remove any extra whitespace that might have been left behind
m['content'] = ' '.join(m['content'].split())
new_messages.append(m)
return new_messages
def remove_duplicate_char_ngrams(messages, n):
new_messages = []
for m in messages:
# Generate character n-grams
text = m['content']
ngrams = [text[i:i+n] for i in range(len(text)-n+1)]
# Keep track of seen n-grams
seen = set()
result = []
for i, ngram in enumerate(ngrams):
if ngram not in seen:
# This is a new n-gram, add it to the result
result.append(text[i])
seen.add(ngram)
else:
# This is a duplicate n-gram, skip it
pass
# Add any remaining characters
result.extend(text[len(text)-n+1:])
# Join the characters back into a string
s = ''.join(result)
m['content'] = s
new_messages.append(m)
return new_messages
def chat_completion_openai(model, messages, temperature, max_tokens, api_dict=None, return_logprobs=False):
import openai
if api_dict:
client = openai.OpenAI(
base_url=api_dict["api_base"],
api_key=api_dict.get("api_key", ""),
)
else:
client = openai.OpenAI()
output = API_ERROR_OUTPUT
for _ in range(API_MAX_RETRY):
try:
# Add logprobs parameter if return_logprobs is True
kwargs = {
"model": model,
"messages": messages,
"temperature": temperature,
"max_tokens": max_tokens,
"timeout": 60,
}
if return_logprobs:
kwargs["logprobs"] = True
completion = client.chat.completions.create(**kwargs)
if return_logprobs:
# Return both content and logprobs
output = {
"content": completion.choices[0].message.content,
"logprobs": completion.choices[0].logprobs
}
else:
# Return just the content as before
output = completion.choices[0].message.content
break
except openai.RateLimitError as e:
print(type(e), e)
time.sleep(API_RETRY_SLEEP)
except openai.BadRequestError as e:
print(messages)
print(type(e), e, dir(e))
#NOTE: this has the potential to produce some pretty weird strings; should be careful here
# if e.error['code'] == 'invalid_prompt':
messages = remove_special_tokens(messages)
messages = remove_duplicate_char_ngrams(messages, 50)
except KeyError:
print(type(e), e)
break
return output
def chat_completion_openai_azure(model, messages, temperature, max_tokens, api_dict=None):
import openai
from openai import AzureOpenAI
api_base = api_dict["api_base"]
client = AzureOpenAI(
azure_endpoint = api_base,
api_key= api_dict["api_key"],
api_version=api_dict["api_version"],
timeout=240,
max_retries=2
)
output = API_ERROR_OUTPUT
for _ in range(API_MAX_RETRY):
try:
response = client.chat.completions.create(
model=model,
messages=messages,
n=1,
temperature=temperature,
max_tokens=max_tokens,
seed=42,
)
output = response.choices[0].message.content
break
except openai.RateLimitError as e:
print(type(e), e)
time.sleep(API_RETRY_SLEEP)
except openai.BadRequestError as e:
print(type(e), e)
break
except KeyError:
print(type(e), e)
break
return output
def chat_completion_anthropic(model, messages, temperature, max_tokens, api_dict=None):
import anthropic
if api_dict:
api_key = api_dict["api_key"]
else:
api_key = os.environ["ANTHROPIC_API_KEY"]
sys_msg = ""
if messages[0]["role"] == "system":
sys_msg = messages[0]["content"]
messages = messages[1:]
output = API_ERROR_OUTPUT
for _ in range(API_MAX_RETRY):
try:
c = anthropic.Anthropic(api_key=api_key)
response = c.messages.create(
model=model,
messages=messages,
stop_sequences=[anthropic.HUMAN_PROMPT],
max_tokens=max_tokens,
temperature=temperature,
system=sys_msg
)
output = response.content[0].text
break
except anthropic.APIError as e:
print(type(e), e)
time.sleep(API_RETRY_SLEEP)
return output
def chat_completion_huggingface(model, conv, temperature, max_tokens, return_logprobs=False):
"""
Makes a request to HuggingFace's API for text generation.
Args:
model: The HuggingFace model ID to use
conv: The conversation/prompt to send to the model (list of message dicts)
temperature: Temperature setting for generation
max_tokens: Maximum number of tokens to generate
return_logprobs: Whether to return logprobs alongside the generated text
Returns:
If return_logprobs is False, returns the generated text.
If return_logprobs is True, returns a dict with 'content' and 'logprobs' keys.
"""
API_BASE = "https://api-inference.huggingface.co/models/"
API_URL = API_BASE + model
headers = {"Authorization": "Bearer " + str(os.environ["HUGGINGFACE_API_KEY"])}
# Convert the conversation format to a single text prompt that HF models expect
prompt = ""
# Format the conversation into a text prompt
for message in conv:
role = message.get("role", "")
content = message.get("content", "")
if role == "system":
prompt += f"<|system|>\n{content}\n"
elif role == "user":
prompt += f"<|user|>\n{content}\n"
elif role == "assistant":
prompt += f"<|assistant|>\n{content}\n"
# Add the final assistant prefix to prompt the model to generate
prompt += "<|assistant|>\n"
# Use different payload configurations based on the model
if "llama" in model.lower():
# Llama-specific parameters
payload = {
"inputs": prompt,
"parameters": {
"temperature": temperature,
"max_new_tokens": max_tokens,
"return_full_text": False,
"details": True, # Llama models may need this for logprobs
}
}
else:
# Generic payload
payload = {
"inputs": prompt,
"parameters": {
"temperature": temperature,
"max_new_tokens": max_tokens,
"return_full_text": False,
}
}
if return_logprobs:
# Add all possible parameters for returning logprobs
# Different models might use different parameter names
payload["parameters"]["return_scores"] = True
payload["parameters"]["return_token_scores"] = True
payload["parameters"]["return_tokens"] = True
payload["parameters"]["return_details"] = True
payload["parameters"]["details"] = True
payload["parameters"]["output_scores"] = True
# print(f"DEBUG - Sending request to HuggingFace API for model: {model}")
# print(f"DEBUG - Payload parameters: {payload['parameters']}")
try:
response = requests.post(API_URL, headers=headers, json=payload)
response.raise_for_status() # Raise an exception for HTTP errors
result = response.json()
# Check the structure of the response
if isinstance(result[0], dict):
# Try different possible keys for scores
score_key = None
token_key = None
# Check for "details" key used by Llama and some other models
if "details" in result[0]:
#print(f"DEBUG - Found details key with type: {type(result[0]['details'])}")
details = result[0]["details"]
if isinstance(details, dict):
#print(f"DEBUG - Details keys: {details.keys()}")
# Look for token logprobs in details
if "logprobs" in details:
# print("DEBUG - Found logprobs in details")
logprobs_data = details["logprobs"]
if isinstance(logprobs_data, dict):
#print(f"DEBUG - Logprobs keys: {logprobs_data.keys()}")
if "tokens" in logprobs_data and "token_logprobs" in logprobs_data:
# print("DEBUG - Found tokens and token_logprobs in logprobs")
token_texts = logprobs_data["tokens"]
token_scores = logprobs_data["token_logprobs"]
#print(f"DEBUG - Found {len(token_scores)} token scores and {len(token_texts)} token texts")
if len(token_scores) > 0 and len(token_texts) > 0:
logprobs = {
"content": [
{"text": token, "logprob": score}
for token, score in zip(token_texts, token_scores)
]
}
return {
"content": result[0].get("generated_text", ""),
"logprobs": logprobs
}
# Extract logprobs from tokens field (present in Llama-3.1 responses)
if "tokens" in details:
# print("DEBUG - Found tokens field in details, attempting to extract logprobs")
tokens_data = details["tokens"]
if isinstance(tokens_data, list) and len(tokens_data) > 0:
#print(f"DEBUG - Found {len(tokens_data)} tokens")
# Print sample token to understand the structure
if len(tokens_data) > 0:
#print(f"DEBUG - Token structure example: {str(tokens_data[0])}")
# Try to extract id and logprob
try:
# Adapt to different possible token structures
token_content = []
for t in tokens_data:
if isinstance(t, dict):
# Extract text/id and logprob from token dict
text = t.get("text", str(t.get("id", "")))
logprob = t.get("logprob", 0)
token_content.append({"text": text, "logprob": logprob})
else:
# If token isn't a dict, use string representation with default logprob
token_content.append({"text": str(t), "logprob": 0})
if token_content:
#print(f"DEBUG - Successfully extracted {len(token_content)} tokens with logprobs")
logprobs = {"content": token_content}
return {
"content": result[0].get("generated_text", ""),
"logprobs": logprobs
}
except Exception as e:
print(f"DEBUG - Error extracting token information: {e}")
# Standard response format
if "scores" in result[0]:
score_key = "scores"
elif "token_scores" in result[0]:
score_key = "token_scores"
elif "log_probs" in result[0]:
score_key = "log_probs"
if "tokens" in result[0]:
token_key = "tokens"
elif "token_texts" in result[0]:
token_key = "token_texts"
content = result[0].get("generated_text", "")
if return_logprobs and score_key and token_key and content:
# Extract the logprobs from the response
token_scores = result[0].get(score_key, [])
token_texts = result[0].get(token_key, [])
#print(f"DEBUG - Found {score_key}: {len(token_scores)}")
#print(f"DEBUG - Found {token_key}: {len(token_texts)}")
if len(token_scores) > 0 and len(token_texts) > 0:
# Create a logprobs object similar to OpenAI's format
logprobs = {
"content": [
{"text": token, "logprob": score}
for token, score in zip(token_texts, token_scores)
]
}
return {
"content": content,
"logprobs": logprobs
}
else:
# print("DEBUG - No token scores or texts found, returning without logprobs")
return content
else:
# print("DEBUG - Missing required keys for logprobs, returning without logprobs")
return content
else:
# print("DEBUG - First result is not a dictionary")
# Return just the generated text
return result[0].get("generated_text", "")
else:
#print(f"DEBUG - Unexpected response format from HuggingFace API: {result}")
return API_ERROR_OUTPUT
except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
print(f"Response: {response.text if 'response' in locals() else 'No response'}")
return API_ERROR_OUTPUT
except requests.exceptions.RequestException as req_err:
print(f"Request error occurred: {req_err}")
return API_ERROR_OUTPUT
except ValueError as json_err:
print(f"JSON parsing error: {json_err}")
print(f"Response text: {response.text if 'response' in locals() else 'No response'}")
return API_ERROR_OUTPUT
except Exception as e:
print(f"Error in HuggingFace API call: {e}")
print(f"Response: {response.text if 'response' in locals() else 'No response'}")
return API_ERROR_OUTPUT
# Global model and tokenizer cache
_HF_MODEL_CACHE = {}
_HF_TOKENIZER_CACHE = {}
def chat_completion_huggingface_local(model, conv, temperature, max_tokens, return_logprobs=False):
"""
Loads and runs a HuggingFace model locally for text generation with support for CUDA, ROCm, and Metal.
Models are cached in memory to avoid reloading them for subsequent calls.
Args:
model: The HuggingFace model ID to load locally
conv: The conversation/prompt to send to the model (list of message dicts)
temperature: Temperature setting for generation
max_tokens: Maximum number of tokens to generate
return_logprobs: Whether to return logprobs alongside the generated text
Returns:
If return_logprobs is False, returns the generated text.
If return_logprobs is True, returns a dict with 'content' and 'logprobs' keys.
"""
try:
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
from threading import Thread
except ImportError:
print("ERROR: Required libraries not installed. Please run: pip install torch transformers")
return API_ERROR_OUTPUT
output = API_ERROR_OUTPUT
for _ in range(API_MAX_RETRY):
try:
# Detect available computing resources
if torch.cuda.is_available():
device = "cuda"
print(f"Using CUDA device: {torch.cuda.get_device_name(0)}")
elif hasattr(torch.backends, 'mps') and torch.backends.mps.is_built() and torch.backends.mps.is_available():
device = "mps" # Apple Silicon
print("Using Apple Metal (MPS) device")
elif hasattr(torch.version, 'hip') and torch.version.hip is not None:
device = "cuda" # ROCm uses the CUDA interface
print("Using AMD ROCm device")
else:
device = "cpu"
print("WARNING: No GPU detected. Using CPU which may be slow.")
# Create a unique key for caching the model based on model ID and configuration
cache_key = model
if os.environ.get("USE_4BIT_QUANTIZATION", "").lower() == "true":
cache_key += "_4bit"
cache_key += f"_{device}"
# Check tokenizer cache first
if cache_key in _HF_TOKENIZER_CACHE:
print(f"Using cached tokenizer for {model}")
tokenizer = _HF_TOKENIZER_CACHE[cache_key]
else:
print(f"Loading tokenizer for {model}...")
tokenizer = AutoTokenizer.from_pretrained(model)
_HF_TOKENIZER_CACHE[cache_key] = tokenizer
# Now check model cache
if cache_key in _HF_MODEL_CACHE:
print(f"Using cached model for {model}")
model_instance = _HF_MODEL_CACHE[cache_key]
else:
# Model not in cache, need to load it
print(f"Loading model {model} (not found in cache)...")
# Set up appropriate model loading parameters
model_kwargs = {
"device_map": device,
"torch_dtype": torch.float16, # Use fp16 for efficiency
}
# Handle CPU-only mode
if device == "cpu":
model_kwargs = {
"low_cpu_mem_usage": True,
"torch_dtype": torch.float16, # Use half precision even on CPU for memory efficiency
}
# Special handling for MPS (Apple Silicon)
if device == "mps":
# For models on MPS, enable lower precision
model_kwargs["torch_dtype"] = torch.float16
# Add 4-bit quantization for memory-constrained environments
if os.environ.get("USE_4BIT_QUANTIZATION", "").lower() == "true":
try:
from transformers import BitsAndBytesConfig
model_kwargs["quantization_config"] = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4"
)
print("Using 4-bit quantization")
except ImportError:
print("Warning: bitsandbytes not available for 4-bit quantization")
try:
# Create offload folder if needed
if "offload_folder" in model_kwargs and not os.path.exists(model_kwargs["offload_folder"]):
os.makedirs(model_kwargs["offload_folder"], exist_ok=True)
# Load the model with specified configuration
print(f"Loading model with settings: device={device}, dtype={model_kwargs.get('torch_dtype', 'default')}")
model_instance = AutoModelForCausalLM.from_pretrained(
model,
**model_kwargs
)
# Cache the loaded model
_HF_MODEL_CACHE[cache_key] = model_instance
print(f"Model {model} successfully loaded and cached (cache key: {cache_key})")
except (RuntimeError, ValueError, OSError) as e:
if "Invalid buffer size" in str(e) or "CUDA out of memory" in str(e) or "MPS backend out of memory" in str(e):
print(f"ERROR: Not enough memory to load model {model}. Trying CPU with minimal memory settings...")
# Fall back to CPU with minimal memory settings
device = "cpu"
os.environ["USE_4BIT_QUANTIZATION"] = "true" # Force quantization
# Make offload directory if it doesn't exist
if not os.path.exists("offload_folder"):
os.makedirs("offload_folder", exist_ok=True)
# Update cache key for fallback configuration
cache_key = f"{model}_4bit_cpu_fallback"
model_kwargs = {
"device_map": "auto",
"low_cpu_mem_usage": True,
"offload_folder": "offload_folder",
"offload_state_dict": True,
"torch_dtype": torch.float16,
}
# Try with 4-bit quantization as last resort
try:
from transformers import BitsAndBytesConfig
model_kwargs["quantization_config"] = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4"
)
print("Using 4-bit quantization with CPU fallback")
except ImportError:
print("Warning: bitsandbytes not available for 4-bit quantization")
try:
model_instance = AutoModelForCausalLM.from_pretrained(
model,
**model_kwargs
)
# Cache the fallback model
_HF_MODEL_CACHE[cache_key] = model_instance
print(f"Fallback model {model} successfully loaded and cached (cache key: {cache_key})")
except Exception as e2:
print(f"ERROR: Failed to load model even with minimal memory settings: {e2}")
raise RuntimeError(f"Could not load model {model} with any configuration: {e2}")
else:
raise # Re-raise the original error if it's not memory-related
# Convert the conversation format to a single text prompt
prompt = ""
# Format the conversation into a text prompt
for message in conv:
role = message.get("role", "")
content = message.get("content", "")
# Handle different chat templates (adjust as needed for specific models)
if tokenizer.chat_template:
# If the model has a chat template, use it
# This is the most robust approach
messages_for_template = [{"role": m["role"], "content": m["content"]} for m in conv]
prompt = tokenizer.apply_chat_template(messages_for_template, tokenize=False)
break # Skip the manual formatting below
elif "llama" in model.lower() or "mistral" in model.lower():
# Llama/Mistral style formatting
if role == "system":
prompt += f"<|system|>\n{content}\n"
elif role == "user":
prompt += f"<|user|>\n{content}\n"
elif role == "assistant":
prompt += f"<|assistant|>\n{content}\n"
elif "falcon" in model.lower():
# Falcon style formatting
if role == "system":
prompt += f"System: {content}\n"
elif role == "user":
prompt += f"User: {content}\n"
elif role == "assistant":
prompt += f"Assistant: {content}\n"
else:
# Generic formatting
if role == "system":
prompt += f"System: {content}\n"
elif role == "user":
prompt += f"User: {content}\n"
elif role == "assistant":
prompt += f"Assistant: {content}\n"
# Add the final assistant prefix to prompt the model to generate
if not tokenizer.chat_template:
if "llama" in model.lower() or "mistral" in model.lower():
prompt += "<|assistant|>\n"
else:
prompt += "Assistant: "
# Tokenize the input
model_inputs = tokenizer(prompt, return_tensors="pt").to(device)
# Setup generation parameters
gen_kwargs = {
"max_new_tokens": max_tokens,
"temperature": temperature,
"do_sample": temperature > 0.0,
"top_p": 0.95,
"repetition_penalty": 1.1,
}
# Add logprobs parameters if requested
if return_logprobs:
gen_kwargs["output_scores"] = True
gen_kwargs["return_dict_in_generate"] = True
# Generate text with or without logprobs
if return_logprobs:
generation_output = model_instance.generate(
**model_inputs,
**gen_kwargs
)
# Extract the generated tokens
generated_tokens = generation_output.sequences[0][model_inputs.input_ids.shape[1]:]
# Get the scores (log probabilities)
scores = generation_output.scores
# Convert scores to token logprobs
token_logprobs = []
for i, token_scores in enumerate(scores):
# Get the score for the selected token (highest probability)
token_id = generated_tokens[i].item()
token_score = token_scores[0, token_id].item() # Use log probabilities
token_logprobs.append(token_score)
# Convert tokens to text
generated_text = tokenizer.decode(generated_tokens, skip_special_tokens=True)
# Create token text list
token_texts = tokenizer.convert_ids_to_tokens(generated_tokens)
# Build the result in the required format
logprobs = {
"content": [
{"text": text, "logprob": logprob}
for text, logprob in zip(token_texts, token_logprobs)
]
}
output = {
"content": generated_text,
"logprobs": logprobs
}
else:
# Without logprobs, just generate the text
generated_tokens = model_instance.generate(
**model_inputs,
**gen_kwargs
)
generated_text = tokenizer.decode(generated_tokens[0][model_inputs.input_ids.shape[1]:], skip_special_tokens=True)
output = generated_text
break
except Exception as e:
print(f"Error in local model inference: {type(e)}: {e}")
time.sleep(API_RETRY_SLEEP)
# We no longer clean up the model as we want to keep it cached
return output
def chat_completion_mistral(model, messages, temperature, max_tokens):
from mistralai.client import MistralClient
from mistralai.models.chat_completion import ChatMessage
from mistralai.exceptions import MistralException
api_key = os.environ["MISTRAL_API_KEY"]
client = MistralClient(api_key=api_key)
prompts = [ChatMessage(role=message["role"], content=message["content"]) for message in messages]
output = API_ERROR_OUTPUT
for _ in range(API_MAX_RETRY):
try:
chat_response = client.chat(
model=model,
messages=prompts,
temperature=temperature,
max_tokens=max_tokens,
)
output = chat_response.choices[0].message.content
break
except MistralException as e:
print(type(e), e)
break
return output
def http_completion_gemini(model, message, temperature, max_tokens):
api_key = os.environ["GEMINI_API_KEY"]
safety_settings = [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_NONE"
},
]
output = API_ERROR_OUTPUT
try:
response = requests.post(
f"https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent?key={api_key}",
json={
"contents": [{
"parts":[
{"text": message}
]
}],
"safetySettings": safety_settings,
"generationConfig":{
"temperature": temperature,
"maxOutputTokens": max_tokens,
}
},
)
except Exception as e:
print(f"**API REQUEST ERROR** Reason: {e}.")
if response.status_code != 200:
print(f"**API REQUEST ERROR** Reason: status code {response.status_code}.")
output = response.json()["candidates"][0]["content"]["parts"][0]["text"]
return output
def chat_completion_cohere(model, messages, temperature, max_tokens):
import cohere
co = cohere.Client(os.environ["COHERE_API_KEY"])
assert len(messages) > 0
template_map = {"system":"SYSTEM",
"assistant":"CHATBOT",
"user":"USER"}
assert messages[-1]["role"] == "user"
prompt = messages[-1]["content"]
if len(messages) > 1:
history = []
for message in messages[:-1]:
history.append({"role":template_map[message["role"]], "message":message["content"]})
else:
history = None
output = API_ERROR_OUTPUT
for _ in range(API_MAX_RETRY):
try:
response = co.chat(
message=prompt,
model=model,
temperature=temperature,
max_tokens=max_tokens,
chat_history=history,
)
output = response.text
break
except cohere.core.api_error.ApiError as e:
print(type(e), e)
raise
except Exception as e:
print(type(e), e)
break
return output
def chat_completion_together(model, messages, temperature, max_tokens, api_dict=None, return_logprobs=False):
"""
Makes a request to Together AI's API for text generation.
Args:
model: The Together AI model ID to use
messages: The conversation/prompt to send to the model (list of message dicts)
temperature: Temperature setting for generation
max_tokens: Maximum number of tokens to generate
api_dict: Dictionary containing API configuration (api_base)
return_logprobs: Whether to return logprobs alongside the generated text
Returns:
If return_logprobs is False, returns the generated text.
If return_logprobs is True, returns a dict with 'content' and 'logprobs' keys.
"""
import requests
# Always get API key from environment variable
api_key = os.environ.get("TOGETHER_API_KEY", "")
if not api_key:
print("Error: TOGETHER_API_KEY environment variable not set")
return API_ERROR_OUTPUT
# Get API base URL from config if provided, otherwise use default
if api_dict:
api_base = api_dict.get("api_base", "https://api.together.xyz/v1")
else:
api_base = os.environ.get("TOGETHER_API_BASE", "https://api.together.xyz/v1")
# Ensure the API base ends with /v1
if not api_base.endswith("/v1"):
api_base = api_base.rstrip("/") + "/v1"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
# Prepare the payload for the API request
payload = {
"model": model,
"messages": messages,
"temperature": temperature,
"max_tokens": max_tokens,
}
# Add logprobs if requested
if return_logprobs:
# Together API uses integer value 1 for logprobs
payload["logprobs"] = 1
output = API_ERROR_OUTPUT
for _ in range(API_MAX_RETRY):
try:
# Send the request to the Together API
response = requests.post(
f"{api_base}/chat/completions",
headers=headers,
json=payload
)
response.raise_for_status()
result = response.json()
# Print the response structure for debugging
if return_logprobs:
# print("DEBUG - Together API logprobs response structure:")
if "choices" in result and len(result["choices"]) > 0 and "logprobs" in result["choices"][0]:
logprobs_data = result["choices"][0]["logprobs"]
# print(f"Logprobs keys: {logprobs_data.keys()}")
# Extract the generated text
if "choices" in result and len(result["choices"]) > 0:
content = result["choices"][0]["message"]["content"]
# Handle logprobs if requested and available
if return_logprobs and "logprobs" in result["choices"][0]:
logprobs_data = result["choices"][0]["logprobs"]
# Format logprobs similar to OpenAI format for compatibility
tokens_with_logprobs = []
# Together API format has parallel arrays for tokens and logprobs
if ("tokens" in logprobs_data and
"token_logprobs" in logprobs_data and
len(logprobs_data["tokens"]) == len(logprobs_data["token_logprobs"])):
tokens = logprobs_data["tokens"]
token_logprobs = logprobs_data["token_logprobs"]
for token_text, token_logprob in zip(tokens, token_logprobs):