【优化】AI:通义千问的集成,从自己集成,换成 spring cloud alibaba ai 库

This commit is contained in:
YunaiV 2024-06-29 19:00:09 +08:00
parent c5db930603
commit 246c233253
53 changed files with 4723 additions and 489 deletions

View File

@ -4,7 +4,6 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum;
import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenOptions;
import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatModel;
import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoOptions;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
@ -23,6 +22,7 @@ import cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.ai.service.model.AiApiKeyService;
import cn.iocoder.yudao.module.ai.service.model.AiChatModelService;
import cn.iocoder.yudao.module.ai.service.model.AiChatRoleService;
import com.alibaba.cloud.ai.tongyi.chat.TongYiChatOptions;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.messages.*;
@ -198,8 +198,7 @@ public class AiChatMessageServiceImpl implements AiChatMessageService {
return new XingHuoOptions().setChatModel(XingHuoChatModel.valueOfModel(model)).setTemperature(temperatureF)
.setMaxTokens(maxTokens);
case QIAN_WEN:
// TODO @fan:增加 modeltemperature 参数
return new QianWenOptions().setMaxTokens(maxTokens);
return TongYiChatOptions.builder().withModel(model).withTemperature(temperature).withMaxTokens(maxTokens).build();
default:
throw new IllegalArgumentException(StrUtil.format("未知平台({})", platform));
}

View File

@ -51,10 +51,11 @@
</dependency>
<!-- 阿里云 通义千问 -->
<!-- TODO 芋艿:等 spring cloud alibaba ai 发布最新的时候,可以替换掉这个依赖,并且删除我们直接 cv 的代码 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dashscope-sdk-java</artifactId>
<version>2.11.0</version>
<version>2.14.0</version>
</dependency>
<dependency>

View File

