OpenAI 结合业务才更有价值
项目从著名的 Python 项目(例如 LangChain 和 LlamaIndex)中汲取灵感,但 Spring AI 并非这些项目的直接移植。该项目的成立基于这样的信念:下一波生成式 AI 应用将不仅面向 Python 开发人员,还将遍及多种编程语言。
Spring AI 提供以下功能:
支持所有主要模型提供商,例如 OpenAI、Microsoft、Amazon、Google 和 Hugging Face。
支持的模型类型包括聊天、文本到图像、音频转录、文本到语音等。
适用于所有模型的跨 AI 提供商的可移植 API。支持同步和流式 API 选项。还支持下拉以访问特定于模型的功能。
将 AI 模型输出映射到 POJO。
支持所有主要的矢量数据库提供商,例如 Apache Cassandra、Azure Vector Search、Chroma、Milvus、MongoDB Atlas、Neo4j、Oracle、PostgreSQL/PGVector、PineCone、Qdrant、Redis 和 Weaviate。
跨 Vector Store 提供商的可移植 API,包括一种新颖的类似 SQL 的元数据过滤器 API,它也是可移植的。
函数调用。
用于 AI 模型和向量存储的 Spring Boot 自动配置和启动器。
数据工程的 ETL 框架。
Spring AI 引入
spring-ai 也在不断的发展,2024年5月22日 发布了 1.0.0 M1 并对代码做了一些调整。在生产级别使用需要注意📢版本的迭代。
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>0.8.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
Could not find artifact org.springframework.ai:spring-ai-core:pom:0.8.1 in aliyunmaven...... 或者Unresolved dependency: 'org.springframework.ai:spring-ai-core:jar:0.8.1'
需要在pom文件中引入仓库
https://docs.spring.io/spring-ai/reference/getting-started.html <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <releases> <enabled>false</enabled> </releases> </repository> </repositories>
在 application-dev.yml 配置对接的信息, spring-ai 提供了各类大模型的对接,都可以按需配置到这里。在它的源码 autoConfig 类里会有要配置参数的名称,可以参考。
spring:
ai:
openai:
base-url: https://api.aws-*****/
api-key: sk-oLkakcax33mJl628D3A533Fd67A24602Ac37D6*****
@Resource
private OpenAiChatClient chatClient;
@Resource
private OpenAiImageClient openaiImageClient;
@Test
public void test_generate() {
ChatResponse chatResponse = chatClient.call(
new Prompt(
"1+1",
OpenAiChatOptions.builder()
.withModel("gpt-4o")
.build()
));
log.info("测试结果:{}", JSON.toJSONString(chatResponse));
}
@Test
public void test_generate_stream() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
Flux<ChatResponse> stream = chatClient.stream(new Prompt("1+1"));
stream.subscribe(
chatResponse -> {
AssistantMessage output = chatResponse.getResult().getOutput();
log.info("测试结果: {}", JSON.toJSONString(output));
},
Throwable::printStackTrace,
() -> {
countDownLatch.countDown();
System.out.println("Stream completed");
}
);
countDownLatch.await();
}
@Test
public void test_generate_image() {
ImageResponse imageResponse = openaiImageClient.call(
new ImagePrompt("画个小狗",
OpenAiImageOptions.builder()
.withModel("dall-e-3")
.withQuality("hd")
.withN(1)
.withHeight(1024)
.withWidth(1024)
.build()
)
);
log.info("测试结果: {}", JSON.toJSONString(imageResponse));
}
@RestController()
@CrossOrigin("*")
@RequestMapping("/api/v1/openai/")
public class OpenAiController {
@Resource
private OpenAiChatClient chatClient;
/**
* curl http://localhost:8090/api/v1/openai/generate?message=1+1
*/
@RequestMapping(value = "generate", method = RequestMethod.GET)
public ChatResponse generate(@RequestParam String message) {
return chatClient.call(
new Prompt(
message,
OpenAiChatOptions.builder()
.withModel("gpt-4o")
.build()
));
}
/**
* curl http://localhost:8090/api/v1/openai/generate_stream?message=1+1
*/
@RequestMapping(value = "generate_stream", method = RequestMethod.GET)
public Flux<ChatResponse> generateStream(@RequestParam String message) {
return chatClient.stream(new Prompt(message));
}
}
pom文件内容
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zhouyu</groupId>
<artifactId>ai-open-courses</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>spring-ai-demo</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>0.8.1-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<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.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.44.0</version>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
</project>