十年匠心定制 · 商业建站与技术教学双线并行 咨询热线:400-886-1026 service@lmnt.cn
ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Spring AI 2.0:Hello World

Spring AI 2.0:Hello World 一、简介Spring Boot 4 Spring 7 IDEA 2025 JDK17 Spring AI 的定位是大模型基础对话 RAG MCP Tool Memory等基础功能。Spring AI 2.0 新特性传输协议发生了变化去掉了Spring AI 1.0中的SSE模式Server Send Events 单向长连接使用新的模式MCP Streamable HTTP。在上层ChatClient增加日志拦截器。增加了Agent的模式观察思考 - 执行工具 - 反思迭代 - 验证确认。应用场景智能客服在项目中嵌入一个聊天窗然后通过Tool调用系统内部的Java方法以及通过MCP调用系统外部的功能(如网络搜索、地图、天气等第三方发布的API)原来通过用户点点点来实现的现在通过聊天完成。RAG知识库。二、项目打架1. 添加依赖 pom.xmlSpringWeb Spring AI 第三方厂商如 DeepSeek 、OpenAI、 Ollama要考虑性能、成本、以及行业评分。2. 配置application.properties获取API Key创建API Key: https://bailian.console.aliyun.com/cn-beijing?tabmodel#/api-key获取API Host https://bailian.console.aliyun.com/cn-beijing?tabapi#/api/?typeappurl2782167示例代码获取模型名称、base_url https://bailian.console.aliyun.com/cn-beijing?tabmodel#/model-market/detail/qwen3.7-plus?serviceSiteasia-pacific-chinarefsuggest# 阿里百炼平台 spring.ai.deepseek.api-keysk-3ac8885b9492111111111111111 spring.ai.deepseek.chat.model qwen3.7-plus spring.ai.deepseek.chat.base-urlhttps://llm-x2l5ucz6vk2xupzh.cn-beijing.maas.aliyuncs.com/compatible-mode/v13. Bean ConfigurationConfigurationpublicclassSpringAiConfig{BeanpublicChatClientchatClient(DeepSeekChatModeldeepSeekChatModel){returnChatClient.builder(deepSeekChatModel).defaultSystem( # 角色 你是一个教育行业的只能小助手 # 要求 1. 永远讲中文 ).build();}}4. Controller测试RestControllerpublicclassSpringAIController{AutowiredprivateChatClientchatClient;GetMapping(value/chat,producesMediaType.TEXT_EVENT_STREAM_VALUE)publicFluxStringchat(RequestParam(message)Stringmessage){FluxStringcontentchatClient.prompt().user(message).stream().content().withConcat(Flux.just([complete]));// 自定义一个结束标记前端解析到就不会再请求表示本轮会话全部结束returncontent;}}
返回列表