AI-powered database query service using natural language processing. Built with clean architecture principles and FastAPI.
- π€ Natural language to SQL query conversion using AI models
- ποΈ SQLite database support
- ποΈ Clean architecture with clear separation of concerns
- π Fast and modern API built with FastAPI
- π Automatic API documentation with Swagger/OpenAPI
- π§ Configurable AI models (Gemini, OpenAI, Groq)
- π Comprehensive logging
- π Database schema exploration
- β‘ Health check endpoints
The project follows clean architecture principles with these layers:
sqlagent/
βββ domain/ # Core business logic and entities
βββ application/ # Use cases and application services
βββ infrastructure/ # Database connections, external services
βββ presentation/ # API controllers and DTOs
βββ config.py # Configuration management
βββ main.py # FastAPI application
- Python 3.9+
- uv (recommended) or pip
- Clone the repository:
git clone https://github.com/mehdiozdemir/sqlAgent.git
cd sqlAgent- Install uv (if not already installed):
# On macOS/Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
# On Windows (PowerShell)
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
# Alternative: using pip
pip install uv- Install dependencies:
# Using uv (recommended)
uv sync
# Alternative: using pip
pip install -e .- Set up environment variables:
# Create .env file with your API keys
cp .env.example .env
# Edit .env with your actual API keysuv run python run_server.pyuv run uvicorn sqlagent.main:app --reload --host 0.0.0.0 --port 8000# Activate virtual environment and run
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
python run_server.pyThe API will be available at:
- API: http://localhost:8000
- Documentation: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
GET /health/- Service health checkGET /health/database- Database connection health check
GET /database/info- Get database informationGET /database/tables- List all tablesGET /database/tables/{table_name}- Get table schema
POST /query/natural-language- Process natural language queries
curl -X GET "http://localhost:8000/database/tables"curl -X POST "http://localhost:8000/query/natural-language" \
-H "Content-Type: application/json" \
-d '{
"query": "How many albums are in the database?",
"limit": 10
}'curl -X GET "http://localhost:8000/database/tables/Album"The service can be configured using environment variables or a .env file:
# Database Configuration
DATABASE_PATH=sqlagent/Chinook_Sqlite.sqlite # Your-db-path
# AI Model Configuration
AI_MODEL_TYPE=gemini # Options: gemini, openai, groq, for local models use openai and set api key "dummy"
# API Keys
GEMINI_API_KEY=your_gemini_api_key
GROQ_API_KEY=your_groq_api_key
OPENAI_API_KEY=your_openai_api_key
OPENAI_BASE_URL=LOCAL_MODEL_URL # Use for local models (Ollama, LMstudio)
# Server Configuration
HOST=0.0.0.0
PORT=8000
# Logging
LOG_LEVEL=INFOThe service supports multiple AI models:
- Gemini (default): Google's Gemini models
- OpenAI: OpenAI GPT models (including local models via compatible APIs)
- Groq: Groq's fast inference models
Configure the model type using the AI_MODEL_TYPE environment variable.
- Gemini: Get your API key from Google AI Studio
- OpenAI: Get your API key from OpenAI Dashboard
- Groq: Get your API key from Groq Console
Currently supports SQLite databases. The service includes a sample Chinook database for testing.
sqlagent/
βββ domain/
β βββ entities.py # Data models and entities
β βββ interfaces.py # Abstract interfaces
βββ application/
β βββ use_cases.py # Business logic use cases
βββ infrastructure/
β βββ database.py # Database repository implementation
β βββ ai_agent.py # AI agent service implementation
βββ presentation/
β βββ controllers.py # FastAPI route handlers
β βββ dependencies.py # Dependency injection
βββ config.py # Configuration management
βββ main.py # FastAPI application setup
- API Key Not Found: Make sure your
.envfile contains the correct API key for your chosen model - Port Already in Use: Change the port in
.envfile or kill the process using port 8000 - Database Connection: Ensure the SQLite database file exists and is readable
Check the log file for detailed error information:
tail -f logs/sql_agent.log- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Ensure code quality checks pass
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For questions and support, please open an issue on the GitHub repository.