Java 调用 DeepSeek
如果本地已经装好 Ollama、Docker 和 Open WebUI,后面的步骤会顺很多;没装的话,先把环境补齐,再往下做。
下载 DeepSeek 模型
先在命令行里拉一下模型:
ollama run deepseek-r1:7b

本地先跑通一次
打开 Docker Desktop,启动 Open WebUI。

在界面里选 DeepSeek-R1 模型,先随便问一句,确认模型和 UI 是通的。

Java 里接入模型
先把之前的 moonshot 相关依赖注释掉,或者直接删掉。不然启动时大概率会因为 Bean 冲突或者配置残留报错。
然后引入 Ollama 依赖:
<!-- 引入 Ollama 依赖-->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>
配置类也要换成 Ollama 版本:
package com.yan.springai;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@RequiredArgsConstructor
public class Init {
//要使用的模型
final OllamaChatModel model2;
@Bean
public ChatClient chatClient(ChatMemory chatMemory){
return ChatClient.builder(model2)
.defaultSystem("假如你是特朗普,接下来的对话你必须以特朗普的语气来进行?")
.defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory))//这里主要负责拼接
.build();
}
@Bean
public ChatMemory chatMemory(){
//负责存和读
return new InMemoryChatMemory();
}
}
配置文件改成这样:
spring:
ai:
ollama:
chat:
options:
model: deepseek-r1:7b
base-url: http://localhost:11434
启动项目后看一下输出,能正常返回就说明模型已经接上了。

构建数据库和 RAG
增强检索
Embedding 就是把文本、物品、用户这些对象映射成向量。它的用处不算玄乎,核心就是让模型能按语义去找内容,而不是只做字符串匹配。
这里先拉一个小一点的 embedding 模型,适合练手,不太适合直接上企业项目:
ollama pull all-minilm

向量数据库
这里用的是 pgvector,也可以换成 Redis。
pgvector 是 PostgreSQL 的向量扩展,能在关系型数据库里做语义搜索。对我来说,它的好处是少引一套系统,坏处是规模上来后要更认真地看索引和性能。
先起一个 pgvector 容器:
docker run -d --name pgvector -p 5433:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres pgvector/pgvector:pg16

Spring Boot 集成 pgvector
先引依赖:
<!-- 引入 pgvector-->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pgvector-store-spring-boot-starter</artifactId>
</dependency>
再补上配置:
spring:
ai:
vectorstore:
pgvector:
index-type: HNSW
distance-type: COSINE_DISTANCE
# 维度,根据选的 embedding 模型所定
dimensions: 384
batching-strategy: TOKEN_COUNT
max-document-batch-size: 1000
ollama:
chat:
options:
model: deepseek-r1:7b
embedding:
enabled: true
model: all-minilm
base-url: http://localhost:11434
datasource:
url: jdbc:postgresql://localhost:5433/springai
username: postgres
password: postgres
连上数据库后,建库并执行建表语句:
create extension if not exists vector;
create extension if not exists hstore;
create extension if not exists "uuid-ossp";
create TABLE if not exists vector_store(
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
content text,
metadata json,
embedding vector(384)
);
create index on vector_store using HNSW(embedding vector_cosine_ops);
然后在 resources 里放一个 txt 文件。

接着新建 vector 包和 VectorAPI 类,把文本切分后写进向量库:
package com.yan.springai.vector;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
@RestController
@RequiredArgsConstructor
public class VectorAPI {
final VectorStore store;
//导入方法
@GetMapping("/vec/write")
public String write() throws IOException {
StringBuffer text = new StringBuffer();//用来存储文件
ClassLoader classLoader=getClass().getClassLoader();//因为打包后,resource 的文件就放在 class:path 下,我们使用这个获取
InputStream inputStream=classLoader.getResourceAsStream("ncode.txt");//获取文件
//把文件一行一行读取出来,放在 text 中去
try(BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream))){
String line;
while ((line=reader.readLine())!=null){
text.append(line);
}
}
//按照句号,将文本 p 成一行一行的
store.write(Arrays.stream(text.toString().split("。")).map(Document::new).toList());
return "success";
}
}
运行后,数据会被写入向量库。控制台和数据库里能看到记录,就说明这步没问题。


控制台输出:

查一下表也能看到导入结果:


