Skip to content

Latest commit

 

History

History
316 lines (241 loc) · 8.75 KB

File metadata and controls

316 lines (241 loc) · 8.75 KB

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 🚀


🌟 Data Flow in LangSQL

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.


🔄 Request-Response Flow

1️⃣ User Input from Frontend

  • 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"
}

2️⃣ Express Server Validates & Forwards to DRF AI Server

  • 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

📤 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"]
      }
    ]
  }
}

3️⃣ DRF AI Server Processes Query

  • 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
}

4️⃣ Express Server Returns Final Response to Frontend

📤 Response (Express → Frontend)

{
  "query": "SELECT id, name, email FROM users WHERE created_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH);",
  "metadata": {
    "confidence": 0.92
  }
}

🔧 Configuration of DRF AI Server

1️⃣ Authentication

  • DRF Server uses ServiceAuthToken to accept requests from Express
  • Express verifies JWT but does not send it to DRF

2️⃣ Storage & Logging

  • MongoDB stores:
    • User Query Logs
    • Model Feedback
    • Query Results (for AI fine-tuning)

3️⃣ AI Processing

  • NLP-Based Parsing
  • Database-Aware Query Generation
  • Vector Embeddings for Query Context

🔹 Summary

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.

Scenario:

  • 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.

Step-by-Step Demo with Data Flow:


1️⃣ Frontend Request to Express Server

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 query
  • database_id: The ID for the database to be used (in this case, the users_db)

The Express Server receives this request and validates the JWT token.


2️⃣ Express Server Sends Data to DRF AI Server

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 month
  • user_id: The user making the request
  • database_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.


3️⃣ AI Processing in DRF AI Server

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)

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.


4️⃣ DRF AI Server Responds Back to Express Server

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)

5️⃣ Express Server Forwards Data to Frontend

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 AI
  • metadata: Contains additional information about the query, like the confidence score

The Frontend receives this response and displays the SQL query to the user.


💡 Complete Data Flow Overview with Demo Data

  1. 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"
    }
  2. 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"]
          }
        ]
      }
    }
  3. 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
    }
  4. 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
      }
    }

Summary of Key Points:

  • 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?