You can definitely address this problem by introducing a “table retrieval” or “table selection” step before generating the SQL. In your current pipeline, the model only “sees” the 30 preselected tables, so if the query is about a table outside that set then the whole system falls apart. Here are a few strategies you might consider:
1. Two-Stage Retrieval Process
a. Table Retrieval/Filtering Stage:
- Semantic Search: Instead of iterating over 1000+ tables one by one, prepare an indexed version of your table metadata (names, descriptions, perhaps even column names). You can embed this metadata into vectors using an embedding model.
- Vector Database: Load these embeddings into a vector search engine (like FAISS, Milvus, or Pinecone). When a user asks a question, embed the query to find the most semantically similar tables.
- Keyword/Rule-based Filtering: Alternatively, extract keywords from the query and perform a quick filtering using a traditional search or matching algorithm to shortlist tables that might be relevant.
b. SQL Generation Stage:
- Once you have a shortlist from the retrieval stage, feed the corresponding table schemas (or at least references) into your text-to-SQL model along with the query. This way, the model operates only over the relevant subset of tables, even if the ultimate candidate comes from outside your initial 30.
2. Leveraging a Local Smaller Model (e.g., Qwen3 0.6B)
Since you mentioned the possibility of running a local model like Qwen3 0.6B, you could use it specifically for the table retrieval/selection task. Here’s how:
-
Fine-Tune or Prompt-Engineer for Metadata Extraction:
Ask Qwen3 0.6B to extract entity mentions, keywords, or even table hints from the natural language query. Because a 0.6B model is small, you’d need to ensure that it’s well-tailored to your domain. You could optionally fine-tune it on examples where the input is a natural language question and the output is a list of candidate tables.
-
Local Evaluation:
Use Qwen3 0.6B to process the entire huge schema (or a pre-indexed summary of it) and return a list of candidate tables based on the query context. For performance reasons, you might preprocess the Excel sheet into a structured, smaller representation (e.g., a JSON file with table names and summaries).
-
Integrate into Your Pipeline:
The candidate table list from Qwen3 0.6B can then be combined with your initial 30 tables (if they overlap) and fed into the next stage where you generate SQL.
3. Additional Considerations
-
Hybrid Approaches:
You might combine a lightweight keyword extraction (possibly using simple NLP methods or a smaller model) with vector-based retrieval. For instance, first extract key domain terms from the query, then use those in a vector similarity search against your table metadata.
-
Caching and Preprocessing:
Since you have over 1000 tables, consider precomputing as much as possible. Build a robust index offline so that at runtime you are not parsing the entire Excel file. This drastically reduces latency and resource usage.
-
Feedback Loop:
As users interact with the system, log errors when the wrong tables are chosen. Use these examples to refine your retrieval process—either by improving the embedding space, adjusting retrieval thresholds, or updating the fine-tuning data for the local model.
-
Evaluation:
Ensure fidelity in mapping user questions to actual table content by testing against known queries. Validating the table selection is as important as the SQL generation part.
Summary
Yes, there is a practical way forward:
- Preprocess your large schema into an indexed or summarized form.
- Implement a retrieval layer that uses semantic vector search or fine-tuned local models (like Qwen3 0.6B) to quickly locate candidate tables relevant to the query.
- Pass the shortlisted table metadata along with the query to your SQL generation engine.
This two-stage approach allows you to work with a huge number of tables without overloading your core SQL generation model with irrelevant schema details. It also makes it easier to update or expand the table universe without having to retrain the entire system each time.
By setting up this intermediary retrieval mechanism, you can address the issue of “losing” topics that aren’t among your primary 30 tables.