Spring Cloud Alibaba AI 入门与探索

概述

Spring AI 是 Spring官方社区项目,旨在简化 Java AI应用程序开发,让Java开发者像使用Spring开发普通应用一样开发AI应用。

Spring Cloud Alibaba AI是一个将Spring Cloud微服务生态与阿里巴巴AI能力无缝集成的框架,帮助开发者快速构建具备AI功能的现代应用。本文将介绍Spring Cloud Alibaba AI的基本概念、主要特性和功能,并演示如何完成一个 在线聊天 和 在线画图 的AI应用。

主要特性和功能

Spring Cloud Alibaba AI目前基于 Spring AI 0.8.1版本API完成通义系列大模型的接入。通义接入是基于阿里云 阿里云百练 服务;而 阿里云百练 建立在 模型即服务(Maas)的理念基础之上,围绕AI各领域模型,通过标准化的API提供包括模型推理、模型微调训练在内的多种模型服务。

主要提供以下核心功能:

简单易用的集成

通过Spring Boot风格的自动配置机制,开发者只需少量代码配置,即可快速接入阿里云的AI服务。

丰富的AI服务支持

支持以下核心能力:

  • 自然语言处理(NLP):文本分析、智能问答、翻译。
  • 计算机视觉(CV):图像生成、图像识别、目标检测。
  • 语音处理:语音识别、语音合成。
  • 数据分析与预测:数据建模、趋势分析。

高度扩展性

通过配置中心和注册中心(如Nacos)实现动态扩展,支持微服务架构的扩展需求。提供接口定义,方便接入第三方AI平台。

构建AI应用

Spring Cloud Alibaba AI对Java版本有要求,所以需要提前预装好Java 17环境。

申请API-KEY

登录阿里云,进入 阿里云百炼 的页面:

https://bailian.console.aliyun.com/?apiKey=1#/api-key

创建自己的API-KEY:
在这里插入图片描述

添加依赖

在Spring Boot项目的 pom.xml 中添加 alibaba-ai依赖

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>

	<dependency>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
		<optional>true</optional>
	</dependency>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>

	<dependency>
		<groupId>com.alibaba.cloud</groupId>
		<artifactId>spring-cloud-starter-alibaba-ai</artifactId>
	</dependency>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>

配置API-KEY

在application.yml中配置服务的相关属性,包括服务器地址、认证信息等。

server:
  port: 8899

spring:
  application:
    name: SpringCloudAiExample
  cloud:
    ai:
      tongyi:
        connection:
          # 配置在阿里云百炼里申请的api-key
          api-key: sk-xxxxxx
  • api-key 的配置是在阿里云百练里申请的api-key。

创建模型调用服务

@Service
@Slf4j
public class TongYiSimpleService {

    @Resource
    private TongYiChatModel chatClient;
    @Resource
    private TongYiImagesModel imageClient;

    public String chat(String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        return chatClient.call(prompt).getResult().getOutput().getContent();
    }

    public String image(String message) {
        ImagePrompt prompt = new ImagePrompt(message);
        Image image = imageClient.call(prompt).getResult().getOutput();
        return image.getB64Json();
    }

}

聊天和图片的服务,分别通过注入 TongYiChatModel 和 TongYiImagesModel 对象来实现,屏蔽底层通义大模型交互细节。

创建controller

@RestController
@RequestMapping("/ai")
@CrossOrigin
@Slf4j
public class TongYiController {
    @Resource
    private TongYiSimpleService tongYiSimpleService;

    @GetMapping("/chat")
    public String chat(@RequestParam(value = "message") String message) {
        return tongYiSimpleService.chat(message);
    }

    @GetMapping("/image")
    public ResponseEntity<byte[]> image(@RequestParam(value = "message") String message) {
        String b64Str = tongYiSimpleService.image(message);
        byte[] imageBytes = Base64.getDecoder().decode(b64Str);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_JPEG); // 或者 MediaType.IMAGE_PNG 等,取决于图片格式

        return new ResponseEntity<>(imageBytes, headers, HttpStatus.OK);
    }

}

编写 Spring 入口类并启动应用

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringCloudAiExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudAiExampleApplication.class, args);
    }

}

至此,便完成了最简单的聊天 AI 应用开发,与普通的 Spring Boot 应用开发步骤完全一致。

测试效果

聊天接口

在浏览器输入:http://localhost:8899/ai/chat?message=你是谁

在这里插入图片描述

图片接口

在浏览器输入:http://localhost:8899/ai/image?message=漂亮的风景
在这里插入图片描述

搭配聊天页面

在这里插入图片描述

总结

当前版本的Spring Cloud Alibaba AI 主要完成了几种常见生成式模型的适配,涵盖对话、文生图、文生语音等。在未来的版本中将继续推出VectorStore、Embedding、ETL Pipeline、RAG 等更多 AI 应用开发场景的建设。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值