news 2026/7/12 17:52:29

Spring AI + Ollama简单使用

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Spring AI + Ollama简单使用

一、环境准备

1. 前置条件

  1. 本地安装 Ollama,启动服务(默认地址:http://localhost:11434
  2. 拉取任意模型(示例用 qwen3.5:4b,轻量快速)
    docker exec -it ollama ollama run qwen3.5:4b
  3. SpringBoot 4.x 项目

2. Maven 依赖 pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>4.1.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.ybw</groupId> <artifactId>spring-ai-demo</artifactId> <version>1.0.0</version> <name>spring-ai-demo</name> <description>spring-ai-demo</description> <modules> <module>quick-start</module> </modules> <packaging>pom</packaging> <properties> <java.version>21</java.version> <spring-ai.version>2.0.0</spring-ai.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-starter-model-ollama</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webmvc-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-bom</artifactId> <version>${spring-ai.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <executions> <execution> <id>default-compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> <configuration> <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </path> </annotationProcessorPaths> </configuration> </execution> <execution> <id>default-testCompile</id> <phase>test-compile</phase> <goals> <goal>testCompile</goal> </goals> <configuration> <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </path> </annotationProcessorPaths> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>

二、配置文件 application.yml

spring: application: name: spring-ai-demo ai: ollama: base-url: http://127.0.0.1:11434 chat: model: qwen3.5:4b # 模型 temperature: 0.7 # 温度,数值越高,输出结果越随机,数值越低,输出结果越一致。范围0-1 top-p: 0.7 # 概率 think: true # 是否思考

温度

temperature 取值范围:0 ~ 1,核心逻辑:数值越大,模型随机性、创造性越强;数值越小,输出越确定、严谨、重复度低。

温度区间核心风格推荐业务场景
0严谨、统一、无幻觉代码、SQL、结构化输出、数学、标准问答
0.1~0.3专业、精准、轻微灵活技术 / 法律 / 医疗专业问答、文档解析
0.4~0.7均衡自然(默认)日常聊天、科普、工作总结、普通文案
0.8~0.9高创意、发散写诗、故事、策划、起名、脑洞创作
1.0极致随机纯艺术自由创作,极少业务使用

三、配置类 + 测试 Demo

配置 ChatClient

package com.ybw.config; import org.springframework.ai.chat.client.ChatClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 配置 ChatClient * * @author ybw * @version V1.0 * @className ChatClientConfig * @date 2026/6/29 **/ @Configuration public class ChatClientConfig { /** * 注入 ChatClient,SpringAI 自动装配 OllamaChatModel * * @param builder 构建 ChatClient * @methodName: chatClient * @return: org.springframework.ai.chat.client.ChatClient * @author: ybw * @date: 2026/6/29 **/ @Bean ChatClient chatClient(ChatClient.Builder builder) { return builder.build(); } }

单元测试

无思考过程

package com.ybw; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.ai.chat.client.ChatClient; import org.springframework.boot.test.context.SpringBootTest; /** * 测试ollama * * @author ybw * @version V1.0 * @className TestOllama * @date 2026/6/29 **/ @SpringBootTest @Slf4j public class TestOllama { @Resource private ChatClient chatClient; /** * 测试ollama:简单聊天demo,打印出聊天结果 */ @Test public void testOllama() { // 1. 单次简单提问 String prompt = "请写一首描述清晨的诗"; log.info("===== 用户提问:{} =====", prompt); // 同步调用 ollama,获取完整返回文本 String response = chatClient.prompt() .user(prompt) .call() .content(); // 打印大模型回答 log.info("===== Ollama 返回结果 ====="); log.info(response); log.info("==========================="); // 2. 多轮对话示例(携带上下文) log.info("===== 多轮对话测试 ====="); String multiRoundRes = chatClient.prompt() .user("什么是Java?") .system("你是简洁的编程讲师,回答不超过两句话") .call() .content(); log.info(multiRoundRes); } /** * 测试ollama:流式输出聊天结果 */ @Test public void testOllamaStream() { // 流式响应,逐段打印 chatClient.prompt() .user("写一段100字的春日短文") .stream() .content() .doOnNext(System.out::print) .blockLast(); } }
  • 流式输出(逐行实时打印,类似打字机效果)

有思考过程

package com.ybw; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.messages.AssistantMessage; import org.springframework.ai.chat.model.ChatResponse; import org.springframework.boot.test.context.SpringBootTest; import reactor.core.publisher.Flux; import java.util.Map; /** * 测试ollama思考过程 * * @author ybw * @version V1.0 * @className TestThinkOllama * @date 2026/6/29 **/ @SpringBootTest @Slf4j public class TestThinkOllama { @Resource private ChatClient chatClient; /** * 测试ollama:思考过程 */ @Test public void testThinkOllama() { //1. 创建一个 Prompt String prompt = "java是什么?"; log.info("===== 用户提问:{} =====", prompt); //2. 调用 ollama ChatResponse chatResponse = chatClient.prompt() .user(prompt) .call() .chatResponse(); // 3. 关键修改:获取完整输出内容 AssistantMessage assistantMessage = chatResponse.getResult().getOutput(); // 3.1 获取思考过程(从 metadata 中提取) Map<String, Object> metadata = assistantMessage.getMetadata(); String thinking = metadata.get("thinking").toString(); log.info("===== 思考过程 ====="); log.info(thinking); // 3.2 获取最终答案 String finalAnswer = assistantMessage.getText(); log.info("===== 最终答案 ====="); log.info(finalAnswer); } /** * 测试ollama:流式输出聊天结果 */ @Test public void testThinkOllamaStream() { //1. 最终结果存储 StringBuilder finishThinkingStr = new StringBuilder(); StringBuilder finishAnswerStr = new StringBuilder(); //1. 流式响应,逐段打印 Flux<ChatResponse> chatResponseFlux = chatClient.prompt() .user("你好") .stream() .chatResponse(); // 2. 逐段打印 chatResponseFlux.doOnNext(chatResponse -> { // 2.1 获取完整输出内容 AssistantMessage assistantMessage = chatResponse.getResult().getOutput(); // 2.2 获取思考过程(从 metadata 中提取) Map<String, Object> metadata = assistantMessage.getMetadata(); Object thinking = metadata.get("thinking"); if (thinking != null) { String thinkingStr = thinking.toString(); // 灰色显示思考过程 System.out.print("\u001B[90m" + thinkingStr + "\u001B[0m"); finishThinkingStr.append(thinkingStr); } else { //只有刚开始打印的时候,换一次行 if (finishAnswerStr.isEmpty()) { System.out.println(); } // 2.3 获取最终答案 String finalAnswer = assistantMessage.getText(); System.out.print(finalAnswer); finishAnswerStr.append(finalAnswer); } }).doOnComplete(() -> { System.out.println(); log.info("思考:{}", finishThinkingStr); log.info("回复:{}", finishAnswerStr); }) // 等待流结束 .blockLast(); } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/13 4:57:32

虚拟化技术中的容器编排资源隔离与性能优化

虚拟化技术中的容器编排资源隔离与性能优化 随着云计算和微服务架构的普及&#xff0c;容器技术凭借其轻量级、快速部署的优势成为现代应用开发的核心工具。在多租户或高密度部署场景下&#xff0c;容器间的资源隔离与性能优化成为关键挑战。容器编排平台&#xff08;如Kubern…

作者头像 李华
网站建设 2026/7/13 4:57:31

2026亲测:专业降AIGC平台首选方案

2026 年降 AIGC 工具已经从“简单语句重构”进化为多维度智能优化系统&#xff0c;核心评估指标涵盖 AI 痕迹识别精准度、语言风格自然度、专业表达一致性、格式结构完整性、长段落逻辑流畅性以及高校检测平台兼容性。本次测评聚焦 5 款主流工具&#xff0c;测试内容覆盖中英文…

作者头像 李华
网站建设 2026/7/13 4:57:33

AHE解读:让Coding Agent的工具、记忆与中间件自动进化

AHE解读&#xff1a;让Coding Agent的工具、记忆与中间件自动进化 摘要 提升 Coding Agent&#xff0c;常见做法是更换模型或继续修改系统提示词。但 Agent 的真实能力还取决于外层 Harness&#xff1a;工具接口、中间件、长期记忆、技能和执行约束。论文 Agentic Harness Engi…

作者头像 李华
网站建设 2026/7/13 4:57:31

linux(2)

mmore 22222 nohup.out

作者头像 李华
网站建设 2026/7/13 4:57:46

VSCode插件变黑客后门!GitHub 3800个仓库被攻破

兄弟们&#xff0c;今天要说个大事。 GitHub 刚刚确认了一起严重的安全事故——3800 个私有仓库被黑了&#xff01; 而且攻击手段简直离谱&#xff1a;黑客伪装成 VSCode 插件&#xff0c;直接从开发者电脑上偷代码... 这波操作有多骚&#xff1f; 事情是这样的。 黑客制作…

作者头像 李华
网站建设 2026/7/13 4:57:50

NFC标签NDEF数据读写实战:从CC/TLV原理到TRF7970A开发全解析

1. 项目概述与核心概念解析如果你曾经用手机触碰公交卡、门禁卡或者一个智能海报&#xff0c;体验过“嘀”一声就完成信息交换的便捷&#xff0c;那么你已经亲身体验了NFC&#xff08;近场通信&#xff09;技术。这项技术背后的核心&#xff0c;是如何在小小的标签里&#xff0…

作者头像 李华