Spring AI + Ollama 实战:5分钟搞定本地Qwen3-72B模型接入(附完整代码)

Spring AI + Ollama 实战:5分钟搞定本地Qwen3-72B模型接入(附完整代码) Spring AI与Ollama深度整合5分钟实现Qwen3-72B本地模型高效调用当Java开发者需要快速集成前沿AI能力时Spring AI与Ollama的组合正在成为技术栈中的新宠。本文将带您跳过繁琐的配置过程直击核心实现方案通过可复用的代码模块快速完成Qwen3-72B大语言模型的本机部署与调用。1. 环境准备与依赖配置在开始前请确保您的开发环境满足以下基础要求已安装并运行Ollama服务默认端口11434本地已加载Qwen3-72B模型通过ollama pull qwen3:72bJDK 17环境Spring Boot 3.x项目关键依赖配置Maven示例dependencyManagement dependencies dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-bom/artifactId version1.0.0-M8/version typepom/type scopeimport/scope /dependency /dependencies /dependencyManagement dependencies !-- Ollama集成核心包 -- dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-ollama-spring-boot-starter/artifactId /dependency !-- Web支持 -- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-webflux/artifactId /dependency /dependencies提示推荐使用Spring Boot 3.1.5及以上版本以获得最佳兼容性。若遇到依赖冲突可尝试通过mvn dependency:tree排查。2. 极简服务层实现Spring AI的自动配置特性让服务层实现异常简洁。以下是经过生产验证的优化实现方案import org.springframework.ai.ollama.OllamaChatClient; import org.springframework.ai.ollama.api.OllamaOptions; import org.springframework.stereotype.Service; import reactor.core.publisher.Flux; Service public class AIGatewayService { private final OllamaChatClient chatClient; public AIGatewayService(OllamaChatClient chatClient) { this.chatClient chatClient; } public FluxString streamCompletion(String prompt) { OllamaOptions options OllamaOptions.create() .withModel(qwen3:72b) .withTemperature(0.7f); return chatClient.stream(prompt, options); } }关键参数说明参数类型说明推荐值modelString模型标识符qwen3:72btemperatureFloat生成多样性0.5-1.0topPFloat核采样阈值0.93. 高性能API接口设计针对大模型响应特点我们采用响应式编程模型实现非阻塞接口import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; RestController RequestMapping(/api/v1/ai) public class AIController { private final AIGatewayService gatewayService; public AIController(AIGatewayService gatewayService) { this.gatewayService gatewayService; } PostMapping(/completions) public FluxString generateCompletion(RequestBody CompletionRequest request) { return gatewayService.streamCompletion(request.prompt()) .onErrorResume(e - Flux.just(Error: e.getMessage())); } public record CompletionRequest(String prompt) {} }性能优化技巧使用WebFlux替代传统MVC配置合理的超时参数建议30-60秒启用响应式背压支持4. 常见问题排查指南在实际部署过程中可能会遇到以下典型问题连接失败排查流程验证Ollama服务状态curl http://localhost:11434/api/tags检查模型是否加载完成确认Spring应用配置spring.ai.ollama.base-urlhttp://localhost:11434 spring.ai.ollama.chat.options.modelqwen3:72b内存优化建议为JVM分配足够堆空间建议4G使用GGUF量化模型版本启用Ollama的GPU加速5. 进阶应用场景突破基础问答功能我们可以实现更复杂的AI集成模式多模型路由示例public FluxString routeToModel(String prompt, ModelType type) { OllamaOptions options switch (type) { case CODE - OllamaOptions.create().withModel(codellama:7b); case GENERAL - OllamaOptions.create().withModel(qwen3:72b); }; return chatClient.stream(prompt, options); }对话历史管理ListMessage messages new ArrayList(); messages.add(new UserMessage(你好)); messages.add(new AssistantMessage(您好有什么可以帮您)); Prompt prompt new Prompt(messages); chatClient.stream(prompt);通过Spring AI的模块化设计我们可以轻松扩展出文档处理、数据分析等专业场景的解决方案。这种轻量级集成方式特别适合需要快速验证AI能力的创新项目。