This project implements an intelligent SQL database chat assistant using LangChain and OpenAI's GPT models. It allows users to interact with SQL databases using natural language, making database querying more accessible to non-technical users.
- Natural language to SQL query conversion
- Context-aware database interactions
- Support for SQLite and other SQL databases
- Advanced chat capabilities with memory
- Environment variable configuration
- Web interface using FastAPI
- Database exploration without SQL knowledge
- Quick data analysis and reporting
- Teaching SQL concepts interactively
- Database administration assistance
- Data validation and verification
- Python 3.8+
- pip
- Virtual environment tool (optional but recommended)
- OpenAI API key
- Clone the repository:
Here's the markdown formatted version:
git clone https://github.com/arjitvermaco/
cd sql-database-chat
python -m venv .venv
source .venv/bin/activate
# On Windows: .venv\Scripts\activate
pip install langchain langchain-community langchain-core python-dotenv fastapi uvicorn openai sqlite3 pydantic
cp .env.example .env
Create .env
file with:
OPENAI_API_KEY=your-api-key-here
OPENAI_MODEL=gpt-4-0125-preview
DATABASE_URL=sqlite:///your_database.db
Database Chain
def get_db_chain()
# Initialize the database chain
def process_query(query: str)
# Process natural language queries
def get_table_info()
# Retrieve database schema information
Memory Management
def setup_memory()
# Initialize conversation memory
def process_chat()
# Process chat messages with context
def get_relevant_context()
# Retrieve relevant context for queries
API Endpoints
@app.post("/chat")
# Process chat messages
@app.websocket("/ws/chat")
# Real-time chat communication
@app.get("/tables")
# Get database schema information
{
"query": "Show me all users who joined last month",
"context": "optional previous context"
}
{
"response": "Here are the users who joined last month...",
"sql_query": "SELECT FROM users WHERE...",
"results": [...]
}
{
"tables": [
{
"name": "users",
"columns": ["id", "name", "email", "joined_date"]
}
]
}
Basic Query
from sqlite_chat import get_db_chain
# Initialize the chain
db_chain = get_db_chain()
# Ask a question
response = db_chain.run("How many users registered last month?")
print(response)
Advanced Chat
from advanced_chat import AdvancedDatabaseChat
chat = AdvancedDatabaseChat()
# Have a conversation
response = chat.chat("Show me sales trends for the last quarter")
print(response)
{
"error": "Database connection failed",
"details": "Could not connect to SQLite database: database is locked",
"code": "DB_CONNECTION_ERROR"
}
Citations: [1] https://github.com/yourusername/sql-database-chat
- SQL injection prevention through parameterized queries
- Input validation using Pydantic models
- Environment variable validation
- Rate limiting on API endpoints
- Proper error handling to prevent information leakage
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to branch (
git push origin feature/amazing-feature
) - Create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
For support:
- Open an issue in the GitHub repository
- Contact maintainers
- Check documentation
- LangChain team for the framework
- OpenAI for GPT models
- Contributors and maintainers