
簡介
向量搜尋根據語義進行搜索,但它不理解實體之間的關係。當問題需要多跳推理時,向量搜尋常常會失敗。
例如: “專案經理 Alpha 的老闆是誰?”
- 向量搜尋找到包含「Alpha專案」的段落→知道PM是Minh
- 但我找不到“Minh 的老闆”,因為它在另一個區塊中,語義上不相關
- 知識圖譜:
Minh --[quản_lý]--> Alpha,Hùng --[quản_lý]--> Minh→ 立即回覆!
本文涵蓋:
- 知識圖譜 — 從文件建立實體關係圖
- Graph RAG——結合圖+向量進行多跳推理
- Microsoft GraphRAG — 框架生產就緒
1.基礎知識圖譜
1.1 概念
Knowledge Graph = Đồ thị gồm:
- Nodes (entities): Người, Địa điểm, Dự án, Phòng ban...
- Edges (relationships): quản_lý, thuộc_về, làm_việc_tại...
Ví dụ:
[Minh] ──quản_lý──→ [Dự án Alpha]
[Minh] ──thuộc_về──→ [Phòng IT]
[Hùng] ──quản_lý──→ [Minh]
[Hùng] ──thuộc_về──→ [Ban Giám đốc]
[Dự án Alpha] ──sử_dụng──→ [Python]
[Dự án Alpha] ──deadline──→ [2025-06-30]
1.2 從文中擷取知識圖譜
"""Dùng LLM để trích xuất entities và relationships"""
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
EXTRACT_PROMPT = ChatPromptTemplate.from_messages([
("system", """Trích xuất entities và relationships từ đoạn văn.
Output dạng JSON:
{{
"entities": [
{{"name": "...", "type": "Person|Org|Project|Location|Tech"}},
],
"relationships": [
{{"source": "...", "relation": "...", "target": "..."}},
]
}}"""),
("human", "{text}"),
])
text = """Minh là Project Manager của dự án Alpha, thuộc phòng IT.
Dự án Alpha sử dụng Python và PostgreSQL, deadline 30/6/2025.
Minh báo cáo trực tiếp cho Giám đốc Hùng."""
result = (EXTRACT_PROMPT | llm).invoke({"text": text})
print(result.content)
# {
# "entities": [
# {"name": "Minh", "type": "Person"},
# {"name": "Alpha", "type": "Project"},
# {"name": "Phòng IT", "type": "Org"},
# {"name": "Hùng", "type": "Person"},
# {"name": "Python", "type": "Tech"},
# {"name": "PostgreSQL", "type": "Tech"}
# ],
# "relationships": [
# {"source": "Minh", "relation": "quản_lý", "target": "Alpha"},
# {"source": "Minh", "relation": "thuộc_về", "target": "Phòng IT"},
# {"source": "Alpha", "relation": "sử_dụng", "target": "Python"},
# {"source": "Alpha", "relation": "sử_dụng", "target": "PostgreSQL"},
# {"source": "Minh", "relation": "báo_cáo", "target": "Hùng"}
# ]
# }
1.3 存到 Neo4j
"""Lưu Knowledge Graph vào Neo4j"""
from neo4j import GraphDatabase
driver = GraphDatabase.driver(
"bolt://localhost:7687",
auth=("neo4j", "password")
)
def create_graph(entities, relationships):
with driver.session() as session:
# Tạo nodes
for entity in entities:
session.run(
"MERGE (n:{type} {{name: $name}})".format(type=entity["type"]),
name=entity["name"]
)
# Tạo edges
for rel in relationships:
session.run(
"""MATCH (a {{name: $source}}), (b {{name: $target}})
MERGE (a)-[:{relation}]->(b)""".format(relation=rel["relation"]),
source=rel["source"],
target=rel["target"]
)
# Query: "Ai quản lý dự án Alpha?"
result = session.run("""
MATCH (person)-[:quản_lý]->(project {name: 'Alpha'})
RETURN person.name
""")
# → "Minh"
# Multi-hop: "Sếp của người quản lý dự án Alpha?"
result = session.run("""
MATCH (boss)-[:quản_lý]->(manager)-[:quản_lý]->(project {name: 'Alpha'})
RETURN boss.name
""")
# → "Hùng" ← Vector search KHÔNG THỂ trả lời!
💡練習 1: 從一段文字(至少 10 個實體)中擷取知識圖。保存到 Neo4j。試試 3 道多跳問題。
2. Graph RAG — 結合圖 + 向量
2.1 架構
User Query
│
┌───────────┼───────────┐
│ │
┌───────┴───────┐ ┌───────┴───────┐
│ Vector Store │ │ Knowledge │
│ (semantic) │ │ Graph (Neo4j)│
└───────┬───────┘ └───────┬───────┘
│ │
Semantic chunks Graph traversal
(context rộng) (quan hệ chính xác)
│ │
└───────────┬───────────┘
│
Merge context
│
LLM → Answer
2.2 LangChain + Neo4j實現
"""Graph RAG: kết hợp Neo4j graph + Chroma vector"""
from langchain_community.graphs import Neo4jGraph
from langchain.chains import GraphCypherQAChain
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o", temperature=0)
# Neo4j graph
graph = Neo4jGraph(
url="bolt://localhost:7687",
username="neo4j",
password="password",
)
# GraphCypherQAChain: LLM tự viết Cypher query
graph_chain = GraphCypherQAChain.from_llm(
llm=llm,
graph=graph,
verbose=True,
)
# Query multi-hop
result = graph_chain.invoke(
"Liệt kê tất cả tech stack mà team của Hùng sử dụng?"
)
# LLM tự generate Cypher:
# MATCH (Hùng {name:'Hùng'})-[:quản_lý]->(person)
# -[:quản_lý]->(project)-[:sử_dụng]->(tech)
# RETURN DISTINCT tech.name
2.3 混合:圖上下文+向量上下文
"""Kết hợp graph traversal + vector search"""
def hybrid_graph_rag(question, graph_chain, vector_retriever, llm):
# 1. Graph context (quan hệ, facts)
try:
graph_context = graph_chain.invoke(question)["result"]
except Exception:
graph_context = "Không tìm thấy thông tin trong graph."
# 2. Vector context (nội dung chi tiết)
vector_docs = vector_retriever.invoke(question)
vector_context = "\n".join([d.page_content for d in vector_docs])
# 3. Merge và trả lời
prompt = f"""Dựa trên thông tin sau, trả lời câu hỏi.
**Thông tin từ Knowledge Graph:**
{graph_context}
**Thông tin từ tài liệu:**
{vector_context}
**Câu hỏi:** {question}
**Trả lời:**"""
return llm.invoke(prompt).content
3.微軟GraphRAG
3.1 GraphRAG 架構
Microsoft GraphRAG 自動化了整個管道:
Documents → Entity Extraction → Community Detection → Summarization
│ │ │
Entities & Groups of related Summary per
Relationships entities (Leiden) community
│ │ │
└───────────────────────┼────────────────────┘
│
Query modes:
Local search │ Global search
(specific) │ (broad themes)
3.2 設定 Microsoft GraphRAG
# Cài đặt
pip install graphrag
# Init project
graphrag init --root ./my-rag-project
# Cấu hình settings.yaml
# - llm: model, api_key
# - embeddings: model
# - chunks: size, overlap
"""Index tài liệu"""
# graphrag index --root ./my-rag-project
# → Tự extract entities, build graph, detect communities, summarize
"""Query"""
# Local search: tìm thông tin cụ thể
# graphrag query --root ./my-rag-project --method local \
# --query "Ai quản lý dự án Alpha?"
# Global search: tổng hợp theo chủ đề
# graphrag query --root ./my-rag-project --method global \
# --query "Tóm tắt các dự án đang triển khai và tech stack?"
3.3 本機搜尋與全域搜尋
| 模式 | 它是如何運作的 | 何時使用 |
|---|---|---|
| 本地 | 尋找相關實體→遍歷圖→上下文 | 具體的、事實性的問題 |
| 全球 | 使用社區摘要 → 總結 | 一般性問題,專題 |
4. 比較 GraphRAG 與 Vector RAG
4.1 基準
| 標準 | 向量抹布 | 圖 RAG | 混合動力 |
|---|---|---|---|
| 單跳查詢 | ⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐⭐ |
| 多跳查詢 | ⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
| 總結 | ⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ |
| 設定複雜性 | ⭐(簡單) | ⭐⭐⭐(複雜) | ⭐⭐⭐⭐ |
| 索引成本 | 低 | 曹(法學碩士摘錄) | 曹 |
| 查詢延遲 | 快 | 慢一點 | 平均 |
4.2 何時使用Graph RAG?
✅ Dùng Graph RAG khi:
- Câu hỏi multi-hop, cần suy luận qua nhiều entities
- Tài liệu có nhiều quan hệ phức tạp (org chart, supply chain)
- Cần tổng hợp theo chủ đề (global search)
- Domain có entity types rõ ràng (legal, medical, HR)
❌ KHÔNG cần Graph RAG khi:
- Câu hỏi đơn giản, 1-hop
- Tài liệu ít quan hệ (blog posts, FAQ)
- Budget thấp (indexing tốn nhiều LLM calls)
- Latency-critical (graph query chậm hơn vector)
💡練習 2: 使用 Microsoft GraphRAG 索引文件資料夾。針對 5 個特定問題 + 5 個一般問題,比較本地搜尋與全域搜尋結果。
總結
| 概念 | 記住 |
|---|---|
| 知識圖譜 | 實體關係圖,適合多跳 |
| 實體擷取 | 使用LLM從文字擷取實體+關係 |
| Neo4j | 圖資料庫,使用Cypher查詢 |
| GraphCypherQAChain | LLM 寫自己的 Cypher 查詢 |
| 微軟GraphRAG | 框架汽車:提取→社區→總結 |
| 本地與全球 | 本地 = 具體事實,全球 = 主題 |
| 混合 | 圖形事實 + 向量上下文 = 最佳結果 |
一般練習
- ✅ 完成 2 個小練習 (1, 2)
- 完整的圖管道: 從 10 多個文件建立自己的知識圖。使用LLM擷取實體→儲存Neo4j→實現GraphCypherQAChain→測試10個多跳句子。
- 混合系統: 建構組合系統:Neo4j 圖 + Chroma 向量。路由器根據問題類型自動選擇資料來源。與純向量 RAG 比較精度。
- 視覺化: 從 Neo4j 匯出圖形 → 使用 NetworkX 或 Neo4j 瀏覽器進行視覺化。螢幕截圖 1 子圖至少有 20 個節點。
下一篇文章: 多模式 RAG — 處理文件中的圖像、表格、圖表 — RAG 不僅僅適用於文字。