声明类型推断验证状态待复核
客服主管面临图谱问答依赖人工查资料——KGQA将答案响应从10分压到1分,年化省14万元
Skill-KGQA-Question-Answering
客服主管面临图谱问答依赖人工查资料——KGQA将答案响应从10分压到1分,年化省14万元
建立品类知识图谱,清晰看到哪些是入口品/引流品/利润品,指导下一步选品扩张方向
待企业数据验证 · 缺失:样本、基线、时间窗、币种、计算方法、证据来源
客服主管面临图谱问答依赖人工查资料——KGQA将答案响应从10分压到1分,年化省14万元
论文:UniKGQA: Unified Question Answering over Knowledge Graphs | 年份:2021
核心问题:构建了产品知识图谱后,如何让非技术人员(运营、客服、业务方)用自然语言查询它?KGQA把"图谱查询"从SPARQL/Cypher简化为人话。
技术架构:
用户提问 → 实体链接 → 关系预测 → 子图检索 → 答案生成
↓ ↓ ↓ ↓ ↓
自然语言 识别提到的 预测需要的 在图谱中 整理为
实体 关系路径 查询子图 自然语言关键步骤:
1. 实体链接(Entity Linking)
爱他美3段和DHA节点2. 关系预测(Relation Prediction)
contains关系suitable_for关系3. 子图检索(Subgraph Retrieval)
contains → ? → 检索所有成分4. 答案生成(Answer Generation)
2025年前沿:LLM + KG融合
反直觉洞察:
业务问题:客服每天回答重复问题:"这款奶粉含DHA吗?"、"3段和2段有什么区别?"、"这款吸奶器适合背奶妈妈吗?"——答案都在知识图谱里,但客服需要手动查。
2. 用户提问:"爱他美3段含有哪些成分?"
3. KGQA处理: - 实体链接:`爱他美3段` - 关系预测:`contains` - 子图检索:`(爱他美3段, contains, ?)` - 答案生成:`"爱他美3段含有DHA、益生菌、GOS/FOS益生元组合"`
未提取到输入条目;请核对原始 Skill 卡片与代码。
代码块数量:3
"""
Knowledge Graph Question Answering (KGQA) — 知识图谱问答
支持:实体链接、关系预测、子图检索、答案生成
"""
import re
from typing import List, Dict, Tuple, Optional
from collections import defaultdict
class SimpleKGQA:
"""简化版KGQA系统"""
def __init__(self):
self.kg = {} # {entity: {relation: [target_entities]}}
self.entity_aliases = {} # {alias: canonical_name}
self.relation_patterns = {
'contains': r'含有什么|成分|包含|有什么',
'suitable_for': r'适合谁|适用|几岁|多大',
'brand': r'什么品牌|牌子|哪个品牌',
'price': r'多少钱|价格|售价',
'compare': r'区别|差异|对比|和.*有什么',
}
def add_fact(self, head: str, relation: str, tail: str):
"""添加知识图谱三元组"""
if head not in self.kg:
self.kg[head] = defaultdict(list)
self.kg[head][relation].append(tail)
def add_alias(self, alias: str, canonical: str):
"""添加实体别名"""
self.entity_aliases[alias.lower()] = canonical
def _link_entity(self, question: str) -> Optional[str]:
"""实体链接:从问题中找提到的实体"""
question_lower = question.lower()
# 先匹配别名
for alias, canonical in self.entity_aliases.items():
if alias in question_lower:
return canonical
# 再匹配主实体名
for entity in self.kg.keys():
if entity.lower() in question_lower:
return entity
return None
def _predict_relation(self, question: str) -> Optional[str]:
"""关系预测:判断用户问的是什么关系"""
for relation, pattern in self.relation_patterns.items():
if re.search(pattern, question.lower()):
return relation
# 默认:如果问题含"是""吗",可能是属性查询
if '是' in question or '吗' in question:
return 'contains'
return None
def _is_compare_question(self, question: str) -> bool:
"""判断是否是比较问题"""
return bool(re.search(self.relation_patterns['compare'], question.lower()))
def _extract_entities_for_compare(self, question: str) -> List[str]:
"""从比较问题中提取两个实体"""
found = []
for entity in self.kg.keys():
if entity.lower() in question.lower():
found.append(entity)
return found[:2]
def answer(self, question: str) -> Dict:
"""回答用户问题"""
result = {
'question': question,
'entity': None,
'relation': None,
'answer': None,
'confidence': 0.0
}
# 1. 判断是否是比较问题
if self._is_compare_question(question):
entities = self._extract_entities_for_compare(question)
if len(entities) == 2:
return self._compare_entities(entities[0], entities[1], question)
# 2. 实体链接
entity = self._link_entity(question)
if not entity:
result['answer'] = "抱歉,我没有找到相关产品信息。"
return result
result['entity'] = entity
# 3. 关系预测
relation = self._predict_relation(question)
if not relation:
result['answer'] = f"关于{entity},您可以问它的成分、适用年龄、品牌或价格。"
return result
result['relation'] = relation
# 4. 子图检索
if entity in self.kg and relation in self.kg[entity]:
targets = self.kg[entity][relation]
result['confidence'] = 0.95
# 5. 答案生成
if relation == 'contains':
result['answer'] = f"{entity}含有:{', '.join(targets)}。"
elif relation == 'suitable_for':
result['answer'] = f"{entity}适合:{', '.join(targets)}。"
elif relation == 'brand':
result['answer'] = f"{entity}的品牌是:{targets[0]}。"
elif relation == 'price':
result['answer'] = f"{entity}的价格是:{targets[0]}。"
else:
result['answer'] = f"{entity}的{relation}是:{', '.join(targets)}。"
else:
result['answer'] = f"关于{entity}的{relation}信息,我暂时没有记录。"
result['confidence'] = 0.3
return result
def _compare_entities(self, entity_a: str, entity_b: str, question: str) -> Dict:
"""对比两个实体"""
info_a = self.kg.get(entity_a, {})
info_b = self.kg.get(entity_b, {})
# 找共同关系和差异关系
all_relations = set(info_a.keys()) | set(info_b.keys())
comparison = []
for rel in all_relations:
vals_a = set(info_a.get(rel, []))
vals_b = set(info_b.get(rel, []))
common = vals_a & vals_b
only_a = vals_a - vals_b
only_b = vals_b - vals_a
if common:
comparison.append(f"两者都{rel}:{', '.join(common)}")
if only_a:
comparison.append(f"只有{entity_a}{rel}:{', '.join(only_a)}")
if only_b:
comparison.append(f"只有{entity_b}{rel}:{', '.join(only_b)}")
return {
'question': question,
'entity': f"{entity_a} vs {entity_b}",
'relation': 'compare',
'answer': '\n'.join(comparison) if comparison else f"{entity_a}和{entity_b}的对比信息不足。",
'confidence': 0.9
}
def build_baby_product_kgqa():
"""构建母婴产品KGQA示例"""
kgqa = SimpleKGQA()
# 添加知识
facts = [
('爱他美3段', 'contains', 'DHA'),
('爱他美3段', 'contains', '益生菌'),
('爱他美3段', 'contains', 'GOS/FOS'),
('爱他美3段', 'suitable_for', '12-36个月'),
('爱他美3段', 'brand', '爱他美'),
('爱他美3段', 'price', '$35'),
('美赞臣3段', 'contains', 'DHA'),
('美赞臣3段', 'contains', '乳铁蛋白'),
('美赞臣3段', 'suitable_for', '12-36个月'),
('美赞臣3段', 'brand', '美赞臣'),
('美赞臣3段', 'price', '$32'),
('Momcozy S12', 'brand', 'Momcozy'),
('Momcozy S12', 'price', '$120'),
('Momcozy S12', 'suitable_for', '背奶妈妈'),
('Momcozy S12', 'contains', '静音马达'),
]
for h, r, t in facts:
kgqa.add_fact(h, r, t)
# 添加别名
aliases = {
'aptamil 3': '爱他美3段',
'爱他美三段': '爱他美3段',
'美赞臣三段': '美赞臣3段',
'enfamil 3': '美赞臣3段',
's12': 'Momcozy S12',
}
for alias, canonical in aliases.items():
kgqa.add_alias(alias, canonical)
return kgqa
if __name__ == '__main__':
kgqa = build_baby_product_kgqa()
questions = [
"爱他美3段含有哪些成分?",
"美赞臣3段适合多大的宝宝?",
"爱他美3段和美赞臣3段有什么区别?",
"Momcozy S12多少钱?",
"Aptamil 3含DHA吗?",
]
for q in questions:
result = kgqa.answer(q)
print(f"\nQ: {q}")
print(f"A: {result['answer']}")
print("[✓] KGQA Question Answering 测试通过")论文链接用于核验方法来源,不自动证明本页商业结果。