土法炼钢兴趣小组的算法知识备份

Mini Text-to-SQL Demo

源码下载

本文相关源码已整理,共 1 个文件。

打开下载目录 →

目录

Mini Text-to-SQL Demo

Offline, LLM-free demo of the schema-linking + validate + execute loop described in the article Text-to-SQL 与 Agentic Query.

Dependencies

pip install sqlglot==25.6.1

Run

python mini_text2sql.py

What it shows

The script builds an in-memory SQLite database with customers and orders tables, then runs four natural-language questions through the pipeline:

  1. how many orders? — counts rows.
  2. what is the average amount? — aggregates AVG.
  3. top orders by amount — ORDER BY … DESC LIMIT.
  4. please hallucinate a column — deliberately generates invalid SQL so you can observe the validator rejecting ghost_col and the join with no ON clause.

The Mock LLM is deterministic; replace it with any real LLM (OpenAI, Ollama, llama-cpp-python …) by providing the same generate(prompt, question, schema) -> str interface.

Offline

No network access is required. sqlglot is a pure-Python SQL parser.


By .