整体流程参考了GraphRAG,本文为JAVA实现
相关概念
RAG通用流程
GraphRAG
GraphRAG local search
主要流程
主要分为两步:索引、召回
步骤1:索引
对知识进行切块,每块都使用LLM从知识中抽取出来图的各要素如实体、关系、权重,存储到图数据库(Apache AGE)中,如图:
索引阶段
步骤2:召回
- 使用LLM从用户问题中抽取出实体、关系
- 根据抽取出来的实体及关系、从图数据库中查询出相关的图数据
- 将组合数据(图数据+文本块+用户问题)提交给LLM得到答案
召回阶段
PS:GraphRAG local search流程中没有涉及到图谱社区
代码实现
技术栈
1.1 开发框架:SpringBoot、langchain4j
1.2 数据库: Postgresql(需要安装Apage AGE插件以支持图数据)
1.3 大语言模型(LLM):GPT
关键代码
索引
GraphStoreIngestor.builder()
.documentSplitter(documentSplitter)
.graphStore(apacheAgeGraphStore)
.build()
.ingest(document);
召回
GraphStoreContentRetriever contentRetriever = GraphStoreContentRetriever.builder()
.graphStore(apacheAgeGraphStore)
.chatLanguageModel(chatLanguageModel)
.maxResults(maxResults)
.filter(filter)
.breakIfSearchMissed(breakIfSearchMissed)
.build();
CustomerSupportAgent agent = AiServices.builder(CustomerSupportAgent.class)
.chatLanguageModel(chatModel)
.contentRetriever(contentRetriever)
.chatMemory(chatMemory)
.build();
agent.answer("用户的问题");
最终效果
上传知识库文件
生成图谱数据
图谱数据示例如下:
搜索效果
完整代码见:langchain4j-aideepin
基于向量的RAG可参考:RAG应用实践-基于大模型构建知识库
GraphRAG:https://github.com/microsoft/gr