当前位置: 首页 > news >正文

Spring AI 1.0 快速入门

一、Spring AI 1.0 快速入门

1. 添加依赖

以 Maven 为例,添加 Spring AI 的 Starter 依赖(以 OpenAI 为例):

<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId><version>1.0.0</version>
</dependency>

其他模型如百度文心一言、MiniMax、Azure 等,只需更换对应的 starter。
在这里插入图片描述

2. 配置 API Key

application.ymlapplication.properties 文件中配置 OpenAI 的 API Key:

spring:ai:openai:api-key: sk-xxxxxxx

3. 编写代码调用 AI 能力

以最常用的文本生成(Chat)为例:

import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/ai")
public class AiController {@Autowiredprivate OpenAiChatClient openAiChatClient;@PostMapping("/chat")public String chat(@RequestBody String prompt) {UserMessage userMessage = new UserMessage(prompt);return openAiChatClient.call(userMessage).getResult().getOutput().getContent();}
}

二、案例:实现一个简单的 AI 聊天接口

1. 启动类

@SpringBootApplication
public class AiDemoApplication {public static void main(String[] args) {SpringApplication.run(AiDemoApplication.class, args);}
}

2. 控制器

@RestController
@RequestMapping("/ai")
public class AiController {@Autowiredprivate OpenAiChatClient openAiChatClient;@PostMapping("/chat")public String chat(@RequestBody String prompt) {UserMessage userMessage = new UserMessage(prompt);return openAiChatClient.call(userMessage).getResult().getOutput().getContent();}
}

3. 请求示例

使用 Postman 或 curl 发送请求:

curl -X POST http://localhost:8080/ai/chat -H "Content-Type: application/json" -d "\"你好,帮我写一段自我介绍\""

返回结果会是 AI 回复的内容。


三、支持的 AI 服务商

Spring AI 1.0 支持多种大模型,常见的依赖如下:

  • OpenAI: spring-ai-openai-spring-boot-starter
  • Azure OpenAI: spring-ai-azure-openai-spring-boot-starter
  • 百度文心一言: spring-ai-baidu-spring-boot-starter
  • MiniMax: spring-ai-minimax-spring-boot-starter
  • Google Gemini: spring-ai-google-gemini-spring-boot-starter

只需替换依赖和配置即可切换不同大模型。


四、常见用法

  • 对话/文本生成(如上案例)
  • 函数调用(Function Calling)
  • 图片生成(通过 Image API)
  • 向量检索/嵌入(Vector Store/Embedding)

http://www.xdnf.cn/news/576217.html

相关文章:

  • NVIDIA GPU 性能调优与诊断完全指南
  • ConcurrentHashMap导致的死锁事故
  • 环境搭建
  • 根据Spring官方文档,三分钟完成Springboot项目集成Spring AI
  • sqli-labs第十七关——POST注入点
  • Spring Boot整合Redis
  • RestTemplate 发送的字段第二个大写字母变成小写的问题探究
  • 9-码蹄集600题基础python篇
  • leetcode 螺旋矩阵 java
  • 5-码蹄集600题基础python篇
  • 如何设计智慧工地系统的数据库?
  • 系统程序变更管理:确保IT环境稳定性和安全性的关键
  • Entity-Relationship Model(实体-关系模型)
  • FlashAttention:传统自注意力( Self-Attention)优化加速实现
  • 用户刷题记录日历——签到表功能实现
  • 基于 Guns v5.1 框架的分页教程
  • SseEmitter是什么
  • 卷积神经网络基础(十)
  • chrono类 根据duration 类的周期类型得到对应的周期名称
  • 预警功能深度测评:如何用系统降低设备突发故障率?
  • JavaScript常用事件
  • 第P10周:Pytorch实现车牌识别
  • 如何解决测试覆盖率与迭代速度的冲突问题?
  • 手搓四人麻将程序
  • 正大模型视角下的高频交易因子构建策略研究
  • 视频监控管理平台EasyCVR工业与公共安全监控:监控中心与防爆系统如何集成?
  • 【免杀】C2免杀技术(八)APC注入
  • 数字化转型到底是什么?如何更好的理解数字化转型
  • NOSQL之Redis群集部署
  • 基于Browser Use + Playwright 实现AI Agent操作Web UI自动化