@ -4,18 +4,16 @@ import cn.iocoder.yudao.framework.ai.core.factory.AiClientFactory;
import cn.iocoder.yudao.framework.ai.core.factory.AiClientFactoryImpl;
import cn.iocoder.yudao.framework.ai.core.model.midjourney.api.MidjourneyApi;
import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi;
import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenChatClient;
import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenChatModal;
import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenOptions;
import cn.iocoder.yudao.framework.ai.core.model.tongyi.api.QianWenApi;
import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatClient;
import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoOptions;
import cn.iocoder.yudao.framework.ai.core.model.xinghuo.api.XingHuoApi;
import com.alibaba.cloud.ai.tongyi.TongYiAutoConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
/**
* 芋道 AI 自动配置
@ -25,6 +23,7 @@ import org.springframework.context.annotation.Bean;
@AutoConfiguration
@EnableConfigurationProperties(YudaoAiProperties.class)
@Slf4j
@Import(TongYiAutoConfiguration.class)
public class YudaoAiAutoConfiguration {
@Bean
@ -54,27 +53,6 @@ public class YudaoAiAutoConfiguration {
);
}
@Bean
@ConditionalOnProperty(value = "yudao.ai.qianwen.enable", havingValue = "true")
public QianWenChatClient qianWenChatClient(YudaoAiProperties yudaoAiProperties) {
YudaoAiProperties.QianWenProperties qianWenProperties = yudaoAiProperties.getQianwen();
// 转换配置
QianWenOptions qianWenOptions = new QianWenOptions();
// qianWenOptions.setModel(qianWenProperties.getModel().getModel()); TODO @fan这里报错了
qianWenOptions.setTemperature(qianWenProperties.getTemperature());
// qianWenOptions.setTopK(qianWenProperties.getTopK()); TODO 芋艿后续弄
qianWenOptions.setTopP(qianWenProperties.getTopP());
qianWenOptions.setMaxTokens(qianWenProperties.getMaxTokens());
// qianWenOptions.setTemperature(qianWenProperties.getTemperature()); TODO 芋艿后续弄
return new QianWenChatClient(
new QianWenApi(
qianWenProperties.getApiKey(),
QianWenChatModal.QWEN_72B_CHAT
),
qianWenOptions
);
}
@Bean
@ConditionalOnProperty(value = "yudao.ai.midjourney.enable", havingValue = "true")
public MidjourneyApi midjourneyApi(YudaoAiProperties yudaoAiProperties) {

View File

@ -1,7 +1,6 @@
package cn.iocoder.yudao.framework.ai.config;
import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum;
import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenChatModal;
import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatModel;
import lombok.Data;
import lombok.experimental.Accessors;
@ -20,7 +19,6 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "yudao.ai")
public class YudaoAiProperties {
private QianWenProperties qianwen;
private XingHuoProperties xinghuo;
private OpenAiImageProperties openAiImage;
private MidjourneyProperties midjourney;
@ -45,21 +43,6 @@ public class YudaoAiProperties {
private Integer maxTokens;
}
@Data
@Accessors(chain = true)
public static class QianWenProperties extends ChatProperties {
/**
* api key
*/
private String apiKey;
/**
* model
*/
private QianWenChatModal model;
}
@Data
public static class XingHuoProperties extends ChatProperties {

View File

@ -11,11 +11,13 @@ import cn.iocoder.yudao.framework.ai.config.YudaoAiProperties;
import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum;
import cn.iocoder.yudao.framework.ai.core.model.midjourney.api.MidjourneyApi;
import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi;
import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenChatClient;
import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenChatModal;
import cn.iocoder.yudao.framework.ai.core.model.tongyi.api.QianWenApi;
import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatClient;
import cn.iocoder.yudao.framework.ai.core.model.xinghuo.api.XingHuoApi;
import com.alibaba.cloud.ai.tongyi.TongYiAutoConfiguration;
import com.alibaba.cloud.ai.tongyi.TongYiConnectionProperties;
import com.alibaba.cloud.ai.tongyi.chat.TongYiChatModel;
import com.alibaba.cloud.ai.tongyi.chat.TongYiChatProperties;
import com.alibaba.dashscope.aigc.generation.Generation;
import org.springframework.ai.autoconfigure.ollama.OllamaAutoConfiguration;
import org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration;
import org.springframework.ai.autoconfigure.qianfan.QianFanAutoConfiguration;
@ -84,7 +86,7 @@ public class AiClientFactoryImpl implements AiClientFactory {
case XING_HUO:
return SpringUtil.getBean(XingHuoChatClient.class);
case QIAN_WEN:
return SpringUtil.getBean(QianWenChatClient.class);
return SpringUtil.getBean(TongYiChatModel.class);
default:
throw new IllegalArgumentException(StrUtil.format("未知平台({})", platform));
}
@ -184,11 +186,16 @@ public class AiClientFactoryImpl implements AiClientFactory {
}
/**
* 可参考 {@link YudaoAiAutoConfiguration#qianWenChatClient(YudaoAiProperties)}
* 可参考 {@link TongYiAutoConfiguration#tongYiChatClient(Generation, TongYiChatProperties, TongYiConnectionProperties)}
*/
private static QianWenChatClient buildQianWenChatClient(String key) {
QianWenApi qianWenApi = new QianWenApi(key, QianWenChatModal.QWEN_72B_CHAT);
return new QianWenChatClient(qianWenApi);
private static TongYiChatModel buildQianWenChatClient(String key) {
com.alibaba.dashscope.aigc.generation.Generation generation = SpringUtil.getBean(Generation.class);
TongYiChatProperties chatOptions = SpringUtil.getBean(TongYiChatProperties.class);
// TODO @芋艿貌似 apiKey 是全局唯一的得测试下
// TODO @芋艿貌似阿里云不是增量返回的
TongYiConnectionProperties connectionProperties = new TongYiConnectionProperties();
connectionProperties.setApiKey(key);
return new TongYiAutoConfiguration().tongYiChatClient(generation, chatOptions, connectionProperties);
}
// private static VertexAiGeminiChatClient buildGoogleGemir(String key) {

View File

@ -1,165 +0,0 @@
package cn.iocoder.yudao.framework.ai.core.model.tongyi;
import cn.iocoder.yudao.framework.ai.core.exception.ChatException;
import cn.iocoder.yudao.framework.ai.core.model.tongyi.api.QianWenApi;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.google.common.collect.Lists;
import io.reactivex.Flowable;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.chat.model.StreamingChatModel;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.http.ResponseEntity;
import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.RetryListener;
import org.springframework.retry.support.RetryTemplate;
import reactor.core.publisher.Flux;
import java.time.Duration;
import java.util.List;
import java.util.stream.Collectors;
// TODO @芋艿暂时不需要重构 spring cloud alibaba ai 发布最新的
/**
* 阿里 通义千问 client
* <p>
* 文档地址https://help.aliyun.com/document_detail/2587494.html?spm=a2c4g.2587492.0.0.53f33c566sXskp
* <p>
* author: fansili
* time: 2024/3/13 21:06
*/
@Slf4j
public class QianWenChatClient implements ChatModel, StreamingChatModel {
private QianWenApi qianWenApi;
private QianWenOptions qianWenOptions;
public QianWenChatClient() {
}
public QianWenChatClient(QianWenApi qianWenApi) {
this.qianWenApi = qianWenApi;
}
public QianWenChatClient(QianWenApi qianWenApi, QianWenOptions qianWenOptions) {
this.qianWenApi = qianWenApi;
this.qianWenOptions = qianWenOptions;
}
// TODO @fansili看看咋公用出来允许传入类似异常之类的参数
public final RetryTemplate retryTemplate = RetryTemplate.builder()
// 最大重试次数 10
.maxAttempts(10)
.retryOn(Exception.class) // TODO 芋艿临时这么写
// 最大重试5次第一次间隔3000ms第二次3000ms * 2第三次3000ms * 3以此类推最大间隔3 * 60000ms
.exponentialBackoff(Duration.ofMillis(3000), 2, Duration.ofMillis(3 * 60000))
.withListener(new RetryListener() {
@Override
public <T extends Object, E extends Throwable> void onError(RetryContext context,
RetryCallback<T, E> callback, Throwable throwable) {
log.warn("重试异常:" + context.getRetryCount(), throwable);
}
})
.build();
@Override
public ChatResponse call(Prompt prompt) {
return this.retryTemplate.execute(ctx -> {
// ctx 会有重试的信息
// 创建 request 请求stream模式需要供应商支持
QwenParam request = this.createRequest(prompt, false);
// 调用 callWithFunctionSupport 发送请求
ResponseEntity<GenerationResult> responseEntity = qianWenApi.chatCompletionEntity(request);
// 获取结果封装 chatCompletion
GenerationResult response = responseEntity.getBody();
// if (!response.isSuccess()) {
// return new ChatResponse(List.of(new Generation(String.format("failed to create completion, requestId: %s, code: %s, message: %s\n",
// response.getRequestId(), response.getCode(), response.getMessage()))));
// }
// 转换为 Generation 返回
return new ChatResponse(response.getOutput().getChoices().stream()
.map(choices -> new Generation(choices.getMessage().getContent()))
.collect(Collectors.toList()));
});
}
@Override
public ChatOptions getDefaultOptions() {
// TODO 芋艿需要跟进下
throw new UnsupportedOperationException();
}
private QwenParam createRequest(Prompt prompt, boolean stream) {
// 获取 ChatOptions
QianWenOptions chatOptions = getChatOptions(prompt);
//
List<Message> messageList = Lists.newArrayList();
prompt.getInstructions().stream().forEach(instruction -> {
Message message = new Message();
message.setRole(instruction.getMessageType().getValue());
message.setContent(instruction.getContent());
messageList.add(message);
});
return QwenParam.builder()
.model(qianWenApi.getQianWenChatModal().getModel())
.prompt(prompt.getContents())
.messages(messageList)
.maxTokens(chatOptions.getMaxTokens())
.resultFormat(QwenParam.ResultFormat.MESSAGE)
.topP(chatOptions.getTopP() == null ? null : Double.valueOf(chatOptions.getTopP()))
.topK(chatOptions.getTopK())
.temperature(chatOptions.getTemperature())
// 控制流式输出模式即后面的内容会包含已经输出的内容设置为True将开启增量输出模式后面的输出不会包含已经输出的内容您需要自行拼接整体输出
.incrementalOutput(true)
/* set the random seed, optional, default to 1234 if not set */
.seed(100)
.apiKey(qianWenApi.getApiKey())
.build();
}
private @NotNull QianWenOptions getChatOptions(Prompt prompt) {
// 两个都为null 则没有配置文件
if (qianWenOptions == null && prompt.getOptions() == null) {
throw new ChatException("ChatOptions 未配置参数!");
}
// 优先使用 Prompt 里面的 ChatOptions
ChatOptions options = qianWenOptions;
if (prompt.getOptions() != null) {
options = (ChatOptions) prompt.getOptions();
}
// Prompt 里面是一个 ChatOptions用户可以随意传入这里做一下判断
if (!(options instanceof QianWenOptions)) {
throw new ChatException("Prompt 传入的不是 QianWenOptions!");
}
return (QianWenOptions) options;
}
@Override
public Flux<ChatResponse> stream(Prompt prompt) {
// ctx 会有重试的信息
// 创建 request 请求stream模式需要供应商支持
QwenParam request = this.createRequest(prompt, true);
// 调用 callWithFunctionSupport 发送请求
Flowable<GenerationResult> responseResult = this.qianWenApi.chatCompletionStream(request);
return Flux.create(fluxSink ->
responseResult.subscribe(
value -> fluxSink.next(
new ChatResponse(value.getOutput().getChoices().stream()
.map(choices -> new Generation(choices.getMessage().getContent()))
.collect(Collectors.toList()))
),
error -> fluxSink.error(error),
() -> fluxSink.complete()
)
);
}
}

View File

@ -1,47 +0,0 @@
package cn.iocoder.yudao.framework.ai.core.model.tongyi;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 千问 chat 模型
*
* 模型地址https://help.aliyun.com/document_detail/2712576.html
* 模型介绍https://help.aliyun.com/document_detail/2666503.html?spm=a2c4g.2701795.0.0.26eb34dfKzcWN4
*
* @author fansili
* @time 2024/4/26 10:15
* @since 1.0
*/
@AllArgsConstructor
@Getter
public enum QianWenChatModal {
// 千问付费模型
QWEN_TURBO("通义千问超大规模语言模型", "qwen-turbo"),
QWEN_PLUS("通义千问超大规模语言模型增强版", "qwen-plus"),
QWEN_MAX("通义千问千亿级别超大规模语言模型", "qwen-max"),
QWEN_MAX_0403("通义千问千亿级别超大规模语言模型-0403", "qwen-max-0403"),
QWEN_MAX_0107("通义千问千亿级别超大规模语言模型-0107", "qwen-max-0107"),
QWEN_MAX_1201("通义千问千亿级别超大规模语言模型-1201", "qwen-max-1201"),
QWEN_MAX_LONGCONTEXT("通义千问千亿级别超大规模语言模型-28k tokens", "qwen-max-longcontext"),
// 开源模型
// https://help.aliyun.com/document_detail/2666503.html?spm=a2c4g.2701795.0.0.26eb34dfKzcWN4
QWEN_72B_CHAT("通义千问1.5对外开源的72B规模参数量的经过人类指令对齐的chat模型", "qwen-72b-chat"),
;
private String name;
private String model;
public static QianWenChatModal valueOfModel(String model) {
for (QianWenChatModal itemEnum : QianWenChatModal.values()) {
if (itemEnum.getModel().equals(model)) {
return itemEnum;
}
}
throw new IllegalArgumentException("Invalid MessageType value: " + model);
}
}

View File

@ -1,119 +0,0 @@
package cn.iocoder.yudao.framework.ai.core.model.tongyi;
import org.springframework.ai.chat.prompt.ChatOptions;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.List;
/**
* 阿里云 千问 属性
*
* 地址https://help.aliyun.com/document_detail/2684682.html?spm=a2c4g.2621347.0.0.195117e7Ytpkyo
*
* author: fansili
* time: 2024/3/15 19:57
*/
@Data
@Accessors(chain = true)
public class QianWenOptions implements ChatOptions {
/**
* 用户与模型的对话历史
*/
private List<Message> messages;
/**
* 生成时核采样方法的概率阈值例如取值为0.8时仅保留累计概率之和大于等于0.8的概率分布中的token
* 作为随机采样的候选集取值范围为0,1.0)取值越大生成的随机性越高取值越低生成的随机性越低
* 默认值为0.8注意取值不要大于等于1
*/
private Float topP;
/**
* 用于限制模型生成token的数量max_tokens设置的是生成上限并不表示一定会生成这么多的token数量其中qwen1.5-14b-chatqwen1.5-7b-chatqwen-14b-chat和qwen-7b-chat最大值和默认值均为1500qwen-1.8b-chatqwen-1.8b-longcontext-chat和qwen-72b-chat最大值和默认值均为2000
*/
private Integer maxTokens = 1500;
/**
* 模型
*/
private String model;
/**
* temperature
*/
private Float temperature;
//
// 适配 ChatOptions
@Override
public Float getTemperature() {
return null;
}
@Override
public Integer getTopK() {
return null;
}
public Float getTopP() {
return topP;
}
@Data
@Accessors
public static class Message {
/**
* 角色: systemuser或assistant
*/
private String role;
/**
* 提示词或模型内容
*/
private String content;
}
@Data
@Accessors
public static class Parameters {
/**
* 输出格式, 默认为"text"
* "text"表示旧版本的text
* "message"表示兼容openai的message
*/
private String resultFormat;
/**
* 生成时采样候选集的大小例如取值为50时仅将单次生成中得分最高的50个token组成随机采样的候选集
* 取值越大生成的随机性越高取值越小生成的确定性越高
* 注意如果top_k参数为空或者top_k的值大于100表示不启用top_k策略此时仅有top_p策略生效默认是空
*/
private Integer topK;
/**
* 生成时使用的随机数种子用户控制模型生成内容的随机性
* seed支持无符号64位整数默认值为1234在使用seed时模型将尽可能生成相同或相似的结果但目前不保证每次生成的结果完全相同
*/
private Integer seed;
/**
* 用于控制随机性和多样性的程度具体来说temperature值控制了生成文本时对每个候选词的概率分布进行平滑的程度
* 较高的temperature值会降低概率分布的峰值使得更多的低概率词被选择
* 生成结果更加多样化而较低的temperature值则会增强概率分布的峰值使得高概率词更容易被选择生成结果更加确定
* 取值范围 [0, 2)系统默认值1.0不建议取值为0无意义
*/
private Float temperature;
/**
* 用于限制模型生成token的数量max_tokens设置的是生成上限并不表示一定会生成这么多的token数量
* 其中qwen-turbo 最大值和默认值为1500 qwen-maxqwen-max-1201 qwen-max-longcontext qwen-plus最大值和默认值均为2000
*/
private Integer maxTokens;
/**
* stop参数用于实现内容生成过程的精确控制在生成内容即将包含指定的字符串或token_ids时自动停止生成内容不包含指定的内容
* 例如如果指定stop为"你好"表示将要生成"你好"时停止如果指定stop为[37763, 367]表示将要生成"Observation"时停止
*/
private List<String> stop;
/**
* 用于控制流式输出模式默认False即后面内容会包含已经输出的内容设置为True将开启增量输出模式
* 后面输出不会包含已经输出的内容您需要自行拼接整体输出参考流式输出示例代码
*/
private Boolean incrementalOutput;
}
}

View File

@ -1,60 +0,0 @@
package cn.iocoder.yudao.framework.ai.core.model.tongyi.api;
import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenChatModal;
import cn.iocoder.yudao.framework.ai.core.exception.AiException;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import io.reactivex.Flowable;
import lombok.Getter;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
// TODO done @fansili是不是挪到 api 包里按照 spring ai 的结构根目录只放 client options
/**
* 阿里 通义千问
* <p>
* author: fansili
* time: 2024/3/13 21:09
*/
@Getter
public class QianWenApi {
// api key 获取地址https://bailian.console.aliyun.com/?spm=5176.28197581.0.0.38db29a4G3GcVb&apiKey=1#/api-key
private String apiKey = "sk-Zsd81gZYg7";
private Generation gen = new Generation();
private QianWenChatModal qianWenChatModal;
public QianWenApi(String apiKey, QianWenChatModal qianWenChatModal) {
this.apiKey = apiKey;
this.qianWenChatModal = qianWenChatModal;
}
public ResponseEntity<GenerationResult> chatCompletionEntity(QwenParam request) {
GenerationResult call;
try {
call = gen.call(request);
} catch (NoApiKeyException e) {
throw new AiException("没有找到apiKey" + e.getMessage());
} catch (InputRequiredException e) {
throw new AiException("chat缺少必填字段" + e.getMessage());
}
// 阿里云的这个 http code 随便设置外面判断是否成功用的 CompletionsResponse.isSuccess
return new ResponseEntity<>(call, HttpStatusCode.valueOf(200));
}
public Flowable<GenerationResult> chatCompletionStream(QwenParam request) {
Flowable<GenerationResult> resultFlowable;
try {
resultFlowable = gen.streamCall(request);
} catch (NoApiKeyException e) {
throw new AiException("没有找到apiKey" + e.getMessage());
} catch (InputRequiredException e) {
throw new AiException("chat缺少必填字段" + e.getMessage());
}
return resultFlowable;
}
}

View File

@ -1,9 +0,0 @@
package cn.iocoder.yudao.framework.ai.core.model.tongyi.api;
/**
* author: fansili
* time: 2024/3/13 21:07
*/
public class QianWenChatCompletion {
}

View File

@ -1,8 +0,0 @@
package cn.iocoder.yudao.framework.ai.core.model.tongyi.api;
/**
* author: fansili
* time: 2024/3/13 21:07
*/
public class QianWenChatCompletionMessage {
}

View File

@ -1,16 +0,0 @@
package cn.iocoder.yudao.framework.ai.core.model.tongyi.api;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
/**
* 千问
*
* author: fansili
* time: 2024/3/13 21:07
*/
public class QianWenChatCompletionRequest extends QwenParam {
protected QianWenChatCompletionRequest(QwenParamBuilder<?, ?> b) {
super(b);
}
}

View File

@ -1,11 +0,0 @@
/**
* 阿里的 通义千问
*
* 链接https://www.aliyun.com/search?k=%E9%80%9A%E4%B9%89%E5%A4%A7%E6%A8%A1%E5%9E%8B&scene=all
*
* 千问所有模型https://bailian.console.aliyun.com/?spm=5176.28515448.J_TC9GqcHi2edq9zUs9ZsDQ.1.417338b17zJTjy#/efm/my_model
*
* author: fansili
* time: 2024/3/13 21:05
*/
package cn.iocoder.yudao.framework.ai.core.model.tongyi;

View File

@ -0,0 +1,253 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi;
import com.alibaba.cloud.ai.tongyi.audio.speech.TongYiAudioSpeechModel;
import com.alibaba.cloud.ai.tongyi.audio.speech.TongYiAudioSpeechProperties;
import com.alibaba.cloud.ai.tongyi.audio.transcription.TongYiAudioTranscriptionModel;
import com.alibaba.cloud.ai.tongyi.audio.transcription.TongYiAudioTranscriptionProperties;
import com.alibaba.cloud.ai.tongyi.chat.TongYiChatModel;
import com.alibaba.cloud.ai.tongyi.chat.TongYiChatProperties;
import com.alibaba.cloud.ai.tongyi.common.constants.TongYiConstants;
import com.alibaba.cloud.ai.tongyi.common.exception.TongYiException;
import com.alibaba.cloud.ai.tongyi.embedding.TongYiTextEmbeddingModel;
import com.alibaba.cloud.ai.tongyi.embedding.TongYiTextEmbeddingProperties;
import com.alibaba.cloud.ai.tongyi.image.TongYiImagesModel;
import com.alibaba.cloud.ai.tongyi.image.TongYiImagesProperties;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.audio.asr.transcription.Transcription;
import com.alibaba.dashscope.audio.tts.SpeechSynthesizer;
import com.alibaba.dashscope.common.MessageManager;
import com.alibaba.dashscope.embeddings.TextEmbedding;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.ApiKey;
import com.alibaba.dashscope.utils.Constants;
import org.springframework.ai.model.function.FunctionCallbackContext;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import java.util.Objects;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
@AutoConfiguration
@ConditionalOnClass({
MessageManager.class,
TongYiChatModel.class,
TongYiImagesModel.class,
TongYiAudioSpeechModel.class,
TongYiTextEmbeddingModel.class,
TongYiAudioTranscriptionModel.class
})
@EnableConfigurationProperties({
TongYiChatProperties.class,
TongYiImagesProperties.class,
TongYiAudioSpeechProperties.class,
TongYiConnectionProperties.class,
TongYiTextEmbeddingProperties.class,
TongYiAudioTranscriptionProperties.class
})
public class TongYiAutoConfiguration {
@Bean
@Scope("prototype")
@ConditionalOnMissingBean
public Generation generation() {
return new Generation();
}
@Bean
@Scope("prototype")
@ConditionalOnMissingBean
public MessageManager msgManager() {
return new MessageManager(10);
}
@Bean
@Scope("prototype")
@ConditionalOnMissingBean
public ImageSynthesis imageSynthesis() {
return new ImageSynthesis();
}
@Bean
@Scope("prototype")
@ConditionalOnMissingBean
public SpeechSynthesizer speechSynthesizer() {
return new SpeechSynthesizer();
}
@Bean
@ConditionalOnMissingBean
public Transcription transcription() {
return new Transcription();
}
@Bean
@ConditionalOnMissingBean
public TextEmbedding textEmbedding() {
return new TextEmbedding();
}
@Bean
@ConditionalOnMissingBean
public FunctionCallbackContext springAiFunctionManager(ApplicationContext context) {
FunctionCallbackContext manager = new FunctionCallbackContext();
manager.setApplicationContext(context);
return manager;
}
@Bean
@ConditionalOnProperty(
prefix = TongYiChatProperties.CONFIG_PREFIX,
name = "enabled",
havingValue = "true",
matchIfMissing = true
)
public TongYiChatModel tongYiChatClient(Generation generation,
TongYiChatProperties chatOptions,
TongYiConnectionProperties connectionProperties
) {
settingApiKey(connectionProperties);
return new TongYiChatModel(generation, chatOptions.getOptions());
}
@Bean
@ConditionalOnProperty(
prefix = TongYiImagesProperties.CONFIG_PREFIX,
name = "enabled",
havingValue = "true",
matchIfMissing = true
)
public TongYiImagesModel tongYiImagesClient(
ImageSynthesis imageSynthesis,
TongYiImagesProperties imagesOptions,
TongYiConnectionProperties connectionProperties
) {
settingApiKey(connectionProperties);
return new TongYiImagesModel(imageSynthesis, imagesOptions.getOptions());
}
@Bean
@ConditionalOnProperty(
prefix = TongYiAudioSpeechProperties.CONFIG_PREFIX,
name = "enabled",
havingValue = "true",
matchIfMissing = true
)
public TongYiAudioSpeechModel tongYiAudioSpeechClient(
SpeechSynthesizer speechSynthesizer,
TongYiAudioSpeechProperties speechProperties,
TongYiConnectionProperties connectionProperties
) {
settingApiKey(connectionProperties);
return new TongYiAudioSpeechModel(speechSynthesizer, speechProperties.getOptions());
}
@Bean
@ConditionalOnProperty(
prefix = TongYiAudioTranscriptionProperties.CONFIG_PREFIX,
name = "enabled",
havingValue = "true",
matchIfMissing = true
)
public TongYiAudioTranscriptionModel tongYiAudioTranscriptionClient(
Transcription transcription,
TongYiAudioTranscriptionProperties transcriptionProperties,
TongYiConnectionProperties connectionProperties) {
settingApiKey(connectionProperties);
return new TongYiAudioTranscriptionModel(
transcriptionProperties.getOptions(),
transcription
);
}
@Bean
@ConditionalOnProperty(
prefix = TongYiTextEmbeddingProperties.CONFIG_PREFIX,
name = "enabled",
havingValue = "true",
matchIfMissing = true
)
public TongYiTextEmbeddingModel tongYiTextEmbeddingClient(
TextEmbedding textEmbedding,
TongYiConnectionProperties connectionProperties
) {
settingApiKey(connectionProperties);
return new TongYiTextEmbeddingModel(textEmbedding);
}
/**
* Setting the API key.
* @param connectionProperties {@link TongYiConnectionProperties}
*/
private void settingApiKey(TongYiConnectionProperties connectionProperties) {
String apiKey;
try {
// It is recommended to set the key by defining the api-key in an environment variable.
var envKey = System.getenv(TongYiConstants.SCA_AI_TONGYI_API_KEY);
if (Objects.nonNull(envKey)) {
Constants.apiKey = envKey;
return;
}
if (Objects.nonNull(connectionProperties.getApiKey())) {
apiKey = connectionProperties.getApiKey();
}
else {
apiKey = ApiKey.getApiKey(null);
}
Constants.apiKey = apiKey;
}
catch (NoApiKeyException e) {
throw new TongYiException(e.getMessage());
}
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi;
import org.springframework.boot.context.properties.ConfigurationProperties;
import static com.alibaba.cloud.ai.tongyi.common.constants.TongYiConstants.SCA_AI_CONFIGURATION;
/**
* Spring Cloud Alibaba AI TongYi LLM connection properties.
*
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
@ConfigurationProperties(TongYiConnectionProperties.CONFIG_PREFIX)
public class TongYiConnectionProperties {
/**
* Spring Cloud Alibaba AI connection configuration Prefix.
*/
public static final String CONFIG_PREFIX = SCA_AI_CONFIGURATION + "tongyi";
/**
* TongYi LLM API key.
*/
private String apiKey;
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
}

View File

@ -0,0 +1,40 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.audio;
/**
* More models see: https://help.aliyun.com/zh/dashscope/model-list?spm=a2c4g.11186623.0.i5
* Support all models in list.
*
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
public final class AudioSpeechModels {
private AudioSpeechModels() {
}
/**
* Male Voice of the Tongue(舌尖男声).
* zh & en.
* Default sample rate: 48 Hz.
*/
public static final String SAMBERT_ZHICHU_V1 = "sambert-zhichu-v1";
}

View File

@ -0,0 +1,43 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.audio;
/**
* @author xYLiu
* @author yuluo
* @since 2023.0.1.0
*/
public final class AudioTranscriptionModels {
private AudioTranscriptionModels() {
}
/**
* Paraformer Chinese and English speech recognition model supports audio or video speech recognition with a sampling rate of 16kHz or above.
*/
public static final String Paraformer_V1 = "paraformer-v1";
/**
* Paraformer Chinese speech recognition model, support 8kHz telephone speech recognition.
*/
public static final String Paraformer_8K_V1 = "paraformer-8k-v1";
/**
* The Paraformer multilingual speech recognition model supports audio or video speech recognition with a sample rate of 16kHz or above.
*/
public static final String Paraformer_MTL_V1 = "paraformer-mtl-v1";
}

View File

@ -0,0 +1,228 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.audio.speech;
import com.alibaba.cloud.ai.tongyi.audio.AudioSpeechModels;
import com.alibaba.cloud.ai.tongyi.audio.speech.api.*;
import com.alibaba.cloud.ai.tongyi.metadata.audio.TongYiAudioSpeechResponseMetadata;
import com.alibaba.dashscope.audio.tts.SpeechSynthesisParam;
import com.alibaba.dashscope.audio.tts.SpeechSynthesisResult;
import com.alibaba.dashscope.audio.tts.SpeechSynthesizer;
import com.alibaba.dashscope.common.ResultCallback;
import io.reactivex.Flowable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;
import java.nio.ByteBuffer;
/**
* TongYiAudioSpeechClient is a client for TongYi audio speech service for Spring Cloud Alibaba AI.
*
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
public class TongYiAudioSpeechModel implements SpeechModel, SpeechStreamModel {
private final Logger logger = LoggerFactory.getLogger(getClass());
/**
* Default speed rate.
*/
private static final float SPEED_RATE = 1.0f;
/**
* TongYi models api.
*/
private final SpeechSynthesizer speechSynthesizer;
/**
* TongYi models options.
*/
private final TongYiAudioSpeechOptions defaultOptions;
/**
* TongYiAudioSpeechClient constructor.
* @param speechSynthesizer the speech synthesizer
*/
public TongYiAudioSpeechModel(SpeechSynthesizer speechSynthesizer) {
this(speechSynthesizer, null);
}
/**
* TongYiAudioSpeechClient constructor.
* @param speechSynthesizer the speech synthesizer
* @param tongYiAudioOptions the tongYi audio options
*/
public TongYiAudioSpeechModel(SpeechSynthesizer speechSynthesizer, TongYiAudioSpeechOptions tongYiAudioOptions) {
Assert.notNull(speechSynthesizer, "speechSynthesizer must not be null");
Assert.notNull(tongYiAudioOptions, "tongYiAudioOptions must not be null");
this.speechSynthesizer = speechSynthesizer;
this.defaultOptions = tongYiAudioOptions;
}
/**
* Call the TongYi audio speech service.
* @param text the text message to be converted to audio.
* @return the audio byte buffer.
*/
@Override
public ByteBuffer call(String text) {
var speechRequest = new SpeechPrompt(text);
return call(speechRequest).getResult().getOutput();
}
/**
* Call the TongYi audio speech service.
* @param prompt the speech prompt.
* @return the speech response.
*/
@Override
public SpeechResponse call(SpeechPrompt prompt) {
var SCASpeechParam = merge(prompt.getOptions());
var speechSynthesisParams = toSpeechSynthesisParams(SCASpeechParam);
speechSynthesisParams.setText(prompt.getInstructions().getText());
logger.info(speechSynthesisParams.toString());
var res = speechSynthesizer.call(speechSynthesisParams);
return convert(res, null);
}
/**
* Call the TongYi audio speech service.
* @param prompt the speech prompt.
* @param callback the result callback.
* {@link SpeechSynthesizer#call(SpeechSynthesisParam, ResultCallback)}
*/
public void call(SpeechPrompt prompt, ResultCallback<SpeechSynthesisResult> callback) {
var SCASpeechParam = merge(prompt.getOptions());
var speechSynthesisParams = toSpeechSynthesisParams(SCASpeechParam);
speechSynthesisParams.setText(prompt.getInstructions().getText());
speechSynthesizer.call(speechSynthesisParams, callback);
}
/**
* Stream the TongYi audio speech service.
* @param prompt the speech prompt.
* @return the speech response.
* {@link SpeechSynthesizer#streamCall(SpeechSynthesisParam)}
*/
@Override
public Flux<SpeechResponse> stream(SpeechPrompt prompt) {
var SCASpeechParam = merge(prompt.getOptions());
Flowable<SpeechSynthesisResult> resultFlowable = speechSynthesizer
.streamCall(toSpeechSynthesisParams(SCASpeechParam));
return Flux.from(resultFlowable)
.flatMap(
res -> Flux.just(res.getAudioFrame())
.map(audio -> {
var speech = new Speech(audio);
var respMetadata = TongYiAudioSpeechResponseMetadata.from(res);
return new SpeechResponse(speech, respMetadata);
})
).publishOn(Schedulers.parallel());
}
public TongYiAudioSpeechOptions merge(TongYiAudioSpeechOptions target) {
var mergeBuilder = TongYiAudioSpeechOptions.builder();
mergeBuilder.withModel(defaultOptions.getModel() != null ? defaultOptions.getModel() : target.getModel());
mergeBuilder.withPitch(defaultOptions.getPitch() != null ? defaultOptions.getPitch() : target.getPitch());
mergeBuilder.withRate(defaultOptions.getRate() != null ? defaultOptions.getRate() : target.getRate());
mergeBuilder.withFormat(defaultOptions.getFormat() != null ? defaultOptions.getFormat() : target.getFormat());
mergeBuilder.withSampleRate(defaultOptions.getSampleRate() != null ? defaultOptions.getSampleRate() : target.getSampleRate());
mergeBuilder.withTextType(defaultOptions.getTextType() != null ? defaultOptions.getTextType() : target.getTextType());
mergeBuilder.withVolume(defaultOptions.getVolume() != null ? defaultOptions.getVolume() : target.getVolume());
mergeBuilder.withEnablePhonemeTimestamp(defaultOptions.isEnablePhonemeTimestamp() != null ? defaultOptions.isEnablePhonemeTimestamp() : target.isEnablePhonemeTimestamp());
mergeBuilder.withEnableWordTimestamp(defaultOptions.isEnableWordTimestamp() != null ? defaultOptions.isEnableWordTimestamp() : target.isEnableWordTimestamp());
return mergeBuilder.build();
}
public SpeechSynthesisParam toSpeechSynthesisParams(TongYiAudioSpeechOptions source) {
var mergeBuilder = SpeechSynthesisParam.builder();
mergeBuilder.model(source.getModel() != null ? source.getModel() : AudioSpeechModels.SAMBERT_ZHICHU_V1);
mergeBuilder.text(source.getText() != null ? source.getText() : "");
if (source.getFormat() != null) {
mergeBuilder.format(source.getFormat());
}
if (source.getRate() != null) {
mergeBuilder.rate(source.getRate());
}
if (source.getPitch() != null) {
mergeBuilder.pitch(source.getPitch());
}
if (source.getTextType() != null) {
mergeBuilder.textType(source.getTextType());
}
if (source.getSampleRate() != null) {
mergeBuilder.sampleRate(source.getSampleRate());
}
if (source.isEnablePhonemeTimestamp() != null) {
mergeBuilder.enablePhonemeTimestamp(source.isEnablePhonemeTimestamp());
}
if (source.isEnableWordTimestamp() != null) {
mergeBuilder.enableWordTimestamp(source.isEnableWordTimestamp());
}
if (source.getVolume() != null) {
mergeBuilder.volume(source.getVolume());
}
return mergeBuilder.build();
}
/**
* Convert the TongYi audio speech service result to the speech response.
* @param result the audio byte buffer.
* @param synthesisResult the synthesis result.
* @return the speech response.
*/
private SpeechResponse convert(ByteBuffer result, SpeechSynthesisResult synthesisResult) {
if (synthesisResult == null) {
return new SpeechResponse(new Speech(result));
}
var responseMetadata = TongYiAudioSpeechResponseMetadata.from(synthesisResult);
var speech = new Speech(synthesisResult.getAudioFrame());
return new SpeechResponse(speech, responseMetadata);
}
}

View File

@ -0,0 +1,261 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.audio.speech;
import com.alibaba.cloud.ai.tongyi.audio.AudioSpeechModels;
import com.alibaba.dashscope.audio.tts.SpeechSynthesisAudioFormat;
import com.alibaba.dashscope.audio.tts.SpeechSynthesisTextType;
import org.springframework.ai.model.ModelOptions;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
public class TongYiAudioSpeechOptions implements ModelOptions {
/**
* Audio Speech models.
*/
private String model = AudioSpeechModels.SAMBERT_ZHICHU_V1;
/**
* Text content.
*/
private String text;
/**
* Input text type.
*/
private SpeechSynthesisTextType textType = SpeechSynthesisTextType.PLAIN_TEXT;
/**
* synthesis audio format.
*/
private SpeechSynthesisAudioFormat format = SpeechSynthesisAudioFormat.WAV;
/**
* synthesis audio sample rate.
*/
private Integer sampleRate = 16000;
/**
* synthesis audio volume.
*/
private Integer volume = 50;
/**
* synthesis audio speed.
*/
private Float rate = 1.0f;
/**
* synthesis audio pitch.
*/
private Float pitch = 1.0f;
/**
* enable word level timestamp.
*/
private Boolean enableWordTimestamp = false;
/**
* enable phoneme level timestamp.
*/
private Boolean enablePhonemeTimestamp = false;
public static Builder builder() {
return new Builder();
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public SpeechSynthesisTextType getTextType() {
return textType;
}
public void setTextType(SpeechSynthesisTextType textType) {
this.textType = textType;
}
public SpeechSynthesisAudioFormat getFormat() {
return format;
}
public void setFormat(SpeechSynthesisAudioFormat format) {
this.format = format;
}
public Integer getSampleRate() {
return sampleRate;
}
public void setSampleRate(Integer sampleRate) {
this.sampleRate = sampleRate;
}
public Integer getVolume() {
return volume;
}
public void setVolume(Integer volume) {
this.volume = volume;
}
public Float getRate() {
return rate;
}
public void setRate(Float rate) {
this.rate = rate;
}
public Float getPitch() {
return pitch;
}
public void setPitch(Float pitch) {
this.pitch = pitch;
}
public Boolean isEnableWordTimestamp() {
return enableWordTimestamp;
}
public void setEnableWordTimestamp(Boolean enableWordTimestamp) {
this.enableWordTimestamp = enableWordTimestamp;
}
public Boolean isEnablePhonemeTimestamp() {
return enablePhonemeTimestamp;
}
public void setEnablePhonemeTimestamp(Boolean enablePhonemeTimestamp) {
this.enablePhonemeTimestamp = enablePhonemeTimestamp;
}
/**
* Build a options instances.
*/
public static class Builder {
private final TongYiAudioSpeechOptions options = new TongYiAudioSpeechOptions();
public Builder withModel(String model) {
options.model = model;
return this;
}
public Builder withText(String text) {
options.text = text;
return this;
}
public Builder withTextType(SpeechSynthesisTextType textType) {
options.textType = textType;
return this;
}
public Builder withFormat(SpeechSynthesisAudioFormat format) {
options.format = format;
return this;
}
public Builder withSampleRate(Integer sampleRate) {
options.sampleRate = sampleRate;
return this;
}
public Builder withVolume(Integer volume) {
options.volume = volume;
return this;
}
public Builder withRate(Float rate) {
options.rate = rate;
return this;
}
public Builder withPitch(Float pitch) {
options.pitch = pitch;
return this;
}
public Builder withEnableWordTimestamp(Boolean enableWordTimestamp) {
options.enableWordTimestamp = enableWordTimestamp;
return this;
}
public Builder withEnablePhonemeTimestamp(Boolean enablePhonemeTimestamp) {
options.enablePhonemeTimestamp = enablePhonemeTimestamp;
return this;
}
public TongYiAudioSpeechOptions build() {
return options;
}
}
}

View File

@ -0,0 +1,77 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.audio.speech;
import com.alibaba.cloud.ai.tongyi.audio.AudioSpeechModels;
import com.alibaba.dashscope.audio.tts.SpeechSynthesisAudioFormat;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import static com.alibaba.cloud.ai.tongyi.common.constants.TongYiConstants.SCA_AI_CONFIGURATION;
/**
* TongYi audio speech configuration properties.
*
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
@ConfigurationProperties(TongYiAudioSpeechProperties.CONFIG_PREFIX)
public class TongYiAudioSpeechProperties {
/**
* Spring Cloud Alibaba AI configuration prefix.
*/
public static final String CONFIG_PREFIX = SCA_AI_CONFIGURATION + "audio.speech";
/**
* Default TongYi Chat model.
*/
public static final String DEFAULT_AUDIO_MODEL_NAME = AudioSpeechModels.SAMBERT_ZHICHU_V1;
/**
* Enable TongYiQWEN ai audio client.
*/
private boolean enabled = true;
@NestedConfigurationProperty
private TongYiAudioSpeechOptions options = TongYiAudioSpeechOptions.builder()
.withModel(DEFAULT_AUDIO_MODEL_NAME)
.withFormat(SpeechSynthesisAudioFormat.WAV)
.build();
public TongYiAudioSpeechOptions getOptions() {
return this.options;
}
public void setOptions(TongYiAudioSpeechOptions options) {
this.options = options;
}
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}

View File

@ -0,0 +1,87 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.audio.speech.api;
import org.springframework.ai.model.ModelResult;
import org.springframework.lang.Nullable;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Objects;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
public class Speech implements ModelResult<ByteBuffer> {
private final ByteBuffer audio;
private SpeechMetadata speechMetadata;
public Speech(ByteBuffer audio) {
this.audio = audio;
}
@Override
public ByteBuffer getOutput() {
return this.audio;
}
@Override
public SpeechMetadata getMetadata() {
return speechMetadata != null ? speechMetadata : SpeechMetadata.NULL;
}
public Speech withSpeechMetadata(@Nullable SpeechMetadata speechMetadata) {
this.speechMetadata = speechMetadata;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Speech that)) {
return false;
}
return Arrays.equals(audio.array(), that.audio.array())
&& Objects.equals(speechMetadata, that.speechMetadata);
}
@Override
public int hashCode() {
return Objects.hash(Arrays.hashCode(audio.array()), speechMetadata);
}
@Override
public String toString() {
return "Speech{" + "text=" + audio + ", speechMetadata=" + speechMetadata + '}';
}
}

