当前位置: 首页 > 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.dtcms.com/a/204058.html

相关文章:

  • 影刀Fun叉鸟-2048
  • Python 包管理工具核心指令uvx解析
  • 有没有其他影视app可以像群晖video station一样可以被Windows的本地网络驱动器找到
  • 啤酒游戏与系统思考
  • 大模型如何助力数学可视化?
  • Supplemental Table 5FAM49B H-SCORE与其他临床特征的关系
  • uni-app使用大集
  • Python打卡训练营day28-类的定义与方法
  • Wireshark抓包分析小程序接口请求教程
  • 前端JavaScript-嵌套事件
  • C++ stack对象创建、入栈、获取栈顶
  • 深入学习LLM开发 第二四章:向量数据库说明
  • 项目优先级不清,如何合理分配资源?
  • ubuntu24.04+RTX5090D 显卡驱动安装
  • 风车聊天室nodejs环境即可无需数据库
  • Android开发——不同布局的定位属性 与 通用属性
  • 【机器学习基础】机器学习与深度学习概述 算法入门指南
  • 高噪声下扩展边缘检测算子对检测边缘的影响
  • Linux中进程控制(上)
  • cmd如何从C盘默认路径切换到D盘某指定目录
  • CVE-2018-1270源码分析与漏洞复现(spring-messaging 表达式注入)
  • f-string 高效的字符串格式化
  • 如何提高独立服务器的安全性?
  • Mysql的binlog日志
  • 实时监控服务器CPU、内存和磁盘使用率
  • [Java实战]Spring Boot整合Prometheus:应用性能监控与可视化(三十二)
  • IDEA推送到gitlab,jenkins识别,然后自动发布到需要的主机(流水线)
  • 【iOS】分类、扩展、关联对象
  • AI数字人一体机和智慧屏方案:开启智能交互新纪元
  • 在实际网络部署中,静态路由的优先级通常高于RIP