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 小时前
第07章 孙德良的阳谋|《互联网产品恩仇录》
javascript·后端
非优秀程序员1 小时前
第06章 浙江帮的内战|《互联网产品恩仇录》
后端
非优秀程序员1 小时前
第08章 马云的反击|《互联网产品恩仇录》
后端
非优秀程序员2 小时前
题记|《互联网产品恩仇录》
后端·程序员·创业
非优秀程序员2 小时前
第04章 邮箱大战,163 与 QQ 的暗战|《互联网产品恩仇录》
java·vue.js·架构
知已难寻2 小时前
Docker 入门:容器技术与 Docker 基础
java·运维·服务器·docker·云原生·容器
文丑颜不良啊4 小时前
LeetCode2481~2500题目解析
java·leetcode
Flynn Liang5 小时前
同一需求三套写法:用 Spring AI、LangChain4j、AgentScope Java 各实现一个带工具调用的 RAG 助手(实战对比)
java·人工智能·大模型·架构设计·spring ai·langchain4j·agentscope
Gopher_HBo5 小时前
Go CPU 剖析的"地基":信号、CAS、写屏障、无锁环形缓冲
后端
panbook6 小时前
从0到1实现一个分布式时序数据库(二):写入成功之后,如何用 WAL 找回数据
后端