微信扫码
添加专属顾问
我要投稿
从Langchain4j迁移到Spring AI?这篇实战指南帮你高效搭建知识库系统,轻松应对技术升级挑战。 核心内容: 1. 知识库表设计与环境搭建关键步骤 2. 基于Docker的Milvus向量库部署详解 3. Spring AI与Ollama模型集成实战方案
框架:Ruoyi-Vue-Plus
版本:5.3.1
Spring-boot版本:3.4.4
JDK:17
spring-ai版本:1.0.0
需安装Ollama,且具备模型"nomic-embed-text"
资料已上传至技术群
ai_knowledge,ai_knowledge_document,ai_knowledge_segment,ai_model,ai_api_key
docker-compose --version
version: '3.5'services: etcd: container_name: milvus-etcd image: quay.io/coreos/etcd:v3.5.18 environment: - ETCD_AUTO_COMPACTION_MODE=revision - ETCD_AUTO_COMPACTION_RETENTION=1000 - ETCD_QUOTA_BACKEND_BYTES=4294967296 - ETCD_SNAPSHOT_COUNT=50000 volumes: - ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/etcd:/etcd command: etcd -advertise-client-urls=http://etcd:2379 -listen-client-urls http://0.0.0.0:2379 --data-dir /etcd healthcheck: test: ["CMD", "etcdctl", "endpoint", "health"] interval: 30s timeout: 20s retries: 3 minio: container_name: milvus-minio image: minio/minio:RELEASE.2025-04-22T22-12-26Z environment: MINIO_ACCESS_KEY: minioadmin MINIO_SECRET_KEY: minioadmin ports: - "9000:9000" - "9001:9001" volumes: - ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/minio:/minio_data command: minio server /minio_data --console-address ":9001" healthcheck: test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"] interval: 30s timeout: 20s retries: 3 standalone: container_name: milvus-standalone image: milvusdb/milvus:v2.5.13 command: ["milvus", "run", "standalone"] security_opt: - seccomp:unconfined environment: ETCD_ENDPOINTS: etcd:2379 MINIO_ADDRESS: minio:9000 volumes: - ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/milvus:/var/lib/milvus healthcheck: test: ["CMD", "curl", "-f", "http://localhost:9091/healthz"] interval: 30s start_period: 90s timeout: 20s retries: 3 ports: - "19530:19530" - "9091:9091" depends_on: - "etcd" - "minio" attu: container_name: attu image: zilliz/attu:v2.5.7 environment: MILVUS_URL: milvus-standalone:19530 ports: - "19500:3000" depends_on: - "standalone"networks: default: name: milvus
docker-compose up -d
docker images
docker ps
docker ps -a
docker restart etcd容器iddocker restart minio容器iddocker restart milvus容器iddocker restart attu容器id
# 浏览器访问miniolocalhost:9000
# 浏览器访问milvuslocalhost:19500
<tika.version>3.0.0</tika.version><spring-ai.version>1.0.0</spring-ai.version><commons-io.version>2.16.1</commons-io.version>
<!-- Tika提取文件 必须 --> <dependency> <groupId>org.apache.tika</groupId> <artifactId>tika-core</artifactId> <version>${tika.version}</version> <exclusions> <exclusion> <artifactId>commons-io</artifactId> <groupId>commons-io</groupId> </exclusion> </exclusions> </dependency> <!-- 解析文档 必须 --> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-tika-document-reader</artifactId> <version>${spring-ai.version}</version> </dependency> <!-- 解析文档 必须 --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>${commons-io.version}</version> </dependency>
spring: application: name: RuoYi-Vue-Plus ai: vectorstore: milvus: initialize-schema: true database-name: default collection-name: test client: host: milvus服务ip地址 port: 19530 username: root password: milvus
# 字段1doc_id:文档id,也就是表ai_knowledge_segment中对应的字段vector_id,方便验证、查询
# 字段2embedding:向量维度,利用向量模型nomic-embed-text解析成向量,需要指定维度768,如果是1024或其它会报错!
# 字段3content:文档内容,利用tika解析文档
# 字段4metadata:元数据,需要存储业务数据,如知识库id、文档id
/** * 上传文档列表 * * @param bo * @return */ @PostMapping("/createKnowledgeDocumentList") public R<List<Long>> createKnowledgeDocumentList(@RequestBody AiKnowledgeDocumentListBo bo) { return R.ok(aiKnowledgeDocumentService.createKnowledgeDocumentList(bo)); }
/** * 上传文档列表 * * @param bo * @return */ List<Long> createKnowledgeDocumentList(AiKnowledgeDocumentListBo bo);
/**
* 上传文档列表
*
* @param bo
* @return
*/
Exception.class) (rollbackFor =
public List<Long> createKnowledgeDocumentList(AiKnowledgeDocumentListBo bo) {
// 校验
aiKnowledgeService.validateKnowledgeExists(bo.getKnowledgeId());
if (ObjectUtil.isEmpty(bo.getList())) {
throw new ServiceException("至少上传一个文件");
}
// 批量读取文档内容
List<AiKnowledgeDocument> aiKnowledgeDocuments = prepareDocuments(bo);
baseMapper.insertBatch(aiKnowledgeDocuments);
// 切片
processDocumentSegment(aiKnowledgeDocuments);
return extractDocumentIds(aiKnowledgeDocuments);
}
VectorStore getOrCreateVectorStore(Class<? extends VectorStore> type, EmbeddingModel embeddingModel, Map<String, Class<?>> metadataFields);
public VectorStore getOrCreateVectorStore(Long embedModelId, Map<String, Class<?>> metadataFields) {
// 获取模型信息
AiModelVo aiModelVo = validateModel(embedModelId);
AiApiKeyVo aiApiKeyVo = aiApiKeyService.validateApiKey(aiModelVo.getKeyId());
AiPlatformEnum aiPlatformEnum = AiPlatformEnum.validatePlatform(aiApiKeyVo.getPlatform());
// 创建或获取嵌入模型
EmbeddingModel embeddingModel = modelFactoryService.getOrCreateEmbeddingModel(
aiPlatformEnum, aiApiKeyVo.getApiKey(), aiApiKeyVo.getUrl(), aiModelVo.getModel()
);
return modelFactoryService.getOrCreateVectorStore(MilvusVectorStore.class, embeddingModel, metadataFields);
}
加入技术群可以获取资料,含AI资料、Spring AI中文文档等,等你加入~
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2025-08-30
大模型的“思维链”(Chain-of-Thought):AI 是怎么一步步“推理”的
2025-08-30
Agentic AI与WorkFlow的相互成就
2025-08-29
刚刚,xAI 发布 Grok Code Fast 1 编程模型,快、便宜、免费
2025-08-29
大模型时代有了自己的「价值高速公路」
2025-08-29
A I智能革命——上下文工程新突破
2025-08-29
知识库检索准不准,关键看模型选没选对!一份评测指南请收好
2025-08-29
我如何用Prompt工程将大模型调教成风控专家
2025-08-29
度小满金融大模型技术创新与应用探索
2025-08-21
2025-06-21
2025-08-21
2025-08-19
2025-06-07
2025-06-12
2025-06-19
2025-06-13
2025-07-29
2025-06-15
2025-08-28
2025-08-28
2025-08-28
2025-08-28
2025-08-27
2025-08-26
2025-08-25
2025-08-25