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 Search
、Chroma
、Milvus
、MongoDB Atlas
、Neo4j
、Oracle
、PostgreSQL/PGVector
、PineCone
、Qdrant
、Redis
和Weaviate
。 - 跨向量存储提供商的便携式 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 快速生成一个工程模板.
生成后下载代码本地用IDE打开即可. 笔者使用的是IntelliJ IDEA
.
配置好jdk, 用maven安装完依赖后,即可尝试运行,注意,默认需要一个OPENAI_API_KEY,设置在application.properties
的spring.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
的对比正在探索中,后续会以博客的形式持续发布,欢迎讨论.