View File

@ -0,0 +1,80 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.audio.speech.api;
import java.util.Objects;
/**
* The {@link SpeechMessage} class represents a single text message to
* be converted to speech by the TongYi LLM TTS.
*
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
public class SpeechMessage {
private String text;
/**
* Constructs a new {@link SpeechMessage} object with the given text.
* @param text the text to be converted to speech
*/
public SpeechMessage(String text) {
this.text = text;
}
/**
* Returns the text of this speech message.
* @return the text of this speech message
*/
public String getText() {
return text;
}
/**
* Sets the text of this speech message.
* @param text the new text for this speech message
*/
public void setText(String text) {
this.text = text;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof SpeechMessage that)) {
return false;
}
return Objects.equals(text, that.text);
}
@Override
public int hashCode() {
return Objects.hash(text);
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.audio.speech.api;
import org.springframework.ai.model.ResultMetadata;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
public interface SpeechMetadata extends ResultMetadata {
/**
* Null Object.
*/
SpeechMetadata NULL = SpeechMetadata.create();
/**
* Factory method used to construct a new {@link SpeechMetadata}.
* @return a new {@link SpeechMetadata}
*/
static SpeechMetadata create() {
return new SpeechMetadata() {
};
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.audio.speech.api;
import org.springframework.ai.model.Model;
import java.nio.ByteBuffer;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.0.0-RC1
*/
@FunctionalInterface
public interface SpeechModel extends Model<SpeechPrompt, SpeechResponse> {
/**
* Generates spoken audio from the provided text message.
* @param message the text message to be converted to audio.
* @return the resulting audio bytes.
*/
default ByteBuffer call(String message) {
SpeechPrompt prompt = new SpeechPrompt(message);
return call(prompt).getResult().getOutput();
}
/**
* Sends a speech request to the TongYi TTS API and returns the resulting speech response.
* @param request the speech prompt containing the input text and other parameters.
* @return the speech response containing the generated audio.
*/
SpeechResponse call(SpeechPrompt request);
}

View File

@ -0,0 +1,89 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.audio.speech.api;
import com.alibaba.cloud.ai.tongyi.audio.speech.TongYiAudioSpeechOptions;
import org.springframework.ai.model.ModelRequest;
import java.util.Objects;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
public class SpeechPrompt implements ModelRequest<SpeechMessage> {
private TongYiAudioSpeechOptions speechOptions;
private final SpeechMessage message;
public SpeechPrompt(String instructions) {
this(new SpeechMessage(instructions), TongYiAudioSpeechOptions.builder().build());
}
public SpeechPrompt(String instructions, TongYiAudioSpeechOptions speechOptions) {
this(new SpeechMessage(instructions), speechOptions);
}
public SpeechPrompt(SpeechMessage speechMessage) {
this(speechMessage, TongYiAudioSpeechOptions.builder().build());
}
public SpeechPrompt(SpeechMessage speechMessage, TongYiAudioSpeechOptions speechOptions) {
this.message = speechMessage;
this.speechOptions = speechOptions;
}
@Override
public SpeechMessage getInstructions() {
return this.message;
}
@Override
public TongYiAudioSpeechOptions getOptions() {
return speechOptions;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof SpeechPrompt that)) {
return false;
}
return Objects.equals(speechOptions, that.speechOptions) && Objects.equals(message, that.message);
}
@Override
public int hashCode() {
return Objects.hash(speechOptions, message);
}
}

View File

@ -0,0 +1,100 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.audio.speech.api;
import com.alibaba.cloud.ai.tongyi.metadata.audio.TongYiAudioSpeechResponseMetadata;
import org.springframework.ai.model.ModelResponse;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
public class SpeechResponse implements ModelResponse<Speech> {
private final Speech speech;
private final TongYiAudioSpeechResponseMetadata speechResponseMetadata;
/**
* Creates a new instance of SpeechResponse with the given speech result.
* @param speech the speech result to be set in the SpeechResponse
* @see Speech
*/
public SpeechResponse(Speech speech) {
this(speech, TongYiAudioSpeechResponseMetadata.NULL);
}
/**
* Creates a new instance of SpeechResponse with the given speech result and speech
* response metadata.
* @param speech the speech result to be set in the SpeechResponse
* @param speechResponseMetadata the speech response metadata to be set in the
* SpeechResponse
* @see Speech
* @see TongYiAudioSpeechResponseMetadata
*/
public SpeechResponse(Speech speech, TongYiAudioSpeechResponseMetadata speechResponseMetadata) {
this.speech = speech;
this.speechResponseMetadata = speechResponseMetadata;
}
@Override
public Speech getResult() {
return speech;
}
@Override
public List<Speech> getResults() {
return Collections.singletonList(speech);
}
@Override
public TongYiAudioSpeechResponseMetadata getMetadata() {
return speechResponseMetadata;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof SpeechResponse that)) {
return false;
}
return Objects.equals(speech, that.speech)
&& Objects.equals(speechResponseMetadata, that.speechResponseMetadata);
}
@Override
public int hashCode() {
return Objects.hash(speech, speechResponseMetadata);
}
}

