当前位置: 首页 > news >正文

Spring AI 系列之三十 - Spring AI Alibaba-其它模型

之前做个几个大模型的应用,都是使用Python语言,后来有一个项目使用了Java,并使用了Spring AI框架。随着Spring AI不断地完善,最近它发布了1.0正式版,意味着它已经能很好的作为企业级生产环境的使用。对于Java开发者来说真是一个福音,其功能已经能满足基于大模型开发企业级应用。借着这次机会,给大家分享一下Spring AI框架。

注意由于框架不同版本改造会有些使用的不同,因此本次系列中使用基本框架是 Spring AI-1.0.0,JDK版本使用的是19,Spring-AI-Alibaba-1.0.0.3-SNAPSHOT
代码参考: https://github.com/forever1986/springai-study

目录

  • 1 embedding模型
    • 1.1 说明
    • 1.1 代码演示
  • 2 ImageModel
    • 2.1 说明
    • 2.2 代码演示
  • 3 语音合成模型
    • 3.1 说明
    • 3.2 代码演示
  • 4 其它模型

在上一章中通过Spring AI Alibaba框架,可以引入更多的聊天记忆存储类型。这一章将针对除了聊天模型之外的模型进行演示

代码参考lesson24子模块的ali-model子模块

前期准备:

1)在lesson24子模块下,新建ali-model子模块,其pom引入如下:

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba.cloud.ai</groupId><artifactId>spring-ai-alibaba-starter-dashscope</artifactId></dependency>
</dependencies>

2)新建application.properties配置文件

spring.ai.dashscope.api-key=你的阿里百炼API KEY

1 embedding模型

1.1 说明

向量化模型能够将文本、图像、视频等数据转换为数学空间中的向量。通过计算向量之间的距离或夹角,可以量化数据的相似度,从而作用于精准搜索、智能推荐、自动分类及异常检测等任务。之前在《系列之二十一 - EmbeddingModel》中讲过。这里通过Spring AI Alibaba框架,可以使用阿里云上的很多个embedding模型。具体可以参考《官方文档》,除了阿里本身的模型之外,还支持多模态的embedding模型:
在这里插入图片描述

1.1 代码演示

代码示例

import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.List;
import java.util.Map;@RestController
public class EmbeddingModelController {private final EmbeddingModel embeddingModel;@Autowiredpublic EmbeddingModelController(EmbeddingModel embeddingModel) {this.embeddingModel = embeddingModel;}@GetMapping("/ai/embedding")public Map embed(@RequestParam(value = "message", defaultValue = "测试embedding") String message) {EmbeddingResponse embeddingResponse = this.embeddingModel.embedForResponse(List.of(message));return Map.of("embedding", embeddingResponse);}
}

演示效果

http://localhost:8080/ai/embedding?message=测试embedding

在这里插入图片描述

2 ImageModel

2.1 说明

通义万相-文生图模型支持通过一句话生成图像,分为V2版和V1版。全面升级的文生图V2版模型提升了语义理解能力,通过预置智能改写功能帮助您快速上手图像创作。此外,V2版支持任意分辨率,输出图像最高可达200万像素。推荐优先使用文生图V2版模型。可以其《官方文档》

除了阿里的通义万相,也支持Stable Diffusion、FLUX等第三方模型

在这里插入图片描述

2.2 代码演示

代码示例

