Springboot整合Ollama实现调用本地多模型

Springboot整合Ollama实现调用本地多模型

1.安装相应的大模型

可以在ollama官网搜索相应的大模型,直接copy相应命令,在powershell窗口执行ollama命令

查看命令:

bash 复制代码
ollama list

2.pom.xml

引入Ollama依赖

xml 复制代码
<dependency>
			<groupId>org.springframework.ai</groupId>
			<artifactId>spring-ai-starter-model-ollama</artifactId>
			<version>1.0.0</version>
		</dependency>

3.application.properties

bash 复制代码
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

java 复制代码
@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

java 复制代码
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

java 复制代码
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);
	}
}

6.测试结果

相关推荐
霸道流氓气质1 小时前
Java 工程师 AI 智能体(Agent)完整学习路线
java·人工智能·学习
aaPIXa6221 小时前
C++模板元编程:编译期计算Fibonacci数列
java·开发语言·c++
SunnyDays10111 小时前
Java 编辑 PDF:从增删改查到加密保护
java·修改 pdf·编辑 pdf
张三丰22 小时前
Agent 不只是聊天框:用 ArkUI 做一个鸿蒙任务工作台
后端
卷无止境2 小时前
从Python的cryptography库出发,从零开始理解密码学,到构建零信任网络
后端·python
Augustzero2 小时前
`co_await` 按下暂停键之后:从零看懂 C++20 协程
c++·后端
球king2 小时前
CC GUI 插件:在 IDEA 中使用 CodeX
java·ide·intellij-idea
宠友信息2 小时前
多端即时通讯源码技术实践,Redis路由、MySQL持久化与Socket接入
spring boot·websocket·mysql·uni-app
豆瓣鸡2 小时前
网关、服务 userId 透传
java·微服务·gateway