View File

@ -0,0 +1,54 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.audio.speech.api;
import org.springframework.ai.model.StreamingModel;
import reactor.core.publisher.Flux;
import java.nio.ByteBuffer;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
@FunctionalInterface
public interface SpeechStreamModel extends StreamingModel<SpeechPrompt, SpeechResponse> {
/**
* Generates a stream of audio bytes from the provided text message.
*
* @param message the text message to be converted to audio
* @return a Flux of audio bytes representing the generated speech
*/
default Flux<ByteBuffer> stream(String message) {
SpeechPrompt prompt = new SpeechPrompt(message);
return stream(prompt).map(SpeechResponse::getResult).map(Speech::getOutput);
}
/**
* Sends a speech request to the TongYi TTS API and returns a stream of the resulting
* speech responses.
* @param prompt the speech prompt containing the input text and other parameters.
* @return a Flux of speech responses, each containing a portion of the generated audio.
*/
@Override
Flux<SpeechResponse> stream(SpeechPrompt prompt);
}

View File

@ -0,0 +1,186 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.audio.transcription;
import com.alibaba.cloud.ai.tongyi.audio.AudioTranscriptionModels;
import com.alibaba.cloud.ai.tongyi.audio.transcription.api.AudioTranscriptionPrompt;
import com.alibaba.cloud.ai.tongyi.audio.transcription.api.AudioTranscriptionResponse;
import com.alibaba.cloud.ai.tongyi.audio.transcription.api.AudioTranscriptionResult;
import com.alibaba.cloud.ai.tongyi.common.exception.TongYiException;
import com.alibaba.cloud.ai.tongyi.metadata.audio.TongYiAudioTranscriptionResponseMetadata;
import com.alibaba.dashscope.audio.asr.transcription.*;
import org.springframework.ai.model.Model;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* TongYiAudioTranscriptionModel is a client for TongYi audio transcription service for
* Spring Cloud Alibaba AI.
* @author xYLiu
* @author yuluo
* @since 2023.0.1.0
*/
public class TongYiAudioTranscriptionModel
implements Model<AudioTranscriptionPrompt, AudioTranscriptionResponse> {
/**
* TongYi models options.
*/
private final TongYiAudioTranscriptionOptions defaultOptions;
/**
* TongYi models api.
*/
private final Transcription transcription;
public TongYiAudioTranscriptionModel(Transcription transcription) {
this(null, transcription);
}
public TongYiAudioTranscriptionModel(TongYiAudioTranscriptionOptions defaultOptions,
Transcription transcription) {
Assert.notNull(transcription, "transcription must not be null");
Assert.notNull(defaultOptions, "defaultOptions must not be null");
this.defaultOptions = defaultOptions;
this.transcription = transcription;
}
@Override
public AudioTranscriptionResponse call(AudioTranscriptionPrompt prompt) {
TranscriptionParam transcriptionParam;
if (prompt.getOptions() != null) {
var param = merge(prompt.getOptions());
transcriptionParam = toTranscriptionParam(param);
transcriptionParam.setFileUrls(prompt.getOptions().getFileUrls());
}
else {
Resource instructions = prompt.getInstructions();
try {
transcriptionParam = TranscriptionParam.builder()
.model(AudioTranscriptionModels.Paraformer_V1)
.fileUrls(List.of(String.valueOf(instructions.getURL())))
.build();
}
catch (IOException e) {
throw new TongYiException("Failed to create resource", e);
}
}
List<TranscriptionTaskResult> taskResultList;
try {
// Submit a transcription request
TranscriptionResult result = transcription.asyncCall(transcriptionParam);
// Wait for the transcription to complete
result = transcription.wait(TranscriptionQueryParam
.FromTranscriptionParam(transcriptionParam, result.getTaskId()));
// Get the transcription results
System.out.println(result.getOutput().getAsJsonObject());
taskResultList = result.getResults();
System.out.println(Arrays.toString(taskResultList.toArray()));
return new AudioTranscriptionResponse(
taskResultList.stream().map(taskResult ->
new AudioTranscriptionResult(taskResult.getTranscriptionUrl())
).collect(Collectors.toList()),
TongYiAudioTranscriptionResponseMetadata.from(result)
);
}
catch (Exception e) {
throw new TongYiException("Failed to call audio transcription", e);
}
}
public TongYiAudioTranscriptionOptions merge(TongYiAudioTranscriptionOptions target) {
var mergeBuilder = TongYiAudioTranscriptionOptions.builder();
mergeBuilder
.withModel(defaultOptions.getModel() != null ? defaultOptions.getModel()
: target.getModel());
mergeBuilder.withChannelId(
defaultOptions.getChannelId() != null ? defaultOptions.getChannelId()
: target.getChannelId());
mergeBuilder.withDiarizationEnabled(defaultOptions.getDiarizationEnabled() != null
? defaultOptions.getDiarizationEnabled()
: target.getDiarizationEnabled());
mergeBuilder.withDisfluencyRemovalEnabled(
defaultOptions.getDisfluencyRemovalEnabled() != null
? defaultOptions.getDisfluencyRemovalEnabled()
: target.getDisfluencyRemovalEnabled());
mergeBuilder.withTimestampAlignmentEnabled(
defaultOptions.getTimestampAlignmentEnabled() != null
? defaultOptions.getTimestampAlignmentEnabled()
: target.getTimestampAlignmentEnabled());
mergeBuilder.withSpecialWordFilter(defaultOptions.getSpecialWordFilter() != null
? defaultOptions.getSpecialWordFilter()
: target.getSpecialWordFilter());
mergeBuilder.withAudioEventDetectionEnabled(
defaultOptions.getAudioEventDetectionEnabled() != null
? defaultOptions.getAudioEventDetectionEnabled()
: target.getAudioEventDetectionEnabled());
return mergeBuilder.build();
}
public TranscriptionParam toTranscriptionParam(
TongYiAudioTranscriptionOptions source) {
var mergeBuilder = TranscriptionParam.builder();
mergeBuilder.model(source.getModel() != null ? source.getModel()
: AudioTranscriptionModels.Paraformer_V1);
mergeBuilder.fileUrls(
source.getFileUrls() != null ? source.getFileUrls() : new ArrayList<>());
if (source.getPhraseId() != null) {
mergeBuilder.phraseId(source.getPhraseId());
}
if (source.getChannelId() != null) {
mergeBuilder.channelId(source.getChannelId());
}
if (source.getDiarizationEnabled() != null) {
mergeBuilder.diarizationEnabled(source.getDiarizationEnabled());
}
if (source.getSpeakerCount() != null) {
mergeBuilder.speakerCount(source.getSpeakerCount());
}
if (source.getDisfluencyRemovalEnabled() != null) {
mergeBuilder.disfluencyRemovalEnabled(source.getDisfluencyRemovalEnabled());
}
if (source.getTimestampAlignmentEnabled() != null) {
mergeBuilder.timestampAlignmentEnabled(source.getTimestampAlignmentEnabled());
}
if (source.getSpecialWordFilter() != null) {
mergeBuilder.specialWordFilter(source.getSpecialWordFilter());
}
if (source.getAudioEventDetectionEnabled() != null) {
mergeBuilder
.audioEventDetectionEnabled(source.getAudioEventDetectionEnabled());
}
return mergeBuilder.build();
}
}

View File

