Schema Tools 提供两个主要功能:
pip install asyncpg asyncio
Schema Tools 使用项目现有的 LLM 配置,无需额外配置数据库连接。
python -m schema_tools \
--db-connection <数据库连接字符串> \
--table-list <表清单文件> \
--business-context <业务上下文> \
[可选参数]
参数 | 说明 | 示例 |
---|---|---|
--db-connection |
PostgreSQL数据库连接字符串 | postgresql://user:pass@localhost:5432/dbname |
--table-list |
表清单文件路径 | ./tables.txt |
--business-context |
业务上下文描述 | "高速公路服务区管理系统" |
参数 | 说明 | 默认值 |
---|---|---|
--output-dir |
输出目录路径 | training/generated_data |
--pipeline |
处理链类型 | full |
--max-concurrent |
最大并发表数量 | 3 |
--verbose |
启用详细日志 | False |
--log-file |
日志文件路径 | 无 |
--no-filter-system-tables |
禁用系统表过滤 | False |
--check-permissions-only |
仅检查数据库权限 | False |
python -m schema_tools \
--db-connection "postgresql://postgres:postgres@localhost:6432/highway_db" \
--table-list ./schema_tools/tables.txt \
--business-context "高速公路服务区管理系统"
python -m schema_tools \
--db-connection "postgresql://postgres:postgres@localhost:6432/highway_db" \
--table-list ./schema_tools/tables.txt \
--business-context "高速公路服务区管理系统" \
--output-dir ./output \
--verbose
python -m schema_tools \
--db-connection "postgresql://postgres:postgres@localhost:6432/highway_db" \
--table-list ./schema_tools/tables.txt \
--business-context "高速公路服务区管理系统" \
--pipeline ddl_only
python -m schema_tools \
--db-connection "postgresql://postgres:postgres@localhost:6432/highway_db" \
--check-permissions-only
创建一个文本文件(如 tables.txt
),每行一个表名:
# 这是注释行
public.bss_service_area
public.bss_company
bss_car_day_count # 默认为public schema
hr.employees # 指定schema
生成的文件都放在输出目录下(不创建子目录):
output/
├── bss_service_area.ddl # DDL文件
├── bss_service_area_detail.md # MD文档
├── bss_company.ddl
├── bss_company_detail.md
├── filename_mapping.txt # 文件名映射
└── logs/ # 日志目录
└── schema_tools_20240123.log
必须先执行DDL和MD文档生成,确保输出目录中有完整的DDL和MD文件。
python -m schema_tools.qs_generator \
--output-dir <输出目录> \
--table-list <表清单文件> \
--business-context <业务上下文> \
[可选参数]
参数 | 说明 | 示例 |
---|---|---|
--output-dir |
包含DDL和MD文件的目录 | ./output |
--table-list |
表清单文件路径(用于验证) | ./tables.txt |
--business-context |
业务上下文描述 | "高速公路服务区管理系统" |
参数 | 说明 | 默认值 |
---|---|---|
--db-name |
数据库名称(用于文件命名) | db |
--verbose |
启用详细日志 | False |
--log-file |
日志文件路径 | 无 |
python -m schema_tools.qs_generator \
--output-dir ./output \
--table-list ./schema_tools/tables.txt \
--business-context "高速公路服务区管理系统" \
--db-name highway_db
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>_<时间戳>_pair.json
output/
├── qs_highway_db_20240123_143052_pair.json # 最终结果
├── qs_intermediate_20240123_143052.json # 中间结果(成功后自动删除)
└── qs_recovery_20240123_143052.json # 恢复文件(异常中断时生成)
[
{
"question": "按服务区统计每日营收趋势(最近30天)?",
"sql": "SELECT service_name AS 服务区, oper_date AS 营业日期, SUM(pay_sum) AS 每日营收 FROM bss_business_day_data WHERE oper_date >= CURRENT_DATE - INTERVAL '30 day' AND delete_ts IS NULL GROUP BY service_name, oper_date ORDER BY 营业日期 ASC;"
},
{
"question": "哪个服务区的车流量最大?",
"sql": "SELECT service_area_id, SUM(customer_count) AS 总车流量 FROM bss_car_day_count WHERE delete_ts IS NULL GROUP BY service_area_id ORDER BY 总车流量 DESC LIMIT 1;"
}
]
配置文件位于 schema_tools/config.py
:
# DDL/MD生成相关配置
"output_directory": "training/generated_data", # 输出目录
"create_subdirectories": False, # 不创建子目录
"max_concurrent_tables": 3, # 最大并发数
"sample_data_limit": 20, # 数据采样量
"filter_system_tables": True, # 过滤系统表
"continue_on_error": True, # 错误后继续
# Question-SQL生成配置
"qs_generation": {
"max_tables": 20, # 最大表数量限制
"theme_count": 5, # 主题数量
"questions_per_theme": 10, # 每主题问题数
"max_concurrent_themes": 3, # 并行主题数
"continue_on_theme_error": True, # 主题失败继续
"save_intermediate": True, # 保存中间结果
}
可以通过编辑 schema_tools/config.py
文件来修改默认配置。
错误信息:
表数量(25)超过限制(20)。请分批处理或调整配置中的max_tables参数。
解决方案:
config.py
中增加 max_tables
限制错误信息:
DDL文件数量(5)与表数量(6)不一致
解决方案:
可能原因:
解决方案:
错误信息:
数据库查询权限不足
解决方案:
--check-permissions-only
检查权限Schema Tools会自动检测大表(超过100万行)并使用智能采样策略:
目前生成的SQL使用PostgreSQL语法。如果需要其他数据库语法:
第一步:生成DDL和MD文档
python -m schema_tools --db-connection "..." --table-list tables.txt --business-context "..." --output-dir ./output
根据需要手动调整
第三步:生成Question-SQL
python -m schema_tools.qs_generator --output-dir ./output --table-list tables.txt --business-context "..."