Question-SQL生成功能是Schema Tools的扩展模块,用于从已生成的DDL和MD文件自动生成高质量的Question-SQL训练数据对。
python -m schema_tools \
  --db-connection "postgresql://postgres:postgres@localhost:6432/highway_db" \
  --table-list ./schema_tools/tables.txt \
  --business-context "高速公路服务区管理系统" \
  --output-dir ./output \
  --pipeline full \
  --verbose
在output目录下检查:
python -m schema_tools.qs_generator \
  --output-dir ./output \
  --table-list ./schema_tools/tables.txt \
  --business-context "高速公路服务区管理系统" \
  --db-name highway_db \
  --verbose
qs_<db_name>_<timestamp>_pair.json格式:
[
{
"question": "业务问题描述?",
"sql": "SELECT ... FROM ... WHERE ...;"
}
]
metadata.txtCREATE TABLE IF NOT EXISTS metadata ( id SERIAL PRIMARY KEY, topic_name VARCHAR(100) NOT NULL, description TEXT, related_tables TEXT[], keywords TEXT[], focus_areas TEXT[], created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
INSERT INTO metadata(topic_name, description, related_tables, keywords, focus_areas) VALUES ( '日营业数据分析', '基于 bss_business_day_data 表,分析每个服务区和档口每天的营业收入、订单数量、支付方式等', '{bss_business_day_data,bss_branch,bss_service_area}', '{收入,订单,支付方式,日报表}', '{收入趋势,服务区对比,支付方式分布}' ); ```
qs_intermediate_<timestamp>.json在schema_tools/config.py中的qs_generation部分:
"qs_generation": {
    "max_tables": 20,              # 最大表数量限制
    "theme_count": 5,              # 生成主题数量
    "questions_per_theme": 10,     # 每主题问题数
    "max_concurrent_themes": 1,    # 并行处理主题数
    "continue_on_theme_error": True,  # 主题失败是否继续
    "save_intermediate": True,     # 是否保存中间结果
}
A: 系统会自动去重,并在日志中报告:
表清单去重统计: 原始11个表,去重后8个表,移除了3个重复项
A: 检查日志中的详细信息:
缺失的DDL文件对应的表: bss_company, bss_service_area
缺失的MD文件对应的表: bss_company
A: 可以直接在PostgreSQL中执行:
psql -U postgres -d your_database -f output/metadata.txt
A: 查看output目录下的qs_intermediate_*.json文件,其中包含已完成的主题结果。