Skip to content

知识图谱

知识图谱是一种结构化的知识表示方法,用于存储实体之间的关系。

概述

知识图谱以图结构存储知识,节点表示实体,边表示实体之间的关系。

构建知识图谱

使用 NetworkX

python
import networkx as nx
import matplotlib.pyplot as plt

# 创建图
G = nx.Graph()

# 添加节点
G.add_node("RAG", label="检索增强生成", color="#3b82f6")
G.add_node("向量数据库", label="向量数据库", color="#8b5cf6")
G.add_node("语言模型", label="语言模型", color="#10b981")

# 添加边
G.add_edge("RAG", "向量数据库", label="使用")
G.add_edge("RAG", "语言模型", label="结合")

# 可视化
pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=True, node_color=[G.nodes[n]['color'] for n in G.nodes])
plt.show()

使用 PyTorch Geometric

python
import torch
from torch_geometric.data import Data

# 创建图数据
edge_index = torch.tensor([
    [0, 1, 0, 2],  # 源节点
    [1, 0, 2, 0]   # 目标节点
], dtype=torch.long)

x = torch.tensor([
    [1.0, 0.0],  # RAG
    [0.0, 1.0],  # 向量数据库
    [0.5, 0.5]   # 语言模型
], dtype=torch.float)

data = Data(x=x, edge_index=edge_index)

知识图谱查询

SPARQL 查询

sparql
# 查询 RAG 相关的实体
SELECT ?relatedEntity ?relationship
WHERE {
    :RAG ?relationship ?relatedEntity .
}

Cypher 查询

cypher
// 查询与 RAG 相关的实体
MATCH (:RAG)-[r]->(related)
RETURN related.name, type(r)

Python API

python
# 查询实体关系
def find_related_entities(G, entity):
    neighbors = list(G.neighbors(entity))
    relationships = []
    
    for neighbor in neighbors:
        edge_data = G.get_edge_data(entity, neighbor)
        relationships.append({
            "entity": neighbor,
            "relationship": edge_data.get("label", "相关")
        })
    
    return relationships

# 查询 RAG 相关实体
related = find_related_entities(G, "RAG")
print(related)

知识图谱与 RAG 结合

检索增强

python
def rag_with_knowledge_graph(query, vector_db, knowledge_graph):
    # 1. 从向量数据库检索
    vector_results = vector_db.query(query_texts=[query])
    
    # 2. 从知识图谱检索
    graph_results = knowledge_graph.query(query)
    
    # 3. 合并结果
    combined_results = vector_results + graph_results
    
    # 4. 构建上下文
    context = "\n\n".join(combined_results)
    
    # 5. 生成回答
    answer = llm.generate(context + "\n\n问题:" + query)
    
    return answer

推理增强

python
def reasoning_with_knowledge_graph(query, knowledge_graph):
    # 解析问题
    entities = extract_entities(query)
    
    # 构建推理路径
    paths = []
    for entity in entities:
        paths.extend(knowledge_graph.find_paths(entity))
    
    # 生成推理回答
    answer = llm.generate("根据以下知识图谱路径回答问题:\n" + str(paths) + "\n\n问题:" + query)
    
    return answer

应用场景

问答系统

python
# 基于知识图谱的问答
def kg_qa(query, knowledge_graph):
    # 提取实体
    entities = extract_entities(query)
    
    # 查询知识图谱
    results = knowledge_graph.query(entities)
    
    # 构建回答
    answer = summarize_results(results)
    
    return answer

推荐系统

python
# 基于知识图谱的推荐
def recommend(user_id, knowledge_graph):
    # 获取用户兴趣
    interests = knowledge_graph.get_user_interests(user_id)
    
    # 查找相关实体
    recommendations = []
    for interest in interests:
        related = knowledge_graph.find_related(interest)
        recommendations.extend(related)
    
    return recommendations[:10]

知识补全

python
# 知识图谱补全
def complete_knowledge_graph(knowledge_graph, new_facts):
    for fact in new_facts:
        # 检查是否存在
        if not knowledge_graph.has_fact(fact):
            # 添加新事实
            knowledge_graph.add_fact(fact)
    
    # 推理新关系
    new_relationships = knowledge_graph.infer_relationships()
    knowledge_graph.add_relationships(new_relationships)

优势

  1. 结构化表示: 清晰的知识结构
  2. 推理能力: 支持逻辑推理
  3. 可解释性: 透明的推理过程
  4. 知识复用: 便于知识共享和复用