LangChain4j检索增强生成RAG
目录
- RAG
- 引入依赖
- yml配置
- 主启动类
- 自定义ChatAssistant接口
- 配置类LLMConfig
- 控制类RagLanChain4jController
- 测试
- 总结
RAG
RAG (Retrieval-Augmented Generation) 即检索增强生成,是一种将外部知识检索与大型语言模型 (LLM) 生成能力相结合的人工智能技术框架。其核心思想是在LLM处理用户查询并生成回答之前,先从一个或多个外部知识源 (如数据库、文档集合、API等) 中检索与查询相关的、最新的信息片段,然后将这些检索到的信息作为额外的上下文注入到LLM的提示中,从而引导LLM生成更准确、更相关、更具事实性的内容。
引入依赖
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- LangChain4j原生 基础--><dependency><groupId>dev.langchain4j</groupId><artifactId>langchain4j-open-ai</artifactId></dependency><!-- LangChain4j原生 高阶--><dependency><groupId>dev.langchain4j</groupId><artifactId>langchain4j</artifactId></dependency><!-- easy-rag --><dependency><groupId>dev.langchain4j</groupId><artifactId>langchain4j-easy-rag</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>
</dependencies>
yml配置
server:port: 9011spring:application:name: langchain4j-rag
主启动类
@SpringBootApplication
public class RagLangChain4jApp {public static void main(String[] args) {SpringApplication.run(RagLangChain4jApp.class, args);}
}
自定义ChatAssistant接口
public interface ChatAssistant {String chat(String msg);
}
配置类LLMConfig
@Configuration
public class LLMConfig {@Bean(name = "qwen")public ChatModel chatModelQwen() {return OpenAiChatModel.builder().apiKey(System.getenv("ALIYUN_KEY")).modelName("qwen-plus").baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1").build();}// 基于内存的向量数据库@Beanpublic InMemoryEmbeddingStore<TextSegment> inMemoryEmbeddingStore() {return new InMemoryEmbeddingStore<>();}@Beanpublic ChatAssistant chatAssistant(ChatModel chatModel,EmbeddingStore<TextSegment> embeddingStore) {return AiServices.builder(ChatAssistant.class).chatModel(chatModel).chatMemory(MessageWindowChatMemory.withMaxMessages(50)).contentRetriever(EmbeddingStoreContentRetriever.from(embeddingStore)).build();}
}
控制类RagLanChain4jController
@RestController
public class RagLanChain4jController {@Resourceprivate ChatAssistant chatAssistant;@Resourceprivate InMemoryEmbeddingStore<TextSegment> inMemoryEmbeddingStore;// http://localhost:9011/rag/chat?msg=错误码A0104和A0301是什么@GetMapping("/rag/chat")public String chat(@RequestParam(value = "msg") String msg) throws FileNotFoundException {//1 加载文档:使用适当的 DocumentLoader 和 DocumentParser 加载文档// Document document = FileSystemDocumentLoader.loadDocument("E:/Java/test.docx");FileInputStream fileInputStream = new FileInputStream("E:/Java/test.docx");Document document = new ApacheTikaDocumentParser().parse(fileInputStream);//2 转换文档:使用 DocumentTransformer 清理或增强文档(可选)//3 拆分文档:使用 DocumentSplitter 将文档拆分为小的片段(可选)//4 嵌入文档:使用 EmbeddingModel 将文档片段转换为嵌入向量//5 存储嵌入:使用 EmbeddingStoreIngestor 存储嵌入向量EmbeddingStoreIngestor.ingest(document, inMemoryEmbeddingStore);//6 检索相关内容:根据用户查询,从 EmbeddingStore 检索最相关的文档片段String result = chatAssistant.chat(msg);//7 生成响应:将检索到的内容与用户查询一起提供给大模型,生成最终的响应System.out.println(result);return result;}
}
测试
访问 http://localhost:9011/rag/chat?msg=错误码A0104和A0301是什么
总结
以上主要介绍了 LangChain4j 检索增强生成 RAG 相关知识,想了解更多 LangChain4j 知识的小伙伴请参考 LangChain4j 官网 进行学习,学习更多 LangChain4j 实战实用技巧的小伙伴,请关注后期发布的文章,认真看完一定能让你有所收获。