介绍:对接第三方平台,减少开发工作量
此次演示以若依plus,JDK17,SpringBoot版本3.4.1
阿里云百炼官方:
获取个人key:
步骤一:前置条件
[1]pom.xml
<dashscope.sdk.version>2.15.1</dashscope.sdk.version>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dashscope-sdk-java</artifactId>
<version>${dashscope.sdk.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
[2]application.yml
spring:
application:
name: ${ruoyi.name}
ai:
dashscope:
api-key: 个人key
chat:
options:
model: qwen-max
audio:
dashscope:
model: qwen-audio-turbo
embed:
dashscope:
model: text-embedding-v4
image:
dashscope:
model: qwen-vl-max
步骤二:功能开发
功能1:音频理解
Controller层
@PostMapping(value = "/recoginzeAudio", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> recoginzeAudio(@RequestBody AudioRequestBo bo) {
return audioService.recoginzeAudio(bo);
}
Service层
@Override
public Flux<String> recoginzeAudio(AudioRequestBo bo) {
return Flux.create(sink -> {
MultiModalConversation multiModalConversation = new MultiModalConversation();
MultiModalMessage userMessage = MultiModalMessage.builder()
.role(Role.USER.getValue())
.content(Arrays.asList(
new HashMap<String, Object>() {{
put("audio", bo.getAudioUrl());
}},
new HashMap<String, Object>() {{
put("text", bo.getPromptText());
}}
)).build();
MultiModalConversationParam param = MultiModalConversationParam.builder()
.apiKey(modelPropertyConfig.getCommonApiKey())
.model(modelPropertyConfig.getAudioModelName())
.message(userMessage)
.incrementalOutput(true)
.build();
try {
Flowable<MultiModalConversationResult> result = multiModalConversation.streamCall(param);
result.blockingForEach(item -> {
log.info("item: {}", JSONUtil.toJsonStr(item));
// 提取文本内容
if (item.getOutput() != null && item.getOutput().getChoices() != null) {
item.getOutput().getChoices().forEach(choice -> {
if (choice.getMessage() != null && choice.getMessage().getContent() != null) {
choice.getMessage().getContent().forEach(content -> {
Object textObj = content.get("text");
if (textObj != null) {
sink.next(textObj.toString());
}
});
}
});
}
});
sink.complete();
} catch (Exception e) {
log.error("音频转换失败: {}", e.getMessage());
sink.error(e);
}
});
}
接口测试
功能2:文本转向量
Controller层
@GetMapping("/embedContent")
public R<Void> embedContent(@RequestBody EmbedRequestBo embedRequestBo) throws Exception {
return toAjax(embedService.embedContent(embedRequestBo));
}
Service层
@Override
public int embedContent(EmbedRequestBo embedRequestBo) throws Exception {
TextEmbeddingParam param = TextEmbeddingParam
.builder()
.apiKey(modelPropertyConfig.getCommonApiKey())
.model(modelPropertyConfig.getEmbedModelName())
.texts(Arrays.asList(
embedRequestBo.getContent()
))
.parameter("dimension", 1024)
.build();
TextEmbedding textEmbedding = new TextEmbedding();
TextEmbeddingResult result = textEmbedding.call(param);
log.info("result: {}", JSONUtil.toJsonStr(result));
return 1;
}
接口测试
日志记录
功能3:图片理解
Controller层
@PostMapping("/recoginzeImage")
public R<Void> recoginzeImage(@RequestBody ImageRequestBo imageRequestBo) throws Exception {
return toAjax(imageService.recoginzeImage(imageRequestBo));
}
Service层
@Override
public int recoginzeImage(ImageRequestBo imageRequestBo) throws Exception {
MultiModalConversation conversation = new MultiModalConversation();
MultiModalMessage userMessage = MultiModalMessage.builder()
.role(Role.USER.getValue())
.content(Arrays.asList(
Collections.singletonMap("image", imageRequestBo.getImageUrl()),
Collections.singletonMap("text", imageRequestBo.getPromptText())
))
.build();
MultiModalConversationParam param = MultiModalConversationParam.builder()
.apiKey(modelPropertyConfig.getCommonApiKey())
.model(modelPropertyConfig.getImageModelName())
.message(userMessage)
.build();
MultiModalConversationResult result = conversation.call(param);
log.info("result: {}", JSONUtil.toJsonStr(result));
return 1;
}
接口测试
日志记录
此次对接阿里云百炼平台实现对音频、图片、文字处理,大家可以利用其他模型完成需求,并结合多模态技术应对挑战!
本人正在打造技术交流群,欢迎志同道合的朋友一起探讨,一起努力,通过自己的努力,在技术岗位这条道路上走得更远。QQ群号:925317809 备注:技术交流 即可通过!