2026/2/22 22:45:54
网站建设
项目流程
低价建设手机网站,想做一个网站,常熟市建设工程发承包网站,北京 成品网站轻松入门 Spring AI 调用 Ollama #xff08;2025-2026 最新最实用写法#xff09;
目前使用 Spring AI Ollama 最推荐的几种组合方式#xff08;按推荐顺序#xff09;#xff1a;
排名方式优点缺点/限制适合场景推荐度1Spring AI Ollama ChatClient配置最少、写法最自…轻松入门 Spring AI 调用 Ollama2025-2026 最新最实用写法目前使用 Spring AI Ollama 最推荐的几种组合方式按推荐顺序排名方式优点缺点/限制适合场景推荐度1Spring AI Ollama ChatClient配置最少、写法最自然、生态最好需要 Ollama 已经在运行绝大多数日常开发★★★★★2使用 OllamaChatModel 手动创建更灵活可精细控制参数代码稍微多一点需要特殊参数或实验★★★★3AiServices Ollama适合做工具调用/结构化输出/Agent学习曲线稍高中高级用法★★★★4直接用 Ollama Java 官方客户端不依赖 Spring AI完全独立失去 Spring AI 所有高级抽象极致轻量/非 Spring 项目★★最推荐写法99% 场景都够用1. 依赖使用最新稳定版dependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-ollama-spring-boot-starter/artifactIdversion1.0.0-M6 或 1.0.0.RELEASE看你用的 spring boot 版本/version/dependency!-- 如果你用的是快照版或 milestone可能需要添加仓库 --2. application.yml 最简配置spring:ai:ollama:base-url:http://localhost:11434# 默认就是这个几乎不用改chat:options:model:qwen2.5:7b-instruct# ← 改这里就切换模型# 常用推荐模型2026年初# qwen2.5:7b-instruct# deepseek-r1:7b# llama3.2:3b# phi4:14b# gemma2:9btemperature:0.75top-p:0.9max-tokens:40963. 最常用代码模板ChatClient 方式RestControllerRequestMapping(/ollama)RequiredArgsConstructorpublicclassOllamaSimpleController{privatefinalChatClientchatClient;// Spring AI 自动注入// 1. 普通调用GetMapping(/chat)publicStringsimpleChat(RequestParamStringmsg){returnchatClient.prompt().user(msg).call().content();}// 2. 流式输出前端打字机效果GetMapping(value/stream,producesMediaType.TEXT_EVENT_STREAM_VALUE)publicFluxStringstreamChat(RequestParamStringmsg){returnchatClient.prompt().user(msg).stream().content();}// 3. 带系统提示 记忆最实用组合privatefinalChatMemorychatMemorynewMessageWindowChatMemory(10);GetMapping(/memory)publicStringchatWithMemory(RequestParamStringsessionId,RequestParamStringmessage){returnchatClient.prompt().system( 你是一位说话很幽默、接地气、喜欢用表情的资深程序员 用中文回答尽量使用 markdown 格式 ).user(message).advisors(MessageChatMemoryAdvisor.builder().chatMemory(chatMemory).sessionId(sessionId).build()).call().content();}}快速上手检查清单按顺序做步骤命令/操作预期结果安装并启动 Ollama |ollama serve| 看到 http://localhost:11434拉取一个常用模型 |ollama pull qwen2.5:7b-instruct| 模型下载完成命令行简单测试 |ollama run qwen2.5:7b-instruct| 可以正常对话启动 Spring Boot 项目 | 正常启动 | 无报错浏览器访问 |/ollama/chat?msg你好啊| 得到中文回答测试流式 |/ollama/stream?msg讲个程序员笑话| 看到逐字出现效果常见问题速查表2026年初常见坑问题原因解决方案连接超时/拒绝连接Ollama 没启动先运行ollama serve模型找不到模型名写错了ollama list查看已下载模型名中文回答很差/乱码用了不擅长中文的模型换 qwen2.5 / deepseek-r1 / glm4 等流式输出不工作模型本身不支持 streaming部分小模型不支持换大一点的模型显存爆了/启动很慢模型太大显卡不够换 3b/7b 量级模型或加--num-gpu 0只用 CPU一句话总结目前2026年1月最舒服的本地组合Spring Boot 3.3.x Spring AI 1.0.x Ollama qwen2.5:7b-instruct 或 deepseek-r1:7b ChatClient 流式 记忆 系统提示祝你本地玩得开心早日做出自己的小 AI 玩具 需要我帮你把某个特定模型比如 deepseek-r1 / phi4 / gemma2的配置和表现做更详细对比吗