RepoNVIDIANVIDIApublished May 8, 2026seen Jun 26

NVIDIA/text2sql-active-learning

Python

Open original ↗

Captured source

source ↗
published May 8, 2026seen Jun 26captured Oct 17http 200method plain

NVIDIA/text2sql-active-learning

Description: SHARP: robust active learning for selecting which natural-language questions to annotate with SQL when building a few-shot example bank for text-to-SQL.

Language: Python

License: Apache-2.0

Stars: 1

Forks: 3

Open issues: 0

Created: 2026-05-08T18:33:39Z

Pushed: 2026-05-18T17:23:34Z

Default branch: main

Fork: no

Archived: no

README:

text2sql-active-learning

SHARP — Stratified Heteroscedastic Active Retrieval on a Partitioned Manifold

Code for the paper *"Robust Active Learning for Few-Shot Example Selection in Text-to-SQL"* (Arash Pourhabib, NVIDIA, 2026).

Overview

SHARP addresses the problem of selecting which natural language queries to annotate with SQL in order to build a high-quality few-shot example bank for text-to-SQL systems. It formalizes the selection as a constrained experimental design problem over the low-dimensional intrinsic manifold of semantic query embeddings, with:

  • Heteroscedastic noise modeling — σ²(x) estimated from LLM self-consistency; hard queries are down-weighted.
  • Partition matroid constraints — one annotated example per semantic cluster, enforcing diversity.
  • Graceful misspecification — the approximation guarantee degrades by an additive constant (not catastrophically) when the kernel is misspecified.

Repository Layout

.
├── pyproject.toml # Package metadata + console scripts
├── requirements.txt # Pinned runtime dependencies
├── example_config.json # Template for domain configuration (--config flag)
├── LICENSE # Apache 2.0
├── README.md
├── src/text2sql_active_learning/
│ ├── __init__.py
│ ├── run_experiment.py # Main entry point — runs all experiments and produces paper figures
│ ├── gp_active_learner.py # GP regression + Algorithm 1 (Stratified Greedy Heteroscedastic MI)
│ ├── embedding_kernel.py # Kernel implementations (SE, Matérn, RFF)
│ ├── sql_evaluator.py # SQL semantic evaluation (Component Match, SemanticScore)
│ ├── estimate_noise_prior.py# Pre-compute heteroscedastic noise priors via LLM self-consistency
│ └── replot.py # Regenerate figures from saved CSVs without re-running experiments
└── tests/
└── test_smoke.py

Installation

Editable install (recommended for development):

pip install -e .

This exposes three console scripts:

| Console script | Module entry point | |---------------------------------|-------------------------------------------------------| | text2sql-run-experiment | text2sql_active_learning.run_experiment:main | | text2sql-estimate-noise-prior | text2sql_active_learning.estimate_noise_prior:main | | text2sql-replot | text2sql_active_learning.replot:main |

You can also invoke modules directly:

python -m text2sql_active_learning.run_experiment --db milvus.db
python -m text2sql_active_learning.replot

Building Your Milvus Database

The experiments require a Milvus Lite database (a local SQLite-backed .db file). The database is not included in this repository — you must build your own from your domain's annotated query corpus.

Required collection schema

SQL examples collection (default name: text2sql_examples):

| Field | Type | Description | |------------|---------------|--------------------------------------------------| | id | int64 (PK) | Row identifier | | text | varchar | Natural language question | | sql | varchar | Ground-truth SQL query | | analysis | varchar | Domain/analysis-type label (used for clustering) | | vector | float_vector | Text embedding (e.g. d=2048) |

DDL collection (optional, default name: text2sql_ddl):

| Field | Type | Description | |-------|---------|--------------------------| | ddl | varchar | Table definition strings |

Example: populating the database

from pymilvus import MilvusClient, DataType

client = MilvusClient("milvus.db")

# Create the SQL examples collection
schema = client.create_schema(auto_id=False, enable_dynamic_field=False)
schema.add_field("id", DataType.INT64, is_primary=True)
schema.add_field("text", DataType.VARCHAR, max_length=2000)
schema.add_field("sql", DataType.VARCHAR, max_length=4000)
schema.add_field("analysis", DataType.VARCHAR, max_length=200)
schema.add_field("vector", DataType.FLOAT_VECTOR, dim=2048)

index_params = client.prepare_index_params()
index_params.add_index("vector", metric_type="L2")

client.create_collection("text2sql_examples", schema=schema, index_params=index_params)

# Insert your annotated examples
data = [
{"id": i, "text": q, "sql": s, "analysis": domain, "vector": embed(q)}
for i, (q, s, domain) in enumerate(your_corpus)
]
client.insert("text2sql_examples", data)

The embedding model used in the paper is nvidia/llama-3.2-nv-embedqa-1b-v2 (d=2048), available via the NVIDIA NIM API. Any sentence embedder works, as long as the vector dimension matches what you put in.

Domain Configuration

By default, domains and test-set sizes are auto-detected from your database:

  • seed_domain — the domain with the most examples (natural cold-start bias).
  • sim_test_sizes — ~20% of each domain's examples, capped to [4, 10].
  • llm_domains — all domains found in the DB.
  • llm_test_sizes — same formula applied to LLM domains.

To override any of these, create a JSON config file (use example_config.json as a template):

{
"seed_domain": "my_domain",
"sim_test_sizes": {"my_domain": 10, "other_domain": 8},
"llm_domains": ["my_domain"],
"llm_test_sizes": {"my_domain": 10}
}

All fields are optional — omit any field to fall back to auto-detection. Pass the file via --config:

text2sql-run-experiment --sim-only --db /path/to/milvus.db --config my_config.json
text2sql-estimate-noise-prior --db /path/to/milvus.db --config my_config.json

Running the Experiments

All scripts accept --db, --collection, and --ddl-collection arguments. You can also set them via environment variables or a .env file in the working directory:

MILVUS_DB_PATH=/path/to/milvus.db
MILVUS_COLLECTION=text2sql_examples
MILVUS_DDL_COLLECTION=text2sql_ddl

Retrieval simulation (fast, no LLM, ~2 min)

Evaluates all four methods using oracle SemanticScore retrieval. No API key required.

text2sql-run-experiment --sim-only --db /path/to/milvus.db

End-to-end LLM evaluation (~25 min)

Requires an LLM API key. Set NGC_API_KEY for the NVIDIA...

Excerpt shown — open the source for the full document.