This project demonstrates the power of LangGraph and LangChain for building an intelligent SQL agent with visualization capabilities. The agent can interact with SQL databases using natural language queries, provide answers based on database content, and generate visual representations of the data.
- LLM Support: Works with GPT-4o (OpenAI) and Gemini-2.0-flash (Google) models
- LangGraph Architecture: Utilizes LangGraph for building a modular, extensible agent workflow
- Conversation Memory: Maintains context across interactions using LangChain's ConversationBufferMemory
- Data Visualization: Generates interactive charts and graphs from SQL query results
- Supports multiple chart types (bar, pie, line)
- Automatically detects visualization requests in natural language
- Renders and displays chart images in the default system viewer
- Clone the repository:
git clone https://github.com/yourusername/langgraph-sql-visualizer.git
- Navigate to the project directory:
cd langgraph-sql-visualizer
- Create a virtual environment and activate it:
python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
- Install the required packages:
pip install -r requirements.txt
- Set up environment variables:
export OPENAI_API_KEY=<your_openai_api_key> export GOOGLE_API_KEY=<your_google_api_key>
Run the application using:
python main.py
You will be prompted to select an LLM model (GPT-4o or Gemini-2.0-flash) and then you can start asking questions about the employee database.
-
Basic SQL queries:
- "What are the salaries of my employees?"
- "How many employees work in the Engineering department?"
- "What is the average salary by department?"
-
Visualization queries:
- "Show me a bar chart of employee salaries"
- "Create a pie chart of employees by department"
- "Visualize the top 5 highest paid employees"
- "Plot the results" (after a previous query)
Here are some examples of the visualizations generated by the application:
The visualization agent automatically generates appropriate chart specifications based on the query and data, which are then rendered into images using Matplotlib.
The application uses a LangGraph-based architecture with the following components:
- SQL Agent: Generates SQL queries based on natural language questions
- SQL Executor: Executes the generated SQL queries against the database
- Answer Generator: Creates natural language answers from SQL results
- Visualization Agent: Generates chart specifications when visualization is requested
- Chart Renderer: Renders chart images from the specifications
agents/
: Contains the SQL and visualization agentscli/
: Command-line interface for the applicationdb/
: Database setup and sample datallm/
: LLM model loading and configurationprompts/
: Prompt templates for the agentsvisualization/
: Chart rendering and image generationvisualizations/
: Generated chart images (not tracked in git)
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.