阿里也出手了!Spring CloudAlibaba AI问世了

写在前面

在之前的文章中我们有介绍过SpringAI这个项目。SpringAI 是Spring 官方社区项目,旨在简化 Java AI 应用程序开发,

让 Java 开发者想使用 Spring 开发普通应用一样开发 AI 应用。

SpringAI 主要面向的是国外的各种大模型接入,对于国内开发者可能不太友好。

于是乎,Spring Cloud Alibaba AI 便问世了,Spring Cloud Alibaba AI 以 Spring AI 为基础,并在此基础上提供阿里云同义系列大模型全面适配,

让用户在 5 分钟内开发基于同义大模型的 Java AI 应用。

一、Spring AI 简介

可能有些小伙伴已经忘记了SpringAI 是啥?我们这儿再来简单回顾一下。

Spring AI是一个面向AI工程的应用框架。其目标是将可移植性和模块化设计等设计原则应用于AI领域的Spring生态系统,

并将POJO作为应用程序的构建块推广到AI领域。

转换为人话来说就是:Spring出了一个AI框架,帮助我们快速调用AI,从而实现各种功能场景。

二、Spring Cloud Alibaba AI 简介

Spring Cloud Alibaba AISpring AI 为基础,并在此基础上,基于 Spring AI 0.8.1 版本 API 完成同义系列大模型的接入

实现阿里云同义系列大模型全面适配。

在当前最新版本中,Spring Cloud Alibaba AI 主要完成了几种常见生成式模型的适配,包括对话、文生图、文生语音等,

开发者可以使用 Spring Cloud Alibaba AI 开发基于同义的聊天、图片或语音生成 AI 应用,

框架还提供 OutParserPrompt TemplateStuff 等实用能力。

三、第一个Spring AI应用开发

① 新建maven 项目

注: 在创建项目的时候,jdk版本必须选择17+

② 添加依赖

xml 复制代码
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
    <version>2023.0.1.0</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

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

注: 这里我们需要配置镜像源,否则是没法下载依赖的。会报如下错误

spring-ai: 0.8.1 dependency not found

xml 复制代码
<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

③ 在 application.yml 配置文件中添加api-key

yml 复制代码
spring:
  cloud:
    ai:
      tongyi:
        api-key: 你自己申请的api-key

小伙伴如果不知道在哪申请,我把申请链接也放这儿了

https://dashscope.console.aliyun.com/apiKey

操作步骤:https://help.aliyun.com/zh/dashscope/developer-reference/activate-dashscope-and-create-an-api-key

④ 新建TongYiController 类,代码如下

java 复制代码
@RestController
@RequestMapping("/ai")
@CrossOrigin
@Slf4j
public class TongYiController {

    @Autowired
    @Qualifier("tongYiSimpleServiceImpl")
    private TongYiService tongYiSimpleService;

    @GetMapping("/example")
    public String completion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {

        return tongYiSimpleService.completion(message);
    }
    
}

⑤ 新建TongYiService 接口,代码如下

java 复制代码
public interface TongYiService {
    String completion(String message);

}

⑥ 新建TongYiSimpleServiceImpl 实现类,代码如下

java 复制代码
@Service
@Slf4j
public  class TongYiSimpleServiceImpl  implements TongYiService {

    private final ChatClient chatClient;

    @Autowired
    public TongYiSimpleServiceImpl(ChatClient chatClient, StreamingChatClient streamingChatClient) {
        this.chatClient = chatClient;
    }

    @Override
    public String completion(String message) {
        Prompt prompt = new Prompt(new UserMessage(message));

        return chatClient.call(prompt).getResult().getOutput().getContent();
    }


}

到这儿我们一个简单的AI应用已经开发完成了,最终项目结构如下

四、运行AI应用

启动服务,我们只需要在浏览器中输入:http://localhost:8080/ai/example 即可与AI交互。

① 不带message参数,则message=Tell me a joke,应用随机返回一个笑话

② 我们在浏览器中输入:http://localhost:8080/ai/example?message=对话内容

五、前端页面对话模式

我们只更加在resources/static 路径下添加一个index.html前端页面,即可拥有根据美观的交互体验。

index.html代码官方github仓库中已给出样例,由于代码比较长,这里就不贴代码了

https://github.com/alibaba/spring-cloud-alibaba/blob/2023.x/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/resources/static/index.html

添加完静态页面之后,我们浏览器中输入:http://localhost:8080/index.html 就可以得到一个美观的交互界面

接下来,我们来实际体验一下

六、其他模型

上面章节中我们只简单体验了对话模型,阿里还有很多其他模型。由于篇幅原因这里就不一一带大家一起体验了。

应用场景:

各个模型概述:

七、怎么样快速接入大模型

各种应用场景阿里官方GitHub都给出了接入例子

https://github.com/alibaba/spring-cloud-alibaba/tree/2023.x/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example

感兴趣的小伙伴可以自己到上面github 仓库看代码研究

本期内容到这儿就结束了,★,° :.☆( ̄▽ ̄)/$:.°★ 。 希望对您有所帮助

我们下期再见 ヾ(•ω•`)o (●'◡'●)

相关推荐
玛卡巴卡0123 分钟前
Maven 从入门到实战:搞定依赖管理与 Spring Boot 项目构建
java·spring boot·maven
vortex535 分钟前
用 Scoop 快速部署 JeecgBoot 开发环境:从依赖安装到服务管理
java·windows·springboot·web·开发·jeecg-boot
CoderJia程序员甲36 分钟前
GitHub 热榜项目 - 日榜(2025-11-01)
ai·开源·大模型·github·ai教程
جيون داد ناالام ميづ1 小时前
Spring Boot 核心原理(一):基础认知篇
java·spring boot·后端
Jouzzy1 小时前
【大模型】大模型微调
大模型
fantasy5_52 小时前
手撕vector:从零实现一个C++动态数组
java·开发语言·c++
十八旬2 小时前
RuoYi-Vue3项目定制修改全攻略
java·windows
任风雨2 小时前
3.1.1.Java基础知识
java·开发语言
脸大是真的好~2 小时前
黑马JAVA+AI 加强03-集合-Collection-List和Set集合-迭代器(Iterator)遍历-并发修改异常
java
大千AI助手2 小时前
Megatron-LM张量并行详解:原理、实现与应用
人工智能·大模型·llm·transformer·模型训练·megatron-lm张量并行·大千ai助手