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.测试结果

相关推荐
彭亚川Allen6 小时前
从零手搓 RAG 之分块策略
前端·后端·面试
喵个咪6 小时前
GoWind Ops 拆解:不引入分布式锁的多副本协调
后端·go·自动化运维
迷途小羔羊6 小时前
环境搭建
前端·javascript·后端
XiYang-DING6 小时前
P5528.带过期时间的 LRU 缓存
java·开发语言·缓存
SimonKing6 小时前
DeepSeek 新模型免费白嫖,夯夯夯,赶紧用起来
java·后端·程序员
打工仔折腾 AI6 小时前
介入式脑机接口的跨尺度解码与闭环调控:从算法落地到后端工程实践
人工智能·后端·python·深度学习·算法·ai agent 实战
SabrinaW7116 小时前
基于 C++17 与 RTTR 的注册式对象工厂:配置驱动的动态对象创建
java·前端·数据库
喵个咪7 小时前
GoWind Ops 深度拆解:纯出站 Agent + MQTT 信令,把「管机器、跑项目、发脚本、构建分发」做扎实
后端·go·自动化运维