Skip to content

This project tries to implement an agent capable of receiving natural language questions, writing and executing SQL queries and if there's an error, it corrects itself until it gets the result. It excels over other projects because the main Python package involved is LangGraph and it shines on iterative processes.

Notifications You must be signed in to change notification settings

Merino1919/Self-Correcting-SQL-Coder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿค– The self-correcting SQL coder

This project tries to implement an agent capable of receiving natural language questions, writing and executing SQL queries and if there's an error, it corrects itself until it gets the result.
It excels over other projects because the main Python package involved is LangGraph and it shines on iterative processes. Here you can see an example of loops, conditional logic and states managenment.

๐Ÿ•ธ๏ธ The graph

The user sends a question like: What is the most listened artist? or How many songs has the database from Bad Bunny?

  • Node 1 (Planner): Analyzes the database schema using a helper function to provide context for the query generation.
  • Node 2 (Coder): Generates the SQL query. It is instructed to strictly use tool calling and follow the schema provided by the Planner.
  • Node 3 (Executor): Uses the execute_sql tool to run the query against a SQLite database. If the model fails to call the tool or the SQL is invalid, it captures the error.
  • Node 4 (Checker): Reviews the output. If it detects the word "ERROR" (either from SQLite or from the Executor's validation), it explains the failure and triggers a retry.
  • Conditional Edge: Uses a should_continue function that reads the state and decides whether to finish the process or send the flow back to the Coder for a fix.

๐Ÿ“ฆ Python packages involved in

  • LangGraph: Orchestrates the agent's control flow using a stateful graph, enabling the cyclical "self-correction" loop.
  • LangChain: Provides the base classes for messages, system prompts, and the logic to bind tools to the LLM.
  • LangChain-ollama: Connects the project to local models (like Qwen) via Ollama, allowing for private and local execution.
  • LangChain-community: Supplies the SQLDatabase utility to interact easily with SQLite and extract table schemas.
  • Streamlit: (Optional/Planned) To provide a user-friendly chat interface for the agent.
  • sqlalchemy: Acts as the underlying engine to handle database connections and execute queries securely.

๐Ÿง  State Management

The project utilizes a TypedDict called MessagesState that leverages Annotated with operator.add. This allows the graph to:

  • Maintain a full history of the conversation (Question -> SQL -> Error -> Correction).
  • Track the number of LLM calls made during the process.
  • Provide the Coder with the necessary context to learn from previous SQL mistakes.

๐Ÿš€ How to run

  • Install dependencies: pip install -r requirements.txt
  • Configure the database: Ensure your .db file path is correctly set in database_utils.py.
  • Execute: Run python main.py.

About

This project tries to implement an agent capable of receiving natural language questions, writing and executing SQL queries and if there's an error, it corrects itself until it gets the result. It excels over other projects because the main Python package involved is LangGraph and it shines on iterative processes.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages