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

Spring AI简介

spring ai

文章目录

    • 主要功能
    • 环境要求
    • 快速上手
    • 总结

在Java开发中,Spring全家桶是我们用的最多的框架,覆盖了几乎所有的应用场景.
随着大语言模型(LLMs)时代的到来,Spring也迅速的推出了 Spring AI来帮助开发者在Spring框架下开发AI应用,集成各种AI基础设施.

主要功能

支持所有主要人工智能模型提供商,包括 Anthropic、OpenAI、Microsoft、Amazon、Google和 Ollama。支持的模型类型包括:

  • 聊天/文字生成
  • 嵌入(embedding)
  • 文本到图像
  • 音频转录
  • 文本到语音
  • 内容审核
  • 支持跨人工智能提供商的便携式 API,包括同步和流式 API 选项。还可以访问特定于模型的功能。
  • 结构化输出——将人工智能模型的输出映射到普通 Java 对象(POJOs)。
  • 支持所有主要向量数据库提供商,包括 Apache Cassandra Azure Vector SearchChromaMilvusMongoDB AtlasNeo4jOraclePostgreSQL/PGVectorPineConeQdrantRedisWeaviate
  • 跨向量存储提供商的便携式 API,包括一种类似 SQL 的元数据过滤器 API。
  • 工具/函数调用——允许模型请求执行客户端工具和函数,从而按需访问必要的实时信息。
  • 可观察性——提供对人工智能相关操作的洞察。
  • 数据工程的文档注入 ETL 框架。
  • 人工智能模型评估——提供工具以帮助评估生成的内容,并防止出现幻觉式响应。
  • ChatClient API——与 WebClient 和 RestClient API 风格类似的流畅 API,用于与人工智能聊天模型进行通信。
  • Advisors API——封装了重复的生成式人工智能模式,转换发送到和从语言模型(LLMs)接收的数据,并提供跨各种模型和用例的可移植性。
  • 支持聊天对话记忆和检索增强生成(RAG)。
  • 为所有人工智能模型和向量存储提供 Spring Boot 自动配置和启动器——可以使用 start.spring.io 来选择所需的模型或向量存储。

环境要求

根据文档显示:

Spring AI supports Spring Boot 3.4.x.

Spring AI要求 Spring Boot 3.4.x

这个版本的Spring Boot 对Java版本也有要求:

Spring Boot 3.4.4 requires at least Java 17 and is compatible with versions up to and including Java 24. Spring Framework 6.2.5 or above is also required.

因此需要至少 Java 17, 当前最高支持到Java 24

快速上手

创建spring ai 项目可以用spring intializr 快速生成一个工程模板.
spring intializr
生成后下载代码本地用IDE打开即可. 笔者使用的是IntelliJ IDEA.
配置好jdk, 用maven安装完依赖后,即可尝试运行,注意,默认需要一个OPENAI_API_KEY,设置在application.propertiesspring.ai.openai.api-key中,否则运行的时候会提示缺少配置. 成功运行后,可以看到如下的控制台输出:

  .   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v3.4.4)2025-04-24T11:36:35.516+08:00  INFO 8640 --- [spring-ai-demo] [           main] s.a.e.s.SpringAiDemoApplication          : Starting SpringAiDemoApplication using Java 24.0.1 with PID 8640 (/Users/yiming/Downloads/spring-ai-demo/target/classes started by yiming in /Users/yiming/Downloads/spring-ai-demo)
