从零搭建私有知识库问答系统:Spring AI + Milvus + 智谱GLM-5实战教程

news2026/3/23 14:52:17
本文详细介绍了如何基于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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SpringBoot-17-MyBatis动态SQL标签之常用标签

文章目录 1 代码1.1 实体User.java1.2 接口UserMapper.java1.3 映射UserMapper.xml1.3.1 标签if1.3.2 标签if和where1.3.3 标签choose和when和otherwise1.4 UserController.java2 常用动态SQL标签2.1 标签set2.1.1 UserMapper.java2.1.2 UserMapper.xml2.1.3 UserController.ja…

wordpress后台更新后 前端没变化的解决方法

使用siteground主机的wordpress网站,会出现更新了网站内容和修改了php模板文件、js文件、css文件、图片文件后,网站没有变化的情况。 不熟悉siteground主机的新手,遇到这个问题,就很抓狂,明明是哪都没操作错误&#x…

网络编程(Modbus进阶)

思维导图 Modbus RTU(先学一点理论) 概念 Modbus RTU 是工业自动化领域 最广泛应用的串行通信协议,由 Modicon 公司(现施耐德电气)于 1979 年推出。它以 高效率、强健性、易实现的特点成为工业控制系统的通信标准。 包…

UE5 学习系列(二)用户操作界面及介绍

这篇博客是 UE5 学习系列博客的第二篇,在第一篇的基础上展开这篇内容。博客参考的 B 站视频资料和第一篇的链接如下: 【Note】:如果你已经完成安装等操作,可以只执行第一篇博客中 2. 新建一个空白游戏项目 章节操作,重…

IDEA运行Tomcat出现乱码问题解决汇总

最近正值期末周,有很多同学在写期末Java web作业时,运行tomcat出现乱码问题,经过多次解决与研究,我做了如下整理: 原因: IDEA本身编码与tomcat的编码与Windows编码不同导致,Windows 系统控制台…

利用最小二乘法找圆心和半径

#include <iostream> #include <vector> #include <cmath> #include <Eigen/Dense> // 需安装Eigen库用于矩阵运算 // 定义点结构 struct Point { double x, y; Point(double x_, double y_) : x(x_), y(y_) {} }; // 最小二乘法求圆心和半径 …

使用docker在3台服务器上搭建基于redis 6.x的一主两从三台均是哨兵模式

一、环境及版本说明 如果服务器已经安装了docker,则忽略此步骤,如果没有安装,则可以按照一下方式安装: 1. 在线安装(有互联网环境): 请看我这篇文章 传送阵>> 点我查看 2. 离线安装(内网环境):请看我这篇文章 传送阵>> 点我查看 说明&#xff1a;假设每台服务器已…

XML Group端口详解

在XML数据映射过程中&#xff0c;经常需要对数据进行分组聚合操作。例如&#xff0c;当处理包含多个物料明细的XML文件时&#xff0c;可能需要将相同物料号的明细归为一组&#xff0c;或对相同物料号的数量进行求和计算。传统实现方式通常需要编写脚本代码&#xff0c;增加了开…

LBE-LEX系列工业语音播放器|预警播报器|喇叭蜂鸣器的上位机配置操作说明

LBE-LEX系列工业语音播放器|预警播报器|喇叭蜂鸣器专为工业环境精心打造&#xff0c;完美适配AGV和无人叉车。同时&#xff0c;集成以太网与语音合成技术&#xff0c;为各类高级系统&#xff08;如MES、调度系统、库位管理、立库等&#xff09;提供高效便捷的语音交互体验。 L…

(LeetCode 每日一题) 3442. 奇偶频次间的最大差值 I (哈希、字符串)

题目&#xff1a;3442. 奇偶频次间的最大差值 I 思路 &#xff1a;哈希&#xff0c;时间复杂度0(n)。 用哈希表来记录每个字符串中字符的分布情况&#xff0c;哈希表这里用数组即可实现。 C版本&#xff1a; class Solution { public:int maxDifference(string s) {int a[26]…

