Spring AI 项目旨在简化开发包含人工智能功能的应用程序,而无需不必要的复杂性。
该项目从著名的 Python 项目中汲取灵感,如 LangChain 和 LlamaIndex,但 Spring AI 并不是这些项目的直接移植。
Spring AI 提供以下功能:
支持所有主要的模型提供商,如 OpenAI、Microsoft、Amazon、Google 和 Huggingface。
支持的模型类型包括聊天和文本到图像,未来将支持更多类型。
跨 AI 提供商的便携式 API,支持同步和流式 API 选项。
将 AI 模型输出映射到 POJO。
支持所有主要的向量数据库提供商,如 Azure Vector Search、Chroma、Milvus、Neo4j、PostgreSQL/PGVector、PineCone、Qdrant、Redis 和 Weaviate。
官方文档:https://docs.spring.io/spring-ai/reference/
本文代码示例是基于OpenAI的,有些小伙伴没有网络环境,可以使用我的中转站:https://one.mengxiaofan.top/
更多信息可点击查看
示例中我将使用中转站
文中示例jdk版本为jdk17,springboot版本为3.2.5
废话不多说直接贴maven信息
<properties>
<java.version>17</java.version>
<spring-ai.version>0.8.1</spring-ai.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-json</artifactId>
<version>5.8.27</version>
</dependency>
</dependencies>
1.1 修改配置文件 修改此项目的application.yml配置文件,核心配置如下
spring:
ai:
openai:
api-key: xxxx
base-url: https://one.mengxiaofan.top/ #这个是我中转站的地址 如果你有接口或者有网络环境这个要换掉
1.2 写一个最简单的调用
@GetMapping("/ai/completion"