2025-04-24T11:36:35.517+08:00  INFO 8640 --- [spring-ai-demo] [           main] s.a.e.s.SpringAiDemoApplication          : No active profile set, falling back to 1 default profile: "default"
2025-04-24T11:36:35.835+08:00  INFO 8640 --- [spring-ai-demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2025-04-24T11:36:35.840+08:00  INFO 8640 --- [spring-ai-demo] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2025-04-24T11:36:35.840+08:00  INFO 8640 --- [spring-ai-demo] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.39]
2025-04-24T11:36:35.854+08:00  INFO 8640 --- [spring-ai-demo] [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2025-04-24T11:36:35.854+08:00  INFO 8640 --- [spring-ai-demo] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 265 ms
2025-04-24T11:36:36.101+08:00  INFO 8640 --- [spring-ai-demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2025-04-24T11:36:36.104+08:00  INFO 8640 --- [spring-ai-demo] [           main] s.a.e.s.SpringAiDemoApplication          : Started SpringAiDemoApplication in 0.711 seconds (process running for 6.114)

根据官方的例子,把SpringAiDemoApplication.java改成如下

package spring.ai.example.spring_ai_demo;import org.springframework.ai.chat.client.ChatClient;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;@SpringBootApplication
public class SpringAiDemoApplication {public static void main(String[] args) {SpringApplication.run(SpringAiDemoApplication.class, args);}@Beanpublic CommandLineRunner runner(ChatClient.Builder builder) {return args -> {ChatClient chatClient = builder.build();String response = chatClient.prompt("Tell me a joke").call().content();System.out.println(response);};}
}

再次尝试运行,即可得到从OpenAI语言模型返回的输出:

.   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v3.4.4)2025-04-24T11:46:03.683+08:00  INFO 8710 --- [spring-ai-demo] [           main] s.a.e.s.SpringAiDemoApplication          : Starting SpringAiDemoApplication using Java 24.0.1 with PID 8710 (/Users/yiming/Downloads/spring-ai-demo/target/classes started by yiming in /Users/yiming/Downloads/spring-ai-demo)
2025-04-24T11:46:03.684+08:00  INFO 8710 --- [spring-ai-demo] [           main] s.a.e.s.SpringAiDemoApplication          : No active profile set, falling back to 1 default profile: "default"
2025-04-24T11:46:04.030+08:00  INFO 8710 --- [spring-ai-demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2025-04-24T11:46:04.035+08:00  INFO 8710 --- [spring-ai-demo] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2025-04-24T11:46:04.035+08:00  INFO 8710 --- [spring-ai-demo] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.39]
2025-04-24T11:46:04.048+08:00  INFO 8710 --- [spring-ai-demo] [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2025-04-24T11:46:04.048+08:00  INFO 8710 --- [spring-ai-demo] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 290 ms
2025-04-24T11:46:04.304+08:00  INFO 8710 --- [spring-ai-demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2025-04-24T11:46:04.307+08:00  INFO 8710 --- [spring-ai-demo] [           main] s.a.e.s.SpringAiDemoApplication          : Started SpringAiDemoApplication in 0.748 seconds (process running for 6.148)
Why did the scarecrow win an award? Because he was outstanding in his field!

证明调用成功.


总结

使用Spring AI调用LLMs来完成任务非常方便,也没有复杂的配置,做到了开箱即用,更多功能以及和LangChain,LlamaIndex的对比正在探索中,后续会以博客的形式持续发布,欢迎讨论.

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

相关文章:

  • vscode vue文件单行注释失效解决办法
  • 基于Keras3.x使用CNN实现简单的猫狗分类
  • WAMP设置外网访问
  • servlet-优化
  • ASP.NET Core 主机模型详解:Host、WebHost与WebApplication的对比与实践【代码之美】
  • 实现优雅的分页导航:从原理到最佳实践
  • Java查询数据库表信息导出Word
  • C++ STL priority_queue 详解:从基础到自定义类型
  • Spring Boot YML配置值“011“在代码中变为9的问题解析
  • 济南国网数字化培训班学习笔记-第二组-4节-输电线路工程安全管理
  • 二分小专题
  • 1Panel+Halo快速部署:简化服务器管理与网站搭建流程探索
  • MySQL 报错解析:SQLSyntaxErrorException caused by extra comma before FROM
  • 美团获全国首张低空物流全境覆盖运营合格证,其第四代无人机具备全域环境适应能力
  • redis经典问题
  • Redis 基础和高级用法入门
  • 【每天一个知识点】熵(Entropy)
  • Redis 核心应用场景
  • Linux 网络基础三 (数据链路层协议:以太网协议、ARP 协议)
  • Linux系统的延迟任务及定时任务
  • 济南国网数字化培训班学习笔记-第二组-6-输电线路现场教学
  • 一个开源且具有直观视觉界面的 API,可实现 DeepSeek 与 SillyTavern 的非官方集成。
  • 关于QT信号、槽、槽函数的讲解
  • Flutter Dart 循环语句 for while do..while break、continue
  • 第二章、安全认证
  • JavaWeb:Web介绍
  • 【Java实战经验】泛型-类型灵活使用与限制
  • 在线地图工具geojson.io
  • 【数据可视化-28】2017-2025 年每月产品零售价数据可视化分析
  • 第53讲 农学科研中的AI伦理与可解释性——探索SHAP值、LIME等可解释工具与科研可信性建设之道