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

相关推荐
念何架构之路2 分钟前
go-grpc服务端调用
开发语言·后端·golang
我的div丢了肿么办12 分钟前
go语言中如何安装第3方的包
后端·go
弹简特15 分钟前
【Java项目-企悦抽】13-奖品管理模块-奖品列表和创建奖品的实现
java·开发语言·网络·springboot
小蒜学长23 分钟前
基于Spring Boot+Vue的“禾源”农产品销售系统设计与实现(代码+数据库+LW)
java·spring boot·后端·农产品销售系统·产品溯源
光年200023 分钟前
Mock-Server开箱即用的Mock服务
java·maven·mock·jetty
弹简特24 分钟前
【Java项目-企悦抽】14-活动管理模块01-创建活动的实现
java·开发语言·springboot
用户EasyAdminBlazor25 分钟前
EasyAdminBlazor 审批事务:为什么审批失败必须全部回滚?
后端
墨鱼老师28 分钟前
SpringBoot+Vue 毕设项目开发踩坑记录(前后端分离)
vue.js·spring boot·毕业设计·课程设计·前后端分离·java 毕设
迅猛龙办公室30 分钟前
Python实现求两个数字之和
java·开发语言·python
计算机魔术师32 分钟前
我让AI教学生写前端,三天后课堂变了——Web教育者的集体反思
后端