-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathoai_runner.py
More file actions
83 lines (76 loc) · 2.85 KB
/
Copy pathoai_runner.py
File metadata and controls
83 lines (76 loc) · 2.85 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
import os
from time import sleep
try:
import openai
from openai import OpenAI
except ImportError as e:
pass
from lcb_runner.lm_styles import LMStyle
from lcb_runner.runner.base_runner import BaseRunner
class OpenAIRunner(BaseRunner):
@staticmethod
def _build_client():
forge_api_key = os.getenv("FORGE_API_KEY")
if forge_api_key:
return OpenAI(
api_key=forge_api_key,
base_url=os.getenv("FORGE_API_BASE")
or "https://api.forge.tensorblock.co/v1",
)
return OpenAI(api_key=os.getenv("OPENAI_KEY"))
def __init__(self, args, model):
super().__init__(args, model)
self.client = self._build_client()
if model.model_style == LMStyle.OpenAIReasonPreview:
self.client_kwargs: dict[str | str] = {
"model": args.model,
"max_completion_tokens": 25000,
}
elif model.model_style == LMStyle.OpenAIReason:
assert (
"__" in args.model
), f"Model {args.model} is not a valid OpenAI Reasoning model as we require reasoning effort in model name."
model, reasoning_effort = args.model.split("__")
self.client_kwargs: dict[str | str] = {
"model": model,
"reasoning_effort": reasoning_effort,
}
else:
self.client_kwargs: dict[str | str] = {
"model": args.model,
"temperature": args.temperature,
"max_tokens": args.max_tokens,
"top_p": args.top_p,
"frequency_penalty": 0,
"presence_penalty": 0,
"n": args.n,
"timeout": args.openai_timeout,
# "stop": args.stop, --> stop is only used for base models currently
}
def _run_single(self, prompt: list[dict[str, str]]) -> list[str]:
assert isinstance(prompt, list)
try:
response = self.client.chat.completions.create(
messages=prompt,
**self.client_kwargs,
)
except (
openai.APIError,
openai.RateLimitError,
openai.InternalServerError,
openai.OpenAIError,
openai.APIStatusError,
openai.APITimeoutError,
openai.InternalServerError,
openai.APIConnectionError,
) as e:
print("Exception: ", repr(e))
print("Sleeping for 30 seconds...")
print("Consider reducing the number of parallel processes.")
sleep(30)
return self._run_single(prompt)
except Exception as e:
print(f"Failed to run the model for {prompt}!")
print("Exception: ", repr(e))
raise e
return [c.message.content for c in response.choices]