使用 Ollama 本地模型和 Spring AI Alibaba 构建 RAG 应用
· 阅读需 8 分钟
RAG 应用架构概述
核心组件
- Spring AI:Spring 生态的 Java AI 开发框架,提供统一 API 接入大模型、向量数据库等 AI 基础设施。
- Ollama:本地大模型运行引擎(类 似于 Docker),支持快速部署开源模型。
- Spring AI Alibaba:对 Spring AI 的增强,集成 DashScope 模型平台。
- Elasticsearch:向量数据库,存储文本向量化数据,支撑语义检索。
模型选型
- Embedding 模型:nomic-embed-text:latest,用于将文本数据向量化。
- Ollama Chat 模型:deepseek-r1:8b,生成最终答案。
环境准备
启动 Ollama 服务
Docker Compose 启动 Ollama:(同时启动一个模型前端系统,和 Ollama 模型交互。)
services:
ollama:
container_name: ollama
image: ollama/ollama:latest
ports:
- 11434:11434
open-webui:
image: ghcr.io/open-webui/open-webui:main
container_name: open-webui
ports:
- 3005:8080
environment:
- 'OLLAMA_BASE_URL=http://host.docker.internal:11434'
# 允许容器访问宿主机网络
extra_hosts:
- host.docker.internal:host-gateway
下载模型
执行以下命令:
docker exec -it ollama ollama pull deepseek-r1:8b
docker exec -it ollama ollama pull nomic-embed-text:latest
在 open-webui 中调用 deepseek-r1:8b 模型:

部署 Elasticsearch
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:8.16.1
container_name: elasticsearch
privileged: true
environment:
- "cluster.name=elasticsearch"
- "discovery.type=single-node"
- "ES_JAVA_OPTS=-Xms512m -Xmx1096m"
- bootstrap.memory_lock=true
volumes:
- ./config/es.yaml:/usr/share/elasticsearch/config/elasticsearch.yml
ports:
- "9200:9200"
- "9300:9300"
deploy:
resources:
limits:
cpus: "2"
memory: 1000M
reservations:
memory: 200M
准备 es 启动的配置文件:
cluster.name: docker-es
node.name: es-node-1
network.host: 0.0.0.0
network.publish_host: 0.0.0.0
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"
bootstrap.memory_lock: true
# 关闭认证授权 es 8.x 默认开启
xpack.security.enabled: false
至此,便完成搭建一个简单 RAG 应用的所有环境准备步骤。下面开始搭建项目。
项目配置
依赖引入
<!-- Spring Boot Web Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.3.4</version>
</dependency>
<!-- Spring AI Ollama Starter -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
<version>1.0.0-M5</version>
</dependency>
<!-- 向量存储 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-elasticsearch-store</artifactId>
<version>1.0.0-M5</version>
</dependency>
<!-- PDF 解析 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pdf-document-reader</artifactId>
<version>1.0.0-M5</version>
</dependency>
核心配置
spring:
ai:
# ollama 配置
ollama:
base-url: http://127.0.0.1:11434
chat:
model: deepseek-r1:8b
embedding:
model: nomic-embed-text:latest
# 向量数据库配置
vectorstore:
elasticsearch:
index-name: ollama-rag-embedding-index
similarity: cosine
dimensions: 768
elasticsearch:
uris: http://127.0.0.1:9200
其中:
- index-name 为 es 向量索引名;
- dimensions 为向量模型生成的向量维度(需要和向量模型生成的向量维度一致,默认值为 1576);
- similarity 定义了用于衡量向量之间相似度的算法或度量方式,这里使用余弦相似度,使用高维稀疏向量。
如果您想自定义 es 的实例化配置,需要引入 spring-ai-elasticsearch-store:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-elasticsearch-store</artifactId>
<version>1.0.0-M5</version>
</dependency>