import com.alibaba.cloud.ai.dashscope.image.DashScopeImageModel;
import com.alibaba.cloud.ai.dashscope.image.DashScopeImageOptions;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ai.image.ImageMessage;
import org.springframework.ai.image.ImageOptions;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.List;@RestController
public class ImageModelController {private final DashScopeImageModel dashScopeImageModel;@Autowiredpublic ImageModelController(DashScopeImageModel dashScopeImageModel) {this.dashScopeImageModel = dashScopeImageModel;}@GetMapping("/ai/imagegenerate")public void generate(@RequestParam(value = "message", defaultValue = "生成一只老虎!") String message, HttpServletResponse response) throws IOException {ImageResponse image = this.dashScopeImageModel.call(new ImagePrompt(new ImageMessage(message), DashScopeImageOptions.builder().build()));// 返回的URLString url = image.getResult().getOutput().getUrl();// 将URL转为Stream输出到HttpServletResponseURL imageURL = URI.create(url).toURL();InputStream inputStream = imageURL.openStream();response.setHeader("Content-Type", MediaType.IMAGE_PNG_VALUE);response.getOutputStream().write(inputStream.readAllBytes());response.getOutputStream().flush();}}

演示效果

http://localhost:8080/ai/imagegenerate

在这里插入图片描述

3 语音合成模型

3.1 说明

Qwen-TTS 系列是通义千问系列的语音合成模型,支持输入中文、英文、中英混合的文本,并流式输出音频。除了Qwen系列,还支持,详情见《官方文档》

3.2 代码演示

代码示例

import com.alibaba.cloud.ai.dashscope.api.DashScopeSpeechSynthesisApi;
import com.alibaba.cloud.ai.dashscope.audio.DashScopeSpeechSynthesisModel;
import com.alibaba.cloud.ai.dashscope.audio.DashScopeSpeechSynthesisOptions;
import com.alibaba.cloud.ai.dashscope.audio.synthesis.SpeechSynthesisMessage;
import com.alibaba.cloud.ai.dashscope.audio.synthesis.SpeechSynthesisPrompt;
import com.alibaba.cloud.ai.dashscope.audio.synthesis.SpeechSynthesisResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;@RestController
public class AudioSpeechController {private final DashScopeSpeechSynthesisModel dashScopeSpeechSynthesisModel;@Autowiredpublic AudioSpeechController(DashScopeSpeechSynthesisModel dashScopeSpeechSynthesisModel) {this.dashScopeSpeechSynthesisModel = dashScopeSpeechSynthesisModel;}@GetMapping("/ai/audio")public void generate(@RequestParam(value = "message", defaultValue = "你怎么这样凭空污人清白……窃书不能算偷……窃书!……读书人的事,能算偷么?") String message) {DashScopeSpeechSynthesisOptions speechOptions = DashScopeSpeechSynthesisOptions.builder().model("cosyvoice-v2") // 使用扣子模型.voice("longtao_v2") // 使用粤语语音.responseFormat(DashScopeSpeechSynthesisApi.ResponseFormat.WAV) // WAV格式.speed(1.0f) // 正常语速.build();SpeechSynthesisPrompt speechPrompt = new SpeechSynthesisPrompt(new SpeechSynthesisMessage(message), speechOptions);SpeechSynthesisResponse speechResponse = dashScopeSpeechSynthesisModel.call(speechPrompt);File file = new File("lesson24/ali-model/ali-output.wav");try (FileOutputStream fileOutputStream = new FileOutputStream(file)){fileOutputStream.write(speechResponse.getResult().getOutput().getAudio().array());fileOutputStream.flush();}catch (IOException e){// do something}}
}

演示效果

http://localhost:8080/ai/audio

在这里插入图片描述

4 其它模型

在阿里的百炼平台上,还有很多其它模型,比如语音合成与识别、视频编辑与生成、特定行业模型等。这里就不一一列举了。之所以写这一章,是为了让用户能够知道使用Spring AI Alibaba框架,基本上和Spring AI用法一致,而且很容易就能集成百炼平台的各类模型。

结语:通过本章演示不同类型大模型的使用,相信用户现在可以知道Spring AI Alibaba的价值。下一章继续讲解Spring AI Alibaba基于企业级应用实践的一个扩展点:基于Nacos的MCP

Spring AI系列上一章:《Spring AI 系列之二十九 - Spring AI Alibaba-聊天记忆》

Spring AI系列下一章:《Spring AI 系列之三十一 - Spring AI Alibaba-基于Nacos的MCP》

http://www.dtcms.com/a/309165.html

相关文章:

  • CSS font-weight:500不生效
  • Git 命令使用指南:从入门到进阶
  • 动态规划(数位统计dp 状态压缩dp 树形dp 记忆化搜索) from y总
  • 【C语言】字符函数与字符串函数详解
  • http请求访问响应慢问题解决的基本思路
  • 基于python大数据的招聘数据可视化及推荐系统
  • natapp的报错Tunnel StatusReconnecting...
  • STM32芯片简述
  • 使用GPU和NPU视频生成的优劣对比
  • 人工智能与金融:金融服务的重塑
  • Linux9 root密码修改
  • armbian 启用nginx并设置访问密码
  • CTF实战:用Sqlmap破解表单输入型SQL注入题(输入账号密码/usernamepassword)
  • SpringBoot AI应用实战:从图像识别到预测分析
  • 【通用视觉框架】基于OpenCvSharp+WPF+YOLO开发的仿VisionMaster的通用视觉框架软件,全套源码,开箱即用
  • 机器人芯片:智能机器的“大脑”与未来趋势
  • Nature Machine Intelligence 面向机器人操作有效滑移控制的仿生轨迹模块
  • alaxea机器人由星海图人工智能科技有限公司研发的高性能仿人形机器人
  • 【LeetCode 热题 100】155. 最小栈
  • PL-0功能拓展及基于VSCode的IDE配置
  • kotlin语法和特性分析
  • PDFsam免费开源!PDF分割合并工具
  • 华为数通HCIP
  • 为什么我们需要提示词增强工程PEE(Prompt Enhancement Engineering )
  • axios请求的取消
  • ICML 2025 | 深度剖析时序 Transformer:为何有效,瓶颈何在?
  • Qt Quick 3D 基础与应用
  • 【数据结构初阶】--排序(一):直接插入排序,希尔排序
  • zabbix平台无法删除已停用主机的处理案例
  • 基于springboot的快递分拣管理系统