-
Notifications
You must be signed in to change notification settings - Fork 4
/
run-openai.py
77 lines (71 loc) · 2.17 KB
/
run-openai.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
import os
import argparse
import typing
import openai
import pfgen
def callback(
tasks: typing.List[typing.Dict[str, str]], params: typing.Dict[str, typing.Any]
) -> typing.Iterator[typing.Optional[str]]:
model = params["model"].split("/")[-1]
mode = params["mode"]
temperature = params["temperature"]
kwargs = {}
kwargs["base_url"] = os.getenv("OPENAI_BASE_URL")
client = openai.OpenAI(
**kwargs
)
for task in tasks:
kwargs = {}
if mode == "chat":
kwargs["messages"] = [
{"role": "system", "content": task["system_prompt"]},
{"role": "user", "content": task["user_prompt"]},
]
elif mode == "qa":
kwargs["messages"] = [{"role": "user", "content": task["prompt"]}]
else:
raise ValueError(f"Unsupported mode: {mode}")
try:
results = client.chat.completions.create(
model=model,
max_tokens=params.get("max_tokens", 500),
temperature=temperature,
stop=params.get("stop", []),
**kwargs,
)
yield results.choices[0].message.content.removeprefix("A:").strip()
except openai.OpenAIError as e:
print(f"API Error: {e}")
yield None
if __name__ == "__main__":
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"--mode",
type=str,
default="qa",
choices=["chat", "qa"],
help="Which chat template to use.",
)
parser.add_argument(
"--model",
type=str,
default="openai/gpt-4o",
help="OpenAI model name.",
)
parser.add_argument(
"--temperature", type=float, default=0.0, help="Temperature for sampling."
)
parser.add_argument(
"--num-trials", type=int, default=10, help="Number of trials to run."
)
args = parser.parse_args()
pfgen.run_tasks(
args.mode,
callback,
engine="openai-api",
model=args.model,
temperature=args.temperature,
num_trials=args.num_trials,
)