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 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_sqltool 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.
- 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.
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.
- Install dependencies: pip install -r requirements.txt
- Configure the database: Ensure your
.dbfile path is correctly set indatabase_utils.py. - Execute: Run
python main.py.