A DB Agent built using phidata, Groq, and SQLAlchemy to interact with SQL databases. This project allows users to query a SQL database using natural language and get responses in a user-friendly format. The agent is integrated with a Gradio UI for easy interaction.
- Natural Language Queries: Ask questions about your database in plain English.
- SQL Database Integration: Connects to Microsoft SQL Server using
pyodbcandSQLAlchemy. - Gradio UI: A simple and interactive web interface for querying the database.
- ANSI Escape Code Removal: Ensures clean and readable responses in the UI.
Before running the project, ensure you have the following installed:
- Python 3.8+
- ODBC Driver 18 for SQL Server: Install the ODBC driver for your operating system.
- Git: To clone the repository.
-
Clone the Repository:
git clone https://github.com/your-username/db-agent-project.git cd db-agent-project -
Set Up a Virtual Environment (Optional but Recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install Dependencies:
pip install -r requirements.txt
-
Set Up Environment Variables:
- Create a
.envfile in the root directory and add your database credentials:SERVER_NAME=server DATABASE_NAME=UCC USERNAME=usn PASSWORD=pwd GROQ_API_KEY=your_groq_api_key
- Create a
-
Update the Code:
- Replace the placeholders in the
db_agent.pyfile with your actual database credentials and Groq API key.
- Replace the placeholders in the
-
Run the Gradio UI:
python db_agent.py
-
Access the UI:
- Open your browser and navigate to the URL provided in the terminal (e.g.,
http://127.0.0.1:7860).
- Open your browser and navigate to the URL provided in the terminal (e.g.,
-
Enter Database Details:
- Provide the server name, database name, username, and password in the respective fields.
-
Ask a Query:
- Enter a natural language query (e.g., "List the tables in the database").
- Click Submit to get the agent's response.
-
List Tables:
List the tables in the database. -
Describe a Table:
Tell me about the contents of the Filings table. -
Count Rows:
How many rows are in the Filings table?
db-agent-project/
├── gradio_ui.py # Main script for the Gradio UI
├── requirements.txt # List of dependencies
├── README.md # Project documentation
├── .env # Environment variables (not included in the repo)
└── .gitignore # Files to ignore in Git
- phidata: For building the agent.
- SQLAlchemy: For database connectivity.
- pyodbc: For connecting to SQL Server.
- gradio: For the web interface.
- python-dotenv: For loading environment variables.
This project is licensed under the MIT License. See the LICENSE file for details.
