forked from stanford-oval/storm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lm.py
595 lines (504 loc) · 21.8 KB
/
lm.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
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
import logging
import os
import random
import threading
from typing import Optional, Literal, Any
import backoff
import dspy
import requests
from dsp import ERRORS, backoff_hdlr, giveup_hdlr
from dsp.modules.hf import openai_to_hf
from dsp.modules.hf_client import send_hfvllm_request_v00, send_hftgi_request_v01_wrapped
from transformers import AutoTokenizer
try:
from anthropic import RateLimitError
except ImportError:
RateLimitError = None
class OpenAIModel(dspy.OpenAI):
"""A wrapper class for dspy.OpenAI."""
def __init__(
self,
model: str = "gpt-3.5-turbo-instruct",
api_key: Optional[str] = None,
model_type: Literal["chat", "text"] = None,
**kwargs
):
super().__init__(model=model, api_key=api_key, model_type=model_type, **kwargs)
self._token_usage_lock = threading.Lock()
self.prompt_tokens = 0
self.completion_tokens = 0
def log_usage(self, response):
"""Log the total tokens from the OpenAI API response."""
usage_data = response.get('usage')
if usage_data:
with self._token_usage_lock:
self.prompt_tokens += usage_data.get('prompt_tokens', 0)
self.completion_tokens += usage_data.get('completion_tokens', 0)
def get_usage_and_reset(self):
"""Get the total tokens used and reset the token usage."""
usage = {
self.kwargs.get('model') or self.kwargs.get('engine'):
{'prompt_tokens': self.prompt_tokens, 'completion_tokens': self.completion_tokens}
}
self.prompt_tokens = 0
self.completion_tokens = 0
return usage
def __call__(
self,
prompt: str,
only_completed: bool = True,
return_sorted: bool = False,
**kwargs,
) -> list[dict[str, Any]]:
"""Copied from dspy/dsp/modules/gpt3.py with the addition of tracking token usage."""
assert only_completed, "for now"
assert return_sorted is False, "for now"
# if kwargs.get("n", 1) > 1:
# if self.model_type == "chat":
# kwargs = {**kwargs}
# else:
# kwargs = {**kwargs, "logprobs": 5}
response = self.request(prompt, **kwargs)
# Log the token usage from the OpenAI API response.
self.log_usage(response)
choices = response["choices"]
completed_choices = [c for c in choices if c["finish_reason"] != "length"]
if only_completed and len(completed_choices):
choices = completed_choices
completions = [self._get_choice_text(c) for c in choices]
if return_sorted and kwargs.get("n", 1) > 1:
scored_completions = []
for c in choices:
tokens, logprobs = (
c["logprobs"]["tokens"],
c["logprobs"]["token_logprobs"],
)
if "<|endoftext|>" in tokens:
index = tokens.index("<|endoftext|>") + 1
tokens, logprobs = tokens[:index], logprobs[:index]
avglog = sum(logprobs) / len(logprobs)
scored_completions.append((avglog, self._get_choice_text(c)))
scored_completions = sorted(scored_completions, reverse=True)
completions = [c for _, c in scored_completions]
return completions
class DeepSeekModel(dspy.OpenAI):
"""A wrapper class for DeepSeek API, compatible with dspy.OpenAI."""
def __init__(
self,
model: str = "deepseek-chat",
api_key: Optional[str] = None,
api_base: str = "https://api.deepseek.com",
**kwargs
):
super().__init__(model=model, api_key=api_key, api_base=api_base, **kwargs)
self._token_usage_lock = threading.Lock()
self.prompt_tokens = 0
self.completion_tokens = 0
self.model = model
self.api_key = api_key or os.getenv("DEEPSEEK_API_KEY")
self.api_base = api_base
if not self.api_key:
raise ValueError("DeepSeek API key must be provided either as an argument or as an environment variable DEEPSEEK_API_KEY")
def log_usage(self, response):
"""Log the total tokens from the DeepSeek API response."""
usage_data = response.get('usage')
if usage_data:
with self._token_usage_lock:
self.prompt_tokens += usage_data.get('prompt_tokens', 0)
self.completion_tokens += usage_data.get('completion_tokens', 0)
def get_usage_and_reset(self):
"""Get the total tokens used and reset the token usage."""
usage = {
self.model:
{'prompt_tokens': self.prompt_tokens, 'completion_tokens': self.completion_tokens}
}
self.prompt_tokens = 0
self.completion_tokens = 0
return usage
@backoff.on_exception(
backoff.expo,
ERRORS,
max_time=1000,
on_backoff=backoff_hdlr,
giveup=giveup_hdlr,
)
def _create_completion(self, prompt: str, **kwargs):
"""Create a completion using the DeepSeek API."""
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.api_key}"
}
data = {
"model": self.model,
"messages": [{"role": "user", "content": prompt}],
**kwargs
}
response = requests.post(f"{self.api_base}/v1/chat/completions", headers=headers, json=data)
response.raise_for_status()
return response.json()
def __call__(
self,
prompt: str,
only_completed: bool = True,
return_sorted: bool = False,
**kwargs,
) -> list[dict[str, Any]]:
"""Call the DeepSeek API to generate completions."""
assert only_completed, "for now"
assert return_sorted is False, "for now"
response = self._create_completion(prompt, **kwargs)
# Log the token usage from the DeepSeek API response.
self.log_usage(response)
choices = response["choices"]
completions = [choice["message"]["content"] for choice in choices]
history = {
"prompt": prompt,
"response": response,
"kwargs": kwargs,
}
self.history.append(history)
return completions
class AzureOpenAIModel(dspy.AzureOpenAI):
"""A wrapper class for dspy.AzureOpenAI."""
def __init__(
self,
api_base: Optional[str] = None,
api_version: Optional[str] = None,
model: str = "gpt-3.5-turbo-instruct",
api_key: Optional[str] = None,
model_type: Literal["chat", "text"] = "chat",
**kwargs,
):
super().__init__(
api_base=api_base, api_version=api_version, model=model, api_key=api_key, model_type=model_type, **kwargs)
self._token_usage_lock = threading.Lock()
self.prompt_tokens = 0
self.completion_tokens = 0
def log_usage(self, response):
"""Log the total tokens from the OpenAI API response.
Override log_usage() in dspy.AzureOpenAI for tracking accumulated token usage."""
usage_data = response.get('usage')
if usage_data:
with self._token_usage_lock:
self.prompt_tokens += usage_data.get('prompt_tokens', 0)
self.completion_tokens += usage_data.get('completion_tokens', 0)
def get_usage_and_reset(self):
"""Get the total tokens used and reset the token usage."""
usage = {
self.kwargs.get('model') or self.kwargs.get('engine'):
{'prompt_tokens': self.prompt_tokens, 'completion_tokens': self.completion_tokens}
}
self.prompt_tokens = 0
self.completion_tokens = 0
return usage
class ClaudeModel(dspy.dsp.modules.lm.LM):
"""Copied from dspy/dsp/modules/anthropic.py with the addition of tracking token usage."""
def __init__(
self,
model: str,
api_key: Optional[str] = None,
api_base: Optional[str] = None,
**kwargs,
):
super().__init__(model)
try:
from anthropic import Anthropic
except ImportError as err:
raise ImportError("Claude requires `pip install anthropic`.") from err
self.provider = "anthropic"
self.api_key = api_key = os.environ.get("ANTHROPIC_API_KEY") if api_key is None else api_key
self.api_base = "https://api.anthropic.com/v1/messages" if api_base is None else api_base
self.kwargs = {"temperature": kwargs.get("temperature", 0.0),
"max_tokens": min(kwargs.get("max_tokens", 4096), 4096), "top_p": kwargs.get("top_p", 1.0),
"top_k": kwargs.get("top_k", 1), "n": kwargs.pop("n", kwargs.pop("num_generations", 1)),
**kwargs, "model": model}
self.history: list[dict[str, Any]] = []
self.client = Anthropic(api_key=api_key)
self.model = model
self._token_usage_lock = threading.Lock()
self.prompt_tokens = 0
self.completion_tokens = 0
def log_usage(self, response):
"""Log the total tokens from the Anthropic API response."""
usage_data = response.usage
if usage_data:
with self._token_usage_lock:
self.prompt_tokens += usage_data.input_tokens
self.completion_tokens += usage_data.output_tokens
def get_usage_and_reset(self):
"""Get the total tokens used and reset the token usage."""
usage = {
self.model:
{'prompt_tokens': self.prompt_tokens, 'completion_tokens': self.completion_tokens}
}
self.prompt_tokens = 0
self.completion_tokens = 0
return usage
def basic_request(self, prompt: str, **kwargs):
raw_kwargs = kwargs
kwargs = {**self.kwargs, **kwargs}
# caching mechanism requires hashable kwargs
kwargs["messages"] = [{"role": "user", "content": prompt}]
kwargs.pop("n")
response = self.client.messages.create(**kwargs)
# history = {
# "prompt": prompt,
# "response": response,
# "kwargs": kwargs,
# "raw_kwargs": raw_kwargs,
# }
json_serializable_history = {
"prompt": prompt,
"response": {
"content": response.content[0].text,
"model": response.model,
"role": response.role,
"stop_reason": response.stop_reason,
"stop_sequence": response.stop_sequence,
"type": response.type,
"usage": {
"input_tokens": response.usage.input_tokens,
"output_tokens": response.usage.output_tokens,
}
},
"kwargs": kwargs,
"raw_kwargs": raw_kwargs,
}
self.history.append(json_serializable_history)
return response
@backoff.on_exception(
backoff.expo,
(RateLimitError,),
max_time=1000,
max_tries=8,
on_backoff=backoff_hdlr,
giveup=giveup_hdlr,
)
def request(self, prompt: str, **kwargs):
"""Handles retrieval of completions from Anthropic whilst handling API errors."""
return self.basic_request(prompt, **kwargs)
def __call__(self, prompt, only_completed=True, return_sorted=False, **kwargs):
"""Retrieves completions from Anthropic.
Args:
prompt (str): prompt to send to Anthropic
only_completed (bool, optional): return only completed responses and ignores completion due to length. Defaults to True.
return_sorted (bool, optional): sort the completion choices using the returned probabilities. Defaults to False.
Returns:
list[str]: list of completion choices
"""
assert only_completed, "for now"
assert return_sorted is False, "for now"
# per eg here: https://docs.anthropic.com/claude/reference/messages-examples
# max tokens can be used as a proxy to return smaller responses
# so this cannot be a proper indicator for incomplete response unless it isnt the user-intent.
n = kwargs.pop("n", 1)
completions = []
for _ in range(n):
response = self.request(prompt, **kwargs)
self.log_usage(response)
# This is the original behavior in dspy/dsp/modules/anthropic.py.
# Comment it out because it can cause "IndexError: list index out of range" silently
# which is not transparent to developers.
# if only_completed and response.stop_reason == "max_tokens":
# continue
completions = [c.text for c in response.content]
return completions
class VLLMClient(dspy.HFClientVLLM):
"""A wrapper class for dspy.HFClientVLLM."""
def __init__(self, model, port, url="http://localhost", **kwargs):
"""Copied from dspy/dsp/modules/hf_client.py with the addition of storing additional kwargs."""
super().__init__(model=model, port=port, url=url, **kwargs)
# Store additional kwargs for the generate method.
self.kwargs = {**self.kwargs, **kwargs}
def _generate(self, prompt, **kwargs):
"""Copied from dspy/dsp/modules/hf_client.py with the addition of passing kwargs to VLLM server."""
kwargs = {**self.kwargs, **kwargs}
# payload = {
# "model": kwargs["model"],
# "prompt": prompt,
# "max_tokens": kwargs["max_tokens"],
# "temperature": kwargs["temperature"],
# }
payload = {
"prompt": prompt,
**kwargs
}
response = send_hfvllm_request_v00(
f"{self.url}/v1/completions",
json=payload,
headers=self.headers,
)
try:
json_response = response.json()
completions = json_response["choices"]
response = {
"prompt": prompt,
"choices": [{"text": c["text"]} for c in completions],
}
return response
except Exception as e:
print("Failed to parse JSON response:", response.text)
raise Exception("Received invalid JSON response from server")
class OllamaClient(dspy.OllamaLocal):
"""A wrapper class for dspy.OllamaClient."""
def __init__(self, model, port, url="http://localhost", **kwargs):
"""Copied from dspy/dsp/modules/hf_client.py with the addition of storing additional kwargs."""
# Check if the URL has 'http://' or 'https://'
if not url.startswith("http://") and not url.startswith("https://"):
url = "http://" + url
super().__init__(model=model, base_url=f"{url}:{port}", **kwargs)
# Store additional kwargs for the generate method.
self.kwargs = {**self.kwargs, **kwargs}
class TGIClient(dspy.HFClientTGI):
def __init__(self, model, port, url, http_request_kwargs=None, **kwargs):
super().__init__(model=model, port=port, url=url, http_request_kwargs=http_request_kwargs, **kwargs)
def _generate(self, prompt, **kwargs):
"""Copied from dspy/dsp/modules/hf_client.py with the addition of removing hard-coded parameters."""
kwargs = {**self.kwargs, **kwargs}
payload = {
"inputs": prompt,
"parameters": {
"do_sample": kwargs["n"] > 1,
"best_of": kwargs["n"],
"details": kwargs["n"] > 1,
**kwargs,
},
}
payload["parameters"] = openai_to_hf(**payload["parameters"])
# Comment out the following lines to remove the hard-coded parameters.
# payload["parameters"]["temperature"] = max(
# 0.1, payload["parameters"]["temperature"],
# )
response = send_hftgi_request_v01_wrapped(
f"{self.url}:{random.Random().choice(self.ports)}" + "/generate",
url=self.url,
ports=tuple(self.ports),
json=payload,
headers=self.headers,
**self.http_request_kwargs,
)
try:
json_response = response.json()
# completions = json_response["generated_text"]
completions = [json_response["generated_text"]]
if (
"details" in json_response
and "best_of_sequences" in json_response["details"]
):
completions += [
x["generated_text"]
for x in json_response["details"]["best_of_sequences"]
]
response = {"prompt": prompt, "choices": [{"text": c} for c in completions]}
return response
except Exception:
print("Failed to parse JSON response:", response.text)
raise Exception("Received invalid JSON response from server")
class TogetherClient(dspy.HFModel):
"""A wrapper class for dspy.Together."""
def __init__(self, model, apply_tokenizer_chat_template=False, hf_tokenizer_name=None, **kwargs):
"""Copied from dspy/dsp/modules/hf_client.py with the support of applying tokenizer chat template."""
super().__init__(model=model, is_client=True)
self.session = requests.Session()
self.api_base = "https://api.together.xyz/v1/completions" if os.getenv(
"TOGETHER_API_BASE") is None else os.getenv("TOGETHER_API_BASE")
self.token = os.getenv("TOGETHER_API_KEY")
self.model = model
# self.use_inst_template = False
# if any(keyword in self.model.lower() for keyword in ["inst", "instruct"]):
# self.use_inst_template = True
self.apply_tokenizer_chat_template = apply_tokenizer_chat_template
if self.apply_tokenizer_chat_template:
logging.info("Loading huggingface tokenizer.")
if hf_tokenizer_name is None:
hf_tokenizer_name = self.model
self.tokenizer = AutoTokenizer.from_pretrained(hf_tokenizer_name, cache_dir=kwargs.get("cache_dir", None))
stop_default = "\n\n---"
self.kwargs = {
"temperature": 0.0,
"max_tokens": 512,
"top_p": 1,
"top_k": 20,
"repetition_penalty": 1,
"n": 1,
"stop": stop_default if "stop" not in kwargs else kwargs["stop"],
**kwargs,
}
self._token_usage_lock = threading.Lock()
self.prompt_tokens = 0
self.completion_tokens = 0
def log_usage(self, response):
"""Log the total tokens from the OpenAI API response."""
usage_data = response.get('usage')
if usage_data:
with self._token_usage_lock:
self.prompt_tokens += usage_data.get('prompt_tokens', 0)
self.completion_tokens += usage_data.get('completion_tokens', 0)
def get_usage_and_reset(self):
"""Get the total tokens used and reset the token usage."""
usage = {
self.model:
{'prompt_tokens': self.prompt_tokens, 'completion_tokens': self.completion_tokens}
}
self.prompt_tokens = 0
self.completion_tokens = 0
return usage
@backoff.on_exception(
backoff.expo,
ERRORS,
max_time=1000,
on_backoff=backoff_hdlr,
)
def _generate(self, prompt, use_chat_api=False, **kwargs):
url = f"{self.api_base}"
kwargs = {**self.kwargs, **kwargs}
stop = kwargs.get("stop")
temperature = kwargs.get("temperature")
max_tokens = kwargs.get("max_tokens", 150)
top_p = kwargs.get("top_p", 0.7)
top_k = kwargs.get("top_k", 50)
repetition_penalty = kwargs.get("repetition_penalty", 1)
if self.apply_tokenizer_chat_template:
prompt = self.tokenizer.apply_chat_template([{"role": "user", "content": prompt}], tokenize=False)
# prompt = f"[INST]{prompt}[/INST]" if self.use_inst_template else prompt
if use_chat_api:
url = f"{self.api_base}/chat/completions"
messages = [
{"role": "system",
"content": "You are a helpful assistant. You must continue the user text directly without *any* additional interjections."},
{"role": "user", "content": prompt},
]
body = {
"model": self.model,
"messages": messages,
"temperature": temperature,
"max_tokens": max_tokens,
"top_p": top_p,
"top_k": top_k,
"repetition_penalty": repetition_penalty,
"stop": stop,
}
else:
body = {
"model": self.model,
"prompt": prompt,
"temperature": temperature,
"max_tokens": max_tokens,
"top_p": top_p,
"top_k": top_k,
"repetition_penalty": repetition_penalty,
"stop": stop,
}
headers = {"Authorization": f"Bearer {self.token}"}
with self.session.post(url, headers=headers, json=body) as resp:
resp_json = resp.json()
# Log the token usage from the Together API response.
self.log_usage(resp_json)
if use_chat_api:
# completions = [resp_json['output'].get('choices', [])[0].get('message', {}).get('content', "")]
completions = [resp_json.get('choices', [])[0].get('message', {}).get('content', "")]
else:
# completions = [resp_json['output'].get('choices', [])[0].get('text', "")]
completions = [resp_json.get('choices', [])[0].get('text', "")]
response = {"prompt": prompt, "choices": [{"text": c} for c in completions]}
return response