Springboot整合Ollama实现调用本地多模型
作者:Listen·Rain
想知道如何用ollama快速安装和调用大模型吗,本文带你从ollama官网搜索模型、使用powershell命令一键安装,到springboot项目中引入依赖、配置参数、编写服务,最后通过controller测试接口,轻松实现ollama大模型的本地部署和api调用,需要的朋友可以参考下
1.安装相应的大模型
可以在ollama官网搜索相应的大模型,直接copy相应命令,在powershell窗口执行ollama命令

查看命令:
ollama list
![]()
2.pom.xml
引入Ollama依赖
<dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-starter-model-ollama</artifactId> <version>1.0.0</version> </dependency>
3.application.properties
server.port: 8088 server.servlet.encoding.charset=UTF-8 server.servlet.encoding.enabled=true server.servlet.encoding.force=true spring.ai.ollama.base-url=http://localhost:11434
4.OllamaConfig
@Configuration
public class OllamaConfig {
private static final String QWEN = "qwen3:0.6b";
private static final String DEEPSEEK = "deepseek-r1:1.5b";
@Bean(name = "qwenChatClient")
public ChatClient qwenChatClient(ChatClient.Builder builder) {
OllamaOptions ollamaOptions = OllamaOptions.builder().build();
ollamaOptions.setModel(QWEN);
return builder.defaultOptions(ollamaOptions).build();
}
@Bean(name = "deepseekChatClient")
public ChatClient deepseekChatClient(ChatClient.Builder builder) {
OllamaOptions ollamaOptions = OllamaOptions.builder().build();
ollamaOptions.setModel(DEEPSEEK);
return builder.defaultOptions(ollamaOptions).build();
}
}
5.OllamaService
package com.example.demoredisstack.service;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
@Service
public class OllamaService {
@Autowired
private ChatClient qwenChatClient;
@Autowired
private ChatClient deepseekChatClient;
public String qwenChat(String message) {
return qwenChatClient
.prompt() // 开始构建提示词
.user(message) // 设置用户消息
.call() // 发起调用
.content(); // 直接获取字符串内容
}
public Flux<String> qwenChatStream(String message) {
return qwenChatClient
.prompt()
.user(message)
.stream()
.content();
}
public String deepseekChat(String message) {
return deepseekChatClient
.prompt() // 开始构建提示词
.user(message) // 设置用户消息
.call() // 发起调用
.content(); // 直接获取字符串内容
}
public Flux<String> deepseekChatStream(String message) {
return deepseekChatClient
.prompt()
.user(message)
.stream()
.content();
}
}
6. TestOllamaController
package com.example.demoredisstack.controller;
import com.example.demoredisstack.service.OllamaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
@RestController
@RequestMapping("/test/ollama")
public class TestOllamaController {
@Autowired
private OllamaService ollamaService;
@GetMapping("/qwen")
public String qwenChat(@RequestParam(value = "message", defaultValue = "你好") String message) {
return ollamaService.qwenChat(message);
}
@GetMapping("/qwenStream")
public Flux<String> qwenChatSteam(@RequestParam(value = "message", defaultValue = "你好") String message) {
return ollamaService.qwenChatStream(message);
}
@GetMapping("/deepseek")
public String deepseekChat(@RequestParam(value = "message", defaultValue = "你好") String message) {
return ollamaService.deepseekChat(message);
}
@GetMapping("/deepseekStream")
public Flux<String> deepseekChatStream(@RequestParam(value = "message", defaultValue = "你好") String message) {
return ollamaService.deepseekChatStream(message);
}
}
7.测试结果


以上就是Springboot整合Ollama实现调用本地多模型的详细内容,更多关于Springboot整合Ollama调用本地多模型的资料请关注脚本之家其它相关文章!
