-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_agent.py
More file actions
179 lines (144 loc) · 6.95 KB
/
sql_agent.py
File metadata and controls
179 lines (144 loc) · 6.95 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
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
import sqlite3
from typing import Dict, List, Any, Optional, Tuple
import json
from models import SQLGenerationRequest, SQLGenerationResponse, SQLQueryResult, SQLAgentResponse
from llm_client import LLMClient
class SQLAgent:
"""Agent for handling natural language to SQL conversion and execution"""
def __init__(self, db_path: str, llm_client: LLMClient, max_retries: int = 3):
self.db_path = db_path
self.llm_client = llm_client
self.conn = None
self.cursor = None
self.max_retries = max_retries
self._connect_to_db()
self.schema_info = self._get_schema_info()
def _connect_to_db(self) -> None:
"""Establish connection to the SQLite database"""
try:
self.conn = sqlite3.connect(self.db_path)
self.conn.row_factory = sqlite3.Row # This enables column access by name
self.cursor = self.conn.cursor()
except sqlite3.Error as e:
raise Exception(f"Database connection error: {str(e)}")
def _get_schema_info(self) -> str:
"""Get the database schema information"""
try:
# Get table names
self.cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = [row[0] for row in self.cursor.fetchall() if not row[0].startswith('sqlite_')]
schema_info = "Database Schema:\n\n"
for table in tables:
schema_info += f"Table: {table}\n"
# Get column information
self.cursor.execute(f"PRAGMA table_info({table});")
columns = self.cursor.fetchall()
schema_info += "Columns:\n"
for col in columns:
col_name = col['name']
col_type = col['type']
is_pk = "PRIMARY KEY" if col['pk'] == 1 else ""
schema_info += f"- {col_name} ({col_type}) {is_pk}\n"
# Get sample data (first 3 rows)
try:
self.cursor.execute(f"SELECT * FROM {table} LIMIT 3;")
rows = self.cursor.fetchall()
if rows:
schema_info += "Sample data:\n"
for row in rows:
row_dict = {k: row[k] for k in row.keys()}
schema_info += f"- {json.dumps(row_dict)}\n"
except sqlite3.Error:
schema_info += "Could not retrieve sample data.\n"
schema_info += "\n"
return schema_info
except sqlite3.Error as e:
return f"Error retrieving schema: {str(e)}"
def generate_sql(self, request: SQLGenerationRequest, error_message: str = None) -> SQLGenerationResponse:
"""Generate SQL from natural language query"""
response = self.llm_client.generate_sql(request.query, self.schema_info, error_message)
return SQLGenerationResponse(
sql_query=response["sql_query"],
explanation=response["explanation"]
)
def execute_sql(self, sql_query: str) -> Tuple[Optional[SQLQueryResult], Optional[str]]:
"""Execute a SQL query and return the results or error"""
try:
self.cursor.execute(sql_query)
rows = self.cursor.fetchall()
if not rows:
return SQLQueryResult(columns=[], rows=[], row_count=0), None
# Get column names
columns = [desc[0] for desc in self.cursor.description]
# Convert rows to lists
row_data = [[row[col] for col in columns] for row in rows]
result = SQLQueryResult(
columns=columns,
rows=row_data,
row_count=len(row_data)
)
return result, None
except sqlite3.Error as e:
error_message = f"SQL execution error: {str(e)}"
return None, error_message
def improve_sql(self, natural_query: str, failed_sql: str, error_message: str) -> SQLGenerationResponse:
"""Improve a SQL query that failed with an error"""
response = self.llm_client.improve_sql(natural_query, self.schema_info, failed_sql, error_message)
return SQLGenerationResponse(
sql_query=response["sql_query"],
explanation=response["explanation"]
)
def process_query(self, natural_query: str) -> SQLAgentResponse:
"""Process a natural language query and return the complete response with retry logic"""
request = SQLGenerationRequest(query=natural_query)
# Initial SQL generation
sql_response = self.generate_sql(request)
current_sql = sql_response.sql_query
current_explanation = sql_response.explanation
# If no SQL was generated, return error
if not current_sql:
return SQLAgentResponse(
natural_query=natural_query,
generated_sql="",
explanation=f"Failed to generate SQL: {current_explanation}",
query_result=None,
error="Could not generate SQL query"
)
# Try to execute the SQL with retries
attempts = 0
result = None
error = None
improvement_history = []
while attempts < self.max_retries:
result, error = self.execute_sql(current_sql)
# If successful or no error, break the loop
if not error:
break
# Record the improvement attempt
improvement_history.append({
"attempt": attempts + 1,
"sql": current_sql,
"error": error
})
# Try to improve the SQL
improved_response = self.improve_sql(natural_query, current_sql, error)
# If improvement failed or returned the same SQL, break the loop
if not improved_response.sql_query or improved_response.sql_query == current_sql:
break
# Update current SQL and explanation for next attempt
current_sql = improved_response.sql_query
current_explanation += f"\n\nImproved SQL (attempt {attempts + 1}): {improved_response.explanation}"
attempts += 1
# Create the response
return SQLAgentResponse(
natural_query=natural_query,
generated_sql=current_sql,
explanation=current_explanation,
query_result=result,
error=error,
improvement_history=improvement_history if improvement_history else None
)
def close(self) -> None:
"""Close the database connection"""
if self.conn:
self.conn.close()