Commit 570281606296dc9a868ea51646ab06f042bae41a
1 parent
11f33a4d
Add viw_kg_form_neighbors: 1-hop subgraph per form (GraphRAG expand step)
Materialize each form's L2 neighbors (upstream/downstream documents) and L3 neighbors (referenced entity tables) so the retriever can expand a vector-recalled form-card into a subgraph context via a single JOIN. 702 forms have downstream, 476 upstream, 1467 reference entity tables.
Showing
2 changed files
with
50 additions
and
4 deletions
sql/README.md
| ... | ... | @@ -33,14 +33,17 @@ mysql --no-defaults -h127.0.0.1 -P33307 -uroot -plocal xlyweberp_saas |
| 33 | 33 | 1597 边;目标多为 `ele*`/`sis*` 主数据字典。`bTgtMaster` 标记引用主数据。 |
| 34 | 34 | 5. **`viw_kg_form_card`** — 每表单一段可向量化的结构化卡片文本 `sCard`(合并节点+上下游+字段中文名)。 |
| 35 | 35 | 6. **`viw_kg_field_dict`** — 中文名→字段→表 术语字典(38342 条),带外键 join 提示,供 NL2SQL 接地。 |
| 36 | -7. **`export_form_cards.sh`** — 导出 1748 张卡片为 JSONL(供灌向量库)。生成文件 gitignore,按需重跑。 | |
| 36 | +7. **`viw_kg_form_neighbors`** — 每表单的 1-hop 子图(上/下游单据 + 引用实体表),GraphRAG 的「图扩展」步物化。 | |
| 37 | +8. **`export_form_cards.sh`** — 导出 1748 张卡片为 JSONL(供灌向量库)。生成文件 gitignore,按需重跑。 | |
| 37 | 38 | |
| 38 | 39 | ## 一键重建 |
| 39 | 40 | |
| 40 | 41 | ```sh |
| 41 | -DB="mysql --no-defaults -h127.0.0.1 -P33307 -uroot -plocal xlyweberp_saas" | |
| 42 | -for v in viw_ai_useful_forms viw_kg_edge_flow viw_kg_form viw_kg_edge_ref viw_kg_form_card viw_kg_field_dict; do | |
| 43 | - $DB < "$v.sql" | |
| 42 | +# 注意:zsh 不对未加引号的变量做分词,用数组 | |
| 43 | +DB=(mysql --no-defaults -h127.0.0.1 -P33307 -uroot -plocal xlyweberp_saas) | |
| 44 | +for v in viw_ai_useful_forms viw_kg_edge_flow viw_kg_form viw_kg_edge_ref \ | |
| 45 | + viw_kg_form_card viw_kg_field_dict viw_kg_form_neighbors; do | |
| 46 | + "${DB[@]}" < "$v.sql" | |
| 44 | 47 | done |
| 45 | 48 | ./export_form_cards.sh form_cards.jsonl # → 1748 行 JSONL |
| 46 | 49 | ``` | ... | ... |
sql/viw_kg_form_neighbors.sql
0 → 100644
| 1 | +-- ============================================================ | |
| 2 | +-- 视图:viw_kg_form_neighbors —— ERP 知识图谱·表单一跳子图 (GraphRAG 扩展步) | |
| 3 | +-- 每个表单一行,物化它的 1-hop 邻域:下游单据 / 上游单据 / 引用的实体表。 | |
| 4 | +-- 这是 GraphRAG 检索的「图扩展」步骤:向量召回一张表单卡片后,直接 JOIN 本视图 | |
| 5 | +-- 取出它的 L2(单据流转) + L3(实体引用) 邻居,拼成子图上下文喂给 LLM。 | |
| 6 | +-- | |
| 7 | +-- L2 下游 sDownIds/sDownForms = 本单据可生成的目标单据(viw_kg_edge_flow, src侧, 非自环) | |
| 8 | +-- L2 上游 sUpForms = 可生成本单据的上游单据(edge_flow, tgt侧) | |
| 9 | +-- L3 引用 sRefTables = 本表数据源引用到的实体/主数据表(viw_kg_edge_ref, 去重) | |
| 10 | +-- | |
| 11 | +-- 节点粒度 = 表单 sFormId (1748)。L2 按菜单粒度,L3 按数据源表粒度。 | |
| 12 | +-- 表内 GROUP_CONCAT 邻居列表都很短(下游≤5, 引用≤18),默认 max_len 足够。 | |
| 13 | +-- ============================================================ | |
| 14 | +CREATE OR REPLACE VIEW viw_kg_form_neighbors AS | |
| 15 | +SELECT | |
| 16 | + fm.sFormId, | |
| 17 | + fm.sModuleId, | |
| 18 | + fm.sFormTitle, | |
| 19 | + fm.sDomain, | |
| 20 | + fm.sDataSource, | |
| 21 | + fm.iUpstream, | |
| 22 | + fm.iDownstream, | |
| 23 | + up.names AS sUpForms, -- 上游单据名 | |
| 24 | + dn.names AS sDownForms, -- 下游单据名 | |
| 25 | + dn.ids AS sDownIds, -- 下游单据 moduleId(便于程序继续扩展) | |
| 26 | + rf.tables AS sRefTables -- 引用的实体/主数据表 | |
| 27 | +FROM viw_kg_form fm | |
| 28 | +LEFT JOIN ( | |
| 29 | + SELECT sSrcModuleId AS mid, | |
| 30 | + GROUP_CONCAT(DISTINCT sTgtName ORDER BY sTgtName SEPARATOR ',') AS names, | |
| 31 | + GROUP_CONCAT(DISTINCT sTgtModuleId SEPARATOR ',') AS ids | |
| 32 | + FROM viw_kg_edge_flow WHERE bSelfLoop = 0 GROUP BY sSrcModuleId | |
| 33 | +) dn ON dn.mid = fm.sModuleId | |
| 34 | +LEFT JOIN ( | |
| 35 | + SELECT sTgtModuleId AS mid, | |
| 36 | + GROUP_CONCAT(DISTINCT sSrcName ORDER BY sSrcName SEPARATOR ',') AS names | |
| 37 | + FROM viw_kg_edge_flow WHERE bSelfLoop = 0 GROUP BY sTgtModuleId | |
| 38 | +) up ON up.mid = fm.sModuleId | |
| 39 | +LEFT JOIN ( | |
| 40 | + SELECT sSrcTable, | |
| 41 | + GROUP_CONCAT(DISTINCT sTgtTable ORDER BY sTgtTable SEPARATOR ',') AS tables | |
| 42 | + FROM viw_kg_edge_ref GROUP BY sSrcTable | |
| 43 | +) rf ON rf.sSrcTable = LOWER(fm.sDataSource); | ... | ... |