springCloudalibabaAI孵化(一)

目录

1、what

1、简介

2、核心概念

3、高级特性 Prompt 和 AiResponse

4、功能

2、How

1、前言

2、在项目 pom.xml 中加入 2023.0.1.0 版本 Spring Cloud Alibaba 依赖:

3、在 配置文件中加入以下配置:application.yml

4、编写聊天服务实现类,由 Spring AI 自动注入 、 屏蔽底层通义大模型交互细节。ChatClientStreamingChatClientChatClient

5、提供具体聊天逻辑实现

6、编写 Spring 入口类并启动应用

7、验证

4、发展


1、what

1、简介

  1. Spring AI 与通义千问集成,使用 Spring AI 开发 Java AI 应用。
  2. Spring Cloud Alibaba AI 目前基于 Spring AI 0.8.1 版本 API 完成通义系列大模型的接入。通义接入是基于阿里云 灵积模型服务,灵积模型服务建立在“模型即服务”(Model-as-a-Service,MaaS)的理念基础之上,围绕 AI 各领域模型,通过标准化的API提供包括模型推理、模型微调训练在内的多种模型服务。
  3. 在当前最新版本中,Spring Cloud Alibaba AI 主要完成了几种常见生成式模型的适配,包括对话、文生图、文生语音等,开发者可以使用 Spring Cloud Alibaba AI 开发基于通义的聊天、图片或语音生成 AI 应用,框架还提供 OutParser、Prompt Template、Stuff 等实用能力。

2、核心概念

        在开始之前,我们先回顾一下一些关键领域术语和概念。 Spring AI 最初专注于设计用于处理语言输入和生成语言输出的模型。该项目背后的想法是为开发人员提供一个抽象接口,这是将生成式 AI API 作为独立组件添加到应用程序中的基础。 其中一种抽象是接口 AiClient,它有两个基本实现 - OpenAI 和 Azure OpenAI。而 Spring Cloud Alibaba AI 提供了对通义系列的全面支持。

public interface AiClient { default String generate(String message); AiResponse generate(Prompt prompt); }

        AiClient为生成功能提供了两种选择。简化的 -generate(String message) -使用 String 作为输入和输出,它可以用来避免 Promt 和 AiResponse 类的额外复杂性。 现在,让我们仔细看看它们的区别。

3、高级特性 Prompt 和 AiResponse

        1、在AI领域,提示是指提供给AI的短信。它由上下文和问题组成,该模型用于生成答案。 从 Spring AI 项目的角度来看,Prompt 是参数化_Message_s 的列表。

 public class Prompt { private final List messages; // constructors and utility methods }

public interface Message { String getContent(); Map getProperties(); MessageType getMessageType(); }

        2、提示使开发人员能够更好地控制文本输入。一个很好的例子是提示模板,它由预定义的文本和一组占位符构成。然后,我们可以使用传递给 Message 构造函数的 Map 值来填充它们。

告诉我一个关于{content}的{形容词}笑话。

        3、消息接口还保存有关 AI 模型可以处理的消息类别的高级信息。例如,OpenAI 实现区分对话角色,并通过 MessageType 有效映射。对于其他模型,它可以反映消息格式或一些其他自定义属性。更多详情请参考官方文档。

public class AiResponse { private final List generations; // getters and setters }

public class Generation { private final String text; private Map info; } 

AiResponse 由 Generation 对象列表组成,每个对象都保存相应提示的输出。此外,Generation对象提供AI响应的元数据信息。

4、功能

2、How

Getting Started :: Spring AI Reference

1、前言

  1. 本项目演示如何使用 完成一个在线聊天 AI 应用,底层使用通义千问提供的模型服务。可在此查看 完整示例源码。spring-cloud-starter-alibaba-ai

  2. 为使示例能够正常接入通义大模型,需要在阿里云开通 DashScope 灵积模型服务,申请有效的 API-KEY 并更新到应用配置文件。具体操作步骤可参见如下文档:如何开通DashScope并创建API-KEY_模型服务灵积(DashScope)-阿里云帮助中心

2、在项目 pom.xml 中加入 2023.0.1.0 版本 Spring Cloud Alibaba 依赖:

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

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

3、在 配置文件中加入以下配置:application.yml

  • spring:
      cloud:
        ai:
          tongyi:
            chat:
              options:
                # Replace the following key with a valid API-KEY.
                api-key: sk-a3d73b1709bf4a178c28ed7c8b3b5axx

4、编写聊天服务实现类,由 Spring AI 自动注入 、 屏蔽底层通义大模型交互细节。ChatClientStreamingChatClientChatClient

@Service
public class TongYiSimpleServiceImpl extends AbstractTongYiServiceImpl {

	private final ChatClient chatClient;

	private final StreamingChatClient streamingChatClient;

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

5、提供具体聊天逻辑实现

@Service
public class TongYiSimpleServiceImpl extends AbstractTongYiServiceImpl {

	// ......

	@Override
	public String completion(String message) {

		Prompt prompt = new Prompt(new UserMessage(message));

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

	@Override
	public Map<String, String> streamCompletion(String message) {

		StringBuilder fullContent = new StringBuilder();

		streamingChatClient.stream(new Prompt(message))
				.flatMap(chatResponse -> Flux.fromIterable(chatResponse.getResults()))
				.map(content -> content.getOutput().getContent())
				.doOnNext(fullContent::append)
				.last()
				.map(lastContent -> Map.of(message, fullContent.toString()))
				.block();

		log.info(fullContent.toString());

		return Map.of(message, fullContent.toString());
	}

}

6、编写 Spring 入口类并启动应用

@SpringBootApplication
public class TongYiApplication {
	public static void main(String[] args) {
		SpringApplication.run(TongYiApplication.class);
	}
}

7、验证

1、方式一

浏览器地址栏输入:http://localhost:8080/ai/example

返回如下响应:

{ "Tell me a joke": "Sure, here's a classic one for you:\n\nWhy was the math book sad?\n\nBecause it had too many problems.\n\nI hope that made you smile! If you're looking for more, just let me know." }

2、方式二

进入 目录下,使用浏览器打开 index.html 文件,输入问题,即可获得输出响应(确保 API-key 有效):resources/static

4、发展

        当前版本 Spring Cloud Alibaba AI 主要完成了几种常见生成式模型适配,包括对话、文生图、文生语音等。接下来的版本中,我们将继续完成 VectorStore、Embedding、ETL Pipeline 等更多适配,简化 RAG 等更多 AI 应用开发场景。

springcloudalibaba ai官网:快速开始 | https://sca.aliyun.com

  • 20
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

平凡之路无尽路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值