从零搭建私有知识库问答系统:Spring AI + Milvus + 智谱GLM-5实战教程
本文详细介绍了如何基于Spring AI框架、Milvus向量数据库以及智谱GLM-5大语言模型从零开始搭建一套完整的私有知识库问答系统。内容涵盖了环境准备、项目搭建、核心代码实现、API接口说明、最佳实践和常见问题解答等方面。通过该系统开发者可以有效地让大语言模型理解并回答企业私有知识库中的问题实现AI在企业知识管理中的应用。文章还提供了详细的配置指南和代码示例帮助开发者快速上手并根据自己的需求进行定制化开发。小编给大家推荐一个开发者的知识库里面收录了 Java 程序员需要掌握的核心知识有兴趣的小伙伴可以收藏一下。网站https://farerboy.com一、项目概述在企业级 AI 应用开发中如何让大语言模型理解并回答企业私有知识库中的问题是一个核心技术挑战。RAGRetrieval Augmented Generation检索增强生成架构正是解决这一问题的最佳方案。本文将带您从零开始基于 Spring AI Milvus 向量数据库 智谱 GLM-5 模型搭建一套完整的私有知识库问答系统。技术栈组件选型说明框架Spring AI 1.0.0Spring 生态 AI 框架LLM智谱 GLM-5国内领先的大语言模型Embedding智谱 Embedding-3文本向量化模型向量库Milvus高性能开源向量数据库文档解析Apache Tika支持 PDF/Word/TXT 等多格式系统架构┌─────────────┐ ┌─────────────┐ ┌─────────────┐│ 用户 │────▶│ Spring AI │────▶│ GLM-5 ││ (提问) │ │ (RAG 编排) │ │ (生成回答) │└─────────────┘ └──────┬──────┘ └─────────────┘ │ ┌────────────┴────────────┐ │ │ ┌──────▼──────┐ ┌──────▼──────┐ │ Milvus │ │ 知识库文档 │ │ (向量检索) │ │ (PDF/Word) │ └─────────────┘ └─────────────┘二、环境准备2.1 Milvus 部署使用 Docker Compose 快速部署 Milvus# docker-compose.ymlversion:3.5services:milvus: image:milvusdb/milvus:v2.3.3 container_name:milvus-standalone environment: ETCD_ENDPOINTS:milvus-etcd:2379 MINIO_ADDRESS:milvus-minio:9000 ports: -19530:19530 -9091:9091 volumes: -./milvus/data:/var/lib/milvusmilvus-etcd: image:quay.io/coreos/etcd:v3.5.5 environment: -ETCD_AUTO_COMPACTION_MODErevision -ETCD_AUTO_COMPACTION_RETENTION1000 -ETCD_QUOTA_BACKEND_BYTES4294967296milvus-minio: image:minio/minio:RELEASE.2023-03-20T20-16-18Z environment: MINIO_ACCESS_KEY:minioadmin MINIO_SECRET_KEY:minioadmin command:server/minio_data--console-address:9001启动命令docker-compose up -d2.2 智谱 API Key 获取访问 智谱 AI 开放平台注册账号并完成企业认证在「API 密钥」页面创建 API Key记录 Key 并设置环境变量三、项目搭建3.1 Maven 依赖配置?xml version1.0 encodingUTF-8?project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version3.3.0/version /parent properties java.version17/java.version spring-ai.version1.0.0-M7/spring-ai.version /properties dependencyManagement dependencies dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-bom/artifactId version${spring-ai.version}/version typepom/type scopeimport/scope /dependency /dependencies /dependencyManagement dependencies !-- Spring AI Core -- dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-core/artifactId /dependency !-- 智谱 GLM 聊天模型 -- dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-zhipuai/artifactId /dependency !-- 智谱 Embedding 模型 -- dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-zhipuai-embedding/artifactId /dependency !-- Milvus 向量存储 -- dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-milvus-store/artifactId /dependency !-- Tika 文档解析 -- dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-tika-document-reader/artifactId /dependency !-- Spring Web -- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency /dependencies/project3.2 配置文件# application.ymlserver:port:8080spring:application: name:spring-ai-rag-milvusai: # 智谱 GLM-5 配置 zhipuai: api-key:${ZHIPUAI_API_KEY:} base-url:https://open.bigmodel.cn/api/paas/v4 chat: options: model:glm-5 temperature:0.7 max-tokens:2048 embedding: options: model:embedding-3 # Milvus 向量数据库配置 vectorstore: milvus: client: host:${MILVUS_HOST:localhost} port:${MILVUS_PORT:19530} username:${MILVUS_USER:root} password:${MILVUS_PASSWORD:milvus} database-name:default collection-name:knowledge_base embedding-dimension:1024 index-type:IVF_FLAT metric-type:COSINE initialize-schema:true3.3 环境变量# 启动前设置环境变量export ZHIPUAI_API_KEYyour-zhipuai-api-keyexport MILVUS_HOSTlocalhostexport MILVUS_PORT19530四、核心代码实现4.1 文档处理服务负责将知识库文档转换为向量并存储到 Milvuspackage com.farerboy.springai.demo.service;import org.springframework.ai.document.Document;import org.springframework.ai.document.DocumentReader;import org.springframework.ai.embedding.EmbeddingModel;import org.springframework.ai.reader.tika.TikaDocumentReader;import org.springframework.ai.transformer.splitter.TokenTextSplitter;import org.springframework.ai.vectorstore.VectorStore;import org.springframework.beans.factory.annotation.Value;import org.springframework.core.io.Resource;import org.springframework.core.io.UrlResource;import org.springframework.stereotype.Service;import java.io.IOException;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.util.List;import java.util.stream.Collectors;Servicepublicclass DocumentService { privatefinal VectorStore vectorStore; privatefinal TokenTextSplitter documentSplitter; Value(${knowledge.base.path:./knowledge-base}) private String knowledgeBasePath; public DocumentService(VectorStore vectorStore, EmbeddingModel embeddingModel) { this.vectorStore vectorStore; // 文档分块512 token/块128 token 重叠 this.documentSplitter new TokenTextSplitter( 512, // chunk size 128, // chunk overlap true, // keepSeparator true // includeTitle ); } /** * 加载单个文档 */ public void loadDocument(String filePath) throws IOException { Resource resource new UrlResource(Paths.get(filePath).toUri()); // 使用 Tika 解析 PDF/Word/TXT 等格式 DocumentReader documentReader new TikaDocumentReader(resource); ListDocument documents documentReader.read(); // 文档分块 ListDocument chunks documentSplitter.apply(documents); // 添加元数据 chunks.forEach(doc - { doc.getMetadata().put(source, filePath); doc.getMetadata().put(filename, Paths.get(filePath).getFileName().toString()); }); // 存入向量数据库 vectorStore.add(chunks); } /** * 批量加载目录下的所有文档 */ public void loadDirectory(String directoryPath) throws IOException { Path path Paths.get(directoryPath); ListPath files Files.walk(path) .filter(Files::isRegularFile) .filter(p - { String name p.getFileName().toString().toLowerCase(); return name.endsWith(.pdf) || name.endsWith(.docx) || name.endsWith(.txt) || name.endsWith(.md); }) .collect(Collectors.toList()); for (Path file : files) { try { loadDocument(file.toAbsolutePath().toString()); } catch (Exception e) { System.err.println(加载失败: file , 错误: e.getMessage()); } } } /** * 加载默认知识库目录 */ public void loadKnowledgeBase() throws IOException { loadDirectory(knowledgeBasePath); }}4.2 RAG 问答服务基于检索增强的对话服务package com.farerboy.springai.demo.service;import org.springframework.ai.chat.client.ChatClient;import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;import org.springframework.ai.chat.model.ChatModel;import org.springframework.ai.vectorstore.SearchRequest;import org.springframework.ai.vectorstore.VectorStore;import org.springframework.stereotype.Service;import java.util.List;import java.util.Map;Servicepublicclass ChatService { privatefinal ChatClient chatClient; privatefinal VectorStore vectorStore; public ChatService(ChatModel chatModel, VectorStore vectorStore) { this.vectorStore vectorStore; // 构建 ChatClient配置系统提示词 this.chatClient ChatClient.builder(chatModel) .defaultSystem(你是一个专业的知识库问答助手。 请根据提供的上下文信息回答用户的问题。 如果上下文中没有相关信息请明确告知用户。) .build(); } /** * 基础 RAG 问答 */ public String chat(String question) { return chatClient.prompt() .user(question) .advisors(QuestionAnswerAdvisor.builder(vectorStore).build()) .call() .content(); } /** * 带过滤条件的 RAG 问答 */ public String chat(String question, MapString, Object filters) { if (filters ! null !filters.isEmpty()) { StringBuilder filterExpr new StringBuilder(); int i 0; for (Map.EntryString, Object entry : filters.entrySet()) { if (i 0) filterExpr.append( AND ); filterExpr.append(entry.getKey()) .append( ) .append(entry.getValue()) .append(); i; } SearchRequest searchRequest SearchRequest.builder() .query(question) .filterExpression(filterExpr.toString()) .topK(5) .similarityThreshold(0.7) .build(); return chatClient.prompt() .user(question) .advisors(QuestionAnswerAdvisor.builder(vectorStore, searchRequest).build()) .call() .content(); } return chat(question); } /** * 带来源标注的问答 */ public MapString, Object chatWithSources(String question) { // 检索相关文档 SearchRequest searchRequest SearchRequest.builder() .query(question) .topK(3) .similarityThreshold(0.6) .build(); var docs vectorStore.similaritySearch(searchRequest); // 构建上下文 StringBuilder context new StringBuilder(); StringBuilder sources new StringBuilder(); for (int i 0; i docs.size(); i) { var doc docs.get(i); context.append(文档 ).append(i 1).append(:\n) .append(doc.getContent()) .append(\n\n); String filename doc.getMetadata().get(filename) ! null ? doc.getMetadata().get(filename).toString() : 未知来源; sources.append(- ).append(filename).append(\n); } // 构建增强 prompt String prompt 基于以下知识库中的信息回答用户问题。\n\n 【知识库内容】\n context.toString() 【用户问题】\n question \n\n 【回答要求】\n 1. 根据提供的知识库内容回答\n 2. 如果没有相关信息请说明\n 3. 在回答末尾列出参考来源; String answer chatClient.prompt() .user(prompt) .call() .content(); return Map.of( answer, answer, sources, sources.toString(), docCount, docs.size() ); }}4.3 REST API 控制器package com.farerboy.springai.demo.controller;import com.farerboy.springai.demo.service.ChatService;import com.farerboy.springai.demo.service.DocumentService;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.*;import org.springframework.web.multipart.MultipartFile;import java.io.IOException;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.util.List;import java.util.Map;RestControllerRequestMapping(/api/rag)publicclass RagController { privatefinal DocumentService documentService; privatefinal ChatService chatService; public RagController(DocumentService documentService, ChatService chatService) { this.documentService documentService; this.chatService chatService; } /** * RAG 问答接口 */ PostMapping(/chat) public ResponseEntityMapString, Object chat(RequestBody MapString, String request) { String question request.get(question); String answer chatService.chat(question); return ResponseEntity.ok(Map.of( answer, answer, question, question )); } /** * 带来源标注的问答接口 */ PostMapping(/chat/with-sources) public ResponseEntityMapString, Object chatWithSources(RequestBody MapString, String request) { String question request.get(question); MapString, Object result chatService.chatWithSources(question); return ResponseEntity.ok(result); } /** * 上传文档接口 */ PostMapping(/document/upload) public ResponseEntityMapString, Object uploadDocument(RequestParam(file) MultipartFile file) throws IOException { Path uploadDir Paths.get(./knowledge-base/uploads); Files.createDirectories(uploadDir); Path filePath uploadDir.resolve(file.getOriginalFilename()); Files.write(filePath, file.getBytes()); documentService.loadDocument(filePath.toAbsolutePath().toString()); return ResponseEntity.ok(Map.of( message, 文档上传成功, filename, file.getOriginalFilename() )); } /** * 加载目录下的所有文档 */ PostMapping(/document/load-directory) public ResponseEntityMapString, Object loadDirectory(RequestBody MapString, String request) throws IOException { String directoryPath request.get(path); documentService.loadDirectory(directoryPath); return ResponseEntity.ok(Map.of( message, 文档加载成功, path, directoryPath )); } /** * 相似文档搜索 */ GetMapping(/search) public ResponseEntityMapString, Object search( RequestParam String query, RequestParam(defaultValue 5) int topK) { ListString results chatService.searchSimilarDocuments(query, topK); return ResponseEntity.ok(Map.of( query, query, results, results, count, results.size() )); }}4.4 启动类package com.farerboy.springai.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplicationpublic class SpringAiRagApplication { public static void main(String[] args) { SpringApplication.run(SpringAiRagApplication.class, args); }}五、API 接口说明5.1 问答接口# 基础 RAG 问答curl -X POST http://localhost:8080/api/rag/chat \ -H Content-Type: application/json \ -d {question: 什么是 Spring AI?}响应示例{ answer: Spring AI 是一个用于 AI 工程的应用框架..., question: 什么是 Spring AI?}5.2 带来源的问答# 获取答案和参考来源curl -X POST http://localhost:8080/api/rag/chat/with-sources \ -H Content-Type: application/json \ -d {question: 如何配置 Milvus 向量库?}5.3 文档上传# 上传知识库文档curl -X POST http://localhost:8080/api/rag/document/upload \ -F file./docs/spring-ai-guide.pdf5.4 相似文档搜索# 搜索相似文档curl http://localhost:8080/api/rag/search?querySpring AI 配置topK3六、最佳实践6.1 分块策略场景建议参数通用文档chunk512, overlap128技术文档chunk1024, overlap256问答场景chunk256, overlap646.2 相似度阈值严格模式0.8高precision平衡模式0.6-0.8推荐宽松模式0.4-0.6高recall6.3 索引类型选择类型特点适用场景IVF_FLAT精确度高召回快中小规模数据HNSW高速搜索内存占用大大规模数据延迟敏感IVF_SQ8压缩存储降低精度超大规模数据七、常见问题Q1: 检索不到相关内容检查文档是否成功加载调整 chunk size 和 overlap降低相似度阈值Q2: LLM 幻觉提高相似度阈值建议 0.7使用带来源的问答模式在 prompt 中强调基于知识库回答Q3: 响应速度慢使用 HNSW 索引开启批量处理考虑本地部署模型八、总结本文详细介绍了基于 Spring AI Milvus 智谱 GLM-5 的私有知识库问答系统搭建方案。该方案具有以下优势国产化支持智谱 GLM-5 是国内领先的大模型响应速度快高性能检索Milvus 向量数据库支持海量数据毫秒级检索Spring 生态Java 开发者零门槛快速上手灵活扩展支持多租户、混合搜索、重排序等高级特性通过本文的代码示例和配置指南您可以快速搭建属于自己的私有知识库问答系统让 AI 真正成为企业知识的智能助手。架构设计之道在于在不同的场景采用合适的架构设计架构设计没有完美只有合适。假如你从2026年开始学大模型按这个步骤走准能稳步进阶。接下来告诉你一条最快的邪修路线3个月即可成为模型大师薪资直接起飞。阶段1:大模型基础阶段2:RAG应用开发工程阶段3:大模型Agent应用架构阶段4:大模型微调与私有化部署配套文档资源全套AI 大模型 学习资料朋友们如果需要可以微信扫描下方二维码免费领取【保证100%免费】配套文档资源全套AI 大模型 学习资料朋友们如果需要可以微信扫描下方二维码免费领取【保证100%免费】
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2440715.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!