微信扫码
添加专属顾问
我要投稿
让问答机器人真正"思考"起来:图增强RAG技术让中文问答更智能、更透明。核心内容: 1. 传统RAG的局限性及图增强方案的优势 2. 从文本分片到知识图谱构建的技术实现路径 3. 可视化检索链路带来的可解释性提升
想象一下,你的问答机器人不再是“关键词搬运工”,而是个真正会“思考”的小伙伴:它能把相关知识串成思路,再用轻松的口吻给你讲清楚。以下这套基于 Graph RAG(Graph-Enhanced Retrieval-Augmented Generation)的方案,既保留了检索效率,又增强了逻辑可解释性,适合中文内容库。
def chunk_text(text, chunk_size=800, overlap=150):
chunks = []
for i in range(0, len(text), chunk_size - overlap):
part = text[i : i + chunk_size]
if part:
chunks.append({
"id": len(chunks),
"text": part
})
return chunks
将长文本分成大约 800 字的小块,重叠一部分保证上下文连贯。
def extract_concepts(text):
prompt = f"请列出下面这段中文的 5–8 个核心关键词或实体:\n{text[:200]}…"
resp = client.chat.completions.create(
model="chatglm-6b",
messages=[{"role":"user","content":prompt}],
temperature=0
)
return [c.strip() for c in resp.choices[0].message.content.split(",")]
给每个片段贴上“标签”,让不同内容靠“共同标签”连接。
import networkx as nx
from scipy.spatial.distance import cosine
def build_graph(chunks, embeddings, concept_lists):
G = nx.Graph()
for idx, chunk in enumerate(chunks):
G.add_node(idx, text=chunk["text"],
emb=embeddings[idx],
concepts=concept_lists[idx])
for i in range(len(chunks)):
for j in range(i+1, len(chunks)):
shared = set(concept_lists[i]) & set(concept_lists[j])
if not shared: continue
sim = 1 - cosine(embeddings[i], embeddings[j])
conc_score = len(shared) / min(len(concept_lists[i]), len(concept_lists[j]))
weight = 0.7 * sim + 0.3 * conc_score
if weight > 0.6:
G.add_edge(i, j, weight=weight)
return G
边的权重由“向量相似度”与“概念重叠度”共同决定,过滤掉微弱关联。
import heapq
def traverse_graph(query_emb, G, top_k=3, max_depth=2):
sims = [(1 - cosine(query_emb, G.nodes[n]["emb"]), n) for n in G.nodes]
sims.sort(reverse=True)
heap = [(-s, n, 0) for s, n in sims[:top_k]]
heapq.heapify(heap)
seen, result = set(), []
while heap:
score, node, depth = heapq.heappop(heap)
if node in seen or depth > max_depth: continue
seen.add(node)
result.append(( -score, node ))
for nbr, data in G[node].items():
next_score = -score * data["weight"]
heapq.heappush(heap, (next_score, nbr, depth + 1))
return [n for _, n in sorted(result, reverse=True)][: top_k * 2]
先选几个相关度最高的“起点”,再沿着图里的边“多跳”扩展,平衡深度和效率。
def generate_reply(query, selected_chunks):
context = "\n\n---\n\n".join(c["text"] for c in selected_chunks)
prompt = f"以下是检索到的上下文:\n{context}\n\n请用轻松自然的口吻回答:{query}"
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role":"user","content":prompt}],
temperature=0.3
)
return resp.choices[0].message.content
把摘出的关键片段拼起来,让大模型在“实材实料”的上下文中输出答案。
效果:
用 Graph RAG,不只是让问答系统“能答”,还能让它在知识间“自如穿梭”,从此告别“答案卡壳”的尴尬。动手试试,让你的中文问答更有温度、更顺畅!
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费场景POC验证,效果验证后签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2025-07-01
从8万+数据源提炼洞察,ChatGPT+Zilliz +LangChain如何成创新药研发新范式
2025-07-01
MPC安全之魂:承诺方案技术深度解析
2025-07-01
一文解读小白怎么快速搭建一个基于MCP协议的AI agent应用
2025-07-01
ZeroSearch:在不进行搜索的情况下激励大语言模型的搜索能力
2025-07-01
AI Agent 的发展:能力、技术架构和软硬件形态
2025-07-01
腾讯大模型应用演进之路:从 RAG 到 MCP 的技术实践
2025-07-01
从理论到应用:AI搜索MCP的最佳实践案例解析
2025-07-01
巨头混战Agent,押注背后是真未来还是新泡沫?
2025-05-29
2025-04-11
2025-04-12
2025-04-06
2025-04-29
2025-04-12
2025-04-29
2025-04-17
2025-05-07
2025-05-07
2025-07-01
2025-07-01
2025-07-01
2025-07-01
2025-06-30
2025-06-30
2025-06-30
2025-06-27