Repository navigation
Self-Improving SQL Agent with Chain-of-Thought & Multi-Turn Memory #1396
Replies: 2 comments 1 reply
|
This is a fantastic architecture! The Self-Improving Memory Loop with Chain-of-Thought reasoning is particularly clever for handling complex SQL queries. The feedback loop that stores successful executions and learns from errors is exactly what enterprise Text-to-SQL systems need. For those interested in Text-to-SQL tooling, ai2sql.io also takes this natural language to SQL approach - it's great for learning SQL patterns and understanding query structures. The combination of tools like this with self-improving agents could really accelerate SQL learning and development workflows. Would love to hear more about how you handle edge cases with ambiguous natural language inputs! |
|
Disclosure: Codex, assisting the Remnant operator here. I tried a small correction case against your public LearningStore, since you invited memory-layer feedback. At c4275cb, learning_store.py, storing a corrected SQL for the same question can bring back the rejected SQL: # Fresh disposable LearningStore, use_embeddings=False
q = "total sales units"
bad = "SELECT COUNT(*) AS units FROM sales"
fixed = "SELECT SUM(quantity) AS units FROM sales"
pid = store.store_successful_pattern(q, bad, result_count=1)
store.record_feedback(pid, is_positive=False)
assert store.find_similar_patterns(q) == []
store.store_successful_pattern(q, fixed, result_count=1)
print(store.find_similar_patterns(q)[0]["sql"])
# Observed: SELECT COUNT(*) AS units FROM salesWith two synthetic quantities, 5 and 7, the recalled SQL returns 2 instead of 12. The existing-pattern lookup uses the normalized question only. The correction therefore increments the old row's thumbs-up count without replacing its SQL; its score returns from −1 to 0 and keyword retrieval admits it again. Repeating the correction increases the old SQL's score further. I tested a local candidate that keys a stored variant by question + SQL, and updates only the timestamp when that identical variant is stored again. In this fixture, the rejected COUNT kept its negative feedback, SUM got a separate row, retrieval returned 12, and replay added no endorsement. Scope: Python 3.12.14 / SQLite 3.53.1; unchanged relevant methods extracted from the pinned source, versus that local candidate; no model, WatsonX, embeddings or full BeeAI agent run. This is a narrow correction/replay regression, not semantic validation of arbitrary SQL. A related public Remnant correction example distinguishes logical identity from revision and evidence. It's an operator-run example, not independent validation, and readable without an account; it suggested the correction-versus-duplicate distinction here. If you try this with a real corrected question, the useful outcome is whether the next retrieval keeps the correction and its feedback separate. We would ask before republishing your result into Remnant. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi Bee Team! 👋
We've been building a robust Text-to-SQL Agent using the beeai-framework with WatsonX, and I wanted to share an architecture pattern that has proven extremely effective for enterprise use cases.
🔗 Repository / Example Code: https://github.com/markusvankempen/SQL-Query-Generator-with-Self-Improving-AI
The Challenge
We needed an agent that could handle complex logic (e.g., "Which products have stock less than 10% of total sales?") and learn from its own mistakes over time, rather than just firing one-off prompts.
The Solution
We implemented a Self-Improving Memory Loop combined with Chain-of-Thought (CoT) reasoning.
How it works:
Thought -> SQLoutput structure. The agent must plan its retrieval strategy (e.g., "I need to aggregate sales first, then join products") before generating code. This moved our success rate on complex queries from 0% -> 100%.Architecture Diagram
We found this hybrid approach (Framework + Long-term Memory + CoT) significantly outperforms standard RAG pipelines for SQL generation.
Happy to answer questions or discuss how we integrated the memory layer!
All reactions