快速入门Spring AI Alibaba


前言

Spring Cloud Alibaba AI 以 Spring AI 为基础,并在此基础上,基于 Spring AI 0.8.1 版本 API 完成同义系列大模型的接入实现阿里云同义系列大模型全面适配。
在当前最新版本中,Spring Cloud Alibaba AI 主要完成了几种常见生成式模型的适配,包括对话、文生图、文生语音等,开发者可以使用 Spring Cloud Alibaba AI 开发基于同义的聊天、图片或语音生成 AI 应用,框架还提供 OutParser、Prompt Template、Stuff 等实用能力。

本文主要内容为Spring AI Alibaba的快速入门demo,基于Spring Boot集成Spring AI Alibaba并完成一个简单的对话模型,以下为相关步骤。

一、环境准备

JDK版本:JDK17(含)以上。
Spring Boot版本:3.3.x及以上。

二、获取API Key

  1. 登录阿里云百炼大模型服务平台

  2. 登录后点击右上角的API-KEY
    鼠标悬停于页面右上角的image图标上,在下拉菜单中单击API-KEY。

  3. 点击创建key
    在这里插入图片描述
    阿里云相关文章:获取API Key

三、代码示例

pom

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

application.yml

填入刚刚获得的API-KEY

spring:
  cloud:
    ai:
      tongyi:
        api-key: xxx

controller

    @Autowired
    private ChatClient chatClient;

    @GetMapping("/test")
    public String test(@RequestParam(value = "message", defaultValue = "hello") String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        return chatClient.call(prompt).getResult().getOutput().getContent();
    }

在浏览器中访问
在这里插入图片描述


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值