-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
282 lines (249 loc) · 9.29 KB
/
Copy pathmain.py
File metadata and controls
282 lines (249 loc) · 9.29 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
import argparse
import logging
from pathlib import Path
from dotenv import load_dotenv
from methods.MultiAgentEvaluation.main import MultiAgentEvaluation
from methods.PromptEvaluation.main import PromptEvaluator
from utils.model_config import ModelManager
logging.basicConfig(
level=logging.ERROR, format="%(asctime)s - %(levelname)s - %(message)s"
)
load_dotenv()
def main():
parser = argparse.ArgumentParser(
description="Evaluate datasets using different AI reasoning methods."
)
### Base
parser.add_argument(
"--testing",
choices=["implicit_understanding", "context_understanding"],
required=True,
help="The dataset to use for evaluation.",
)
parser.add_argument(
"--method",
required=True,
choices=[
"input_output",
"chain_of_thought",
"multi_agent_base",
"multi_agent_debate",
"agent_roundtable",
],
help="The evaluation method to use.",
)
### Prompt Evaluation
parser.add_argument(
"--model",
choices=[
"gpt-35-turbo-1106",
"gpt-35-turbo",
"gpt-4",
"gpt-4.1",
"gpt-4o",
"gpt-4o-mini",
"o4-mini",
"o3-mini",
"o3",
"o1",
"llama-8b",
"llama-405b",
"mistral-large",
"mistral-nemo",
"DeepSeek-R1-qcbar",
"DeepSeek-V3-0324",
],
help="The AI model to use for evaluation.",
default="o4-mini",
)
parser.add_argument(
"--temp", type=float, default=0, help="Temperature setting for the model"
)
# Chain of Thought - choosing number of shots
parser.add_argument(
"--cot_shots",
type=int,
default=1,
help="Number of shot prompting using chain of thought method",
)
# Self Consistency settings
parser.add_argument(
"--self_consistency", action="store_true", help="Use self-consistency method."
)
parser.add_argument(
"--loops", type=int, default=3, help="Number of loops for self-consistency."
)
### Multi Agent
# Choose llms for different agents
parser.add_argument(
"--agent_llm",
nargs="+",
type=str,
help="List of LLMs to use for the agents. Provide one model per agent, e.g., gpt-4 gpt-35-turbo gpt-4o.",
)
# Choose temperature for different agents
parser.add_argument(
"--agent_llm_temp",
nargs="+",
type=float,
help="Temperature settings for the debator LLMs in agent_debate",
)
parser.add_argument(
"--discussion_loops", type=int, default=1, help="Number of discussion rounds"
)
args = parser.parse_args()
# Set the dataset path based on the selected dataset
if args.testing == "implicit_understanding":
data_path = "data/implicit_understanding/utterance_dataset.json"
# data_path = "data/implicit_understanding/test.json"
dataset_name = "implicit_understanding"
elif args.testing == "context_understanding":
data_path = "data/context_understanding/dataset.json"
# data_path = "data/context_understanding/test.json"
dataset_name = "context_understanding"
method_shortcuts = {
"input_output": "IO",
"chain_of_thought": "CoT",
"multi_agent_base": "MAB",
"multi_agent_debate": "MAD",
"agent_debate": "AD",
"agent_roundtable": "AR",
}
method_shortcut = method_shortcuts.get(args.method)
if not method_shortcut:
raise ValueError(f"Unsupported method: {args.method}")
results_dir = Path("new_results")
reasoning_models = set(["DeepSeek-R1-qcbar", "o3", "o1", "o3-mini", "o4-mini"])
## Start
if args.method in ["input_output", "chain_of_thought"]:
if args.self_consistency:
results_dir = results_dir / "SC"
method_shortcut = f"SC{args.loops}-{method_shortcut}"
else:
results_dir = results_dir / f"{method_shortcut}"
# Include number of shots in the filename if using CoT method
if args.method == "chain_of_thought":
shots_suffix = args.cot_shots
else:
shots_suffix = ""
output_filename = (
f"{method_shortcut}{shots_suffix}_{args.model}_{args.temp}_results.json"
)
output_dir = results_dir / dataset_name
output_path = results_dir / dataset_name / output_filename
output_dir.mkdir(parents=True, exist_ok=True)
# Ensure unique filename by appending _2, _3, etc., if necessary
# file_number = 1
# while os.path.exists(output_path):
# file_number += 1
# output_filename = f"{method_shortcut}{shots_suffix}_{args.model}_{args.temp}_results{file_number}.json"
# output_path = results_dir / dataset_name / output_filename
print(f"Results will be saved to: {output_path}")
# Initialize model manager using create_chat_model
if args.model in [
"gpt-35-turbo-1106",
"gpt-35-turbo",
"gpt-4",
"gpt-4.1",
"gpt-4o",
"gpt-4o-mini",
"o4-mini",
"o3-mini",
"o3",
"o1",
]:
model_type = "openai"
elif args.model in ["llama-8b", "llama-405b", "mistral-large", "mistral-nemo"]:
model_type = "opensource_api"
elif args.model in ["DeepSeek-R1-qcbar", "DeepSeek-V3-0324"]:
model_type = "deepseek"
else:
raise ValueError(f"Model {args.model} is not supported.")
# Create the model using create_chat_model
model_manager = ModelManager(model_type=model_type)
llm = model_manager.create_chat_model(
model_name=args.model, temperature=args.temp
)
# Initialize the evaluator and run the evaluation
evaluator = PromptEvaluator(
llm=llm,
method=args.method,
testing=args.testing,
input_path=data_path,
output_path=output_path,
self_consistency=args.self_consistency,
loops=args.loops,
cot_shots=args.cot_shots,
)
evaluator.run_evaluation()
elif args.method in ["multi_agent_base", "multi_agent_debate", "agent_roundtable"]:
# Handle agent LLMs
if len(args.agent_llm) == 1:
agent_llms = args.agent_llm * 3
elif len(args.agent_llm) == 3:
agent_llms = args.agent_llm
else:
raise ValueError(
"You must provide either 1 or exactly 3 models for the debator agents."
)
# Handle agent temperatures
agent_temps = [None] * 3
for idx, llm in enumerate(agent_llms):
if llm in reasoning_models or args.agent_llm_temp is None:
agent_temps[idx] = None
else:
if len(args.agent_llm_temp) == 1:
agent_temps[idx] = args.agent_llm_temp
elif len(args.agent_llm_temp) == 3:
agent_temps[idx] = args.agent_llm_temp[idx]
else:
raise ValueError(
"You must provide either 1 or exactly 3 temperatures for the debator agents."
)
# Get agents
created_agent_llms = []
for i in range(3):
model_type = (
"openai"
if "gpt" in agent_llms[i]
or agent_llms[i] in ["o3", "o1", "o3-mini", "o4-mini"]
else "opensource_api"
if agent_llms[i]
in ["llama-8b", "llama-405b", "mistral-large", "mistral-nemo"]
else "deepseek"
if agent_llms[i] in ["DeepSeek-R1-qcbar", "DeepSeek-V3-0324"]
else "unknown"
)
model_manager = ModelManager(model_type=model_type)
created_agent_llms.append(
model_manager.create_chat_model(
model_name=agent_llms[i], temperature=agent_temps[i]
)
)
if args.method == "multi_agent_base":
discussion_loops = 1
method_shortcut = "MAB"
elif args.method == "multi_agent_debate":
discussion_loops = args.discussion_loops
method_shortcut = "MAD"
elif args.method == "agent_roundtable":
discussion_loops = args.discussion_loops
method_shortcut = "AR"
# output_filename = f"{method_shortcut}_{'+'.join(agent_llms)}_{(str(agent_temps[0]) + '+') * 2 + str(agent_temps[0])}_results.json"
output_filename = (
f"{method_shortcut}_{str(agent_llms[0])}_{str(agent_temps[0])}_results.json"
)
output_path = results_dir / method_shortcut / dataset_name / output_filename
output_path.parent.mkdir(parents=True, exist_ok=True)
print(f"Results will be saved to: {output_path}")
evaluator = MultiAgentEvaluation(
agent_llms=created_agent_llms,
testing=args.testing,
input_path=data_path,
output_path=output_path,
method=args.method,
discussion_loops=discussion_loops,
)
evaluator.run_evaluation()
if __name__ == "__main__":
main()