这时候直接问模型,回答通常还是偏'普通聊天模型'的风格,没真正用上知识库。
RAG 也是通过 advisor 拼进去的,所以要改一下 Init:
package com.yan.springai;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@RequiredArgsConstructor
public class Init {
//要使用的模型
final OllamaChatModel model2;
final VectorStore vectorStore;
@Bean
public ChatClient chatClient(ChatMemory chatMemory){
return ChatClient.builder(model2)
.defaultSystem("假如你是特朗普,接下来的对话你必须以特朗普的语气来进行?")
.defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory), new QuestionAnswerAdvisor(vectorStore) )//这里主要负责拼接
.build();
}
@Bean
public ChatMemory chatMemory(){
//负责存和读
return new InMemoryChatMemory();
}
}
再测一次,RAG 生效后,回答会明显更贴近导入的内容。

chatpdf
再往前一步,就是把 PDF 文档也塞进向量库。
先引依赖:
<!-- 将 pdf 引入向量数据库-->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pdf-document-reader</artifactId>
</dependency>
然后写读取代码:
package com.yan.springai.Pdf;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.reader.ExtractedTextFormatter;
import org.springframework.ai.reader.pdf.PagePdfDocumentReader;
import org.springframework.ai.reader.pdf.config.PdfDocumentReaderConfig;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequiredArgsConstructor
public class Pdf {
final VectorStore store;
@GetMapping("/pdf/read")
public String getDocsFromPdf() {
PagePdfDocumentReader pdfReader=new PagePdfDocumentReader("classpath:/baogao.pdf", PdfDocumentReaderConfig.builder()
.withPageTopMargin(0)
.withPageExtractedTextFormatter(ExtractedTextFormatter.builder()
.withNumberOfTopTextLinesToDelete(0)
.build())
.withPagesPerDocument(1)
.build() );
store.write(pdfReader.read());
return "success";
}
}
跑一下,PDF 就能进向量数据库。

Markdown 文档也可以照这个思路处理。

function call 调用自定义函数

DeepSeek 这类模型目前还不支持这个能力,Moonshot、OpenAI、Gemini 之类可以直接用。
先写一个业务函数,假装这里是请假逻辑:
package com.yan.springai.func;
import java.util.function.Function;
public class OaService implements Function<OaService.Rquest, OaService.Response> {
public Response apply(Rquest rquest) {
//实现逻辑,这里是请假逻辑
System.err.printf("%s is token off%n",rquest.who);
return new Response(10);
}
public record Rquest(String who) { }
public record Response(int days) { }
}
再把它注册到 Spring 容器里:
package com.yan.springai.func;
import org.springframework.ai.model.function.FunctionCallback;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//将 function 注册到 spring 容器中
@Configuration
public class FunctionRegistry {
@Bean
public FunctionCallback askForLeaveCallBack(){
return FunctionCallback.builder()
.function("askForLeave",new OaService())//注册的名字和函数
.description("当有人请假时,返回请假天数")//描述功能
.build();
}
}
调用的时候直接在 ChatClient 里挂上函数名:
package com.yan.springai.func;
//使用刚刚定义的函数
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequiredArgsConstructor
public class FuncAPI {
final ChatClient chatClient;
@GetMapping("/ai/func")
public String funcCall(@RequestParam(value = "message")String message){
return chatClient.prompt(message)
.functions("askForLeave")//调用名称
.call().content();
}
}
跑起来后,就能看到函数被调用了。
多模态能力
多模态模型能同时处理文本、图片、音频、视频。这里我只拿'图片转文字'做例子,因为这个最直观,也最容易看出模型有没有真的读懂图。
不过要先说清楚,DeepSeek、Moonshot 这类模型不支持这个能力,要试的话得换成 llava。

命令行里拉一下:
ollama run llava
下载完成后,在 resources 里放一张图片:



然后写接口,让模型读取图片并输出描述:
package com.yan.springai.model;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.Media;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaModel;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequiredArgsConstructor
public class ImageAPI {
final OllamaChatModel model;
@GetMapping("/ai/chatWithPic")
public String chatWithPic() {
ClassPathResource imageData=new ClassPathResource("/cat.png");
Message userMessage=new UserMessage("请用中文描述一下这张图片是什么东西?", List.of(new Media(MimeTypeUtils.IMAGE_PNG,imageData)));
return model.call(new Prompt(
List.of(userMessage),
ChatOptions.builder()
.model(OllamaModel.LLAVA.getName()).build()))
.getResult().getOutput().getText();
}
}
这样就能把图片交给模型处理了。

