-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathevaluate.py
More file actions
302 lines (268 loc) · 11.2 KB
/
evaluate.py
File metadata and controls
302 lines (268 loc) · 11.2 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
#!/usr/bin/env python3
import os
import sys
import click
import inspect
import json
from pathlib import Path
from src import utils
from src import local_inference
from src.default_click_type import (
DefaultBaseUrlPromptOptions,
DefaultModelPathPromptOptions,
DefaultResetPromptOptions,
DefaultSamplePromptOptions,
DefaultDebugPromptOptions,
DefaultGPidPromptOptions,
DefaultGLocPromptOptions,
DefaultApiKeyPromptOptions,
DefaultToolParserPromptOptions,
DefaultServedModelNamePromptOptions,
DefaultServingWaitTimeoutPromptOptions,
DefaultBatchPromptOptions,
DefaultOnlyExactPromptOptions
)
from src.payload_creator import PayloadCreatorFactory
from src.response_handler import ResponseHandler
from src.evaluation_handler import EvaluationHandler
from src.constants import DEFAULT_TEMPERATURE, LOCALHOST_BASE_URL, EXIT_SUCCESS, EXIT_FAILURE
REPO_PATH = os.path.dirname(os.path.abspath(__file__))
# program options
@click.group()
@click.option("-q", help="disable all prompts", flag_value=True, default=True)
@click.pass_context
def cli(ctx, q):
ctx.ensure_object(dict)
ctx.obj['q'] = q
def default_eval_options(f):
f = click.option('--model', prompt='model name', help='gpt-3.5-turbo, gpt-4 ..etc')(f)
f = click.option('--input_path', prompt='input file path', help='golden set file name (*.jsonl)')(f)
# test option
f = click.option('--reset', prompt='recreate request file', help='reset request file', cls=DefaultResetPromptOptions)(f)
f = click.option('--sample', prompt='Run only 1 case.', help='run sample', cls=DefaultSamplePromptOptions)(f)
f = click.option('--debug', prompt='debug flag', help='debugging', cls=DefaultDebugPromptOptions)(f)
f = click.option('--only_exact', prompt='evaluate exact match', help='only exact match(True, False)', cls=DefaultOnlyExactPromptOptions)(f)
f = click.option('--is_batch', prompt='batch processing', help='batch processing(True, False)', cls=DefaultBatchPromptOptions)(f)
# openai type
f = click.option('--api_key', prompt='model api key', help='api key', cls=DefaultApiKeyPromptOptions)(f)
f = click.option('--temperature', prompt='temperature', help='generate temperature', default=DEFAULT_TEMPERATURE)(f)
# openai - hosting server type
f = click.option('--base_url', prompt='model api url', help='base url', cls=DefaultBaseUrlPromptOptions)(f)
f = click.option('--served_model_name', prompt='served model name', help='gpt-3.5-turbo, gpt-4 ..etc', cls=DefaultServedModelNamePromptOptions)(f)
# gemini
f = click.option('--gcloud_project_id', prompt='gemini project id', help='google pid', cls=DefaultGPidPromptOptions)(f)
f = click.option('--gcloud_location', prompt='gemini location', help='google cloud location', cls=DefaultGLocPromptOptions)(f)
# for local inference
f = click.option('--model_path', prompt='inhouse model path', help='model path in header', cls=DefaultModelPathPromptOptions)(f)
f = click.option('--serving_wait_timeout', prompt='serving_wait_timeout', help='--serving-wait-timeout', cls=DefaultServingWaitTimeoutPromptOptions)(f)
f = click.option('--tool_parser', prompt='tool_parser', help='--tool-call-parser (like functionary_llama_v3)', cls=DefaultToolParserPromptOptions)(f)
return f
def dialog_eval_options(f):
f = click.option('--system_prompt_path', prompt='system_prompt_path', help='system prompt file path')(f)
return f
def singlecall_eval_options(f):
f = click.option('--system_prompt_path', prompt='system_prompt_path', help='system prompt file path')(f)
f = click.option('--tools_type', prompt='tools type', help='tools_type = {exact, 4_random, 4_close, 8_random, 8_close}')(f)
return f
def get_file_paths(test_prefix, model_name, tools_type=None):
output_path = f'{REPO_PATH}/output/'
utils.create_directory(output_path)
# 모델별 디렉토리 생성
model_output_path = f'{output_path}/{model_name}'
utils.create_directory(model_output_path)
print(f"output_path: {model_output_path}")
base_path = f'{model_output_path}/{test_prefix}'
if tools_type:
return {
"request": f"{base_path}.input.jsonl",
"predict": f"{base_path}.{model_name}.{tools_type}.output.jsonl",
"eval": f"{base_path}.{model_name}.{tools_type}.eval.jsonl",
"eval_log": f"{base_path}.{model_name}.{tools_type}.eval_report.tsv",
}
else:
return {
"request": f"{base_path}.input.jsonl",
"predict": f"{base_path}.{model_name}.output.jsonl",
"eval": f"{base_path}.{model_name}.eval.jsonl",
"eval_log": f"{base_path}.{model_name}.eval_report.tsv",
}
def get_eval_subtype(eval_type, input_path):
import os
from src.constants import SINGLECALL, DIALOG, COMMON
if eval_type == SINGLECALL:
return eval_type
elif eval_type == DIALOG:
return eval_type
elif eval_type == COMMON:
filename = os.path.basename(input_path)
# ex : FunctionChat-CallDecision.jsonl -> CallDecision
subtype = filename.split('FunctionChat-')[-1].split('.jsonl')[0]
return subtype
else:
return eval_type
def run_evaluate(
eval_type,
test_prefix,
model,
input_path,
temperature, api_key,
# inhouse
base_url, served_model_name,
# inhouse-local
model_path, tool_parser, serving_wait_timeout,
reset, sample, debug, only_exact,
gcloud_project_id, gcloud_location,
system_prompt_path=None, # common일때 불필요
tools_type=None, # singlecall 일때만 필요
is_batch=True, # batch processing 옵션
):
eval_subtype = get_eval_subtype(eval_type, input_path)
model_name = None
if model == 'inhouse-local':
model_name = Path(model_path).name
elif model == 'inhouse':
model_name = served_model_name
else: # gpt, mistral, etc.
model_name = model
file_paths = get_file_paths(test_prefix, model_name)
print(f"[[{model_name} {test_prefix} evaluate start]]")
process_meta = None
try:
# 파일이 없거나, predict 라인수가 requests 보다 작을때만 모델을 띄우게
if not utils.compare_file_line_counts(file_paths['request'], file_paths['predict']):
if model == 'inhouse-local':
if Path(model_path).exists():
process_meta = local_inference.initialize_vllm(
model_path,
model_name,
tool_parser,
serving_wait_timeout
)
api_key = model
base_url = LOCALHOST_BASE_URL
model_path = model_name # vllm_fc 에서 request.model 에 model_name 만 들어감
else:
raise Exception("Invalid model_path")
api_request_list = PayloadCreatorFactory.get_payload_creator(
eval_type, temperature,
system_prompt_path # option arguments
).create_payload(
input_file_path=input_path,
request_file_path=file_paths['request'],
reset=reset,
tools_type=tools_type # option arguments
)
api_response_list = ResponseHandler(
model, api_key, base_url, model_name,
gcloud_project_id, gcloud_location
).fetch_and_save(
api_request_list, file_paths['predict'], reset, sample, debug
)
if process_meta is not None:
local_inference.kill_vllm(process_meta)
# Get LLM judge name from config
cfg = json.loads(open(f'{REPO_PATH}/config/openai.cfg', 'r').read())
llm_judge_name = cfg.get('api_version', 'unknown')
EvaluationHandler(eval_type).evaluate(
api_request_list, api_response_list,
file_paths['eval'], file_paths['eval_log'],
reset, sample, debug, only_exact,
model_name=model_name,
llm_judge_name=llm_judge_name,
model_path=model_path if model == 'inhouse-local' else model_name,
is_batch=is_batch,
eval_subtype=eval_subtype
)
except KeyboardInterrupt:
print("Ctrl+C detected. Terminating the process.")
if process_meta is not None:
local_inference.kill_vllm(process_meta)
sys.exit(EXIT_SUCCESS)
except Exception as e:
import traceback
traceback.print_exc()
if process_meta is not None:
local_inference.kill_vllm(process_meta)
sys.exit(EXIT_FAILURE)
# program command
@cli.command()
@default_eval_options
@dialog_eval_options
def dialog(model,
input_path, system_prompt_path,
temperature, api_key,
# inhouse
base_url, served_model_name,
# inhouse-local
model_path, tool_parser, serving_wait_timeout,
reset, sample, debug, only_exact,
gcloud_project_id, gcloud_location,
is_batch):
eval_type = inspect.stack()[0][3]
run_evaluate(
eval_type, f'FunctionChat-{eval_type.capitalize()}',
model,
input_path,
temperature, api_key,
base_url, served_model_name,
model_path, tool_parser, serving_wait_timeout,
reset, sample, debug, only_exact,
gcloud_project_id, gcloud_location,
system_prompt_path=system_prompt_path,
is_batch=is_batch
)
@cli.command()
@default_eval_options
@singlecall_eval_options
def singlecall(model, input_path, system_prompt_path,
temperature, api_key,
# inhouse
base_url, served_model_name,
# inhouse-local
model_path, tool_parser, serving_wait_timeout,
reset, sample, debug, only_exact,
gcloud_project_id, gcloud_location,
tools_type,
is_batch):
eval_type = inspect.stack()[0][3]
run_evaluate(
eval_type, f'FunctionChat-{eval_type.capitalize()}',
model,
input_path,
temperature, api_key,
base_url, served_model_name,
model_path, tool_parser, serving_wait_timeout,
reset, sample, debug, only_exact,
gcloud_project_id, gcloud_location,
system_prompt_path=system_prompt_path,
tools_type=tools_type,
is_batch=is_batch
)
@cli.command()
@default_eval_options
def common(model, input_path,
# common
temperature, api_key,
# inhouse
base_url, served_model_name,
# inhouse-local
model_path, tool_parser, serving_wait_timeout,
# eval option
reset, sample, debug, only_exact,
# gemini option
gcloud_project_id, gcloud_location,
is_batch):
eval_type = inspect.stack()[0][3]
run_evaluate(
eval_type, os.path.splitext(os.path.basename(input_path))[0],
model,
input_path,
temperature, api_key,
base_url, served_model_name,
model_path, tool_parser, serving_wait_timeout,
reset, sample, debug, only_exact,
gcloud_project_id, gcloud_location,
is_batch=is_batch
)
if __name__ == '__main__':
cli()