Spring AI:是一个面向AI工程的应用框架,旨在将可移植性和模块化设计等设计原则应用于AI领域的Spring生态系统。它的核心是提供抽象,作为开发Java AI应用程序的基础。Spring AI支持多种大模型服务对接,包括OpenAI、Microsoft、Amazon、Google和Huggingface等。它还支持多模态生成式AI能力,如对话、文生图、文生语音等
Spring AI Alibaba:基于Spring AI开发,专门用于接入阿里云的百炼大模型服务。
1 环境准备
Spring AI JDK需要最底 17版本,本例子使用 阿里巴巴提供的 Dragonwell
下载地址 https://github.com/alibaba/dragonwell17
说明:如果机器上已有JDK8 不想改环境变量,只需要解压到指定目录下,在IEDA项目上指定jdk 17。
2 登录 阿里云百炼网站
注册账号 获取 API-KEY 和开通服务
3 Spring AI Alibaba 官网 https://sca.aliyun.com/
4 使用IDEA 创建testai项目 运行 官网提供 helloworld-example 例子
配置项目pom.xml文件
<?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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.synball</groupId>
<artifactId>testai</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven-deploy-plugin.version>3.1.1</maven-deploy-plugin.version>
<!-- Spring AI -->
<spring-ai-alibaba.version>1.0.0-M3.2</spring-ai-alibaba.version>
</properties>
<dependencies>
<!-- Spring AI Alibaba 依赖配置-->
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter</artifactId>
<version>${spring-ai-alibaba.version}</version>
</dependency>
<!-- Spring web 依赖配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>${maven-deploy-plugin.version}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
项目结构
windows 在cmd
setx DASHSCOPE_API_KEY "YOUR_DASHSCOPE_API_KEY"
设置成功后,运行项目还是报key无效,需要重启IEDA在次运行解决。
package com.alibaba.cloud.ai.example.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWorldExampleApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldExampleApplication.class, args);
}
}
package com.alibaba.cloud.ai.example.helloworld;
import java.util.Objects;
import reactor.core.publisher.Flux;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/ai")
public class ChatController {
private final ChatClient chatClient;
public ChatController(ChatClient.Builder builder) {
this.chatClient = builder.build();
}
@GetMapping("/chat")
public String chat(String input) {
return this.chatClient.prompt().user(input).call().content();
}
@GetMapping("/stream")
public String stream(String input) {
Flux<String> content = this.chatClient.prompt().user(input).stream().content();
return Objects.requireNonNull(content.collectList().block()).stream().reduce((a, b) -> a + b).get();
}
}
运行成功后访问
http://localhost:8080/ai/chat?input=给我讲一个笑话吧