Views
No views yet
Important: The existingMyModelclass and its formatting are kept exactly as-is.
This project does not modify, refactor, or reformat the demo code. The README only documents how the current implementation works.
MyModel class structure, method names, and prompt formatting remain unchangedtemperature = 0)1User Question
2 │
3 ▼
4Prompt Builder (System + User)
5 │
6 ▼
7OpenAI ChatCompletion API
8 │
9 ▼
10Generated SQL
11 │
12 ▼
13Evaluation Metrics1.
2├── main.py # Entry point, runs inference and prints metrics
3├── model.py # OpenAI client wrapper (MyModel)
4├── evaluator.py # Evaluation metrics implementation
5├── prompts/
6│ └── text2sql.txt # System prompt with banking rules
7├── README.md
8└── requirements.txtINNER JOIN between system_data and branchsystem_data.data_codesystem_data.yearsystem_data.branch_idbranch.namesystem_data.valueLIKE '%keyword%'metric_code prefix:| Group | Description |
|---|---|
| A | Inbound metrics (MET_A_%) |
| B | Outbound metrics (MET_B_%) |
| C | Stock / snapshot metrics (MET_C_%) |
| D | Exposure / obligation metrics |
| E | Resource mobilization metrics |
| F | Ratio & efficiency metrics |
10001CREATE TABLE entity_a (
2 id INTEGER,
3 group_id INTEGER,
4 org_id INTEGER,
5 code VARCHAR(100),
6 name VARCHAR(255),
7 attr_1 VARCHAR(255),
8 attr_2 VARCHAR(255),
9 attr_3 TEXT
10);
11
12CREATE TABLE entity_b (
13 id INTEGER,
14 group_id INTEGER,
15 entity_a_id INTEGER,
16 time_key INTEGER,
17 metric_name VARCHAR(255),
18 metric_code VARCHAR(100),
19 metric_value REAL,
20 metric_unit VARCHAR(100)
21);| Label | Value |
|---|---|
| rouge | 0.9290708304 |
| meteor | 0.9191570862 |
| binary | 0.55 |
| llm-as-a-judge | 0.65 |
1import argparse
2
3from openai import OpenAI
4
5DEFAULT_QUESTION = """CREATE TABLE entity_a (
6 id INTEGER,
7 group_id INTEGER,
8 org_id INTEGER,
9 code VARCHAR(100),
10 name VARCHAR(255),
11 attr_1 VARCHAR(255),
12 attr_2 VARCHAR(255),
13 attr_3 TEXT
14);
15CREATE TABLE entity_b (
16 id INTEGER,
17 group_id INTEGER,
18 entity_a_id INTEGER,
19 time_key INTEGER,
20 metric_name VARCHAR(255),
21 metric_code VARCHAR(100),
22 metric_value REAL,
23 metric_unit VARCHAR(100)
24);
25ENTITIES = {
26 "metric": {
27 "metric_code": "METRIC_X",
28 "metric_unit": "UNIT_A"
29 },
30 "entity_a_field": {
31 "attr_1": [],
32 "attr_2": [],
33 "attr_3": [],
34 "id": []
35 },
36 "time_key": [year],
37Query:
38}
39
40
41"""
42
43
44class MyModel(object):
45 def __init__(self, model_name: str, api_key: str):
46 self.model_name = model_name
47 self.client = OpenAI(base_url="", api_key=api_key)
48 def get_prompt(
49 self,
50 question: str,
51 ) -> list[dict[str, str]]:
52 return [
53 {
54 "role": "system",
55 "content": """
56You are a problem solving model working on task_description XML block:
57<task_description>You are a specialized Text-to-SQL assistant in the banking domain. Your objective is to translate natural language questions into valid SQLite queries using the provided schema and banking business logic.
58### Input:
59- Schema: Table definitions in SQL DDL format.
60- Relationships: Key linking logic between tables (system_data.branch_id = branch.id).
61- Data Content Context:
62 Indicator_Categories:
63 Group_A:
64 description: Primary metrics – inbound type
65 rule:
66 - metric_code LIKE 'MET_A_%'
67
68 Group_B:
69 description: Primary metrics – outbound type
70 rule:
71 - metric_code LIKE 'MET_B_%'
72
73 Group_C:
74 description: Stock / snapshot metrics
75 rule:
76 - metric_code LIKE 'MET_C_%'
77
78 Group_D:
79 description: Exposure / obligation related metrics
80 rule:
81 - metric_code LIKE 'MET_D_%'
82 - metric_code LIKE 'MET_D_TOTAL_%'
83 - metric_code = 'MET_D_SPECIAL'
84
85 Group_E:
86 description: Resource mobilization metrics
87 rule:
88 - metric_code LIKE 'MET_E_%'
89
90 Group_F:
91 description: Ratio & efficiency indicators
92 rule:
93 - Unit Logic: {Which dmain} data is stored in 'Triệu VND'. If the Question mentions 'Tỷ', multiply the value by 1000.
94- Entities: Extracted key information including data_code, year, and branch filtering criteria.
95### Rules:
961. ALWAYS perform an INNER JOIN between system_data and branch on system_data.branch_id = branch.id.
972. ALWAYS SELECT system_data.data_code, system_data.year, system_data.branch_id, branch.name, system_data.value.
983. Use exact Vietnamese accents for location values.
994. Use LIKE '%keyword%' for text matching.
1005. Use UPPERCASE for SQL keywords.
1016. Output ONLY the SQL query. No explanations or markdown blocks.</task_description>
102You will be given a single task in the question XML block
103Solve only the task in question block.
104Generate only the answer, do not generate anything else
105""",
106 },
107 {
108 "role": "user",
109 "content": f"""
110Now for the real task, solve the task in question block.
111Generate only the solution, do not generate anything else
112<question>{question}</question>
113""",
114 },
115 ]
116 def invoke(self, question: str) -> str:
117 chat_response = self.client.chat.completions.create(
118 model=self.model_name,
119 messages=self.get_prompt(question),
120 temperature=0,
121 reasoning_effort="none",
122 )
123 return chat_response.choices[0].message.content
124
125if __name__ == "__main__":
126 parser = argparse.ArgumentParser()
127 parser.add_argument("--question", type=str, default=DEFAULT_QUESTION, required=False)
128 parser.add_argument("--api-key", type=str, default="", required=False)
129 parser.add_argument("--model", type=str, default="model", required=False)
130 args = parser.parse_args()
131 client = MyModel(model_name=args.model, api_key=args.api_key)
132 print(client.invoke(args.question))1python main.py \
2 --question "<QUESTION_TEXT>" \
3 --api-key "YOUR_OPENAI_API_KEY" \
4 --model "model"temperature = 0 ensures reproducible resultsIN or ranges