-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
96 lines (76 loc) · 2.76 KB
/
Copy pathmain.py
File metadata and controls
96 lines (76 loc) · 2.76 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
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
"""
CLI interface for the NL2SQL Pipeline.
Usage:
# Interactive mode
python main.py --db clinic.db
# Single query
python main.py --db clinic.db --query "How many patients do we have?"
# Run evaluation suite
python evaluate.py
"""
import argparse
import json
import os
import sys
from datetime import datetime
from pipeline import NL2SQLPipeline, PipelineConfig
def interactive_loop(pipe: NL2SQLPipeline, log_path: str | None):
"""Run an interactive NL2SQL session."""
results_log = []
print("\n" + "=" * 60)
print("NL2SQL Pipeline — Interactive Mode")
print("Ask business questions in plain English.")
print("Type 'quit' to exit.")
print("=" * 60 + "\n")
while True:
try:
question = input("Q: ").strip()
except (EOFError, KeyboardInterrupt):
print("\nExiting.")
break
if not question or question.lower() in ("quit", "exit", "q"):
break
result = pipe.query(question)
print(f"\n{result}\n")
results_log.append({
"question": result.question,
"sql": result.cleaned_sql,
"success": result.success,
"row_count": result.execution_result.get("row_count", 0),
"retries": result.retries_used,
"timestamp": datetime.now().isoformat(),
})
# Save log
if results_log and log_path:
os.makedirs(os.path.dirname(log_path) if os.path.dirname(log_path) else ".", exist_ok=True)
with open(log_path, "w") as f:
json.dump(results_log, f, indent=2)
print(f"Query log saved to {log_path}")
def main():
parser = argparse.ArgumentParser(description="NL2SQL Pipeline")
parser.add_argument("--db", type=str, default="clinic.db", help="Path to SQLite database")
parser.add_argument("--ddl", type=str, help="Path to DDL file (alternative to --db)")
parser.add_argument("--query", type=str, help="Single query (non-interactive)")
parser.add_argument("--log", type=str, default="logs/queries.json", help="Query log path")
parser.add_argument("--model", type=str, default=None, help="Ollama model name")
args = parser.parse_args()
config = PipelineConfig()
if args.model:
config.model_name = args.model
config.db_path = args.db
pipe = NL2SQLPipeline(config)
if args.ddl:
pipe.initialize(ddl_path=args.ddl)
elif os.path.exists(args.db):
pipe.initialize(db_path=args.db)
else:
print(f"ERROR: Database not found at {args.db}")
print("Run: python create_sample_db.py")
sys.exit(1)
if args.query:
result = pipe.query(args.query)
print(f"\n{result}")
else:
interactive_loop(pipe, args.log)
if __name__ == "__main__":
main()