-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.py
473 lines (390 loc) · 21.1 KB
/
pipeline.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
from pathlib import Path
import sys
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
import os
import rdflib
from openai import OpenAI
import re
import json
import string
import torch
import time
class DatasetGenerator():
QUESTIONS_ONLY_PATH="01-questions_only"
QUESIONS_WITH_ANSWERS_PATH="02-answered_questions"
QUESIONS_WITH_QUERIES_PATH="03-answers_and_queries"
ENRICHED_WITH_GPT_PATH="04-enriched_with_gpt"
EXECUTED_QUERIES_PATH="05-sparql_queries_executed"
small_models = [
"microsoft/Phi-3-mini-128k-instruct",
"microsoft/Phi-3-medium-4k-instruct",
"openchat/openchat-3.6-8b-20240522",
"google/gemma-7b-it",
"mistralai/Mistral-7B-Instruct-v0.3",
"Qwen/Qwen1.5-7B-Chat",
"Qwen/Qwen2-7B-Instruct",
"occiglot/occiglot-7b-eu5-instruct",
"01-ai/Yi-1.5-9B-Chat-16K",
]
medium_models = [
"01-ai/Yi-1.5-34B-Chat-16K",
"google/gemma-2-27b-it",
"internlm/internlm2_5-20b-chat",
"jpacifico/Chocolatine-14B-Instruct-4k-DPO",
"Azure99/blossom-v5.1-34b",
"mistralai/Mistral-Nemo-Instruct-2407"
]
gpt_models = [
"gpt-4o-2024-05-13",
"gpt-4o-mini-2024-07-18",
"gpt-4-turbo-2024-04-09",
"gpt-3.5-turbo-0125"
]
def __init__(self,
list_of_model_checkpoints,
path_to_ttl,
number_of_questions_per_model = 5,
gpt_versions = None,
feedback_dialog = True,
max_rounds_of_feedback = 3
):
self.model_checkpoints = list_of_model_checkpoints
self.feedback_dialog = feedback_dialog
self.max_rounds_of_feedback = max_rounds_of_feedback
self.graph_path = path_to_ttl
with open(path_to_ttl, "r") as fp:
self.graph_ttl = fp.read()
self.bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="fp4",
bnb_4bit_compute_dtype=torch.float16
)
self.gpt_versions = gpt_versions
self.n_questions = number_of_questions_per_model
self._result_dict = {
"meta": { "models": self.model_checkpoints },
"data": []
}
def __repr__(self):
return f"""DatasetGenerator based on LLMs
----------------------------------------------
Model checkpoints:
{self.model_checkpoints}
----------------------------------------------
Questions per Model:
{self.n_questions}
----------------------------------------------
TTL file:
{self.graph_path}
----------------------------------------------
GPT versions for data enrichment:
{self.gpt_versions}
"""
def run(self):
self.generate_questions()
self.generate_answers()
self.generate_queries()
self.generate_gpt_queries()
self.execute_queries_and_store_results()
def _send_prompt_and_parse_questions(self, m, t, msg, missing_questions):
input_ids = t.apply_chat_template(msg, tokenize=True, add_generation_prompt=True, return_tensors="pt").to('cuda')
generated_ids = m.generate(input_ids, max_new_tokens=1024)
# Cutting off because generated_ids contains the input ids
generated_ids = [ generated_ids[0][len(input_ids[0]):] ]
response = t.batch_decode(generated_ids, skip_special_tokens=True)[0]
questions = response.split("\n")
parsed_questions = []
counter = 0
for q in questions:
counter += 1
if counter > missing_questions:
break
q = q.strip().replace("<|im_end|>", "")
if not q.endswith("?"):
counter -= 1
else:
q = re.sub(r"^[^ ]*[0-9]+\.[^ ]* ", "", q)
parsed_questions.append(q)
print(parsed_questions, response)
return parsed_questions, response
def update_meta(self):
self._result_dict["meta"]["timestamp_unix"] = time.time()
self._result_dict["meta"]["timestamp_pretty"] = time.strftime("%Y-%m-%d %H:%M:%S")
def generate_questions(self):
# .--..--..--..--..--..--..--..--..--..--..--..--..--..--..--..--.
# / .. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \
# \ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/ /
# \/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /
# / /\/ /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /\/ /\
# / /\ \/`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'\ \/\ \
# \ \/\ \ /\ \/ /
# \/ /\ \ / /\/ /
# / /\/ / \ \/ /\
# / /\ \/ Generating natural language \ \/\ \
# \ \/\ \ questions /\ \/ /
# \/ /\ \ / /\/ /
# / /\/ / \ \/ /\
# / /\ \/ \ \/\ \
# \ \/\ \.--..--..--..--..--..--..--..--..--..--..--..--..--./\ \/ /
# \/ /\/ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ /\/ /
# / /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\
# / /\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \
# \ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `' /
# `--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'
print("=================================")
print(" Generating questions")
print("=================================")
Path(DatasetGenerator.QUESTIONS_ONLY_PATH).mkdir(exist_ok=True)
for cp in self.model_checkpoints:
print("===============================================")
print(f" {cp}")
print("===============================================")
prompt = f"""Generate {self.n_questions} questions that fit the following knowledge graph in ttl format:
{self.graph_ttl}
One question per line. No additional line breaks. No enumeration."""
model = AutoModelForCausalLM.from_pretrained(
cp,
device_map="auto",
quantization_config = self.bnb_config,
trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained(cp)
messages = [
{ "role": "user", "content": prompt }
]
questions = []
# Some LLMS (I'm looking at you gemma!) tend to provide less questions
# than we asked for. This is why we have to provide a feedback loop to
# allow those LLMs to fix their mistake
feedback_count = 0
missing_questions = self.n_questions
while len(questions) < self.n_questions:
new_questions, raw_response = self._send_prompt_and_parse_questions(model, tokenizer, messages, missing_questions)
print(new_questions, raw_response)
questions += [ { "question": q, "generated_by": cp, "feedback_count": feedback_count } for q in new_questions ]
missing_questions = self.n_questions - len(questions)
messages.append( { "role": "assistant", "content": raw_response } )
messages.append( {"role": "user", "content": f"Please generate {missing_questions} more questions." } )
feedback_count += 1
if not self.feedback_dialog or feedback_count > self.max_rounds_of_feedback:
break
self._result_dict["data"] += questions
for idx in range(len(self._result_dict["data"])):
self._result_dict["data"][idx]["index"] = idx+1
self.update_meta()
with open(f"{DatasetGenerator.QUESTIONS_ONLY_PATH}/merged.json", "w") as fp:
json.dump(self._result_dict, fp, indent=2)
def generate_answers(self):
# .--..--..--..--..--..--..--..--..--..--..--..--..--..--..--..--.
# / .. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \
# \ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/ /
# \/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /
# / /\/ /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /\/ /\
# / /\ \/`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'\ \/\ \
# \ \/\ \ /\ \/ /
# \/ /\ \ / /\/ /
# / /\/ / \ \/ /\
# / /\ \/ Generating answers via LLMs \ \/\ \
# \ \/\ \ /\ \/ /
# \/ /\ \ / /\/ /
# / /\/ / \ \/ /\
# / /\ \/ \ \/\ \
# \ \/\ \.--..--..--..--..--..--..--..--..--..--..--..--..--./\ \/ /
# \/ /\/ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ /\/ /
# / /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\
# / /\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \
# \ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `' /
# `--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'
print("=================================")
print(" Generating answers")
print("=================================")
Path(DatasetGenerator.QUESIONS_WITH_ANSWERS_PATH).mkdir(exist_ok=True)
self.prompt = string.Template("""You are given the following knowledge graph in ttl format:
${graph_ttl}
${question}
Answer as short as possible. Give only facts, no full sentences.""")
for idx in range(len(self._result_dict["data"])):
if "generated_answers" not in self._result_dict["data"][idx].keys():
self._result_dict["data"][idx]["generated_answers"] = {}
for cp in self.model_checkpoints:
print("===============================================")
print(f" {cp}")
print("===============================================")
model = AutoModelForCausalLM.from_pretrained(cp, device_map="auto", quantization_config = self.bnb_config, trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(cp)
for q in self._result_dict["data"]:
filled_prompt = self.prompt.substitute(graph_ttl=self.graph_ttl, question=q["question"])
messages = [
{ "role": "user", "content": filled_prompt }
]
input_ids = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt").to('cuda')
generated_ids = model.generate(input_ids, max_new_tokens=128)
generated_ids = [ generated_ids[0][len(input_ids[0]):] ]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
q["generated_answers"][cp] = response
self.update_meta()
with open(f"{DatasetGenerator.QUESIONS_WITH_ANSWERS_PATH}/merged.json", "w") as fp:
json.dump(self._result_dict, fp, indent=2)
def generate_queries(self):
# .--..--..--..--..--..--..--..--..--..--..--..--..--..--..--..--.
# / .. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \
# \ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/ /
# \/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /
# / /\/ /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /\/ /\
# / /\ \/`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'\ \/\ \
# \ \/\ \ /\ \/ /
# \/ /\ \ / /\/ /
# / /\/ / \ \/ /\
# / /\ \/ Generating SPARQL queries \ \/\ \
# \ \/\ \ /\ \/ /
# \/ /\ \ / /\/ /
# / /\/ / \ \/ /\
# / /\ \/ \ \/\ \
# \ \/\ \.--..--..--..--..--..--..--..--..--..--..--..--..--./\ \/ /
# \/ /\/ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ /\/ /
# / /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\
# / /\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \
# \ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `' /
# `--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'
print("=================================")
print(" Generating queries")
print("=================================")
Path(DatasetGenerator.QUESIONS_WITH_QUERIES_PATH).mkdir(exist_ok=True)
self.prompt = string.Template("""
You are given the following knowledge graph in ttl format:
${graph_ttl}
Create a SPARQL query to answer the following question: ${question}
Give only the query. Do not generate any other text. Wrap the query in code tags: ```
""")
for idx in range(len(self._result_dict["data"])):
if "generated_queries" not in self._result_dict["data"][idx].keys():
self._result_dict["data"][idx]["generated_queries"] = {}
for cp in self.model_checkpoints:
print("===============================================")
print(f" {cp}")
print("===============================================")
model = AutoModelForCausalLM.from_pretrained(cp, device_map="auto", quantization_config = self.bnb_config, trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(cp, trust_remote_code=True)
for q in self._result_dict["data"]:
filled_prompt = self.prompt.substitute(graph_ttl=self.graph_ttl, question=q["question"])
messages = [
{ "role": "user", "content": filled_prompt }
]
input_ids = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt").to('cuda')
generated_ids = model.generate(input_ids, max_new_tokens=128)
generated_ids = [ generated_ids[0][len(input_ids[0]):] ]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
q["generated_queries"][cp] = response
self.update_meta()
with open(f"{DatasetGenerator.QUESIONS_WITH_QUERIES_PATH}/merged.json", "w") as fp:
json.dump(self._result_dict, fp, indent=2)
def generate_gpt_queries(self):
# .--..--..--..--..--..--..--..--..--..--..--..--..--..--..--..--.
# / .. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \
# \ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/ /
# \/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /
# / /\/ /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /\/ /\
# / /\ \/`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'\ \/\ \
# \ \/\ \ /\ \/ /
# \/ /\ \ / /\/ /
# / /\/ / \ \/ /\
# / /\ \/ Generating reference Queries \ \/\ \
# \ \/\ \ via GPT /\ \/ /
# \/ /\ \ / /\/ /
# / /\/ / \ \/ /\
# / /\ \/ \ \/\ \
# \ \/\ \.--..--..--..--..--..--..--..--..--..--..--..--..--./\ \/ /
# \/ /\/ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ /\/ /
# / /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\
# / /\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \
# \ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `' /
# `--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'
if self.gpt_versions:
print("==========================================")
print(" Generating reference queries via GPT")
print("==========================================")
Path(self.ENRICHED_WITH_GPT_PATH).mkdir(exist_ok=True)
prompt_template = string.Template("""
You are given the following knowledge graph in ttl format:
${graph_ttl}
Create a SPARQL query to answer the following question: ${question}
Give only the query. Do not generate any other text.
""")
cl = OpenAI(
api_key=os.environ["OPENAI_API_KEY"]
)
for entry in self._result_dict["data"]:
question = entry["question"]
prompt = prompt_template.substitute(question=question, graph_ttl=graph_ttl)
for gpt_version in self.gpt_versions:
chat_completion = cl.chat.completions.create(
messages = [
{
"role": "user",
"content": prompt
}
],
model=gpt_version
)
entry["generated_queries"][gpt_version] = chat_completion.choices[0].message.content
self.update_meta()
with open(f"{self.ENRICHED_WITH_GPT_PATH}/merged.json", "w") as fp:
json.dump(self._result_dict, fp, indent=2)
def execute_queries_and_store_results(self):
# .--..--..--..--..--..--..--..--..--..--..--..--..--..--..--..--.
# / .. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \.. \
# \ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/ /
# \/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /
# / /\/ /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /`' /\/ /\
# / /\ \/`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'\ \/\ \
# \ \/\ \ /\ \/ /
# \/ /\ \ / /\/ /
# / /\/ / \ \/ /\
# / /\ \/ Executing queries and saving \ \/\ \
# \ \/\ \ the results /\ \/ /
# \/ /\ \ / /\/ /
# / /\/ / \ \/ /\
# / /\ \/ \ \/\ \
# \ \/\ \.--..--..--..--..--..--..--..--..--..--..--..--..--./\ \/ /
# \/ /\/ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ ../ /\/ /
# / /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\/ /\
# / /\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \
# \ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `'\ `' /
# `--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'`--'
print("==========================================")
print(" Executing queries and saving the results")
print("==========================================")
Path(DatasetGenerator.EXECUTED_QUERIES_PATH).mkdir(exist_ok=True)
g = rdflib.Graph()
g.parse(self.graph_path)
def prep_query(query):
candidate = re.findall(r"```.*```", query, re.DOTALL | re.IGNORECASE)
candidate = " ".join(candidate).replace("`", "").replace("\"", "'").replace("sparql", "").replace("SPARQL", "").replace("sql", "").replace("?", " ?")
return re.sub(r"prefix.*","",candidate, 0, re.IGNORECASE)
for item in self._result_dict["data"]:
question = item["question"]
queries = item["generated_queries"]
if "sparql_result_sets" not in item.keys():
item["sparql_result_sets"] = {}
for k,v in queries.items():
query = prep_query(v)
try:
item["sparql_result_sets"][k] = {
"cleaned_query": query,
"result": list(g.query(query))
}
except Exception as e:
item["sparql_result_sets"][k] = {
"cleaned_query": query,
"result": None,
"error": str(e)
}
self.update_meta()
with open(f"{DatasetGenerator.EXECUTED_QUERIES_PATH}/merged.json", "w") as fp:
json.dump(self._result_dict, fp, indent=2)
if __name__ == "__main__":
graph_ttl = "ttl/org.ttl"
dg = DatasetGenerator(DatasetGenerator.small_models, graph_ttl, number_of_questions_per_model=10)
print(dg)
dg.run()