序
本文主要研究一下如何使用spring-ai-alibaba本地集成ollama+deepseek
步骤
pom.xml
4.0.0
com.example
ollama-deepseek-chat
pom
1.0.0-SNAPSHOT
Spring AI Alibaba Ollama DeepSeek Chat Example
Spring AI Alibaba Ollama DeepSeek Chat Examples
UTF-8
UTF-8
17
17
17
1.0.0-M5
1.0.0-M5.1
3.4.0
3.1.1
1.3.0
3.8.1
ollama-deepseek-chat-model
ollama-deepseek-chat-client
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
org.springframework.ai
spring-ai-bom
${spring-ai.version}
pom
import
com.alibaba.cloud.ai
spring-ai-alibaba-starter
${spring-ai-alibaba.version}
org.springframework.boot
spring-boot-starter-web
org.springframework.ai
spring-ai-ollama-spring-boot-starter
org.springframework.boot
spring-boot-maven-plugin
${spring-boot.version}
org.apache.maven.plugins
maven-deploy-plugin
${maven-deploy-plugin.version}
true
org.apache.maven.plugins
maven-compiler-plugin
${maven-compiler-plugin.version}
${java.version}
-parameters
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
aliyunmaven
aliyun
https://maven.aliyun.com/repository/public
public
aliyun nexus
https://maven.aliyun.com/repository/public
true
false
主要是引入
spring-ai-ollama-spring-boot-starter
application.yaml
server:
port: 10005
spring:
application:
name: spring-ai-alibaba-ollama-chat-model-example
ai:
ollama:
base-url: http://localhost:11434
chat:
model: deepseek-r1:8b
这里配置ai.ollama.base-url、ai.ollama.chat.model
ChatModel
call
@GetMapping("/simple/chat")
public String simpleChat() {
return ollamaChatModel.call(new Prompt(DEFAULT_PROMPT)).getResult().getOutput().getContent();
}
使用call是阻塞一次性输出
stream
@GetMapping("/stream/chat")
public Flux streamChat(HttpServletResponse response) {
// 避免返回乱码
response.setCharacterEncoding("UTF-8");
Flux stream = ollamaChatModel.stream(new Prompt(DEFAULT_PROMPT));
return stream.map(resp -> resp.getResult().getOutput().getContent());
}
使用stream配合reactor的Flux就可以实现流式输出
也可以自定义options,之后传递给Prompt
OllamaOptions customOptions = OllamaOptions.builder()
.withTopP(0.7)
.withModel("llama3")
.withTemperature(0.8)
.build();
new Prompt(DEFAULT_PROMPT, customOptions)
ChatClient
初始化
ChatClient ollamaiChatClient = ChatClient.builder(chatModel)
// 实现 Chat Memory 的 Advisor
// 在使用 Chat Memory 时,需要指定对话 ID,以便 Spring AI 处理上下文。
.defaultAdvisors(
new MessageChatMemoryAdvisor(new InMemoryChatMemory())
)
// 实现 Logger 的 Advisor
.defaultAdvisors(
new SimpleLoggerAdvisor()
)
// 设置 ChatClient 中 ChatModel 的 Options 参数
.defaultOptions(
OllamaOptions.builder()
.withTopP(0.7)
.build()
)
.build();
ChatClient是在ChatModel的基础上做了进一步的封装,可以设置advisor
call
@GetMapping("/simple/chat")
public String simpleChat() {
return ollamaiChatClient.prompt(DEFAULT_PROMPT).call().content();
}
使用call是阻塞一次性输出
stream
@GetMapping("/stream/chat")
public Flux streamChat(HttpServletResponse response) {
response.setCharacterEncoding("UTF-8");
return ollamaiChatClient.prompt(DEFAULT_PROMPT).stream().content();
}
使用stream是流式输出
小结
spring-ai-alibaba提供了
spring-ai-ollama-spring-boot-starter来集成ollama,通过ai.ollama.chat.model可以指定使用本地的deepseek-r1:8b。
spring-ai-ollama-spring-boot-starter提供了基础的ChatModel以及高级版的ChatClient来调用ollama,ChatClient是在ChatModel的基础上做了进一步的封装,可以设置advisor,支持聊天记忆(Chat Memory)、工具/函数调用(Function Calling)、RAG。他们都提供了call及stream方式,call是阻塞一次性输出,stream是流式输出。
doc
- MacBook Pro M2安装deepseek
- spring-ai-alibaba
- spring-ai-alibaba-examples
- 基于开源框架Spring AI Alibaba快速构建Java应用
- 支持 40+ 插件,Spring AI Alibaba 简化智能体私有数据集成