【大模型RAG】拍照搜题技术架构速览:三层管道、两级检索、兜底大模型

摘要 拍照搜题系统采用“三层管道&#xff08;多模态 OCR → 语义检索 → 答案渲染&#xff09;、两级检索&#xff08;倒排 BM25 向量 HNSW&#xff09;并以大语言模型兜底”的整体框架&#xff1a; 多模态 OCR 层 将题目图片经过超分、去噪、倾斜校正后&#xff0c;分别用…

【Axure高保真原型】引导弹窗

今天和大家中分享引导弹窗的原型模板&#xff0c;载入页面后&#xff0c;会显示引导弹窗&#xff0c;适用于引导用户使用页面&#xff0c;点击完成后&#xff0c;会显示下一个引导弹窗&#xff0c;直至最后一个引导弹窗完成后进入首页。具体效果可以点击下方视频观看或打开下方…

接口测试中缓存处理策略

在接口测试中&#xff0c;缓存处理策略是一个关键环节&#xff0c;直接影响测试结果的准确性和可靠性。合理的缓存处理策略能够确保测试环境的一致性&#xff0c;避免因缓存数据导致的测试偏差。以下是接口测试中常见的缓存处理策略及其详细说明&#xff1a; 一、缓存处理的核…

龙虎榜——20250610

上证指数放量收阴线&#xff0c;个股多数下跌&#xff0c;盘中受消息影响大幅波动。 深证指数放量收阴线形成顶分型&#xff0c;指数短线有调整的需求&#xff0c;大概需要一两天。 2025年6月10日龙虎榜行业方向分析 1. 金融科技 代表标的&#xff1a;御银股份、雄帝科技 驱动…

观成科技:隐蔽隧道工具Ligolo-ng加密流量分析

1.工具介绍 Ligolo-ng是一款由go编写的高效隧道工具&#xff0c;该工具基于TUN接口实现其功能&#xff0c;利用反向TCP/TLS连接建立一条隐蔽的通信信道&#xff0c;支持使用Let’s Encrypt自动生成证书。Ligolo-ng的通信隐蔽性体现在其支持多种连接方式&#xff0c;适应复杂网…

铭豹扩展坞 USB转网口 突然无法识别解决方法

当 USB 转网口扩展坞在一台笔记本上无法识别,但在其他电脑上正常工作时,问题通常出在笔记本自身或其与扩展坞的兼容性上。以下是系统化的定位思路和排查步骤,帮助你快速找到故障原因: 背景: 一个M-pard(铭豹)扩展坞的网卡突然无法识别了,扩展出来的三个USB接口正常。…

未来机器人的大脑:如何用神经网络模拟器实现更智能的决策?

编辑&#xff1a;陈萍萍的公主一点人工一点智能 未来机器人的大脑&#xff1a;如何用神经网络模拟器实现更智能的决策&#xff1f;RWM通过双自回归机制有效解决了复合误差、部分可观测性和随机动力学等关键挑战&#xff0c;在不依赖领域特定归纳偏见的条件下实现了卓越的预测准…

Linux应用开发之网络套接字编程(实例篇)

服务端与客户端单连接 服务端代码 #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #include <pthread.h> …

华为云AI开发平台ModelArts

华为云ModelArts&#xff1a;重塑AI开发流程的“智能引擎”与“创新加速器”&#xff01; 在人工智能浪潮席卷全球的2025年&#xff0c;企业拥抱AI的意愿空前高涨&#xff0c;但技术门槛高、流程复杂、资源投入巨大的现实&#xff0c;却让许多创新构想止步于实验室。数据科学家…

深度学习在微纳光子学中的应用

深度学习在微纳光子学中的主要应用方向 深度学习与微纳光子学的结合主要集中在以下几个方向&#xff1a; 逆向设计 通过神经网络快速预测微纳结构的光学响应&#xff0c;替代传统耗时的数值模拟方法。例如设计超表面、光子晶体等结构。 特征提取与优化 从复杂的光学数据中自…