0.1.1 暂存

This commit is contained in:
陈辅元
2026-04-14 10:28:22 +08:00
parent 4a0638ba2e
commit cc83fe963a
83 changed files with 4005 additions and 179 deletions
+50 -51
View File
@@ -95,26 +95,30 @@
```
text2sql_agent_camel/
├── agents/ # Agent 层
│ ├── orchestrator.py # 主编排器(协调 Schema Linker → Generator → Validator)
│ ├── schema_linker.py # 表筛选 Agent(粗筛 + LLM 精筛 + 外键扩展)
│ ├── sql_generator.py # SQL 生成 Agent(集成 Few-Shot 注入)
│ └── validator.py # SQL 验证 Agent(程序 + LLM 双重验证)
├── config/
│ ├── prompts.py # 系统与用户 Prompt 模板
│ └── settings.py # 配置类(基于 Pydantic)
├── schema/ # Schema 管理层
│ ├── models.py # 数据模型(Table, Column, ForeignKey, DatabaseSchema)
│ ├── loader.py # Schema 加载器(JSON / G3SB 格式解析)
│ ├── manager.py # Schema 管理器(查询、过滤、转字符串)
│ └── indexer.py # 向量索引构建器(ChromaDB 封装)
├── llm/
│ └── deepseek_client.py # DeepSeek API 客户端(chat / validate_sql / select_tables)
├── utils/
│ ├── embedding.py # Qwen3-Embedding 封装(本地 / 远程 API)
│ ├── sql_parser.py # sqlglot 工具(语法验证、方言转换、规范化)
│ ├── validators.py # 验证逻辑(危险操作检测、完整验证流水线)
│ └── fewshot_selector.py # Few-Shot 示例选择器(语义/关键词检索)
├── api_server.py # FastAPI NL 网关(根目录入口;启动前将 backend 加入 sys.path)
├── backend/ # Python 后端(除 api_server 外的编排与工具)
│ ├── main.py # CLI 入口(交互模式:python backend/main.py)
│ ├── nl_lite_store.py # 轻量会话/收藏内存存储(API 演示用)
│ ├── agents/ # Agent 层
│ │ ├── orchestrator.py # 主编排器(协调 Schema Linker → Generator → Validator)
│ │ ├── schema_linker.py # 表筛选 Agent(粗筛 + LLM 精筛 + 外键扩展)
│ │ ├── sql_generator.py # SQL 生成 Agent(集成 Few-Shot 注入)
│ │ └── validator.py # SQL 验证 Agent(程序 + LLM 双重验证)
│ ├── config/
│ │ ├── prompts.py # 系统与用户 Prompt 模板
│ │ └── settings.py # 配置类(基于 Pydantic)
│ ├── schema/ # Schema 管理层
│ │ ├── models.py # 数据模型(Table, Column, ForeignKey, DatabaseSchema)
│ │ ├── loader.py # Schema 加载器(JSON / G3SB 格式解析)
│ │ ├── manager.py # Schema 管理器(查询、过滤、转字符串)
│ │ └── indexer.py # 向量索引构建器(ChromaDB 封装)
│ ├── llm/
│ │ └── deepseek_client.py # DeepSeek API 客户端(chat / validate_sql / select_tables)
│ └── utils/
│ ├── embedding.py # Qwen3-Embedding 封装(本地 / 远程 API)
│ ├── sql_parser.py # sqlglot 工具(语法验证、方言转换、规范化)
│ ├── validators.py # 验证逻辑(危险操作检测、完整验证流水线)
│ └── fewshot_selector.py # Few-Shot 示例选择器(语义/关键词检索)
├── data/
│ ├── schemas/ # Schema JSON 文件
│ │ ├── G3SB_MCDataDictionary_table_structure.json # 表结构
@@ -134,7 +138,6 @@ text2sql_agent_camel/
├── logs/
│ └── text2sql.log # 运行日志(默认)
├── .env # 环境变量配置(API Key、路径、参数)
├── main.py # CLI 入口(单次查询 / 批量 / 交互模式)
├── pyproject.toml # 项目元数据与依赖
├── README.md # 本文档
└── SQL_GENERATION_LOGIC.md # SQL 生成逻辑详解(流程图、示例)
@@ -230,11 +233,14 @@ Schema JSON 格式示例:
### 4. 构建向量索引(首次运行)
```bash
# 自动构建:首次查询时会自动创建
python main.py "测试查询"
# 自动构建:首次在交互模式里提问时会自动创建
python backend/main.py
# 或手动预构建(推荐,加速首次查询)
# 或手动预构建(推荐,加速首次查询;请在仓库根目录执行)
python -c "
import sys
from pathlib import Path
sys.path.insert(0, str(Path.cwd() / 'backend'))
from agents.orchestrator import Text2SQLOrchestrator
from schema.manager import SchemaManager
@@ -247,30 +253,13 @@ orch.build_vector_index(force_rebuild=True)
### 5. 运行演示
```bash
# 单次查询(默认 T-SQL / SQL Server)
python main.py "查询2024年1月的销售额"
# 启动后进入交互式问答(默认 T-SQL / SQL Server)
python backend/main.py
# 交互模式
python main.py --interactive
# 批量查询(每行一个问题)
python main.py --batch queries.txt
# 指定 Schema 和方言
python main.py "统计每个市场的未结算交易量" \
--schema ./data/schemas/custom_schema.json \
--dialect postgresql
# 禁用 Few-Shot(对比实验)
python main.py "查询活跃账户数" --no-fewshot
# 调整 Few-Shot 参数
python main.py "查询2024年1月的销售额" \
--fewshot-top-k 5 \
--fewshot-min-rating 8
# 详细日志(调试用)
python main.py "查询持仓余额" --verbose
# 可选:启动时附带参数(均在进入交互前生效),例如指定 Schema、方言、Few-Shot、日志等
python backend/main.py --schema ./data/schemas/custom_schema.json --dialect postgresql --verbose
python backend/main.py --no-fewshot
python backend/main.py --fewshot-top-k 5 --fewshot-min-rating 8
```
## 🎯 使用示例
@@ -279,6 +268,12 @@ python main.py "查询持仓余额" --verbose
```python
import os
import sys
from pathlib import Path
# 在仓库根目录运行时,将 backend 加入模块搜索路径(请先 cd 到项目根)
sys.path.insert(0, str(Path.cwd() / "backend"))
from agents.orchestrator import Text2SQLOrchestrator
from schema.manager import SchemaManager
from llm.deepseek_client import DeepSeekConfig
@@ -376,7 +371,7 @@ VECTOR_DB_PATH=./data/embeddings/chroma
### CLI 参数速查
```bash
python main.py [问题] [选项]
python backend/main.py [选项]
# 核心选项
--schema PATH Schema 文件路径
@@ -398,8 +393,6 @@ python main.py [问题] [选项]
--embedding-model PATH Embedding 模型路径
# 其他
--interactive, -i 交互模式
--batch FILE 批量文件路径
--verbose, -v 详细日志
```
@@ -564,8 +557,11 @@ WHERE i.AccountID = 'ACC001'
# 检查索引文件
ls -la data/embeddings/chroma/
# 强制重建索引
# 强制重建索引(在仓库根目录执行)
python -c "
import sys
from pathlib import Path
sys.path.insert(0, str(Path.cwd() / 'backend'))
from agents.orchestrator import Text2SQLOrchestrator
from schema.manager import SchemaManager
@@ -587,6 +583,9 @@ orch.build_vector_index(force_rebuild=True)
```bash
# 检查数据文件
python -c "
import sys
from pathlib import Path
sys.path.insert(0, str(Path.cwd() / 'backend'))
from utils.fewshot_selector import FewShotSelector
s = FewShotSelector('./data/experiences/all_samples.jsonl')
print(f'样本数: {len(s.samples)}')