-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsql_agent.py
More file actions
375 lines (298 loc) · 11.4 KB
/
sql_agent.py
File metadata and controls
375 lines (298 loc) · 11.4 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""SQL Agent using LangGraph - follows agent-lightning APO pattern."""
from eval_utils import evaluate_query
from agentlightning.types import PromptTemplate
from agentlightning.litagent import rollout
from langgraph.graph import END, START, MessagesState, StateGraph
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.messages import AnyMessage, BaseMessage, HumanMessage
from langchain_community.utilities import SQLDatabase
from langchain_community.tools.sql_database.tool import QuerySQLDatabaseTool
import os
import re
from typing import Any, Dict, Literal
from dotenv import load_dotenv
from langchain.chat_models import init_chat_model
load_dotenv()
# Default prompts (WRITE_QUERY_PROMPT will be optimized by APO)
CHECK_QUERY_PROMPT = ChatPromptTemplate([
("system", """You are a SQL expert with a strong attention to detail.
Double check the {dialect} query for common mistakes, including:
- Using NOT IN with NULL values
- Using UNION when UNION ALL should have been used
- Using BETWEEN for exclusive ranges
- Data type mismatch in predicates
- Properly quoting identifiers
- Using the correct number of arguments for functions
- Casting to the correct data type
- Using the proper columns for joins
- Explicit query execution failures
- Clearly unreasonable query execution results
## Table Schema ##
{table_info}
## Output Format ##
If any mistakes from the list above are found, list each error clearly.
After listing mistakes (if any), conclude with **ONE** of the following exact phrases in all caps and without surrounding quotes:
- If mistakes are found: `THE QUERY IS INCORRECT.`
- If no mistakes are found: `THE QUERY IS CORRECT.`
DO NOT write the corrected query in the response. You only need to report the mistakes.""".strip()),
("user", """Question: {input}
Query:
```{dialect}
{query}
```
Execution result:
```
{execution}
```"""),
])
REWRITE_QUERY_PROMPT = ChatPromptTemplate([
("system", """You are an agent designed to interact with a SQL database.
Rewrite the previous {dialect} query to fix errors based on the provided feedback.
The goal is to answer the original question.
Make sure to address all points in the feedback.
Pay attention to use only the column names that you can see in the schema description.
Be careful to not query for columns that do not exist.
Also, pay attention to which column is in which table.
## Table Schema ##
Only use the following tables:
{table_info}
## Output Format ##
Respond in the following format:
```{dialect}
REWRITTEN QUERY
```""".strip()),
("user", """Question: {input}
## Previous query ##
```{dialect}
{query}
```
## Previous execution result ##
```
{execution}
```
## Feedback ##
{feedback}
Please rewrite the query to address the feedback."""),
])
class State(MessagesState):
question: str
query: str
execution: str
answer: str
feedback: str
num_turns: int
messages: list[AnyMessage]
class SQLAgent:
"""SQL agent with query generation, execution, checking, and rewriting."""
def __init__(
self,
db_path: str,
max_turns: int,
write_prompt: str,
model: str,
temperature: float,
table_info_truncate: int = 2048,
execution_truncate: int = 2048,
debug: bool = False,
):
self.db = SQLDatabase.from_uri(f"sqlite:///{db_path}")
self.max_turns = max_turns
self.table_info_truncate = table_info_truncate
self.execution_truncate = execution_truncate
self.debug = debug
# Use the APO-provided write prompt
self.write_query_prompt = ChatPromptTemplate([
("system", write_prompt),
("user", "Question: {input}")
])
self.check_query_prompt = CHECK_QUERY_PROMPT
self.rewrite_query_prompt = REWRITE_QUERY_PROMPT
# Initialize LLM
self.llm = init_chat_model(
model,
model_provider="openai",
openai_api_base=os.environ.get(
"OPENAI_API_BASE", "https://api.openai.com/v1"),
openai_api_key=os.environ.get("OPENAI_API_KEY"),
temperature=temperature,
max_tokens=2048,
)
self.query_tool = QuerySQLDatabaseTool(db=self.db)
def get_table_info(self) -> str:
"""Get the table information in a human-readable format."""
try:
table_info = self.db.get_table_info()
if len(table_info) > self.table_info_truncate:
table_info = table_info[: self.table_info_truncate] + \
"\n... (truncated)"
return table_info
except Exception as e:
print(f"Failed to get table info: {e}")
return "No schema available."
def invoke_prompt(self, prompt: Any) -> AnyMessage:
"""Invoke LLM with prompt and handle errors."""
try:
result = self.llm.invoke(prompt)
except Exception as e:
print(f"Failed to invoke prompt: {e}")
result = self.llm.invoke(
[HumanMessage(content="Please create a random SQL query as an example.")])
return result
def truncate_execution(self, execution: str) -> str:
"""Truncate the execution result to a reasonable length."""
if len(execution) > self.execution_truncate:
return execution[: self.execution_truncate] + "\n... (truncated)"
return execution
def parse_query(self, message: AnyMessage) -> str | None:
"""Parse SQL query from LLM response."""
result: str | None = None
for match in re.finditer(r".*```\w*\n(.*?)\n```.*", message.content, re.DOTALL):
result = match.group(1).strip()
return result
def write_query(self, state: State) -> State:
"""Generate SQL query to fetch information."""
prompt: Any = self.write_query_prompt.invoke({
"dialect": self.db.dialect,
"table_info": self.get_table_info(),
"input": state["question"]
})
result = self.invoke_prompt(prompt)
query = self.parse_query(result) or result.content
return {
**state,
"query": query,
"num_turns": 1,
"messages": [*prompt.messages, result],
}
def execute_query(self, state: State) -> State:
"""Execute SQL query."""
execution_result = self.query_tool.invoke(
state["query"])
if not isinstance(execution_result, str):
execution_result = str(execution_result)
return {**state, "execution": execution_result}
def check_query(self, state: State) -> State:
"""Check the SQL query for correctness."""
prompt: Any = self.check_query_prompt.invoke({
"dialect": self.db.dialect,
"table_info": self.get_table_info(),
"input": state["question"],
"query": state["query"],
"execution": self.truncate_execution(state["execution"]),
})
result = self.invoke_prompt(prompt)
res = {
**state,
"feedback": result.content,
"messages": [*state.get("messages", []), *prompt.messages, result],
}
return res
def rewrite_query(self, state: State) -> State:
"""Rewrite SQL query if necessary."""
prompt: Any = self.rewrite_query_prompt.invoke({
"dialect": self.db.dialect,
"table_info": self.get_table_info(),
"input": state["question"],
"query": state["query"],
"execution": self.truncate_execution(state["execution"]),
"feedback": state["feedback"],
})
result = self.invoke_prompt(prompt)
rewritten_query = self.parse_query(result)
return {
**state,
"query": rewritten_query or state["query"],
"num_turns": state.get("num_turns", 0) + 1,
"messages": [*prompt.messages, result],
}
def should_continue(self, state: State) -> Literal[END, "rewrite_query"]:
"""Determine if the agent should continue based on the result."""
if state["messages"] and isinstance(state["messages"][-1], BaseMessage):
last_message = state["messages"][-1]
if "THE QUERY IS CORRECT" in last_message.content:
if "THE QUERY IS INCORRECT" in last_message.content:
correct_index = last_message.content.rfind(
"THE QUERY IS CORRECT")
incorrect_index = last_message.content.rfind(
"THE QUERY IS INCORRECT")
if correct_index > incorrect_index:
return END
else:
return END
if state.get("num_turns", 0) >= self.max_turns:
return END
return "rewrite_query"
def graph(self):
"""Build the LangGraph workflow."""
builder = StateGraph(State)
builder.add_node(self.write_query)
builder.add_node(self.execute_query)
builder.add_node(self.check_query)
builder.add_node(self.rewrite_query)
builder.add_edge(START, "write_query")
builder.add_edge("write_query", "execute_query")
builder.add_edge("execute_query", "check_query")
builder.add_conditional_edges(
"check_query",
self.should_continue,
)
builder.add_edge("rewrite_query", "execute_query")
return builder.compile()
def prompt_template_baseline() -> PromptTemplate:
"""Baseline prompt template for APO to optimize."""
return PromptTemplate(
template="""You are an agent designed to interact with a SQL database.
Given an input question, create a syntactically correct {dialect} query to run to help find the answer.
Pay attention to use only the column names that you can see in the schema description.
Be careful to not query for columns that do not exist.
Also, pay attention to which column is in which table.
## Table Schema ##
Only use the following tables:
{table_info}
## Output Format ##
Respond in the following format:
```{dialect}
GENERATED QUERY
```""",
engine="f-string",
)
@rollout
def sql_agent_rollout(task: Dict[str, Any], prompt_template: PromptTemplate) -> float:
"""SQL agent rollout following APO pattern from room_selector.py."""
question = task["question"]
db_id = task["db_id"]
ground_truth = task["query"]
db_path = os.path.join("databases", db_id, f"{db_id}.sqlite")
if not os.path.exists(db_path):
print(f"Database not found: {db_path}")
return 0.0
db = SQLDatabase.from_uri(f"sqlite:///{db_path}")
table_info = db.get_table_info()
formatted_prompt = prompt_template.format(
dialect="SQLite",
table_info=table_info,
input=question,
)
agent = SQLAgent(
db_path=db_path,
max_turns=3,
write_prompt=formatted_prompt,
model="gpt-5-mini",
temperature=0.0,
).graph()
try:
result = agent.invoke(
{"question": question},
{"recursion_limit": 100}
)
predicted_query = result.get("query", "")
except Exception as e:
print(f"Agent execution failed: {e}")
return 0.0
# Evaluate
reward = evaluate_query(predicted_query, ground_truth,
db_path, raise_on_error=False)
print(f"Question: {question[:80]}...")
print(f"Predicted: {predicted_query[:80]}...")
print(f"Reward: {reward}")
return reward