feat: GraphRAG — LLM 抽三元组建 Neo4j 图谱 + 混合检索加图谱第三路
混合检索从 2 路(向量+全文)升级为 3 路(+图谱)。入库时 LLM 抽实体/关系建 Neo4j 图,检索时图谱路(实体关联三元组)融进 RRF;UI 可视化图谱。 - mcp-go rag: chat.go(OpenAI 兼容非流式 chat 客户端,抽取用) + graph.go(neo4j-go-driver 连接 + LLM 抽三元组 + MERGE 实体/关系 + 图谱召回/全量三元组) + rag.go(Config 结构; graph+chat 路;Ingest 加 抽实体/写Neo4j 阶段;Search 三路 RRF 融合;SetChat 热更新) - mcp-go: Neo4j env(默认 neo4j://localhost:7687, neo4j/sundynix);订阅 chat 控制面配置 (复用 DeepSeek 做抽取);新工具 kb_graph(返回三元组) - gateway: GET /api/v1/kb/graph;frontend KbView 知识图谱面板(实体—关系→实体) - 验证: 全模块 build✓ + e2e PASS; live——入库'sundynix用Milvus...'→DeepSeek 抽 4 三元组 →Neo4j(8 实体);检索三路融合 向量=4 全文=2 图谱=1;浏览器图谱面板渲染 4 三元组 - 边界: 实体链接用 CONTAINS 朴素匹配(可升级 LLM 查询实体抽取);全文/图谱重启随入库重建 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -105,6 +105,20 @@ export interface KbHit {
|
||||
score: number;
|
||||
}
|
||||
|
||||
export interface Triple {
|
||||
s: string;
|
||||
p: string;
|
||||
o: string;
|
||||
}
|
||||
|
||||
// graphKb: GET /api/v1/kb/graph —— 取某知识库的图谱三元组(→ mcp-go kb_graph,Neo4j)。
|
||||
export async function graphKb(kb: string): Promise<Triple[]> {
|
||||
const res = await fetch(`${GATEWAY}/api/v1/kb/graph?kb=${encodeURIComponent(kb)}`);
|
||||
const data = (await res.json()) as { triples?: Triple[]; error?: string };
|
||||
if (!res.ok) throw new Error(data.error ?? `graph failed: ${res.status}`);
|
||||
return data.triples ?? [];
|
||||
}
|
||||
|
||||
// searchKb: POST /api/v1/kb/search,检索台查询(→ mcp-go kb_search,带分数)。
|
||||
export async function searchKb(kb: string, q: string, topK = 5): Promise<KbHit[]> {
|
||||
const res = await fetch(`${GATEWAY}/api/v1/kb/search`, {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useRef, useState } from "react";
|
||||
import { ingestKb, ingestFile, streamIngest, searchKb, type IngestEvent, type KbHit } from "../lib/api";
|
||||
import { ingestKb, ingestFile, streamIngest, searchKb, graphKb, type IngestEvent, type KbHit, type Triple } from "../lib/api";
|
||||
|
||||
interface IngestLog {
|
||||
t: string;
|
||||
@@ -29,6 +29,15 @@ export function KbView() {
|
||||
const [hits, setHits] = useState<KbHit[] | null>(null);
|
||||
const [searching, setSearching] = useState(false);
|
||||
const [err, setErr] = useState("");
|
||||
const [graph, setGraph] = useState<Triple[] | null>(null);
|
||||
|
||||
const onGraph = async () => {
|
||||
try {
|
||||
setGraph(await graphKb(kb));
|
||||
} catch (e) {
|
||||
setErr((e as Error).message);
|
||||
}
|
||||
};
|
||||
|
||||
const stamp = () => new Date().toLocaleTimeString();
|
||||
const ingesting = prog?.active ?? false;
|
||||
@@ -234,7 +243,7 @@ export function KbView() {
|
||||
</button>
|
||||
</div>
|
||||
{err && <p className="mt-2 text-xs text-rose-600">✗ {err}</p>}
|
||||
<ul className="mt-3 flex-1 space-y-2 overflow-auto">
|
||||
<ul className="mt-3 max-h-[40%] space-y-2 overflow-auto">
|
||||
{hits === null && <li className="text-xs text-gray-400">输入查询后展示命中片段与分数。</li>}
|
||||
{hits !== null && hits.length === 0 && (
|
||||
<li className="text-xs text-gray-400">无命中(知识库为空或 RAG 未配置)。</li>
|
||||
@@ -250,6 +259,27 @@ export function KbView() {
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
|
||||
{/* 知识图谱(Neo4j / GraphRAG) */}
|
||||
<div className="mt-3 flex items-center justify-between border-t pt-2">
|
||||
<h3 className="text-xs font-semibold text-gray-600">知识图谱(Neo4j)</h3>
|
||||
<button onClick={onGraph} className="rounded border px-2 py-0.5 text-xs hover:bg-gray-50">
|
||||
查看图谱
|
||||
</button>
|
||||
</div>
|
||||
<ul className="mt-2 flex-1 space-y-1 overflow-auto">
|
||||
{graph === null && <li className="text-[11px] text-gray-400">点「查看图谱」展示入库抽取的实体关系。</li>}
|
||||
{graph !== null && graph.length === 0 && (
|
||||
<li className="text-[11px] text-gray-400">该库暂无图谱(需配置 chat 模型 + 入库触发抽取)。</li>
|
||||
)}
|
||||
{graph?.map((t, i) => (
|
||||
<li key={i} className="flex items-center gap-1 text-[11px]">
|
||||
<span className="rounded bg-amber-100 px-1.5 py-0.5 text-amber-700">{t.s}</span>
|
||||
<span className="text-gray-400">—{t.p}→</span>
|
||||
<span className="rounded bg-emerald-100 px-1.5 py-0.5 text-emerald-700">{t.o}</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user