Spring Boot + Spring AI快速体验

📅 发布时间:2026/7/6 7:19:41 👁️ 浏览次数:
Spring Boot + Spring AI快速体验
Spring AI快速体验1 什么是Spring AI主要功能2 快速开始2.1 版本说明2.2 配置文件2.3 pom依赖2.3.1 spring maven仓库2.3.2 核心依赖2.4 定义ChatClient2.5 启动类2.6 测试3 参考链接1 什么是Spring AISpring AI是Spring的一个子项目是Spring专门面向于AI的应用框架。Spring AI 项目旨在简化整合人工智能功能的应用程序开发避免不必要的复杂性。汲取了著名的 Python 项目 LangChain 和 LlamaIndex 的灵感但 Spring AI 并不是这些项目的直接移植。该项目的成立的信念下一波生成式人工智能应用程序不仅将面向Python开发人员而且将在许多编程语言中无处不在。主要功能● 跨 AI 供应商的便携式 API支持聊天、文生图、嵌入模型支持同步和流式API选项访问特定模型功能。● 支持几乎所有的ai模型提供商如如 Anthropic、OpenAI、Microsoft、Amazon、Google 和 Ollama包括国内的千帆、智谱AI等● 结构化输出将AI模型输出映射为POJO● 支持所有主要向量数据库。● 功能调用Tools/Function Calling工具/功能调用-允许模型请求执行客户端工具和功能从而根据需要访问必要的实时信息。● 可观测● springboot自动装配● 支持聊天对话记忆和检索增强生成RAG2 快速开始2.1 版本说明工具版本jdk17Spring Boot3.2.x、3.3.xSpring AI1.0.0-SNAPSHOT2.2 配置文件注意使用官方直连的api-key、url或者使用基于转发的api-key、url。主要区别是官方的价格较贵需要在程序中设置代理连接转发类的国内可直接使用价格便宜适合学习使用。2.3 pom依赖2.3.1 spring maven仓库repositories !-- Spring Milestones 仓库 -- repository idspring-milestones/id nameSpring Milestones/name urlhttps://repo.spring.io/milestone/url snapshots enabledfalse/enabled /snapshots /repository !-- Spring Snapshots 仓库 -- repository idspring-snapshots/id nameSpring Snapshots/name urlhttps://repo.spring.io/snapshot/url releases enabledfalse/enabled /releases /repository /repositories2.3.2 核心依赖!--open ai-- dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-openai-spring-boot-starter/artifactId /dependency !--Spring AI 物料清单BOM声明了 Spring AI 发布版本使用的所有依赖项的推荐版本。 使用应用程序构建脚本中的 BOM 可以避免自行指定和维护依赖项版本的需要。-- dependencyManagement dependencies dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-bom/artifactId version1.0.0-SNAPSHOT/version typepom/type scopeimport/scope /dependency /dependencies /dependencyManagement2.4 定义ChatClientimport org.springframework.ai.chat.client.ChatClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; Configuration class Config { Bean ChatClient chatClient(ChatClient.Builder builder) { return builder.build(); } }2.5 启动类import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; SpringBootApplication public class SpringAiDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringAiDemoApplication.class, args); } }2.6 测试import org.springframework.ai.chat.client.ChatClient; 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; RestController RequestMapping(/api) public class ChatController { Autowired private ChatClient chatClient; GetMapping(/chat) public String chat(RequestParam(value message, defaultValue Hi) String message) { return chatClient.prompt() .user(message) .call() .content(); } }文本返回流式返回打字机效果GetMapping(value /chat/flux, produces MediaType.TEXT_HTML_VALUE ;charsetUTF-8) public FluxString chatFlux(RequestParam(value message, defaultValue Hi) String message) { FluxString output chatClient.prompt() .user(message) .stream() .content(); return output; }完整代码可参考https://github.com/xgxizz/spring-ai-demo3 参考链接https://docs.spring.io/spring-ai/reference/