当前位置:   article > 正文

Spring Cloud Alibaba AI 介绍及使用_sping cloud alibaba 3.2.4 示例

sping cloud alibaba 3.2.4 示例

一、Spring Cloud Alibaba AI 介绍

Spring AISpring 官方社区项目,旨在简化 Java AI 应用程序开发,让 Java 开发者像使用 Spring 开发普通应用一样开发 AI 应用。而 Spring Cloud Alibaba AI 是阿里以 Spring AI 为基础,并在此基础上提供阿里云通义系列大模型全面适配,让用户在 5 分钟内开发基于通义大模型的 Java AI 应用。

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

在当前最新版本中,Spring Cloud Alibaba AI 主要完成了几种常见生成式模型的适配,包括对话、文生图、文生语音等,开发者可以使用 Spring Cloud Alibaba AI 开发基于通义的聊天、图片或语音生成 AI 应用,框架还提供 OutParser、Prompt Template、Stuff 等实用能力。

官方文档:https://sca.aliyun.com/docs/2023/user-guide/ai/overview/?spm=5176.29160081.0.0.1f467a3ctbrdEJ

二、Spring Cloud Alibaba AI 实践

2.1 环境构建

新建 SpringBoot 项目,SpringBoot 版本需要 3.x 以上,jdk 版本 17 及以上。

这里我使用的版本信息如下:

spring-boot: 3.2.4
spring-cloud: 2023.0.1
spring-cloud-alibaba: 2023.0.1.0
jdk: 17
  • 1
  • 2
  • 3
  • 4

pom 中增加相关依赖,整体 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>ai-demo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ai-demo1</name>
    <description>ai-demo1</description>
    <properties>
        <java.version>17</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>3.2.4</spring-boot.version>
        <spring-cloud.version>2023.0.1</spring-cloud.version>
        <spring-cloud-alibaba.version>2023.0.1.0</spring-cloud-alibaba.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

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

    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>

    </dependencyManagement>

    <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.example.aidemo1.AiDemo1Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116

2.2 申请 Api Key

申请 Api key 可以参考下面官方介绍:

https://help.aliyun.com/zh/dashscope/developer-reference/acquisition-and-configuration-of-api-key?spm=5176.29160081.0.0.1f467a3ctbrdEJ

可以将将申请的 key 放在环境变量 SPRING_CLOUD_AI_TONGYI_API_KEY 中:

export SPRING_CLOUD_AI_TONGYI_API_KEY=sk-a3dxxxx
  • 1

或者配置在 application.yml 配置文件中:

spring:
  cloud:
    ai:
      tongyi:
        api-key: sk-a3dxxxx
  • 1
  • 2
  • 3
  • 4
  • 5

2.3 文本交互对接

@Slf4j
@SpringBootTest
class AiDemo1ApplicationTests {

    @Resource
    ChatClient chatClient;

    @Test
    void chatTest() {
        String message = "你好,介绍一下你自己";
        Prompt prompt = new Prompt(new UserMessage(message));
        String content = chatClient.call(prompt).getResult().getOutput().getContent();
        log.info(content);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在这里插入图片描述

2.4 文本交互-流式输出

@Slf4j
@SpringBootTest
class AiDemo1ApplicationTests {

    @Resource
    StreamingChatClient streamingChatClient;

    @Test
    void chatStreamTest() {
        String message = "你好,介绍一下你自己";
        Prompt prompt = new Prompt(new UserMessage(message));

        streamingChatClient.stream(prompt)
                .flatMap(chatResponse -> Flux.fromIterable(chatResponse.getResults()))
                .map(content -> content.getOutput().getContent())
                .doOnNext(System.out::println)
                .last()
                .block();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

在这里插入图片描述

2.5 文本生成图片

@Slf4j
@SpringBootTest
class AiDemo1ApplicationTests {

    @Resource
    ImageClient imageClient;

    @Test
    void testToImageTest() {
        String message = "生成一幅夕阳下程序员看海的图片";
        ImagePrompt prompt = new ImagePrompt(message);
        String url = imageClient.call(prompt).getResult().getOutput().getUrl();
        log.info(url);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在这里插入图片描述

图片内容如下:

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/873731
推荐阅读
相关标签
  

闽ICP备14008679号