https://github.com/nitintf/nl2sql
Natural Language to SQL conversion API using LangChain, FastAPI and React
https://github.com/nitintf/nl2sql
fastapi langchain langgraph openai react
Last synced: 3 months ago
JSON representation
Natural Language to SQL conversion API using LangChain, FastAPI and React
- Host: GitHub
- URL: https://github.com/nitintf/nl2sql
- Owner: nitintf
- Created: 2025-11-26T20:56:08.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-11-28T11:29:11.000Z (7 months ago)
- Last Synced: 2025-11-30T17:28:58.721Z (7 months ago)
- Topics: fastapi, langchain, langgraph, openai, react
- Language: TypeScript
- Homepage:
- Size: 786 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# NL2SQL
A FastAPI-based chat API for converting natural language queries to SQL using LangChain. This application provides an intuitive web interface where users can ask questions in natural language and receive SQL query results.
## Screenshots
### Chat Interface


The chat interface allows users to interact with the AI assistant, ask questions about the database, and receive formatted SQL query results.
## Tech Stack
### Frontend
- **React** - UI framework
- **Vite** - Build tool and dev server
- **Tailwind CSS** - Utility-first CSS framework
- **AI SDK** (`@ai`) - AI integration library for streaming responses
- **Shiki** - Syntax highlighting
### Backend
- **FastAPI** - Modern Python web framework
- **LangChain** - Framework for building LLM applications
- **OpenAI** (via `langchain-openai`) - AI model integration
- **PostgreSQL** (via `psycopg2-binary`) - Database connectivity
- **Uvicorn** - ASGI server
## API Endpoints
### POST `/api/v1/chat`
Streams AI responses for natural language to SQL conversion.
**Request Body:**
```json
{
"message": "Which products are in stock?",
"model": "gpt-4o-mini",
"chat_id": "unique-chat-id"
}
```
**What it does:**
- Accepts natural language queries about the database
- Uses LangChain agents with SQL tools to generate and execute SQL queries
- Streams Server-Sent Events (SSE) with token-by-token AI responses
- Returns formatted results including tables, data, and SQL query information
- Supports multiple AI models (gpt-4o, gpt-4o-mini, gpt-3.5-turbo)
### GET `/api/v1/chat/suggestions`
Returns query suggestions based on the database schema.
**Response:**
```json
{
"suggestions": [
{
"description": "What are the top 5 expensive products?",
},
...
]
}
```
**What it does:**
- Analyzes the database schema to understand available tables and relationships
- Uses AI to generate 5 interesting and useful query suggestions
- All suggestions are read-only SELECT queries (no mutations)
- Helps users discover what they can ask about the database