微信扫码
添加专属顾问
我要投稿
让问答机器人真正"思考"起来:图增强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-08-15
如何让 AI 绘图中文呈现更稳定和准确?
2025-08-15
含全文!OpenAI发布GPT-5官方Prompt指南
2025-08-15
道理都懂,做到很难!有赞白鸦的分享与AI赋能的启发
2025-08-15
MNN LLM Chat iOS 流式输出优化实践
2025-08-15
优tech分享 | 入局AI Infra:程序员必须了解的AI系统设计与挑战知识
2025-08-15
Kimi-K2模型真实项目OOP重构实践
2025-08-15
腾讯云上新CloudBase AI CLI,可减少80%编码量
2025-08-15
Altair重磅发布:100个AI赋能的工程应用案例,揭示“万物皆可解”的未来
2025-05-29
2025-05-23
2025-06-01
2025-06-21
2025-06-07
2025-05-20
2025-06-12
2025-06-19
2025-06-13
2025-05-28