Spaces:
Sleeping
Sleeping
Delete unused files
Browse files- evaluation_results.json +0 -72
- freeze +0 -0
- src/nl2sql/qwen_engine.py +0 -63
- src/scripts/evaluate.py +0 -69
- src/scripts/evaluate_hf.py +0 -108
- test_bench.py +0 -36
evaluation_results.json
DELETED
|
@@ -1,72 +0,0 @@
|
|
| 1 |
-
[
|
| 2 |
-
{
|
| 3 |
-
"id": 1,
|
| 4 |
-
"status": "ERROR: Execution failed on sql 'SELECT t.Genre, COUNT(t.TrackId) AS TrackCount FROM Track t GROUP BY t.Genre;': no such column: t.Genre"
|
| 5 |
-
},
|
| 6 |
-
{
|
| 7 |
-
"id": 2,
|
| 8 |
-
"status": "PASS",
|
| 9 |
-
"generated": "SELECT t1.Title, ar.Name FROM Album AS t1 INNER JOIN Artist AS ar ON t1.ArtistId = ar.ArtistId;"
|
| 10 |
-
},
|
| 11 |
-
{
|
| 12 |
-
"id": 3,
|
| 13 |
-
"status": "ERROR: Execution failed on sql 'SELECT T4.Country, SUM(T3.UnitPrice * T3.Quantity) as TotalRevenue\nFROM InvoiceLine AS T1\nJOIN Track AS T2 ON T1.TrackId = T2.TrackId\nJOIN Album AS T3 ON T2.AlbumId = T3.AlbumId\nJOIN Genre AS T4 ON T2.GenreId = T4.GenreId\nGROUP BY T4.Country;': no such column: T4.Country"
|
| 14 |
-
},
|
| 15 |
-
{
|
| 16 |
-
"id": 4,
|
| 17 |
-
"status": "ERROR: Execution failed on sql 'SELECT e.FirstName, e.LastName FROM Employee AS e JOIN Customer AS c ON e.SupportRepId = c.CustomerId WHERE c.Company LIKE 'Sales Support';': no such column: e.SupportRepId"
|
| 18 |
-
},
|
| 19 |
-
{
|
| 20 |
-
"id": 5,
|
| 21 |
-
"status": "FAIL",
|
| 22 |
-
"generated": "SELECT c.FirstName, c.LastName, SUM(il.UnitPrice * il.Quantity) as TotalSpent\nFROM Customer c\nJOIN Invoice i ON c.CustomerId = i.CustomerId\nJOIN InvoiceLine il ON i.InvoiceId = il.InvoiceId\nGROUP BY c.CustomerId\nORDER BY TotalSpent DESC\nLIMIT 5;"
|
| 23 |
-
},
|
| 24 |
-
{
|
| 25 |
-
"id": 6,
|
| 26 |
-
"status": "FAIL",
|
| 27 |
-
"generated": "SELECT T3.Name AS ArtistName, T1.Name AS SongName\nFROM Track AS T1\nJOIN Album AS T2 ON T1.AlbumId = T2.AlbumId\nJOIN Genre AS T3 ON T1.GenreId = T3.GenreId\nWHERE T3.Name = 'Rock'\nGROUP BY T1.TrackId;"
|
| 28 |
-
},
|
| 29 |
-
{
|
| 30 |
-
"id": 7,
|
| 31 |
-
"status": "ERROR: Execution failed on sql 'SELECT T3.Name AS MediaTypeName, COUNT(T1.TrackId) AS TotalTracksSold\nFROM Track AS T1\nJOIN MediaType AS T2 ON T1.MediaTypeId = T2.MediaTypeId\nGROUP BY T2.Name;': no such column: T3.Name"
|
| 32 |
-
},
|
| 33 |
-
{
|
| 34 |
-
"id": 8,
|
| 35 |
-
"status": "PASS",
|
| 36 |
-
"generated": "SELECT T3.Name FROM Playlist AS T1 INNER JOIN PlaylistTrack AS T2 ON T1.PlaylistId = T2.PlaylistId INNER JOIN Track AS T3 ON T2.TrackId = T3.TrackId WHERE T1.Name = 'TV Shows';"
|
| 37 |
-
},
|
| 38 |
-
{
|
| 39 |
-
"id": 9,
|
| 40 |
-
"status": "PASS",
|
| 41 |
-
"generated": "SELECT a.Name, COUNT(t.TrackId) AS TrackCount\nFROM Artist a\nJOIN Album ar ON a.ArtistId = ar.ArtistId\nJOIN Track t ON ar.AlbumId = t.AlbumId\nGROUP BY a.ArtistId\nORDER BY TrackCount DESC\nLIMIT 1;"
|
| 42 |
-
},
|
| 43 |
-
{
|
| 44 |
-
"id": 10,
|
| 45 |
-
"status": "FAIL",
|
| 46 |
-
"generated": "SELECT g.Name FROM Genre g JOIN Track t ON g.GenreId = t.GenreId GROUP BY g.Name HAVING COUNT(t.TrackId) > 100;"
|
| 47 |
-
},
|
| 48 |
-
{
|
| 49 |
-
"id": 11,
|
| 50 |
-
"status": "FAIL",
|
| 51 |
-
"generated": "SELECT a.Name, SUM(il.UnitPrice * il.Quantity) as TotalRevenue\nFROM Artist a\nJOIN Album al ON a.ArtistId = al.ArtistId\nJOIN Track tr ON al.AlbumId = tr.AlbumId\nJOIN InvoiceLine il ON tr.TrackId = il.TrackId\nGROUP BY a.ArtistId\nORDER BY TotalRevenue DESC\nLIMIT 1;"
|
| 52 |
-
},
|
| 53 |
-
{
|
| 54 |
-
"id": 12,
|
| 55 |
-
"status": "FAIL",
|
| 56 |
-
"generated": "SELECT a.Title, AVG(t.Milliseconds) / 1000 AS AverageLengthInSeconds\nFROM Album a\nJOIN Track t ON a.AlbumId = t.AlbumId\nGROUP BY a.AlbumId;"
|
| 57 |
-
},
|
| 58 |
-
{
|
| 59 |
-
"id": 13,
|
| 60 |
-
"status": "PASS",
|
| 61 |
-
"generated": "SELECT c.FirstName, c.LastName, e.FirstName, e.LastName\nFROM Customer c\nJOIN Employee e ON c.SupportRepId = e.EmployeeId\nWHERE e.LastName = 'Peacock';"
|
| 62 |
-
},
|
| 63 |
-
{
|
| 64 |
-
"id": 14,
|
| 65 |
-
"status": "PASS",
|
| 66 |
-
"generated": "SELECT t2.City FROM Invoice AS t1 INNER JOIN Customer AS t2 ON t1.CustomerId = t2.CustomerId WHERE strftime('%Y', t1.InvoiceDate) = '2013' GROUP BY t2.City ORDER BY COUNT(*) DESC LIMIT 1;"
|
| 67 |
-
},
|
| 68 |
-
{
|
| 69 |
-
"id": 15,
|
| 70 |
-
"status": "ERROR: Execution failed on sql 'SELECT T1.Title FROM Album AS T1 INNER JOIN InvoiceLine AS T2 ON T1.AlbumId = T2.AlbumId GROUP BY T1.AlbumId HAVING SUM(T2.UnitPrice * T2.Quantity) > 20;': no such column: T2.AlbumId"
|
| 71 |
-
}
|
| 72 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
freeze
DELETED
|
File without changes
|
src/nl2sql/qwen_engine.py
DELETED
|
@@ -1,63 +0,0 @@
|
|
| 1 |
-
# Inference Script for Qwen-2.5:1B
|
| 2 |
-
|
| 3 |
-
import requests
|
| 4 |
-
import json
|
| 5 |
-
|
| 6 |
-
def query_qwen(prompt):
|
| 7 |
-
url = "http://localhost:11434/api/generate"
|
| 8 |
-
payload = {
|
| 9 |
-
"model": "qwen2.5-coder:1.5b",
|
| 10 |
-
"prompt": prompt,
|
| 11 |
-
"stream": False,
|
| 12 |
-
"options": {
|
| 13 |
-
# Set temperature to 0 for deterministic output
|
| 14 |
-
"temperature": 0
|
| 15 |
-
}
|
| 16 |
-
}
|
| 17 |
-
response = requests.post(url, json=payload)
|
| 18 |
-
raw_response = response.json().get("response", "").strip()
|
| 19 |
-
|
| 20 |
-
# Clean the response to extract only the SQL query
|
| 21 |
-
if raw_response.startswith("```sql") and raw_response.endswith("```"):
|
| 22 |
-
return raw_response.replace("```sql", "").replace("```", "").strip()
|
| 23 |
-
else:
|
| 24 |
-
return raw_response
|
| 25 |
-
|
| 26 |
-
def build_qwen_prompt(question, schema):
|
| 27 |
-
return f"""## Instructions:
|
| 28 |
-
You are an expert SQLite assistant that converts natural language questions into SQL queries.
|
| 29 |
-
|
| 30 |
-
Use ONLY SQLite syntax.
|
| 31 |
-
|
| 32 |
-
Important database relationships:
|
| 33 |
-
- Artist(ArtistId) -> Album(ArtistId)
|
| 34 |
-
- Album(AlbumId) -> Track(AlbumId)
|
| 35 |
-
- Track(GenreId) -> Genre(GenreId)
|
| 36 |
-
- Track(MediaTypeId) -> MediaType(MediaTypeId)
|
| 37 |
-
- Track(TrackId) -> InvoiceLine(TrackId)
|
| 38 |
-
- InvoiceLine(InvoiceId) -> Invoice(InvoiceId)
|
| 39 |
-
- Customer(CustomerId) -> Invoice(CustomerId)
|
| 40 |
-
- Customer(SupportRepId) -> Employee(EmployeeId)
|
| 41 |
-
- Playlist(PlaylistId) -> PlaylistTrack(PlaylistId)
|
| 42 |
-
- PlaylistTrack(TrackId) -> Track(TrackId)
|
| 43 |
-
|
| 44 |
-
SQL Rules:
|
| 45 |
-
1. Use JOIN when data comes from multiple tables.
|
| 46 |
-
2. Use GROUP BY when using COUNT, SUM, or AVG.
|
| 47 |
-
3. Use HAVING for filtering aggregated values.
|
| 48 |
-
4. Use ORDER BY for ranking results.
|
| 49 |
-
5. Use LIMIT instead of TOP.
|
| 50 |
-
6. Always produce executable SQLite SQL.
|
| 51 |
-
7. Always use table aliases (t, a, ar, g, etc.) when joining tables.
|
| 52 |
-
|
| 53 |
-
Return ONLY the SQL query.
|
| 54 |
-
Do not provide explanations.
|
| 55 |
-
|
| 56 |
-
## Database Schema:
|
| 57 |
-
{schema}
|
| 58 |
-
|
| 59 |
-
## Question:
|
| 60 |
-
{question}
|
| 61 |
-
|
| 62 |
-
## SQL Query:
|
| 63 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/scripts/evaluate.py
DELETED
|
@@ -1,69 +0,0 @@
|
|
| 1 |
-
# Evaluation Script for NL2SQL Models
|
| 2 |
-
|
| 3 |
-
import json
|
| 4 |
-
import pandas as pd
|
| 5 |
-
from src.database.db_manager import get_db_connection, get_schema_context
|
| 6 |
-
from src.nl2sql.qwen_engine import query_qwen, build_qwen_prompt
|
| 7 |
-
|
| 8 |
-
def compare_results(df_generated_sql, df_gold_sql):
|
| 9 |
-
"""Compares the generated SQL query with the expected SQL query."""
|
| 10 |
-
if df_generated_sql is None or df_gold_sql is None:
|
| 11 |
-
return False
|
| 12 |
-
try:
|
| 13 |
-
return df_generated_sql.values.tolist() == df_gold_sql.values.tolist()
|
| 14 |
-
except Exception as e:
|
| 15 |
-
print(f"Error comparing results: {e}")
|
| 16 |
-
return False
|
| 17 |
-
|
| 18 |
-
def run_evaluation():
|
| 19 |
-
# Load test cases from a JSON file
|
| 20 |
-
with open('src/scripts/test_cases.json', 'r') as f:
|
| 21 |
-
test_cases = json.load(f)
|
| 22 |
-
|
| 23 |
-
results = []
|
| 24 |
-
correct_count = 0
|
| 25 |
-
schema = get_schema_context()
|
| 26 |
-
|
| 27 |
-
print(f"Running evaluation on {len(test_cases)} test cases...\n")
|
| 28 |
-
|
| 29 |
-
for case in test_cases:
|
| 30 |
-
print(f"Testing ID {case['id']}: {case['question'][:50]}...")
|
| 31 |
-
|
| 32 |
-
# Generate SQL query using the LLM
|
| 33 |
-
prompt = build_qwen_prompt(case['question'], schema)
|
| 34 |
-
generated_sql = query_qwen(prompt)
|
| 35 |
-
|
| 36 |
-
# Execute both generated SQL and gold SQL
|
| 37 |
-
connection = get_db_connection()
|
| 38 |
-
try:
|
| 39 |
-
df_generated = pd.read_sql_query(generated_sql, connection)
|
| 40 |
-
df_gold = pd.read_sql_query(case['gold_sql'], connection)
|
| 41 |
-
|
| 42 |
-
# Compare results
|
| 43 |
-
is_correct = compare_results(df_generated, df_gold)
|
| 44 |
-
if is_correct:
|
| 45 |
-
correct_count += 1
|
| 46 |
-
|
| 47 |
-
results.append({
|
| 48 |
-
"id": case['id'],
|
| 49 |
-
"status": "PASS" if is_correct else "FAIL",
|
| 50 |
-
"generated": generated_sql
|
| 51 |
-
})
|
| 52 |
-
except Exception as e:
|
| 53 |
-
results.append({"id": case['id'], "status": f"ERROR: {str(e)}"})
|
| 54 |
-
finally:
|
| 55 |
-
connection.close()
|
| 56 |
-
|
| 57 |
-
# Print summary
|
| 58 |
-
accuracy = (correct_count / len(test_cases)) * 100
|
| 59 |
-
print(f"\n EVALUATION COMPLETE")
|
| 60 |
-
print(f"Total Test Cases: {len(test_cases)}")
|
| 61 |
-
print(f"Correctly Generated SQL: {correct_count}")
|
| 62 |
-
print(f"Execution Accuracy: {accuracy:.2f}%")
|
| 63 |
-
|
| 64 |
-
# Save results to a JSON file
|
| 65 |
-
with open('evaluation_results.json', 'w') as f:
|
| 66 |
-
json.dump(results, f, indent=4)
|
| 67 |
-
|
| 68 |
-
if __name__ == "__main__":
|
| 69 |
-
run_evaluation()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/scripts/evaluate_hf.py
DELETED
|
@@ -1,108 +0,0 @@
|
|
| 1 |
-
# src/scripts/evaluate_hf.py
|
| 2 |
-
# Evaluation script for Hugging Face SQL generation.
|
| 3 |
-
|
| 4 |
-
import json
|
| 5 |
-
from pathlib import Path
|
| 6 |
-
import pandas as pd
|
| 7 |
-
from src.database.db_manager import get_db_connection
|
| 8 |
-
from src.nl2sql.sql_agent import nl2sql_agent
|
| 9 |
-
|
| 10 |
-
TEST_CASES_PATH = Path("src/scripts/test_cases.json")
|
| 11 |
-
RESULTS_PATH = Path("hf_evaluation_results.json")
|
| 12 |
-
|
| 13 |
-
def _normalize_dataframe(dataframe: pd.DataFrame) -> pd.DataFrame:
|
| 14 |
-
# Normalize dataframe to ensure accurate comparison
|
| 15 |
-
normalized = dataframe.copy()
|
| 16 |
-
normalized.columns = [str(column).lower() for column in normalized.columns]
|
| 17 |
-
|
| 18 |
-
for column in normalized.columns:
|
| 19 |
-
normalized[column] = normalized[column].map(
|
| 20 |
-
lambda value: round(float(value), 6)
|
| 21 |
-
if isinstance(value, float)
|
| 22 |
-
else value
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
sort_columns = list(normalized.columns)
|
| 26 |
-
if sort_columns:
|
| 27 |
-
normalized = normalized.sort_values(by=sort_columns, kind="mergesort").reset_index(drop=True)
|
| 28 |
-
|
| 29 |
-
return normalized
|
| 30 |
-
|
| 31 |
-
# Compare generated SQL results with expected results
|
| 32 |
-
def compare_results(df_generated: pd.DataFrame, df_gold: pd.DataFrame) -> bool:
|
| 33 |
-
"""Compare generated and expected query results."""
|
| 34 |
-
if df_generated is None or df_gold is None:
|
| 35 |
-
return False
|
| 36 |
-
|
| 37 |
-
try:
|
| 38 |
-
normalized_generated = _normalize_dataframe(df_generated)
|
| 39 |
-
normalized_gold = _normalize_dataframe(df_gold)
|
| 40 |
-
return normalized_generated.equals(normalized_gold)
|
| 41 |
-
except Exception as error:
|
| 42 |
-
print(f"Error comparing results: {error}")
|
| 43 |
-
return False
|
| 44 |
-
|
| 45 |
-
def run_evaluation():
|
| 46 |
-
if not TEST_CASES_PATH.exists():
|
| 47 |
-
print(f"Error: Could not find test cases at {TEST_CASES_PATH}")
|
| 48 |
-
return
|
| 49 |
-
|
| 50 |
-
with TEST_CASES_PATH.open("r", encoding="utf-8") as handle:
|
| 51 |
-
test_cases = json.load(handle)
|
| 52 |
-
|
| 53 |
-
results = []
|
| 54 |
-
correct_count = 0
|
| 55 |
-
|
| 56 |
-
print(f"Running evaluation on {len(test_cases)} test cases...\n")
|
| 57 |
-
|
| 58 |
-
for case in test_cases:
|
| 59 |
-
question = case["question"]
|
| 60 |
-
print(f"Testing ID {case['id']}: {question[:50]}...")
|
| 61 |
-
|
| 62 |
-
# Implement agent to handle RAG retrieval and SQL generation
|
| 63 |
-
agent_response = nl2sql_agent(user_question=question)
|
| 64 |
-
generated_sql = agent_response.get("query", "")
|
| 65 |
-
|
| 66 |
-
connection = get_db_connection()
|
| 67 |
-
if connection is None:
|
| 68 |
-
raise RuntimeError("Unable to connect to the SQLite database.")
|
| 69 |
-
|
| 70 |
-
try:
|
| 71 |
-
df_generated = pd.read_sql_query(generated_sql, connection)
|
| 72 |
-
df_gold = pd.read_sql_query(case["gold_sql"], connection)
|
| 73 |
-
|
| 74 |
-
is_correct = compare_results(df_generated, df_gold)
|
| 75 |
-
if is_correct:
|
| 76 |
-
correct_count += 1
|
| 77 |
-
|
| 78 |
-
results.append(
|
| 79 |
-
{
|
| 80 |
-
"id": case["id"],
|
| 81 |
-
"question": question,
|
| 82 |
-
"status": "PASS" if is_correct else "FAIL",
|
| 83 |
-
"generated_sql": generated_sql,
|
| 84 |
-
"gold_sql": case["gold_sql"],
|
| 85 |
-
}
|
| 86 |
-
)
|
| 87 |
-
except Exception as error:
|
| 88 |
-
results.append(
|
| 89 |
-
{
|
| 90 |
-
"id": case["id"],
|
| 91 |
-
"question": question,
|
| 92 |
-
"status": "ERROR",
|
| 93 |
-
"generated_sql": generated_sql,
|
| 94 |
-
"gold_sql": case["gold_sql"],
|
| 95 |
-
"error": str(error),
|
| 96 |
-
}
|
| 97 |
-
)
|
| 98 |
-
finally:
|
| 99 |
-
connection.close()
|
| 100 |
-
|
| 101 |
-
accuracy = (correct_count / len(test_cases)) * 100 if test_cases else 0.0
|
| 102 |
-
print("\nEVALUATION COMPLETE")
|
| 103 |
-
print(f"Total Test Cases: {len(test_cases)}")
|
| 104 |
-
print(f"Correctly Generated SQL: {correct_count} / {len(test_cases)}")
|
| 105 |
-
print(f"Execution Accuracy: {accuracy:.2f}%")
|
| 106 |
-
|
| 107 |
-
with RESULTS_PATH.open("w", encoding="utf-8") as handle:
|
| 108 |
-
json.dump(results, handle, indent=4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_bench.py
DELETED
|
@@ -1,36 +0,0 @@
|
|
| 1 |
-
# Test the database connection and schema extraction
|
| 2 |
-
|
| 3 |
-
import pandas as pd
|
| 4 |
-
from src.database.db_manager import get_db_connection, get_schema_context
|
| 5 |
-
from src.nl2sql.qwen_engine import query_qwen, build_qwen_prompt
|
| 6 |
-
|
| 7 |
-
def run_test(question):
|
| 8 |
-
print(f"\n[USER QUESTION]: {question}")
|
| 9 |
-
|
| 10 |
-
# Step 1: Get the database schema context from db_manager
|
| 11 |
-
schema = get_schema_context()
|
| 12 |
-
|
| 13 |
-
# Step 2: Build the prompt and Generate SQL query
|
| 14 |
-
prompt = build_qwen_prompt(question, schema)
|
| 15 |
-
generated_sql = query_qwen(prompt)
|
| 16 |
-
print(f"\n[GENERATED SQL]:\n{generated_sql}")
|
| 17 |
-
|
| 18 |
-
# Step 3: Execute the generated SQL query against the database
|
| 19 |
-
try:
|
| 20 |
-
connection = get_db_connection()
|
| 21 |
-
# Use pandas to execute the SQL query and display results
|
| 22 |
-
result_df = pd.read_sql_query(generated_sql, connection)
|
| 23 |
-
connection.close()
|
| 24 |
-
|
| 25 |
-
print("[EXECUTION SUCCESS]:")
|
| 26 |
-
if result_df.empty:
|
| 27 |
-
print("Query returned no results.")
|
| 28 |
-
else:
|
| 29 |
-
print(result_df.head(5)) # Display the first 5 rows of the result
|
| 30 |
-
except Exception as e:
|
| 31 |
-
print(f"[EXECUTION ERROR]: {e}")
|
| 32 |
-
|
| 33 |
-
if __name__ == "__main__":
|
| 34 |
-
# Example test question
|
| 35 |
-
test_question = "What is the total revenue generated from each country?"
|
| 36 |
-
run_test(test_question)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|