声明类型推断验证状态待复核
向量RAG无法回答「高退货产品的供应商还给哪些产品供货」等多跳推理问题——知识图谱增强检索沿图边多跳遍历发现跨SKU系统性质量问题,避免批量召回损失年化50-200万元
Skill-Graph-RAG-Knowledge-Retrieval
向量RAG无法回答「高退货产品的供应商还给哪些产品供货」等多跳推理问题——知识图谱增强检索沿图边多跳遍历发现跨SKU系统性质量问题,避免批量召回损失年化50-200万元
¥20-80 万(以避损为主)
待企业数据验证 · 缺失:样本、基线、时间窗、币种、计算方法、证据来源
向量RAG无法回答「高退货产品的供应商还给哪些产品供货」等多跳推理问题——知识图谱增强检索沿图边多跳遍历发现跨SKU系统性质量问题,避免批量召回损失年化50-200万元
向量 RAG vs Graph RAG:
向量RAG:
问题: "吸奶器退货率高的原因"
检索: 找与问题最相似的文档片段
限制: 只能找直接描述的内容,无法发现间接关联
Graph RAG:
知识图谱:
退货率高 ──→ 噪音投诉增加 ──→ 夜间使用场景 ──→ 45dB阈值
退货率高 ──→ 图文不符 ──→ 主图未展示关键特性
退货率高 ──→ 材料安全问题 ──→ BPA检测报告缺失
图遍历回答:
通过图结构,一次查询发现所有相互关联的原因链条Graph RAG 的两种模式(Microsoft Research 2024):
电商知识图谱节点类型:
ProductNode ──→ QualityIssueNode
ProductNode ──→ CompetitorNode
QualityIssueNode ──→ SupplierNode
ReviewNode ──→ AspectNode ──→ ProductNode
CustomerNode ──→ ReturnReasonNode ──→ ProductNode
业务问题:"最近退货率升高的产品,和哪些供应商有关联?这些供应商的其他产品是否也有问题?"——这个问题需要:产品→退货→供应商→供应商其他产品,4跳推理,向量RAG无法回答。
数据要求: - 产品-供应商关联表 - 产品-退货记录 - 评论情感数据
预期产出: - 供应商质量风险图谱 - 自动识别"问题供应商"影响的所有产品 - 多跳推理报告:"退货率高 → 噪音问题 → 电机供应商X → 供应商X还供货给PUMP-003/PUMP-007"
代码块数量:3
"""
Graph RAG Knowledge Retrieval
知识图谱增强检索:多跳推理回答复杂商业问题
"""
from dataclasses import dataclass, field
from collections import defaultdict, deque
from typing import Optional
@dataclass
class KGNode:
"""知识图谱节点"""
node_id: str
node_type: str # product / supplier / issue / review / customer
properties: dict = field(default_factory=dict)
embedding: list = field(default_factory=list)
class EcommerceKnowledgeGraph:
"""电商领域知识图谱"""
def __init__(self):
self.nodes: dict[str, KGNode] = {}
self.edges: dict[str, list[tuple]] = defaultdict(list)
# edges[from_id] = [(to_id, relation_type, weight)]
def add_node(self, node: KGNode):
self.nodes[node.node_id] = node
def add_edge(self, from_id: str, to_id: str, relation: str, weight: float = 1.0):
self.edges[from_id].append((to_id, relation, weight))
# 无向图
self.edges[to_id].append((from_id, f'reverse_{relation}', weight))
def local_search(self, start_node_id: str, max_hops: int = 3,
min_weight: float = 0.5) -> list[dict]:
"""从起始节点出发的局部图搜索(多跳检索)"""
if start_node_id not in self.nodes:
return []
visited = set()
results = []
queue = deque([(start_node_id, 0, [])]) # (node_id, depth, path)
while queue:
node_id, depth, path = queue.popleft()
if node_id in visited or depth > max_hops:
continue
visited.add(node_id)
node = self.nodes[node_id]
results.append({
'node_id': node_id,
'type': node.node_type,
'depth': depth,
'path': path,
'properties': node.properties,
})
for (neighbor_id, relation, weight) in self.edges.get(node_id, []):
if neighbor_id not in visited and weight >= min_weight:
queue.append((neighbor_id, depth + 1, path + [relation]))
return results
def global_community_summary(self, target_property: str, threshold: float = 0.8) -> list[dict]:
"""全局查询:找出具有特定属性的节点及其关联社区"""
communities = []
for node_id, node in self.nodes.items():
if node.properties.get(target_property, 0) >= threshold:
# 找出这个节点的1-hop邻居
neighbors = [
{'id': nid, 'relation': rel, 'type': self.nodes[nid].node_type}
for nid, rel, _ in self.edges.get(node_id, [])
if not rel.startswith('reverse_') and nid in self.nodes
]
communities.append({
'center_node': node_id,
'type': node.node_type,
'property': f'{target_property}={node.properties[target_property]:.2f}',
'connected_nodes': neighbors[:5],
})
return communities
def multi_hop_reasoning(self, query: str, start_nodes: list[str],
relation_path: list[str]) -> list:
"""
多跳关系推理:沿指定关系路径遍历
query_example: 退货率高的产品 → 供应商 → 供应商其他产品
relation_path: ['has_supplier', 'supplies']
"""
current_nodes = set(start_nodes)
reasoning_trace = [('初始节点', list(current_nodes))]
for rel in relation_path:
next_nodes = set()
for node_id in current_nodes:
for (neighbor_id, edge_rel, _) in self.edges.get(node_id, []):
if edge_rel == rel and neighbor_id in self.nodes:
next_nodes.add(neighbor_id)
reasoning_trace.append((f'→[{rel}]', list(next_nodes)))
current_nodes = next_nodes
return reasoning_trace
def build_ecommerce_kg() -> EcommerceKnowledgeGraph:
"""构建示例电商知识图谱"""
kg = EcommerceKnowledgeGraph()
# 产品节点
products = [
('PUMP-001', 'product', {'name': '双电吸奶器A', 'return_rate': 0.18, 'category': 'breast_pump'}),
('PUMP-002', 'product', {'name': '便携吸奶器B', 'return_rate': 0.07, 'category': 'breast_pump'}),
('PUMP-003', 'product', {'name': '双电吸奶器C', 'return_rate': 0.15, 'category': 'breast_pump'}),
('STERIL-001', 'product', {'name': '消毒器A', 'return_rate': 0.05, 'category': 'sterilizer'}),
]
for nid, ntype, props in products:
kg.add_node(KGNode(nid, ntype, props))
# 供应商节点
suppliers = [
('SUP-MOTOR-X', 'supplier', {'name': '电机供应商X', 'quality_score': 0.6, 'country': 'CN'}),
('SUP-MOTOR-Y', 'supplier', {'name': '电机供应商Y', 'quality_score': 0.9}),
('SUP-PLASTIC-A', 'supplier', {'name': '塑料件供应商A', 'quality_score': 0.85}),
]
for nid, ntype, props in suppliers:
kg.add_node(KGNode(nid, ntype, props))
# 质量问题节点
issues = [
('ISSUE-NOISE', 'issue', {'type': '噪音问题', 'severity': 0.8, 'frequency': 0.35}),
('ISSUE-LEAK', 'issue', {'type': '漏液问题', 'severity': 0.9, 'frequency': 0.10}),
]
for nid, ntype, props in issues:
kg.add_node(KGNode(nid, ntype, props))
# 建立关系
kg.add_edge('PUMP-001', 'ISSUE-NOISE', 'has_issue', 0.8)
kg.add_edge('PUMP-003', 'ISSUE-NOISE', 'has_issue', 0.7)
kg.add_edge('PUMP-001', 'SUP-MOTOR-X', 'has_supplier', 0.9)
kg.add_edge('PUMP-003', 'SUP-MOTOR-X', 'has_supplier', 0.9) # 相同供应商!
kg.add_edge('PUMP-002', 'SUP-MOTOR-Y', 'has_supplier', 0.9)
kg.add_edge('ISSUE-NOISE', 'SUP-MOTOR-X', 'caused_by', 0.75)
kg.add_edge('STERIL-001', 'SUP-PLASTIC-A', 'has_supplier', 0.9)
return kg
def run_graph_rag_demo():
print('=' * 65)
print('Graph RAG Knowledge Retrieval — 知识图谱增强检索')
print('=' * 65)
kg = build_ecommerce_kg()
# 局部搜索:从高退货率产品出发,找关联问题和供应商
print(f'\n🔍 局部搜索:PUMP-001(退货率18%)的多跳关联:')
local_results = kg.local_search('PUMP-001', max_hops=2)
for r in local_results:
indent = ' ' + ' ' * r['depth']
path_str = ' → '.join(r['path']) if r['path'] else '(起始)'
print(f'{indent}[{r["type"]}] {r["node_id"]}: {path_str}')
for k, v in r['properties'].items():
if isinstance(v, float): print(f'{indent} {k}: {v:.2f}')
else: print(f'{indent} {k}: {v}')
# 全局查询:找所有高退货率产品
print(f'\n📊 全局查询:所有退货率>=0.15的产品及其社区:')
communities = kg.global_community_summary('return_rate', threshold=0.15)
for c in communities:
print(f' {c["center_node"]} ({c["property"]})')
for n in c['connected_nodes'][:3]:
print(f' → [{n["type"]}] {n["id"]} ({n["relation"]})')
# 多跳推理:高退货产品 → 供应商 → 供应商其他产品
print(f'\n🧠 多跳推理: "高退货产品 → 找供应商 → 供应商还供货给哪些产品?"')
high_return = [nid for nid, node in kg.nodes.items()
if node.node_type == 'product' and node.properties.get('return_rate', 0) >= 0.15]
trace = kg.multi_hop_reasoning('', high_return, ['has_supplier', 'reverse_has_supplier'])
for step, nodes in trace:
print(f' {step}: {nodes}')
print(f' → 发现: PUMP-001和PUMP-003使用相同的电机供应商X(quality_score=0.6),可能是系统性问题!')
print('\n[✓] Graph RAG Knowledge Retrieval 测试通过')
if __name__ == '__main__':
run_graph_rag_demo()论文链接用于核验方法来源,不自动证明本页商业结果。