Schema Tools 提供两个主要功能:
pip install asyncpg asyncio
Schema Tools 使用项目现有的 LLM 配置,无需额外配置数据库连接。
SchemaWorkflowOrchestrator
是 Schema Tools 的核心组件,提供端到端的自动化处理流程:
python -m schema_tools.schema_workflow_orchestrator \
--db-connection "postgresql://user:pass@localhost:5432/dbname" \
--table-list tables.txt \
--business-context "高速公路服务区管理系统" \
--db-name highway_db \
--output-dir ./output
python -m schema_tools.schema_workflow_orchestrator \
--db-connection "postgresql://user:pass@localhost:5432/dbname" \
--table-list tables.txt \
--business-context "电商系统" \
--db-name ecommerce_db \
--skip-validation
python -m schema_tools.schema_workflow_orchestrator \
--db-connection "postgresql://user:pass@localhost:5432/dbname" \
--table-list tables.txt \
--business-context "管理系统" \
--db-name management_db \
--disable-llm-repair
python -m schema_tools.schema_workflow_orchestrator \
--db-connection "postgresql://user:pass@localhost:5432/dbname" \
--table-list tables.txt \
--business-context "业务系统" \
--db-name business_db \
--no-modify-file
import asyncio
from schema_tools.schema_workflow_orchestrator import SchemaWorkflowOrchestrator
async def run_complete_workflow():
# 创建工作流编排器
orchestrator = SchemaWorkflowOrchestrator(
db_connection="postgresql://user:pass@localhost:5432/dbname",
table_list_file="tables.txt",
business_context="高速公路服务区管理系统",
db_name="highway_db",
output_dir="./output",
enable_sql_validation=True, # 启用SQL验证
enable_llm_repair=True, # 启用LLM修复
modify_original_file=True # 修改原始JSON文件
)
# 执行完整工作流程
report = await orchestrator.execute_complete_workflow()
# 处理结果
if report["success"]:
print(f"✅ 工作流程执行成功!")
print(f"📄 最终输出文件: {report['final_outputs']['primary_output_file']}")
print(f"❓ 最终问题数量: {report['final_outputs']['final_question_count']}")
print(f"⏱️ 总耗时: {report['performance_metrics']['total_duration']} 秒")
else:
print(f"❌ 工作流程执行失败: {report['error']['message']}")
print(f"💥 失败步骤: {report['error']['failed_step']}")
# 运行工作流程
asyncio.run(run_complete_workflow())
参数 | 说明 | 默认值 |
---|---|---|
--db-connection |
数据库连接字符串 | 必需 |
--table-list |
表清单文件路径 | 必需 |
--business-context |
业务上下文描述 | 必需 |
--db-name |
数据库名称(用于文件命名) | 必需 |
--output-dir |
输出目录 | ./output |
--skip-validation |
跳过SQL验证步骤 | False (默认执行SQL验证) |
--disable-llm-repair |
禁用LLM修复功能 | False (默认启用LLM修复) |
--no-modify-file |
不修改原始JSON文件 | False (默认修改原文件) |
--verbose |
启用详细日志 | False |
--log-file |
日志文件路径 | 无 |
工作流编排器会生成详细的执行报告,包括:
{
"success": True,
"workflow_summary": {
"total_duration": 245.67,
"completed_steps": ["ddl_md_generation", "question_sql_generation", "sql_validation"],
"total_steps": 3
},
"processing_results": {
"ddl_md_generation": {
"total_tables": 8,
"processed_successfully": 8,
"duration": 89.23
},
"question_sql_generation": {
"total_questions": 50,
"total_themes": 5,
"duration": 123.45
},
"sql_validation": {
"success_rate": 0.94,
"valid_sql_count": 47,
"invalid_sql_count": 3,
"duration": 32.99
}
},
"final_outputs": {
"primary_output_file": "./output/qs_highway_db_20240123_143052_pair.json",
"final_question_count": 47
},
"performance_metrics": {
"step1_duration": 89.23,
"step2_duration": 123.45,
"step3_duration": 32.99,
"total_duration": 245.67
}
}
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 |
最大并发表数量 | 1 |
--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
生成Question-SQL对后,可以使用SQL验证功能。注意:命令行使用时,默认启用LLM修复和文件修改功能。
python -m schema_tools.sql_validator \
--db-connection "postgresql://user:pass@localhost:5432/dbname" \
--input-file ./qs_highway_db_20240123_143052_pair.json \
--output-dir ./validation_reports
参数 | 说明 | 默认值 |
---|---|---|
--db-connection |
数据库连接字符串 | 必需 |
--input-file |
Question-SQL文件路径 | 必需 |
--output-dir |
验证报告输出目录 | 输入文件同目录 |
--disable-llm-repair |
禁用LLM修复功能 | False (默认启用修复) |
--no-modify-file |
不修改原始JSON文件 | False (默认修改原文件) |
--max-concurrent |
最大并发验证数 | 5 |
--batch-size |
批处理大小 | 10 |
--timeout |
单个验证超时时间(秒) | 30 |
--verbose |
启用详细日志 | False |
--dry-run |
仅解析文件不执行验证 | False |
--save-json |
保存详细JSON报告 | False |
# 基本验证(默认:启用LLM修复和文件修改)
python -m schema_tools.sql_validator \
--db-connection "postgresql://user:pass@localhost:5432/dbname" \
--input-file ./data.json
# 仅生成报告,不修改文件
python -m schema_tools.sql_validator \
--db-connection "postgresql://user:pass@localhost:5432/dbname" \
--input-file ./data.json \
--no-modify-file
# 启用文件修改,但禁用LLM修复(仅删除无效SQL)
python -m schema_tools.sql_validator \
--db-connection "postgresql://user:pass@localhost:5432/dbname" \
--input-file ./data.json \
--disable-llm-repair
# 性能调优参数
python -m schema_tools.sql_validator \
--db-connection "postgresql://user:pass@localhost:5432/dbname" \
--input-file ./data.json \
--max-concurrent 10 \
--batch-size 20 \
--timeout 60 \
--verbose
qs_<db_name>_<时间戳>_pair.json
output/
├── qs_highway_db_20240123_143052_pair.json # 最终结果
├── qs_highway_db_20240123_143052_pair.json.backup # 原文件备份(如果启用文件修改)
├── qs_intermediate_20240123_143052.json # 中间结果(成功后自动删除)
├── qs_recovery_20240123_143052.json # 恢复文件(异常中断时生成)
├── sql_validation_20240123_150000_summary.txt # SQL验证报告
└── file_modifications_20240123_150000.log # 文件修改日志(如果启用文件修改)
[
{
"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": 1, # 最大并发数(避免LLM并发问题)
"sample_data_limit": 20, # 数据采样量
"large_table_threshold": 1000000, # 大表阈值(100万行)
"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": 1, # 并行主题数(避免LLM并发问题)
"continue_on_theme_error": True, # 主题失败继续
"save_intermediate": True, # 保存中间结果
}
# SQL验证配置
"sql_validation": {
"max_concurrent_validations": 5, # 并发验证数
"validation_timeout": 30, # 单个验证超时(秒)
"batch_size": 10, # 批处理大小
"enable_sql_repair": False, # SQL修复功能(命令行覆盖为True)
"modify_original_file": False, # 文件修改功能(命令行覆盖为True)
"readonly_mode": True, # 启用只读模式
}
可以通过编辑 schema_tools/config.py
文件来修改默认配置。
错误信息:
表数量(25)超过限制(20)。请分批处理或调整配置中的max_tables参数。
解决方案:
config.py
中增加 max_tables
限制错误信息:
DDL文件数量(5)与表数量(6)不一致
解决方案:
可能原因:
解决方案:
错误信息:
数据库查询权限不足
解决方案:
--check-permissions-only
检查权限Schema Tools会自动检测大表(超过100万行)并使用智能采样策略:
SELECT * FROM table LIMIT N
获取前N行TABLESAMPLE SYSTEM
进行随机采样(失败时回退到OFFSET采样)大表阈值:默认为100万行(可在config.py中修改large_table_threshold
)
目前生成的SQL使用PostgreSQL语法。如果需要其他数据库语法:
Q: 工作流中途失败,如何恢复? A: 工作流编排器会保留已完成步骤的输出文件,可以手动从失败步骤开始重新执行。
Q: 如何只执行部分步骤?
A: 使用 --skip-validation
跳过SQL验证,或使用分步执行方式调用各个模块。
Q: 工作流执行时间过长怎么办? A: 可以通过减少表数量、调整并发参数、或分批处理来优化执行时间。
重要:SQL验证器的命令行模式与配置文件中的默认值不同:
enable_sql_repair=False
, modify_original_file=False
如需禁用,请明确使用 --disable-llm-repair
或 --no-modify-file
参数。
方式一:一键执行(推荐)
# 完整工作流程,一个命令搞定
python -m schema_tools.schema_workflow_orchestrator \
--db-connection "postgresql://user:pass@localhost:5432/dbname" \
--table-list tables.txt \
--business-context "高速公路服务区管理系统" \
--db-name highway_db \
--output-dir ./output
方式二:分步执行(调试时使用)
第一步:生成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 "..."
第四步:验证SQL(可选)
python -m schema_tools.sql_validator --db-connection "..." --input-file ./qs_xxx.json
--verbose
)观察执行过程--skip-validation
跳过验证步骤加快执行--no-modify-file
仅生成报告,手动审查后再决定是否修改