Here's a structured breakdown of the data flow with request and response bodies for the DRF AI Server in LangSQL. This will help us understand how data moves between the Frontend → Express Server → DRF AI Server 🚀
Since the DRF Server is responsible for AI processing (not query execution), it will receive relevant requests from the Express Server, process them, and return responses.
- Triggered When: A user enters a query in the UI
- Request: Sent from Frontend → Express Server
📤 Request (Frontend → Express Server)
POST /api/generate-query
Content-Type: application/json
Authorization: Bearer <JWT Token>
{
"query_text": "Get all users who signed up last month",
"database_id": "db_12345"
}- Actions:
- Express validates JWT and extracts
user_id - It fetches database metadata (schema, table details) from MongoDB
- Forwards the request to DRF for AI-powered SQL generation
- Express validates JWT and extracts
📤 Request (Express → DRF AI Server)
POST /api/ai/generate-query
Content-Type: application/json
Authorization: Bearer <ServiceAuthToken>
{
"query_text": "Get all users who signed up last month",
"user_id": "user_789",
"database_metadata": {
"db_name": "users_db",
"tables": [
{
"name": "users",
"columns": ["id", "name", "email", "created_at"]
}
]
}
}- Actions:
- Parses query intent using NLP
- Uses Vector Search to understand past queries
- Fetches Database Schema & context
- Generates a SQL Query using the AI model
📤 Response (DRF AI Server → Express Server)
{
"generated_sql": "SELECT id, name, email FROM users WHERE created_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH);",
"confidence_score": 0.92
}📤 Response (Express → Frontend)
{
"query": "SELECT id, name, email FROM users WHERE created_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH);",
"metadata": {
"confidence": 0.92
}
}- DRF Server uses ServiceAuthToken to accept requests from Express
- Express verifies JWT but does not send it to DRF
- MongoDB stores:
- User Query Logs
- Model Feedback
- Query Results (for AI fine-tuning)
- NLP-Based Parsing
- Database-Aware Query Generation
- Vector Embeddings for Query Context
✅ Frontend → Express: User submits query
✅ Express → DRF: Express validates & forwards with metadata
✅ DRF AI Server: Generates SQL using AI
✅ DRF → Express → Frontend: Query returned
This ensures context-aware AI query generation while keeping authentication & logging separate 🔥
Let's walk through the data flow with demo data to better understand how the entire process works.
- A user submits a natural language query asking for users who signed up last month.
- The Express Server forwards the query and related metadata to the DRF AI Server for processing.
- The DRF AI Server uses AI to convert the query into a valid SQL statement and returns it back to the Express Server, which then forwards the response to the Frontend.
The user submits a natural language query via the frontend:
POST /api/generate-query
Content-Type: application/json
Authorization: Bearer <JWT Token>
{
"query_text": "Get all users who signed up last month",
"database_id": "users_db"
}Here:
query_text: User's natural language querydatabase_id: The ID for the database to be used (in this case, theusers_db)
The Express Server receives this request and validates the JWT token.
After validating the JWT token and ensuring the user is authorized, Express Server sends the data to the DRF AI Server:
POST /api/ai/generate-query
Content-Type: application/json
Authorization: Bearer <ServiceAuthToken>
{
"query_text": "Get all users who signed up last month",
"user_id": "user_789",
"database_metadata": {
"db_name": "users_db",
"tables": [
{
"name": "users",
"columns": ["id", "name", "email", "created_at"]
}
]
}
}Here:
query_text: The user's query asking for users who signed up last monthuser_id: The user making the requestdatabase_metadata: Metadata about the database that contains the table and columns the AI will use to generate the query
The DRF AI Server receives this information and begins the AI processing.
On the DRF AI Server, the AI model processes the request. Here's how it works:
- The AI understands that the user is asking for a query to fetch users who signed up last month.
- It identifies the relevant table (
users) and columns (id,name,email,created_at). - The AI uses NLP to break down the query and maps it to the SQL query.
Example logic:
- Input Query: "Get all users who signed up last month"
- Extracted Information:
- Table:
users - Columns:
id,name,email,created_at - Condition:
created_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH)
- Table:
The AI generates the following SQL query:
SELECT id, name, email FROM users WHERE created_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH);The AI also calculates a confidence score for how accurate the generated SQL is. Let's assume the AI model returned a confidence score of 0.92.
Once the AI generates the SQL, the DRF AI Server sends a response back to the Express Server:
{
"generated_sql": "SELECT id, name, email FROM users WHERE created_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH);",
"confidence_score": 0.92
}- generated_sql: The SQL query generated by the AI
- confidence_score: The AI's confidence in the generated SQL query (from 0 to 1, where 1 means very confident)
Finally, the Express Server forwards the response to the Frontend, so that the user sees the generated SQL query.
{
"query": "SELECT id, name, email FROM users WHERE created_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH);",
"metadata": {
"confidence": 0.92
}
}Here:
query: The SQL query generated by the AImetadata: Contains additional information about the query, like the confidence score
The Frontend receives this response and displays the SQL query to the user.
-
Frontend → Express Server
- User submits a query to the Express Server.
{ "query_text": "Get all users who signed up last month", "database_id": "users_db" } -
Express Server → DRF AI Server
- Express validates JWT and forwards the query and database metadata to the DRF AI Server.
{ "query_text": "Get all users who signed up last month", "user_id": "user_789", "database_metadata": { "db_name": "users_db", "tables": [ { "name": "users", "columns": ["id", "name", "email", "created_at"] } ] } } -
DRF AI Server → Express Server
- DRF processes the query, generates the SQL, and sends it back to Express with a confidence score.
{ "generated_sql": "SELECT id, name, email FROM users WHERE created_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH);", "confidence_score": 0.92 } -
Express Server → Frontend
- Express forwards the generated SQL to the frontend for display.
{ "query": "SELECT id, name, email FROM users WHERE created_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH);", "metadata": { "confidence": 0.92 } }
- Frontend asks the question.
- Express Server validates and forwards the request.
- DRF AI Server processes the query using AI and generates the SQL.
- Express Server sends the result back to the frontend.
This should give you a good understanding of how data flows with requests and responses. Would you like to explore any specific part further?