-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (48 loc) · 2.11 KB
/
Copy pathmain.py
File metadata and controls
59 lines (48 loc) · 2.11 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
import json
import yaml
import os
import pickle
import nlparser.natural_language_parser as nlparser
from querygenerator.query_generator import get_database_query
from queryjudge.query_judge import judge_sql_responses
from schemaanalyzer.schema_analyzer import get_analyzed_schema
if __name__ == "__main__":
user_prompt = 'Find all athletes from the United States'
print('User prompt: 🗣️ ', user_prompt)
processed_query = nlparser.get_processed_query(user_prompt, model='phi4-mini:3.8b')
print('Processed query: 🔍 ', processed_query)
db_config = {
'host': 'localhost',
'port': 3306,
'user': 'daver',
'password': 'pizzatime',
'database': 'daver_db'
}
reanalyze_schema = False
if reanalyze_schema:
# If reanalyzing schema, we need to query the database for the schema
analyzed_schema = get_analyzed_schema(db_config=db_config, model='gemma3:4b')
print('Schema analyzed successfully.')
# save the analyzed schema to a file for debugging
with open('analyzed_schema.yaml', 'w') as f:
f.write(analyzed_schema)
else:
# If not reanalyzing, we can load the schema from a file
yaml_schema = yaml.load(open('analyzed_schema.yaml'), Loader=yaml.FullLoader)
analyzed_schema = yaml.dump(yaml_schema)
print('Schema loaded from file.')
n = 10
sql_responses = [get_database_query(processed_query, analyzed_schema, model='deepseek-coder:6.7b') for _ in range(n)]
for i, sql in enumerate(sql_responses):
print(f'Response {i}: \n{sql}')
resp = judge_sql_responses(sql_responses, user_prompt, processed_query, analyzed_schema, model='deepseek-coder:6.7b')
# check if thinking field is present
resp_json = json.loads(resp)
if 'thinking' in resp_json:
print(f'Thinking: {resp_json["thinking"]}')
resp = json.loads(resp)['choice']
print(f'Judged Response: {resp}')
# int from string
idx = int(resp.lower().removeprefix('response '))
print(f'Response {idx}: \n{sql_responses[idx]}')
# print(generated_query)