forked from defog-ai/sql-eval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_runner.py
353 lines (317 loc) · 11.5 KB
/
api_runner.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
import json
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Optional
from eval.eval import compare_query_results
import pandas as pd
from utils.gen_prompt import generate_prompt
from utils.questions import prepare_questions_df
from utils.creds import db_creds_all
from tqdm import tqdm
from time import time
import requests
from utils.reporting import upload_results
import sqlparse
def clean_generated_query(query: str):
"""
Clean up the generated query by
- formatting the query using sqlparse
- fixing common problems in LLM-powered query generation with post-processing heuristics
KNOWN ISSUES: the division fix will only work with Postgres/Redshift/Snowflake/Databricks. It might not work with other databases.
"""
query = sqlparse.format(query, reindent_aligned=True)
# if the string `< =` is present, replace it with `<=`. Similarly for `> =` and `>=`
query = query.replace("< =", "<=").replace("> =", ">=")
# if the string ` / NULLIF (` is present, replace it with `/ NULLIF ( 1.0 * `.
# This is a fix for ensuring that the denominator is always a float in division operations.
query = query.replace("/ NULLIF (", "/ NULLIF (1.0 * ")
return query
def mk_vllm_json(
prompt, num_beams, logprobs=False, sql_lora_path=None, sql_lora_name=None
):
payload = {
"prompt": prompt,
"n": 1,
"use_beam_search": num_beams > 1,
"best_of": num_beams,
"temperature": 0,
"stop": [";", "```"],
"max_tokens": 4000,
"seed": 42,
"sql_lora_path": sql_lora_path,
"sql_lora_name": sql_lora_name,
}
if logprobs:
payload["logprobs"] = 2
return payload
def mk_tgi_json(prompt, num_beams):
# see swagger docs for /generate for the full list of parameters:
# https://huggingface.github.io/text-generation-inference/#/Text%20Generation%20Inference/generate
return {
"inputs": prompt,
"parameters": {
"best_of": num_beams,
"do_sample": num_beams > 1,
"return_full_text": False,
"max_new_tokens": 1024,
},
}
def process_row(
row,
api_url: str,
api_type: str,
num_beams: int,
decimal_points: int,
logprobs: bool = False,
sql_lora_path: Optional[str] = None,
sql_lora_name: Optional[str] = None,
):
start_time = time()
if api_type == "tgi":
json_data = mk_tgi_json(row["prompt"], num_beams)
elif api_type == "vllm":
json_data = mk_vllm_json(
row["prompt"], num_beams, logprobs, sql_lora_path, sql_lora_name
)
else:
# add any custom JSON data here, e.g. for a custom API
json_data = {
"prompt": row["prompt"],
"n": 1,
"use_beam_search": num_beams > 1,
"best_of": num_beams,
"temperature": 0,
"stop": [";", "```"],
"max_tokens": 4000,
}
try:
r = requests.post(
api_url,
json=json_data,
timeout=200,
)
except:
row["generated_query"] = ""
row["exact_match"] = 0
row["correct"] = 0
row["error_db_exec"] = 1
row["error_msg"] = "API TIMEOUT"
row["tokens_used"] = None
if logprobs:
row["logprobs"] = []
return row
end_time = time()
logprobs = []
if api_type == "tgi":
# we do not return the original prompt in tgi
try:
generated_query = r.json()["generated_text"]
except KeyError:
print(r.json())
generated_query = ""
elif "[SQL]" not in row["prompt"]:
generated_query = (
r.json()["text"][0]
.split("```sql")[-1]
.split("```")[0]
.split(";")[0]
.strip()
+ ";"
)
else:
generated_query = r.json()["text"][0]
if "[SQL]" in generated_query:
generated_query = generated_query.split("[SQL]", 1)[1].strip()
else:
generated_query = generated_query.strip()
# clean up the generated query
generated_query = clean_generated_query(generated_query)
# remove extra spaces around brackets especially for MySQL
generated_query = generated_query.replace(" ( ", "(").replace(" )", ")")
if "logprobs" in r.json():
logprobs = r.json()["logprobs"]
row["generated_query"] = generated_query
logprobs_display = []
for item in logprobs:
probs = list(item.values())
probs_to_append = {}
for prob in probs:
rank = prob["rank"]
logprob = prob["logprob"]
token = prob["decoded_token"]
probs_to_append.update(
{
f"rank_{rank}_token": token,
f"rank_{rank}_logprob": logprob,
f"rank_{rank}_prob": 10**logprob,
}
)
probs_to_append["prob_diff"] = (
probs_to_append["rank_1_prob"] - probs_to_append["rank_2_prob"]
)
logprobs_display.append(probs_to_append)
row["logprobs"] = logprobs_display
row["latency_seconds"] = end_time - start_time
row["tokens_used"] = None
golden_query = row["query"]
db_name = row["db_name"]
db_type = row["db_type"]
question = row["question"]
query_category = row["query_category"]
table_metadata_string = row["table_metadata_string"]
exact_match = correct = 0
try:
exact_match, correct = compare_query_results(
query_gold=golden_query,
query_gen=generated_query,
db_name=db_name,
db_type=db_type,
db_creds=db_creds_all.get(row["db_type"], {}),
question=question,
query_category=query_category,
table_metadata_string=table_metadata_string,
decimal_points=decimal_points,
)
row["exact_match"] = int(exact_match)
row["correct"] = int(correct)
row["error_msg"] = ""
except Exception as e:
row["error_db_exec"] = 1
row["error_msg"] = f"QUERY EXECUTION ERROR: {e}"
return row
def run_api_eval(args):
# get params from args
questions_file_list = args.questions_file
prompt_file_list = args.prompt_file
num_questions = args.num_questions
public_data = not args.use_private_data
api_url = args.api_url
api_type = args.api_type
output_file_list = args.output_file
k_shot = args.k_shot
num_beams = args.num_beams
max_workers = args.parallel_threads
db_type = args.db_type
decimal_points = args.decimal_points
logprobs = args.logprobs
cot_table_alias = args.cot_table_alias
sql_lora_path = args.adapter if args.adapter else None
sql_lora_name = args.adapter_name if args.adapter_name else None
run_name = args.run_name if args.run_name else None
if sql_lora_path:
print("Using LoRA adapter at:", sql_lora_path)
if logprobs:
# check that the eval-visualizer/public directory exists
if not os.path.exists("./eval-visualizer"):
# thorow error
raise Exception(
"The eval-visualizer directory does not exist. Please clone it with `git clone https://github.com/defog-ai/eval-visualizer/` before running sql-eval with the --logprobs flag."
)
if not os.path.exists("./eval-visualizer/public"):
os.makedirs("./eval-visualizer/public")
for questions_file, prompt_file, output_file in zip(
questions_file_list, prompt_file_list, output_file_list
):
print(f"Using prompt file {prompt_file}")
# get questions
print("Preparing questions...")
print(
f"Using {'all' if num_questions is None else num_questions} question(s) from {questions_file}"
)
df = prepare_questions_df(
questions_file, db_type, num_questions, k_shot, cot_table_alias
)
# create a prompt for each question
df["prompt"] = df.apply(
lambda row: generate_prompt(
prompt_file,
row["question"],
row["db_name"],
row["db_type"],
row["instructions"],
row["k_shot_prompt"],
row["glossary"],
row["table_metadata_string"],
row["prev_invalid_sql"],
row["prev_error_msg"],
row["question_0"],
row["query_0"],
row["question_1"],
row["query_1"],
row["cot_instructions"],
row["cot_pregen"],
public_data,
args.num_columns,
args.shuffle_metadata,
row["table_aliases"],
),
axis=1,
)
total_tried = 0
total_correct = 0
output_rows = []
with ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = []
for row in df.to_dict("records"):
futures.append(
executor.submit(
process_row,
row,
api_url,
api_type,
num_beams,
decimal_points,
logprobs,
sql_lora_path,
sql_lora_name,
)
)
with tqdm(as_completed(futures), total=len(futures)) as pbar:
for f in pbar:
row = f.result()
output_rows.append(row)
if row["correct"]:
total_correct += 1
total_tried += 1
pbar.update(1)
pbar.set_description(
f"Correct so far: {total_correct}/{total_tried} ({100*total_correct/total_tried:.2f}%)"
)
output_df = pd.DataFrame(output_rows)
print(output_df.groupby("query_category")[["correct", "error_db_exec"]].mean())
output_df = output_df.sort_values(by=["db_name", "query_category", "question"])
# get directory of output_file and create if not exist
output_dir = os.path.dirname(output_file)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
results = output_df.to_dict("records")
if logprobs:
print(
f"Writing logprobs to JSON file at eval-visualizer/public/{output_file.split('/')[-1].replace('.csv', '.json')}"
)
with open(
f"./eval-visualizer/public/{output_file.split('/')[-1].replace('.csv', '.json')}",
"w",
) as f:
json.dump(results, f)
del output_df["prompt"]
try:
output_df.to_csv(output_file, index=False, float_format="%.2f")
except:
output_df.to_pickle(output_file)
# upload results
# with open(prompt_file, "r") as f:
# prompt = f.read()
if args.run_name is None:
run_name = output_file.split("/")[-1].replace(".csv", "")
print(
"Run name not provided. Using a output filename for run name:", run_name
)
if args.upload_url is not None:
upload_results(
results=results,
url=args.upload_url,
runner_type="api_runner",
args=args,
run_name=run_name,
)