@ -0,0 +1,203 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.audio.transcription;
import com.alibaba.cloud.ai.tongyi.audio.AudioTranscriptionModels;
import org.springframework.ai.model.ModelOptions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author xYLiu
* @author yuluo
* @since 2023.0.1.0
*/
public class TongYiAudioTranscriptionOptions implements ModelOptions {
private String model = AudioTranscriptionModels.Paraformer_V1;
private List<String> fileUrls = new ArrayList<>();
private String phraseId = null;
private List<Integer> channelId = Collections.singletonList(0);
private Boolean diarizationEnabled = false;
private Integer speakerCount = null;
private Boolean disfluencyRemovalEnabled = false;
private Boolean timestampAlignmentEnabled = false;
private String specialWordFilter = "";
private Boolean audioEventDetectionEnabled = false;
public static Builder builder() {
return new Builder();
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public List<String> getFileUrls() {
return fileUrls;
}
public void setFileUrls(List<String> fileUrls) {
this.fileUrls = fileUrls;
}
public String getPhraseId() {
return phraseId;
}
public void setPhraseId(String phraseId) {
this.phraseId = phraseId;
}
public List<Integer> getChannelId() {
return channelId;
}
public void setChannelId(List<Integer> channelId) {
this.channelId = channelId;
}
public Boolean getDiarizationEnabled() {
return diarizationEnabled;
}
public void setDiarizationEnabled(Boolean diarizationEnabled) {
this.diarizationEnabled = diarizationEnabled;
}
public Integer getSpeakerCount() {
return speakerCount;
}
public void setSpeakerCount(Integer speakerCount) {
this.speakerCount = speakerCount;
}
public Boolean getDisfluencyRemovalEnabled() {
return disfluencyRemovalEnabled;
}
public void setDisfluencyRemovalEnabled(Boolean disfluencyRemovalEnabled) {
this.disfluencyRemovalEnabled = disfluencyRemovalEnabled;
}
public Boolean getTimestampAlignmentEnabled() {
return timestampAlignmentEnabled;
}
public void setTimestampAlignmentEnabled(Boolean timestampAlignmentEnabled) {
this.timestampAlignmentEnabled = timestampAlignmentEnabled;
}
public String getSpecialWordFilter() {
return specialWordFilter;
}
public void setSpecialWordFilter(String specialWordFilter) {
this.specialWordFilter = specialWordFilter;
}
public Boolean getAudioEventDetectionEnabled() {
return audioEventDetectionEnabled;
}
public void setAudioEventDetectionEnabled(Boolean audioEventDetectionEnabled) {
this.audioEventDetectionEnabled = audioEventDetectionEnabled;
}
/**
* Builder class for constructing TongYiAudioTranscriptionOptions instances.
*/
public static class Builder {
private final TongYiAudioTranscriptionOptions options = new TongYiAudioTranscriptionOptions();
public Builder withModel(String model) {
options.model = model;
return this;
}
public Builder withFileUrls(List<String> fileUrls) {
options.fileUrls = fileUrls;
return this;
}
public Builder withPhraseId(String phraseId) {
options.phraseId = phraseId;
return this;
}
public Builder withChannelId(List<Integer> channelId) {
options.channelId = channelId;
return this;
}
public Builder withDiarizationEnabled(Boolean diarizationEnabled) {
options.diarizationEnabled = diarizationEnabled;
return this;
}
public Builder withSpeakerCount(Integer speakerCount) {
options.speakerCount = speakerCount;
return this;
}
public Builder withDisfluencyRemovalEnabled(Boolean disfluencyRemovalEnabled) {
options.disfluencyRemovalEnabled = disfluencyRemovalEnabled;
return this;
}
public Builder withTimestampAlignmentEnabled(Boolean timestampAlignmentEnabled) {
options.timestampAlignmentEnabled = timestampAlignmentEnabled;
return this;
}
public Builder withSpecialWordFilter(String specialWordFilter) {
options.specialWordFilter = specialWordFilter;
return this;
}
public Builder withAudioEventDetectionEnabled(
Boolean audioEventDetectionEnabled) {
options.audioEventDetectionEnabled = audioEventDetectionEnabled;
return this;
}
public TongYiAudioTranscriptionOptions build() {
// Perform any necessary validation here before returning the built object
return options;
}
}
}

View File

@ -0,0 +1,72 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.audio.transcription;
import com.alibaba.cloud.ai.tongyi.audio.AudioTranscriptionModels;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import static com.alibaba.cloud.ai.tongyi.common.constants.TongYiConstants.SCA_AI_CONFIGURATION;
/**
* @author xYLiu
* @author yuluo
* @since 2023.0.1.0
*/
@ConfigurationProperties(TongYiAudioTranscriptionProperties.CONFIG_PREFIX)
public class TongYiAudioTranscriptionProperties {
/**
* Spring Cloud Alibaba AI configuration prefix.
*/
public static final String CONFIG_PREFIX = SCA_AI_CONFIGURATION + "audio.transcription";
/**
* Default TongYi Chat model.
*/
public static final String DEFAULT_AUDIO_MODEL_NAME = AudioTranscriptionModels.Paraformer_V1;
/**
* Enable TongYiQWEN ai audio client.
*/
private boolean enabled = true;
@NestedConfigurationProperty
private TongYiAudioTranscriptionOptions options = TongYiAudioTranscriptionOptions
.builder().withModel(DEFAULT_AUDIO_MODEL_NAME).build();
public TongYiAudioTranscriptionOptions getOptions() {
return this.options;
}
public void setOptions(TongYiAudioTranscriptionOptions options) {
this.options = options;
}
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}

View File

@ -0,0 +1,56 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.audio.transcription.api;
import com.alibaba.cloud.ai.tongyi.audio.transcription.TongYiAudioTranscriptionOptions;
import org.springframework.ai.model.ModelRequest;
import org.springframework.core.io.Resource;
/**
* @author xYLiu
* @author yuluo
* @since 2023.0.1.0
*/
public class AudioTranscriptionPrompt implements ModelRequest<Resource> {
private Resource audioResource;
private TongYiAudioTranscriptionOptions transcriptionOptions;
public AudioTranscriptionPrompt(Resource resource) {
this.audioResource = resource;
}
public AudioTranscriptionPrompt(Resource resource, TongYiAudioTranscriptionOptions transcriptionOptions) {
this.audioResource = resource;
this.transcriptionOptions = transcriptionOptions;
}
@Override
public Resource getInstructions() {
return audioResource;
}
@Override
public TongYiAudioTranscriptionOptions getOptions() {
return transcriptionOptions;
}
}

View File

@ -0,0 +1,67 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.audio.transcription.api;
import com.alibaba.cloud.ai.tongyi.metadata.audio.TongYiAudioTranscriptionResponseMetadata;
import org.springframework.ai.model.ModelResponse;
import org.springframework.ai.model.ResponseMetadata;
import java.util.List;
/**
* @author xYLiu
* @author yuluo
* @since 2023.0.1.0
*/
public class AudioTranscriptionResponse implements ModelResponse<AudioTranscriptionResult> {
private List<AudioTranscriptionResult> resultList;
private TongYiAudioTranscriptionResponseMetadata transcriptionResponseMetadata;
public AudioTranscriptionResponse(List<AudioTranscriptionResult> result) {
this(result, TongYiAudioTranscriptionResponseMetadata.NULL);
}
public AudioTranscriptionResponse(List<AudioTranscriptionResult> result,
TongYiAudioTranscriptionResponseMetadata transcriptionResponseMetadata) {
this.resultList = List.copyOf(result);
this.transcriptionResponseMetadata = transcriptionResponseMetadata;
}
@Override
public AudioTranscriptionResult getResult() {
return this.resultList.get(0);
}
@Override
public List<AudioTranscriptionResult> getResults() {
return this.resultList;
}
@Override
public ResponseMetadata getMetadata() {
return this.transcriptionResponseMetadata;
}
}

View File

@ -0,0 +1,68 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.audio.transcription.api;
import com.alibaba.cloud.ai.tongyi.metadata.audio.TongYiAudioTranscriptionMetadata;
import org.springframework.ai.model.ModelResult;
import org.springframework.ai.model.ResultMetadata;
import java.util.Objects;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
public class AudioTranscriptionResult implements ModelResult<String> {
private String text;
private TongYiAudioTranscriptionMetadata transcriptionMetadata;
public AudioTranscriptionResult(String text) {
this.text = text;
}
@Override
public String getOutput() {
return this.text;
}
@Override
public ResultMetadata getMetadata() {
return transcriptionMetadata != null ? transcriptionMetadata : TongYiAudioTranscriptionMetadata.NULL;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AudioTranscriptionResult that = (AudioTranscriptionResult) o;
return Objects.equals(text, that.text) && Objects.equals(transcriptionMetadata, that.transcriptionMetadata);
}
@Override
public int hashCode() {
return Objects.hash(text, transcriptionMetadata);
}
}

View File

@ -0,0 +1,481 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.chat;
import com.alibaba.cloud.ai.tongyi.common.exception.TongYiException;
import com.alibaba.dashscope.aigc.conversation.ConversationParam;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationOutput;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.MessageManager;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.tools.FunctionDefinition;
import com.alibaba.dashscope.tools.ToolCallBase;
import com.alibaba.dashscope.tools.ToolCallFunction;
import com.alibaba.dashscope.utils.ApiKeywords;
import com.alibaba.dashscope.utils.JsonUtils;
import io.reactivex.Flowable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.metadata.ChatGenerationMetadata;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.model.StreamingChatModel;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.ModelOptionsUtils;
import org.springframework.ai.model.function.AbstractFunctionCallSupport;
import org.springframework.ai.model.function.FunctionCallbackContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
/**
* {@link ChatModel} and {@link StreamingChatModel} implementation for {@literal Alibaba DashScope}
* backed by {@link Generation}.
*
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
* @see ChatModel
* @see com.alibaba.dashscope.aigc.generation
*/
public class TongYiChatModel extends
AbstractFunctionCallSupport<
com.alibaba.dashscope.common.Message,
ConversationParam,
GenerationResult>
implements ChatModel, StreamingChatModel {
private static final Logger logger = LoggerFactory.getLogger(TongYiChatModel.class);
/**
* DashScope generation client.
*/
private final Generation generation;
/**
* The TongYi models default chat completion api.
*/
private TongYiChatOptions defaultOptions;
/**
* User role message manager.
*/
@Autowired
private MessageManager msgManager;
/**
* Initializes an instance of the TongYiChatClient.
* @param generation DashScope generation client.
*/
public TongYiChatModel(Generation generation) {
this(generation,
TongYiChatOptions.builder()
.withTopP(0.8)
.withEnableSearch(true)
.withResultFormat(ConversationParam.ResultFormat.MESSAGE)
.build(),
null
);
}
/**
* Initializes an instance of the TongYiChatClient.
* @param generation DashScope generation client.
* @param options TongYi model params.
*/
public TongYiChatModel(Generation generation, TongYiChatOptions options) {
this(generation, options, null);
}
/**
* Create a TongYi models client.
* @param generation DashScope model generation client.
* @param options TongYi default chat completion api.
*/
public TongYiChatModel(Generation generation, TongYiChatOptions options,
FunctionCallbackContext functionCallbackContext) {
super(functionCallbackContext);
this.generation = generation;
this.defaultOptions = options;
}
/**
* Get default sca chat options.
*
* @return TongYiChatOptions default object.
*/
public TongYiChatOptions getDefaultOptions() {
return this.defaultOptions;
}
@Override
public ChatResponse call(Prompt prompt) {
ConversationParam params = toTongYiChatParams(prompt);
// TongYi models context loader.
com.alibaba.dashscope.common.Message message = new com.alibaba.dashscope.common.Message();
message.setRole(Role.USER.getValue());
message.setContent(prompt.getContents());
msgManager.add(message);
params.setMessages(msgManager.get());
logger.trace("TongYi ConversationOptions: {}", params);
GenerationResult chatCompletions = this.callWithFunctionSupport(params);
logger.trace("TongYi ConversationOptions: {}", params);
msgManager.add(chatCompletions);
List<org.springframework.ai.chat.model.Generation> generations =
chatCompletions
.getOutput()
.getChoices()
.stream()
.map(choice ->
new org.springframework.ai.chat.model.Generation(
choice
.getMessage()
.getContent()
).withGenerationMetadata(generateChoiceMetadata(choice)
))
.toList();
return new ChatResponse(generations);
}
@Override
public Flux<ChatResponse> stream(Prompt prompt) {
Flowable<GenerationResult> genRes;
ConversationParam tongYiChatParams = toTongYiChatParams(prompt);
// See https://help.aliyun.com/zh/dashscope/developer-reference/api-details?spm=a2c4g.11186623.0.0.655fc11aRR0jj7#b9ad0a10cfhpe
// tongYiChatParams.setIncrementalOutput(true);
try {
genRes = generation.streamCall(tongYiChatParams);
}
catch (NoApiKeyException | InputRequiredException e) {
logger.warn("TongYi chat client: " + e.getMessage());
throw new TongYiException(e.getMessage());
}
return Flux.from(genRes)
.flatMap(
message -> Flux.just(
message.getOutput()
.getChoices()
.get(0)
.getMessage()
.getContent())
.map(content -> {
var gen = new org.springframework.ai.chat.model.Generation(content)
.withGenerationMetadata(generateChoiceMetadata(
message.getOutput()
.getChoices()
.get(0)
));
return new ChatResponse(List.of(gen));
})
)
.publishOn(Schedulers.parallel());
}
/**
* Configuration properties to Qwen model params.
* Test access.
*
* @param prompt {@link Prompt}
* @return Qwen models params {@link ConversationParam}
*/
public ConversationParam toTongYiChatParams(Prompt prompt) {
Set<String> functionsForThisRequest = new HashSet<>();
List<com.alibaba.dashscope.common.Message> tongYiMessage = prompt.getInstructions().stream()
.map(this::fromSpringAIMessage)
.toList();
ConversationParam chatParams = ConversationParam.builder()
.messages(tongYiMessage)
// models setting
// {@link HalfDuplexServiceParam#models}
.model(Generation.Models.QWEN_TURBO)
// {@link GenerationOutput}
.resultFormat(ConversationParam.ResultFormat.MESSAGE)
.build();
if (this.defaultOptions != null) {
chatParams = merge(chatParams, this.defaultOptions);
Set<String> defaultEnabledFunctions = this.handleFunctionCallbackConfigurations(this.defaultOptions, !IS_RUNTIME_CALL);
functionsForThisRequest.addAll(defaultEnabledFunctions);
}
if (prompt.getOptions() != null) {
if (prompt.getOptions() instanceof ChatOptions runtimeOptions) {
TongYiChatOptions updatedRuntimeOptions = ModelOptionsUtils.copyToTarget(runtimeOptions,
ChatOptions.class, TongYiChatOptions.class);
chatParams = merge(updatedRuntimeOptions, chatParams);
Set<String> promptEnabledFunctions = this.handleFunctionCallbackConfigurations(updatedRuntimeOptions,
IS_RUNTIME_CALL);
functionsForThisRequest.addAll(promptEnabledFunctions);
}
else {
throw new IllegalArgumentException("Prompt options are not of type ConversationParam:"
+ prompt.getOptions().getClass().getSimpleName());
}
}
// Add the enabled functions definitions to the request's tools parameter.
if (!CollectionUtils.isEmpty(functionsForThisRequest)) {
List<FunctionDefinition> tools = this.getFunctionTools(functionsForThisRequest);
// todo chatParams.setTools(tools)
}
return chatParams;
}
private ChatGenerationMetadata generateChoiceMetadata(GenerationOutput.Choice choice) {
return ChatGenerationMetadata.from(
String.valueOf(choice.getFinishReason()),
choice.getMessage().getContent()
);
}
private List<FunctionDefinition> getFunctionTools(Set<String> functionNames) {
return this.resolveFunctionCallbacks(functionNames).stream().map(functionCallback -> {
FunctionDefinition functionDefinition = FunctionDefinition.builder()
.name(functionCallback.getName())
.description(functionCallback.getDescription())
.parameters(JsonUtils.parametersToJsonObject(
ModelOptionsUtils.jsonToMap(functionCallback.getInputTypeSchema())
))
.build();
return functionDefinition;
}).toList();
}
private ConversationParam merge(ConversationParam tongYiParams, TongYiChatOptions scaChatParams) {
if (scaChatParams == null) {
return tongYiParams;
}
return ConversationParam.builder()
.messages(tongYiParams.getMessages())
.maxTokens((tongYiParams.getMaxTokens() != null) ? tongYiParams.getMaxTokens() : scaChatParams.getMaxTokens())
// When merge options. Because ConversationParams is must not null. So is setting.
.model(scaChatParams.getModel())
.resultFormat((tongYiParams.getResultFormat() != null) ? tongYiParams.getResultFormat() : scaChatParams.getResultFormat())
.enableSearch((tongYiParams.getEnableSearch() != null) ? tongYiParams.getEnableSearch() : scaChatParams.getEnableSearch())
.topK((tongYiParams.getTopK() != null) ? tongYiParams.getTopK() : scaChatParams.getTopK())
.topP((tongYiParams.getTopP() != null) ? tongYiParams.getTopP() : scaChatParams.getTopP())
.incrementalOutput((tongYiParams.getIncrementalOutput() != null) ? tongYiParams.getIncrementalOutput() : scaChatParams.getIncrementalOutput())
.temperature((tongYiParams.getTemperature() != null) ? tongYiParams.getTemperature() : scaChatParams.getTemperature())
.repetitionPenalty((tongYiParams.getRepetitionPenalty() != null) ? tongYiParams.getRepetitionPenalty() : scaChatParams.getRepetitionPenalty())
.seed((tongYiParams.getSeed() != null) ? tongYiParams.getSeed() : scaChatParams.getSeed())
.build();
}
private ConversationParam merge(TongYiChatOptions scaChatParams, ConversationParam tongYiParams) {
if (scaChatParams == null) {
return tongYiParams;
}
ConversationParam mergedTongYiParams = ConversationParam.builder()
.model(Generation.Models.QWEN_TURBO)
.messages(tongYiParams.getMessages())
.build();
mergedTongYiParams = merge(tongYiParams, scaChatParams);
if (scaChatParams.getMaxTokens() != null) {
mergedTongYiParams.setMaxTokens(scaChatParams.getMaxTokens());
}
if (scaChatParams.getStop() != null) {
mergedTongYiParams.setStopStrings(scaChatParams.getStop());
}
if (scaChatParams.getTemperature() != null) {
mergedTongYiParams.setTemperature(scaChatParams.getTemperature());
}
if (scaChatParams.getTopK() != null) {
mergedTongYiParams.setTopK(scaChatParams.getTopK());
}
if (scaChatParams.getTopK() != null) {
mergedTongYiParams.setTopK(scaChatParams.getTopK());
}
return mergedTongYiParams;
}
private com.alibaba.dashscope.common.Message fromSpringAIMessage(Message message) {
return switch (message.getMessageType()) {
case USER -> com.alibaba.dashscope.common.Message.builder()
.role(Role.USER.getValue())
.content(message.getContent())
.build();
case SYSTEM -> com.alibaba.dashscope.common.Message.builder()
.role(Role.SYSTEM.getValue())
.content(message.getContent())
.build();
case ASSISTANT -> com.alibaba.dashscope.common.Message.builder()
.role(Role.ASSISTANT.getValue())
.content(message.getContent())
.build();
default -> throw new IllegalArgumentException("Unknown message type " + message.getMessageType());
};
}
@Override
protected ConversationParam doCreateToolResponseRequest(
ConversationParam previousRequest,
com.alibaba.dashscope.common.Message responseMessage,
List<com.alibaba.dashscope.common.Message> conversationHistory
) {
for (ToolCallBase toolCall : responseMessage.getToolCalls()) {
if (toolCall instanceof ToolCallFunction toolCallFunction) {
if (toolCallFunction.getFunction() != null) {
var functionName = toolCallFunction.getFunction().getName();
var functionArguments = toolCallFunction.getFunction().getArguments();
if (!this.functionCallbackRegister.containsKey(functionName)) {
throw new IllegalStateException("No function callback found for function name: " + functionName);
}
String functionResponse = this.functionCallbackRegister.get(functionName).call(functionArguments);
// Add the function response to the conversation.
conversationHistory
.add(com.alibaba.dashscope.common.Message.builder()
.content(functionResponse)
.role(Role.BOT.getValue())
.toolCallId(toolCall.getId())
.build()
);
}
}
}
ConversationParam newRequest = ConversationParam.builder().messages(conversationHistory).build();
// todo: No @JsonProperty fields.
newRequest = ModelOptionsUtils.merge(newRequest, previousRequest, ConversationParam.class);
return newRequest;
}
@Override
protected List<com.alibaba.dashscope.common.Message> doGetUserMessages(ConversationParam request) {
return request.getMessages();
}
@Override
protected com.alibaba.dashscope.common.Message doGetToolResponseMessage(GenerationResult response) {
var message = response.getOutput().getChoices().get(0).getMessage();
var assistantMessage = com.alibaba.dashscope.common.Message.builder().role(Role.ASSISTANT.getValue())
.content("").build();
assistantMessage.setToolCalls(message.getToolCalls());
return assistantMessage;
}
@Override
protected GenerationResult doChatCompletion(ConversationParam request) {
GenerationResult result;
try {
result = generation.call(request);
}
catch (NoApiKeyException | InputRequiredException e) {
throw new RuntimeException(e);
}
return result;
}
@Override
protected Flux<GenerationResult> doChatCompletionStream(ConversationParam request) {
final Flowable<GenerationResult> genRes;
try {
genRes = generation.streamCall(request);
}
catch (NoApiKeyException | InputRequiredException e) {
logger.warn("TongYi chat client: " + e.getMessage());
throw new TongYiException(e.getMessage());
}
return Flux.from(genRes);
}
@Override
protected boolean isToolFunctionCall(GenerationResult response) {
if (response == null || CollectionUtils.isEmpty(response.getOutput().getChoices())) {
return false;
}
var choice = response.getOutput().getChoices().get(0);
if (choice == null || choice.getFinishReason() == null) {
return false;
}
return Objects.equals(choice.getFinishReason(), ApiKeywords.TOOL_CALLS);
}
}

View File

@ -0,0 +1,463 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.chat;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.model.function.FunctionCallback;
import org.springframework.ai.model.function.FunctionCallingOptions;
import org.springframework.util.Assert;
import java.util.*;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
public class TongYiChatOptions implements FunctionCallingOptions, ChatOptions {
/**
* TongYi Models.
* {@link Generation.Models}
*/
private String model = Generation.Models.QWEN_TURBO;
/**
* The random number seed used in generation, the user controls the randomness of the content generated by the model.
* seed supports unsigned 64-bit integers, with a default value of 1234.
* when using seed, the model will generate the same or similar results as much as possible, but there is currently no guarantee that the results will be exactly the same each time.
*/
private Integer seed = 1234;
/**
* Used to specify the maximum number of tokens that the model can generate when generating content,
* it defines the upper limit of generation but does not guarantee that this number will be generated every time.
* For qwen-turbo the maximum and default values are 1500 tokens.
* The qwen-max, qwen-max-1201, qwen-max-longcontext, and qwen-plus models have a maximum and default value of 2000 tokens.
*/
private Integer maxTokens = 1500;
/**
* The generation process kernel sampling method probability threshold,
* for example, takes the value of 0.8, only retains the smallest set of the most probable tokens with probabilities that add up to greater than or equal to 0.8 as the candidate set.
* The range of values is (0,1.0), the larger the value, the higher the randomness of generation; the lower the value, the higher the certainty of generation.
*/
private Double topP = 0.8;
/**
* The size of the sampling candidate set at the time of generation.
* For example, with a value of 50, only the 50 highest scoring tokens in a single generation will form a randomly sampled candidate set.
* The larger the value, the higher the randomness of the generation; the smaller the value, the higher the certainty of the generation.
* This parameter is not passed by default, and a value of None or when top_k is greater than 100 indicates that the top_k policy is not enabled,
* at which time, only the top_p policy is in effect.
*/
private Integer topK;
/**
* Used to control the repeatability of model generation.
* Increasing repetition_penalty reduces the repetition of model generation. 1.0 means no penalty.
*/
private Double repetitionPenalty = 1.1;
/**
* is used to control the degree of randomness and diversity.
* Specifically, the temperature value controls the extent to which the probability distribution of each candidate word is smoothed when generating text.
* Higher values of temperature reduce the peak of the probability distribution, allowing more low-probability words to be selected and generating more diverse results,
* while lower values of temperature enhance the peak of the probability distribution, making it easier for high-probability words to be selected and generating more certain results.
* Range: [0, 2), 0 is not recommended, meaningless.
* java version >= 2.5.1
*/
private Double temperature = 0.85;
/**
* The stop parameter is used to realize precise control of the content generation process, automatically stopping when the generated content is about to contain the specified string or token_ids,
* and the generated content does not contain the specified content.
* For example, if stop is specified as "Hello", it means stop when "Hello" will be generated; if stop is specified as [37763, 367], it means stop when "Observation" will be generated.
* The stop parameter can be passed as a list of arrays of strings or token_ids to support the scenario of using multiple stops.
* Explanation: Do not mix strings and token_ids in list mode, the element types should be the same in list mode.
*/
private List<String> stop;
/**
* Whether or not to use stream output. When outputting the result in stream mode, the interface returns the result as generator,
* you need to iterate to get the result, the default output is the whole sequence of the current generation for each output,
* the last output is the final result of all the generation, you can change the output mode to non-incremental output by the parameter incremental_output to False.
*/
private Boolean stream = false;
/**
* The model has a built-in Internet search service.
* This parameter controls whether the model refers to the use of Internet search results when generating text. The values are as follows:
* True: enable internet search, the model will use the search result as the reference information in the text generation process, but the model will "judge by itself" whether to use the internet search result based on its internal logic.
* False (default): Internet search is disabled.
*/
private Boolean enableSearch = false;
/**
* [text|message], defaults to text, when it is message,
* the output refers to the message result example.
* It is recommended to prioritize the use of message format.
*/
private String resultFormat = GenerationParam.ResultFormat.MESSAGE;
/**
* Control the streaming output mode, that is, the content will contain the content has been output;
* set to True, will open the incremental output mode, the output will not contain the content has been output,
* you need to splice the whole output, refer to the streaming output sample code.
*/
private Boolean incrementalOutput = false;
/**
* A list of tools that the model can optionally call.
* Currently only functions are supported, and even if multiple functions are entered, the model will only select one to generate the result.
*/
private List<String> tools;
@Override
public Float getTemperature() {
return this.temperature.floatValue();
}
public void setTemperature(Float temperature) {
this.temperature = temperature.doubleValue();
}
@Override
public Float getTopP() {
return this.topP.floatValue();
}
public void setTopP(Float topP) {
this.topP = topP.doubleValue();
}
@Override
public Integer getTopK() {
return this.topK;
}
public void setTopK(Integer topK) {
this.topK = topK;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public Integer getSeed() {
return seed;
}
public String getResultFormat() {
return resultFormat;
}
public void setResultFormat(String resultFormat) {
this.resultFormat = resultFormat;
}
public void setSeed(Integer seed) {
this.seed = seed;
}
public Integer getMaxTokens() {
return maxTokens;
}
public void setMaxTokens(Integer maxTokens) {
this.maxTokens = maxTokens;
}
public Float getRepetitionPenalty() {
return repetitionPenalty.floatValue();
}
public void setRepetitionPenalty(Float repetitionPenalty) {
this.repetitionPenalty = repetitionPenalty.doubleValue();
}
public List<String> getStop() {
return stop;
}
public void setStop(List<String> stop) {
this.stop = stop;
}
public Boolean getStream() {
return stream;
}
public void setStream(Boolean stream) {
this.stream = stream;
}
public Boolean getEnableSearch() {
return enableSearch;
}
public void setEnableSearch(Boolean enableSearch) {
this.enableSearch = enableSearch;
}
public Boolean getIncrementalOutput() {
return incrementalOutput;
}
public void setIncrementalOutput(Boolean incrementalOutput) {
this.incrementalOutput = incrementalOutput;
}
public List<String> getTools() {
return tools;
}
public void setTools(List<String> tools) {
this.tools = tools;
}
private List<FunctionCallback> functionCallbacks = new ArrayList<>();
private Set<String> functions = new HashSet<>();
@Override
public List<FunctionCallback> getFunctionCallbacks() {
return this.functionCallbacks;
}
@Override
public void setFunctionCallbacks(List<FunctionCallback> functionCallbacks) {
this.functionCallbacks = functionCallbacks;
}
@Override
public Set<String> getFunctions() {
return this.functions;
}
@Override
public void setFunctions(Set<String> functions) {
this.functions = functions;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TongYiChatOptions that = (TongYiChatOptions) o;
return Objects.equals(model, that.model)
&& Objects.equals(seed, that.seed)
&& Objects.equals(maxTokens, that.maxTokens)
&& Objects.equals(topP, that.topP)
&& Objects.equals(topK, that.topK)
&& Objects.equals(repetitionPenalty, that.repetitionPenalty)
&& Objects.equals(temperature, that.temperature)
&& Objects.equals(stop, that.stop)
&& Objects.equals(stream, that.stream)
&& Objects.equals(enableSearch, that.enableSearch)
&& Objects.equals(resultFormat, that.resultFormat)
&& Objects.equals(incrementalOutput, that.incrementalOutput)
&& Objects.equals(tools, that.tools)
&& Objects.equals(functionCallbacks, that.functionCallbacks)
&& Objects.equals(functions, that.functions);
}
@Override
public int hashCode() {
return Objects.hash(
model,
seed,
maxTokens,
topP,
topK,
repetitionPenalty,
temperature,
stop,
stream,
enableSearch,
resultFormat,
incrementalOutput,
tools,
functionCallbacks,
functions
);
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("TongYiChatOptions{");
sb.append(", model='").append(model).append('\'');
sb.append(", seed=").append(seed);
sb.append(", maxTokens=").append(maxTokens);
sb.append(", topP=").append(topP);
sb.append(", topK=").append(topK);
sb.append(", repetitionPenalty=").append(repetitionPenalty);
sb.append(", temperature=").append(temperature);
sb.append(", stop=").append(stop);
sb.append(", stream=").append(stream);
sb.append(", enableSearch=").append(enableSearch);
sb.append(", resultFormat='").append(resultFormat).append('\'');
sb.append(", incrementalOutput=").append(incrementalOutput);
sb.append(", tools=").append(tools);
sb.append(", functionCallbacks=").append(functionCallbacks);
sb.append(", functions=").append(functions);
sb.append('}');
return sb.toString();
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
protected TongYiChatOptions options;
public Builder() {
this.options = new TongYiChatOptions();
}
public Builder(TongYiChatOptions options) {
this.options = options;
}
public Builder withModel(String model) {
this.options.model = model;
return this;
}
public Builder withMaxTokens(Integer maxTokens) {
this.options.maxTokens = maxTokens;
return this;
}
public Builder withResultFormat(String rf) {
this.options.resultFormat = rf;
return this;
}
public Builder withEnableSearch(Boolean enableSearch) {
this.options.enableSearch = enableSearch;
return this;
}
public Builder withFunctionCallbacks(List<FunctionCallback> functionCallbacks) {
this.options.functionCallbacks = functionCallbacks;
return this;
}
public Builder withFunctions(Set<String> functionNames) {
Assert.notNull(functionNames, "Function names must not be null");
this.options.functions = functionNames;
return this;
}
public Builder withFunction(String functionName) {
Assert.hasText(functionName, "Function name must not be empty");
this.options.functions.add(functionName);
return this;
}
public Builder withSeed(Integer seed) {
this.options.seed = seed;
return this;
}
public Builder withStop(List<String> stop) {
this.options.stop = stop;
return this;
}
public Builder withTemperature(Double temperature) {
this.options.temperature = temperature;
return this;
}
public Builder withTopP(Double topP) {
this.options.topP = topP;
return this;
}
public Builder withTopK(Integer topK) {
this.options.topK = topK;
return this;
}
public Builder withRepetitionPenalty(Double repetitionPenalty) {
this.options.repetitionPenalty = repetitionPenalty;
return this;
}
public TongYiChatOptions build() {
return this.options;
}
}
}

View File

@ -0,0 +1,83 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.chat;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import static com.alibaba.cloud.ai.tongyi.common.constants.TongYiConstants.SCA_AI_CONFIGURATION;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
@ConfigurationProperties(TongYiChatProperties.CONFIG_PREFIX)
public class TongYiChatProperties {
/**
* Spring Cloud Alibaba AI configuration prefix.
*/
public static final String CONFIG_PREFIX = SCA_AI_CONFIGURATION + "chat";
/**
* Default TongYi Chat model.
*/
public static final String DEFAULT_DEPLOYMENT_NAME = Generation.Models.QWEN_TURBO;
/**
* Default temperature speed.
*/
private static final Double DEFAULT_TEMPERATURE = 0.8;
/**
* Enable TongYiQWEN ai chat client.
*/
private boolean enabled = true;
@NestedConfigurationProperty
private TongYiChatOptions options = TongYiChatOptions.builder()
.withModel(DEFAULT_DEPLOYMENT_NAME)
.withTemperature(DEFAULT_TEMPERATURE)
.withEnableSearch(true)
.withResultFormat(GenerationParam.ResultFormat.MESSAGE)
.build();
public TongYiChatOptions getOptions() {
return this.options;
}
public void setOptions(TongYiChatOptions options) {
this.options = options;
}
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}

View File

@ -0,0 +1,44 @@
/*
* Copyright 2024-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.common.constants;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
*/
public final class TongYiConstants {
private TongYiConstants() {
}
/**
* Spring Cloud Alibaba AI configuration prefix.
*/
public static final String SCA_AI_CONFIGURATION = "spring.cloud.ai.tongyi.";
/**
* Spring Cloud Alibaba AI constants prefix.
*/
public static final String SCA_AI = "SPRING_CLOUD_ALIBABA_";
/**
* TongYi AI apikey env name.
*/
public static final String SCA_AI_TONGYI_API_KEY = SCA_AI + "TONGYI_API_KEY";
}

View File

@ -0,0 +1,38 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.common.exception;
/**
* TongYi models runtime exception.
*
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
public class TongYiException extends RuntimeException {
public TongYiException(String message) {
super(message);
}
public TongYiException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -0,0 +1,39 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.common.exception;
/**
* TongYi models images exception.
*
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
public class TongYiImagesException extends TongYiException {
public TongYiImagesException(String message) {
super(message);
}
public TongYiImagesException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -0,0 +1,84 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.embedding;
import com.alibaba.dashscope.embeddings.TextEmbeddingParam;
import org.springframework.ai.embedding.EmbeddingOptions;
import java.util.List;
/**
* @author why_ohh
* @author yuluo
* @author <a href="mailto:550588941@qq.com">why_ohh</a>
* @since 2023.0.1.0
*/
public final class TongYiEmbeddingOptions implements EmbeddingOptions {
private List<String> texts;
private TextEmbeddingParam.TextType textType;
public List<String> getTexts() {
return texts;
}
public void setTexts(List<String> texts) {
this.texts = texts;
}
public TextEmbeddingParam.TextType getTextType() {
return textType;
}
public void setTextType(TextEmbeddingParam.TextType textType) {
this.textType = textType;
}
public static Builder builder() {
return new Builder();
}
public final static class Builder {
private final TongYiEmbeddingOptions options;
private Builder() {
this.options = new TongYiEmbeddingOptions();
}
public Builder withtexts(List<String> texts) {
options.setTexts(texts);
return this;
}
public Builder withtextType(TextEmbeddingParam.TextType textType) {
options.setTextType(textType);
return this;
}
public TongYiEmbeddingOptions build() {
return options;
}
}
}

View File

@ -0,0 +1,175 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.embedding;
import com.alibaba.cloud.ai.tongyi.common.exception.TongYiException;
import com.alibaba.cloud.ai.tongyi.metadata.TongYiTextEmbeddingResponseMetadata;
import com.alibaba.dashscope.embeddings.TextEmbedding;
import com.alibaba.dashscope.embeddings.TextEmbeddingParam;
import com.alibaba.dashscope.embeddings.TextEmbeddingResult;
import com.alibaba.dashscope.embeddings.TextEmbeddingResultItem;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.document.Document;
import org.springframework.ai.document.MetadataMode;
import org.springframework.ai.embedding.AbstractEmbeddingModel;
import org.springframework.ai.embedding.Embedding;
import org.springframework.ai.embedding.EmbeddingRequest;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.util.Assert;
import java.util.List;
import java.util.stream.Collectors;
/**
* {@link TongYiTextEmbeddingModel} implementation for {@literal Alibaba DashScope}.
*
* @author why_ohh
* @author yuluo
* @author <a href="mailto:550588941@qq.com">why_ohh</a>
* @since 2023.0.1.0
*/
public class TongYiTextEmbeddingModel extends AbstractEmbeddingModel {
private final Logger logger = LoggerFactory.getLogger(TongYiTextEmbeddingModel.class);
/**
* TongYi Text Embedding client.
*/
private final TextEmbedding textEmbedding;
/**
* {@link MetadataMode}.
*/
private final MetadataMode metadataMode;
private final TongYiEmbeddingOptions defaultOptions;
public TongYiTextEmbeddingModel(TextEmbedding textEmbedding) {
this(textEmbedding, MetadataMode.EMBED);
}
public TongYiTextEmbeddingModel(TextEmbedding textEmbedding, MetadataMode metadataMode) {
this(textEmbedding, metadataMode,
TongYiEmbeddingOptions.builder()
.withtextType(TextEmbeddingParam.TextType.DOCUMENT)
.build()
);
}
public TongYiTextEmbeddingModel(
TextEmbedding textEmbedding,
MetadataMode metadataMode,
TongYiEmbeddingOptions options
) {
Assert.notNull(textEmbedding, "textEmbedding must not be null");
Assert.notNull(metadataMode, "Metadata mode must not be null");
Assert.notNull(options, "TongYiEmbeddingOptions must not be null");
this.metadataMode = metadataMode;
this.textEmbedding = textEmbedding;
this.defaultOptions = options;
}
public TongYiEmbeddingOptions getDefaultOptions() {
return this.defaultOptions;
}
@Override
public List<Double> embed(Document document) {
return this.call(
new EmbeddingRequest(
List.of(document.getFormattedContent(this.metadataMode)),
null)
).getResults().stream()
.map(Embedding::getOutput)
.flatMap(List::stream)
.toList();
}
@Override
public EmbeddingResponse call(EmbeddingRequest request) {
TextEmbeddingParam embeddingParams = toEmbeddingParams(request);
logger.debug("Embedding request: {}", embeddingParams);
TextEmbeddingResult resp;
try {
resp = textEmbedding.call(embeddingParams);
}
catch (NoApiKeyException e) {
throw new TongYiException(e.getMessage());
}
return genEmbeddingResp(resp);
}
private EmbeddingResponse genEmbeddingResp(TextEmbeddingResult result) {
return new EmbeddingResponse(
genEmbeddingList(result.getOutput().getEmbeddings()),
TongYiTextEmbeddingResponseMetadata.from(result.getUsage())
);
}
private List<Embedding> genEmbeddingList(List<TextEmbeddingResultItem> embeddings) {
return embeddings.stream()
.map(embedding ->
new Embedding(
embedding.getEmbedding(),
embedding.getTextIndex()
))
.collect(Collectors.toList());
}
/**
* We recommend setting the model parameters by passing the embedding parameters through the code;
* yml configuration compatibility is not considered here.
* It is not recommended that users set parameters from yml,
* as this reduces the flexibility of the configuration.
* Because the model name keeps changing, strings are used here and constants are undefined:
* Model list: <a href="https://help.aliyun.com/zh/dashscope/developer-reference/text-embedding-quick-start">Text Embedding Model List</a>
* @param requestOptions Client params. {@link EmbeddingRequest}
* @return {@link TextEmbeddingParam}
*/
private TextEmbeddingParam toEmbeddingParams(EmbeddingRequest requestOptions) {
TextEmbeddingParam tongYiEmbeddingParams = TextEmbeddingParam.builder()
.texts(requestOptions.getInstructions())
.textType(defaultOptions.getTextType() != null ? defaultOptions.getTextType() : TextEmbeddingParam.TextType.DOCUMENT)
.model("text-embedding-v1")
.build();
try {
tongYiEmbeddingParams.validate();
}
catch (InputRequiredException e) {
throw new TongYiException(e.getMessage());
}
return tongYiEmbeddingParams;
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.embedding;
import org.springframework.boot.context.properties.ConfigurationProperties;
import static com.alibaba.cloud.ai.tongyi.common.constants.TongYiConstants.SCA_AI_CONFIGURATION;
/**
* @author why_ohh
* @author yuluo
* @author <a href="mailto:550588941@qq.com">why_ohh</a>
* @since 2023.0.1.0
*/
@ConfigurationProperties(TongYiTextEmbeddingProperties.CONFIG_PREFIX)
public class TongYiTextEmbeddingProperties {
/**
* Prefix of TongYi Text Embedding properties.
*/
public static final String CONFIG_PREFIX = SCA_AI_CONFIGURATION + "embedding";
private boolean enabled = true;
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}

View File

@ -0,0 +1,237 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.image;
import com.alibaba.cloud.ai.tongyi.common.exception.TongYiImagesException;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
import com.alibaba.dashscope.exception.NoApiKeyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.image.*;
import org.springframework.util.Assert;
import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.util.Base64;
import java.util.stream.Collectors;
import static com.alibaba.cloud.ai.tongyi.metadata.TongYiImagesResponseMetadata.from;
/**
* TongYiImagesClient is a class that implements the ImageClient interface. It provides a
* client for calling the TongYi AI image generation API.
*
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
public class TongYiImagesModel implements ImageModel {
private final Logger logger = LoggerFactory.getLogger(TongYiImagesModel.class);
/**
* Gen Images API.
*/
private final ImageSynthesis imageSynthesis;
/**
* TongYi Gen images properties.
*/
private TongYiImagesOptions defaultOptions;
/**
* Adapt TongYi images api size properties.
*/
private final String sizeConnection = "*";
/**
* Get default images options.
*
* @return Default TongYiImagesOptions.
*/
public TongYiImagesOptions getDefaultOptions() {
return this.defaultOptions;
}
/**
* TongYiImagesClient constructor.
* @param imageSynthesis the image synthesis
* {@link ImageSynthesis}
*/
public TongYiImagesModel(ImageSynthesis imageSynthesis) {
this(imageSynthesis, TongYiImagesOptions.
builder()
.withModel(ImageSynthesis.Models.WANX_V1)
.withN(1)
.build()
);
}
/**
* TongYiImagesClient constructor.
* @param imageSynthesis {@link ImageSynthesis}
* @param imagesOptions {@link TongYiImagesOptions}
*/
public TongYiImagesModel(ImageSynthesis imageSynthesis, TongYiImagesOptions imagesOptions) {
Assert.notNull(imageSynthesis, "ImageSynthesis must not be null");
Assert.notNull(imagesOptions, "TongYiImagesOptions must not be null");
this.imageSynthesis = imageSynthesis;
this.defaultOptions = imagesOptions;
}
/**
* Call the TongYi images service.
* @param imagePrompt the image prompt.
* @return the image response.
* {@link ImageSynthesis#call(ImageSynthesisParam)}
*/
@Override
public ImageResponse call(ImagePrompt imagePrompt) {
ImageSynthesisResult result;
String prompt = imagePrompt.getInstructions().get(0).getText();
var imgParams = ImageSynthesisParam.builder()
.prompt("")
.model(ImageSynthesis.Models.WANX_V1)
.build();
if (this.defaultOptions != null) {
imgParams = merge(this.defaultOptions);
}
if (imagePrompt.getOptions() != null) {
imgParams = merge(toTingYiImageOptions(imagePrompt.getOptions()));
}
imgParams.setPrompt(prompt);
try {
result = imageSynthesis.call(imgParams);
}
catch (NoApiKeyException e) {
logger.error("TongYi models gen images failed: {}.", e.getMessage());
throw new TongYiImagesException(e.getMessage());
}
return convert(result);
}
public ImageSynthesisParam merge(TongYiImagesOptions target) {
var builder = ImageSynthesisParam.builder();
builder.model(this.defaultOptions.getModel() != null ? this.defaultOptions.getModel() : target.getModel());
builder.n(this.defaultOptions.getN() != null ? this.defaultOptions.getN() : target.getN());
builder.size((this.defaultOptions.getHeight() != null && this.defaultOptions.getWidth() != null)
? this.defaultOptions.getHeight() + "*" + this.defaultOptions.getWidth()
: target.getHeight() + "*" + target.getWidth()
);
// prompt is marked non-null but is null.
builder.prompt("");
return builder.build();
}
private ImageResponse convert(ImageSynthesisResult result) {
return new ImageResponse(
result.getOutput().getResults().stream()
.flatMap(value -> value.entrySet().stream())
.map(entry -> {
String key = entry.getKey();
String value = entry.getValue();
try {
String base64Image = convertImageToBase64(value);
Image image = new Image(value, base64Image);
return new ImageGeneration(image);
}
catch (Exception e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.toList()),
from(result)
);
}
public TongYiImagesOptions toTingYiImageOptions(ImageOptions runtimeImageOptions) {
var builder = TongYiImagesOptions.builder();
if (runtimeImageOptions != null) {
if (runtimeImageOptions.getN() != null) {
builder.withN(runtimeImageOptions.getN());
}
if (runtimeImageOptions.getModel() != null) {
builder.withModel(runtimeImageOptions.getModel());
}
if (runtimeImageOptions.getHeight() != null) {
builder.withHeight(runtimeImageOptions.getHeight());
}
if (runtimeImageOptions.getWidth() != null) {
builder.withWidth(runtimeImageOptions.getWidth());
}
// todo ImagesParams.
}
return builder.build();
}
/**
* Convert image to base64.
* @param imageUrl the image url.
* @return the base64 image.
* @throws Exception the exception.
*/
public String convertImageToBase64(String imageUrl) throws Exception {
var url = new URL(imageUrl);
var inputStream = url.openStream();
var outputStream = new ByteArrayOutputStream();
var buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
var imageBytes = outputStream.toByteArray();
String base64Image = Base64.getEncoder().encodeToString(imageBytes);
inputStream.close();
outputStream.close();
return base64Image;
}
}

View File

@ -0,0 +1,187 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.image;
import com.alibaba.cloud.ai.tongyi.common.exception.TongYiImagesException;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import org.springframework.ai.image.ImageOptions;
import java.util.Objects;
/**
* TongYi Image API options.
*
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
public class TongYiImagesOptions implements ImageOptions {
/**
* Specify the model name, currently only wanx-v1 is supported.
*/
private String model = ImageSynthesis.Models.WANX_V1;
/**
* Gen images number.
*/
private Integer n;
/**
* The width of the generated images.
*/
private Integer width = 1024;
/**
* The height of the generated images.
*/
private Integer height = 1024;
@Override
public Integer getN() {
return this.n;
}
@Override
public String getModel() {
return this.model;
}
@Override
public Integer getWidth() {
return this.width;
}
@Override
public Integer getHeight() {
return this.height;
}
@Override
public String getResponseFormat() {
throw new TongYiImagesException("unimplemented!");
}
public void setModel(String model) {
this.model = model;
}
public void setN(Integer n) {
this.n = n;
}
public void setWidth(Integer width) {
this.width = width;
}
public void setHeight(Integer height) {
this.height = height;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TongYiImagesOptions that = (TongYiImagesOptions) o;
return Objects.equals(model, that.model)
&& Objects.equals(n, that.n)
&& Objects.equals(width, that.width)
&& Objects.equals(height, that.height);
}
@Override
public int hashCode() {
return Objects.hash(model, n, width, height);
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("TongYiImagesOptions{");
sb.append("model='").append(model).append('\'');
sb.append(", n=").append(n);
sb.append(", width=").append(width);
sb.append(", height=").append(height);
sb.append('}');
return sb.toString();
}
public static Builder builder() {
return new Builder();
}
public final static class Builder {
private final TongYiImagesOptions options;
private Builder() {
this.options = new TongYiImagesOptions();
}
public Builder withN(Integer n) {
options.setN(n);
return this;
}
public Builder withModel(String model) {
options.setModel(model);
return this;
}
public Builder withWidth(Integer width) {
options.setWidth(width);
return this;
}
public Builder withHeight(Integer height) {
options.setHeight(height);
return this;
}
public TongYiImagesOptions build() {
return options;
}
}
}

View File

@ -0,0 +1,77 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.image;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import static com.alibaba.cloud.ai.tongyi.common.constants.TongYiConstants.SCA_AI_CONFIGURATION;
/**
* TongYi Image API properties.
*
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
@ConfigurationProperties(TongYiImagesProperties.CONFIG_PREFIX)
public class TongYiImagesProperties {
/**
* Spring Cloud Alibaba AI configuration prefix.
*/
public static final String CONFIG_PREFIX = SCA_AI_CONFIGURATION + "images";
/**
* Default TongYi Chat model.
*/
public static final String DEFAULT_IMAGES_MODEL_NAME = ImageSynthesis.Models.WANX_V1;
/**
* Enable TongYiQWEN ai images client.
*/
private boolean enabled = true;
@NestedConfigurationProperty
private TongYiImagesOptions options = TongYiImagesOptions.builder()
.withModel(DEFAULT_IMAGES_MODEL_NAME)
.withN(1)
.build();
public TongYiImagesOptions getOptions() {
return this.options;
}
public void setOptions(TongYiImagesOptions options) {
this.options = options;
}
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}

View File

@ -0,0 +1,89 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.metadata;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import org.springframework.ai.chat.metadata.ChatResponseMetadata;
import org.springframework.ai.chat.metadata.PromptMetadata;
import org.springframework.ai.chat.metadata.Usage;
import org.springframework.util.Assert;
import java.util.HashMap;
/**
* {@link ChatResponseMetadata} implementation for {@literal Alibaba DashScope}.
*
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
public class TongYiAiChatResponseMetadata extends HashMap<String, Object> implements ChatResponseMetadata {
protected static final String AI_METADATA_STRING = "{ @type: %1$s, id: %2$s, usage: %3$s, rateLimit: %4$s }";
@SuppressWarnings("all")
public static TongYiAiChatResponseMetadata from(GenerationResult chatCompletions,
PromptMetadata promptFilterMetadata) {
Assert.notNull(chatCompletions, "Alibaba ai ChatCompletions must not be null");
String id = chatCompletions.getRequestId();
TongYiAiUsage usage = TongYiAiUsage.from(chatCompletions);
return new TongYiAiChatResponseMetadata(
id,
usage,
promptFilterMetadata
);
}
private final String id;
private final Usage usage;
private final PromptMetadata promptMetadata;
protected TongYiAiChatResponseMetadata(String id, TongYiAiUsage usage, PromptMetadata promptMetadata) {
this.id = id;
this.usage = usage;
this.promptMetadata = promptMetadata;
}
public String getId() {
return this.id;
}
@Override
public Usage getUsage() {
return this.usage;
}
@Override
public PromptMetadata getPromptMetadata() {
return this.promptMetadata;
}
@Override
public String toString() {
return AI_METADATA_STRING.formatted(getClass().getTypeName(), getId(), getUsage(), getRateLimit());
}
}

View File

@ -0,0 +1,81 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.metadata;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.GenerationUsage;
import org.springframework.ai.chat.metadata.Usage;
import org.springframework.util.Assert;
/**
* {@link Usage} implementation for {@literal Alibaba DashScope}.
*
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
public class TongYiAiUsage implements Usage {
private final GenerationUsage usage;
public TongYiAiUsage(GenerationUsage usage) {
Assert.notNull(usage, "GenerationUsage must not be null");
this.usage = usage;
}
public static TongYiAiUsage from(GenerationResult chatCompletions) {
Assert.notNull(chatCompletions, "ChatCompletions must not be null");
return from(chatCompletions.getUsage());
}
public static TongYiAiUsage from(GenerationUsage usage) {
return new TongYiAiUsage(usage);
}
protected GenerationUsage getUsage() {
return this.usage;
}
@Override
public Long getPromptTokens() {
throw new UnsupportedOperationException("Unimplemented method 'getPromptTokens'");
}
@Override
public Long getGenerationTokens() {
return this.getUsage().getOutputTokens().longValue();
}
@Override
public Long getTotalTokens() {
return this.getUsage().getTotalTokens().longValue();
}
@Override
public String toString() {
return this.getUsage().toString();
}
}

View File

@ -0,0 +1,131 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.metadata;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisTaskMetrics;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisUsage;
import org.springframework.ai.image.ImageResponseMetadata;
import org.springframework.util.Assert;
import java.util.HashMap;
import java.util.Objects;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
public class TongYiImagesResponseMetadata extends HashMap<String, Object> implements ImageResponseMetadata {
private final Long created;
private String taskId;
private ImageSynthesisTaskMetrics metrics;
private ImageSynthesisUsage usage;
public static TongYiImagesResponseMetadata from(ImageSynthesisResult synthesisResult) {
Assert.notNull(synthesisResult, "TongYiAiImageResponse must not be null");
return new TongYiImagesResponseMetadata(
System.currentTimeMillis(),
synthesisResult.getOutput().getTaskMetrics(),
synthesisResult.getOutput().getTaskId(),
synthesisResult.getUsage()
);
}
protected TongYiImagesResponseMetadata(
Long created,
ImageSynthesisTaskMetrics metrics,
String taskId,
ImageSynthesisUsage usage
) {
this.taskId = taskId;
this.metrics = metrics;
this.created = created;
this.usage = usage;
}
public ImageSynthesisUsage getUsage() {
return usage;
}
public void setUsage(ImageSynthesisUsage usage) {
this.usage = usage;
}
@Override
public Long getCreated() {
return created;
}
public String getTaskId() {
return taskId;
}
public void setTaskId(String taskId) {
this.taskId = taskId;
}
public ImageSynthesisTaskMetrics getMetrics() {
return metrics;
}
void setMetrics(ImageSynthesisTaskMetrics metrics) {
this.metrics = metrics;
}
public Long created() {
return this.created;
}
@Override
public String toString() {
return "TongYiImagesResponseMetadata {" + "created=" + created + '}';
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TongYiImagesResponseMetadata that = (TongYiImagesResponseMetadata) o;
return Objects.equals(created, that.created)
&& Objects.equals(taskId, that.taskId)
&& Objects.equals(metrics, that.metrics);
}
@Override
public int hashCode() {
return Objects.hash(created, taskId, metrics);
}
}

View File

@ -0,0 +1,53 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.metadata;
import com.alibaba.dashscope.embeddings.TextEmbeddingUsage;
import org.springframework.ai.embedding.EmbeddingResponseMetadata;
/**
* @author why_ohh
* @author yuluo
* @author <a href="mailto:550588941@qq.com">why_ohh</a>
* @since 2023.0.1.0
*/
public class TongYiTextEmbeddingResponseMetadata extends EmbeddingResponseMetadata {
private Integer totalTokens;
protected TongYiTextEmbeddingResponseMetadata(Integer totalTokens) {
this.totalTokens = totalTokens;
}
public static TongYiTextEmbeddingResponseMetadata from(TextEmbeddingUsage usage) {
return new TongYiTextEmbeddingResponseMetadata(usage.getTotalTokens());
}
public Integer getTotalTokens() {
return totalTokens;
}
public void setTotalTokens(Integer totalTokens) {
this.totalTokens = totalTokens;
}
}

View File

@ -0,0 +1,133 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.metadata.audio;
import com.alibaba.dashscope.audio.tts.SpeechSynthesisResult;
import com.alibaba.dashscope.audio.tts.SpeechSynthesisUsage;
import com.alibaba.dashscope.audio.tts.timestamp.Sentence;
import org.springframework.ai.chat.metadata.EmptyRateLimit;
import org.springframework.ai.chat.metadata.RateLimit;
import org.springframework.ai.model.ResponseMetadata;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import java.util.HashMap;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
public class TongYiAudioSpeechResponseMetadata extends HashMap<String, Object> implements ResponseMetadata {
private SpeechSynthesisUsage usage;
private String requestId;
private Sentence time;
protected static final String AI_METADATA_STRING = "{ @type: %1$s, requestsLimit: %2$s }";
/**
* NULL objects.
*/
public static final TongYiAudioSpeechResponseMetadata NULL = new TongYiAudioSpeechResponseMetadata() {
};
public static TongYiAudioSpeechResponseMetadata from(SpeechSynthesisResult result) {
Assert.notNull(result, "TongYi AI speech must not be null");
TongYiAudioSpeechResponseMetadata speechResponseMetadata = new TongYiAudioSpeechResponseMetadata();
return speechResponseMetadata;
}
public static TongYiAudioSpeechResponseMetadata from(String result) {
Assert.notNull(result, "TongYi AI speech must not be null");
TongYiAudioSpeechResponseMetadata speechResponseMetadata = new TongYiAudioSpeechResponseMetadata();
return speechResponseMetadata;
}
@Nullable
private RateLimit rateLimit;
public TongYiAudioSpeechResponseMetadata() {
this(null);
}
public TongYiAudioSpeechResponseMetadata(@Nullable RateLimit rateLimit) {
this.rateLimit = rateLimit;
}
@Nullable
public RateLimit getRateLimit() {
RateLimit rateLimit = this.rateLimit;
return rateLimit != null ? rateLimit : new EmptyRateLimit();
}
public TongYiAudioSpeechResponseMetadata withRateLimit(RateLimit rateLimit) {
this.rateLimit = rateLimit;
return this;
}
public TongYiAudioSpeechResponseMetadata withUsage(SpeechSynthesisUsage usage) {
this.usage = usage;
return this;
}
public TongYiAudioSpeechResponseMetadata withRequestId(String id) {
this.requestId = id;
return this;
}
public TongYiAudioSpeechResponseMetadata withSentence(Sentence sentence) {
this.time = sentence;
return this;
}
public SpeechSynthesisUsage getUsage() {
return usage;
}
public String getRequestId() {
return requestId;
}
public Sentence getTime() {
return time;
}
@Override
public String toString() {
return AI_METADATA_STRING.formatted(getClass().getName(), getRateLimit());
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.metadata.audio;
import org.springframework.ai.model.ResultMetadata;
/**
* @author xYLiu
* @author yuluo
* @since 2023.0.1.0
*/
public interface TongYiAudioTranscriptionMetadata extends ResultMetadata {
/**
* A constant instance of {@link TongYiAudioTranscriptionMetadata} that represents a null or empty metadata.
*/
TongYiAudioTranscriptionMetadata NULL = TongYiAudioTranscriptionMetadata.create();
/**
* Factory method for creating a new instance of {@link TongYiAudioTranscriptionMetadata}.
* @return a new instance of {@link TongYiAudioTranscriptionMetadata}
*/
static TongYiAudioTranscriptionMetadata create() {
return new TongYiAudioTranscriptionMetadata() {
};
}
}

View File

@ -0,0 +1,98 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.tongyi.metadata.audio;
import com.alibaba.dashscope.audio.asr.transcription.TranscriptionResult;
import com.google.gson.JsonObject;
import org.springframework.ai.chat.metadata.EmptyRateLimit;
import org.springframework.ai.chat.metadata.RateLimit;
import org.springframework.ai.model.ResponseMetadata;
import org.springframework.util.Assert;
import javax.annotation.Nullable;
import java.util.HashMap;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @since 2023.0.1.0
*/
public class TongYiAudioTranscriptionResponseMetadata extends HashMap<String, Object> implements ResponseMetadata {
@Nullable
private RateLimit rateLimit;
private JsonObject usage;
protected static final String AI_METADATA_STRING = "{ @type: %1$s, rateLimit: %4$s }";
/**
* NULL objects.
*/
public static final TongYiAudioTranscriptionResponseMetadata NULL = new TongYiAudioTranscriptionResponseMetadata() {
};
protected TongYiAudioTranscriptionResponseMetadata() {
this(null, new JsonObject());
}
protected TongYiAudioTranscriptionResponseMetadata(JsonObject usage) {
this(null, usage);
}
protected TongYiAudioTranscriptionResponseMetadata(@Nullable RateLimit rateLimit, JsonObject usage) {
this.rateLimit = rateLimit;
this.usage = usage;
}
public static TongYiAudioTranscriptionResponseMetadata from(TranscriptionResult result) {
Assert.notNull(result, "TongYi Transcription must not be null");
return new TongYiAudioTranscriptionResponseMetadata(result.getUsage());
}
@Nullable
public RateLimit getRateLimit() {
return this.rateLimit != null ? this.rateLimit : new EmptyRateLimit();
}
public void setRateLimit(@Nullable RateLimit rateLimit) {
this.rateLimit = rateLimit;
}
public JsonObject getUsage() {
return usage;
}
public void setUsage(JsonObject usage) {
this.usage = usage;
}
@Override
public String toString() {
return AI_METADATA_STRING.formatted(getClass().getName(), getRateLimit());
}
}

View File

@ -163,6 +163,11 @@ spring:
qianfan: # 文心一言
api-key: x0cuLZ7XsaTCU08vuJWO87Lg
secret-key: R9mYF9dl9KASgi5RUq0FQt3wRisSnOcK
cloud:
ai:
tongyi: # 通义千问
tongyi:
api-key: sk-Zsd81gZYg7
yudao.ai:
xinghuo: