This application allows users to generate SQL queries using natural language input. It connects to a database (or uses a mock database) and provides an interface for users to ask questions in plain English, which are then converted into SQL queries and executed against the database.
- Natural language to SQL query conversion
- Database connection management
- Mock database option for testing
- Real-time query results display
-
Frontend:
- Next.js for server-side rendering and API routes
- Tailwind CSS for styling
- Shadcn/UI for components
-
Backend:
- FastAPI for handling database connections and text-to-SQL conversion
- Python for backend logic
- OpenAI for text-to-SQL conversion
-
API Routes:
db-structure/route.ts
: Next.js API route for fetching database structureproxy/route.ts
: Next.js API route for communicating with FastAPI backend- FastAPI backend: Handles database connections and text-to-SQL conversion
-
Clone the repository:
git clone https://github.com/CubeStar1/text-to-sql.git cd text-to-sql
-
Install frontend dependencies:
npm install
-
Set up frontend environment variables: Create a
.env.local
file in the root directory and add:API_URL=http://localhost:8000 # FastAPI backend URL
-
Install backend dependencies:
- Clone the backend repository:
git clone https://github.com/CubeStar1/N2SQL-API.git cd N2SQL-API
-
Install backend dependencies:
pip install -r requirements.txt
-
Set up backend environment variables: Create a
.env
file in theN2SQL-API
directory and add necessary variables.OPENAI_API_KEY=<your-openai-api-key>
-
Start the FastAPI backend:
cd N2SQL-API uvicorn main:app --reload
-
In a new terminal, start the Next.js frontend:
npm run dev
-
Open http://localhost:3000 in your browser.
-
Choose to use a mock database or connect to your own database.
-
If using your own database, enter the connection details.
-
Click "Connect" to establish a database connection.
-
Enter a natural language query in the text area.
-
Click "Generate SQL" to convert your query to SQL and execute it.
-
View the results displayed below the query input.