赞
踩
一、引入依赖spring-cloud-starter-alibaba-ai:
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-alibaba-dependencies</artifactId>
- <version>2023.0.1.0</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
-
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-ai</artifactId>
- </dependency>
- </dependencies>

这里遇到个坑,我用阿里云的Maven仓库拉不到里面关联的spring-ai-core依赖,可以使用其他Maven仓库试试,或者直接下载我上传的jar包。
二、配置文件:
- spring:
- application:
- name: spring-ai-demo
- cloud:
- ai:
- tongyi:
- api-key: ${your-api-key}
api-key申请方式:如何开通DashScope并创建API-KEY_模型服务灵积(DashScope)-阿里云帮助中心
三、Controller层代码:
- import com.example.springaidemo.pojo.Msg;
- import com.example.springaidemo.service.impl.TongYiSimpleServiceImpl;
- 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.IOException;
-
- @RestController
- public class TongYiDemoController {
- @Autowired
- private TongYiSimpleServiceImpl tongYiSimpleService;
-
- /**
- * 对话问答
- * @param message
- * @return
- */
- @GetMapping("/completion")
- public Msg completion(
- @RequestParam(value = "message", defaultValue = "Tell me a joke")
- String message
- ) {
-
- return Msg.success().setResp(tongYiSimpleService.completion(message));
- }
-
- /**
- * 图像生成
- * @param message
- * @return
- */
- @GetMapping("/image")
- public Msg image(
- @RequestParam(value = "message", defaultValue = "")
- String message
- ) {
-
- return Msg.success().setResp(tongYiSimpleService.image(message));
- }
-
- /**
- * 文本转音频
- * @param message
- * @return
- */
- @GetMapping("/speech")
- public Msg speech(
- @RequestParam(value = "message", defaultValue = "")
- String message
- ) {
- try {
- tongYiSimpleService.speech(message);
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- return Msg.success();
- }
-
- }

四、Service层代码:
- import com.alibaba.cloud.ai.tongyi.audio.TongYiAudioSpeechClient;
- import com.alibaba.cloud.ai.tongyi.audio.api.SpeechPrompt;
- import com.alibaba.cloud.ai.tongyi.chat.TongYiChatClient;
- import com.alibaba.cloud.ai.tongyi.image.TongYiImagesClient;
- import jakarta.servlet.ServletOutputStream;
- import jakarta.servlet.http.HttpServletResponse;
- import lombok.RequiredArgsConstructor;
- import org.springframework.ai.chat.messages.UserMessage;
- import org.springframework.ai.chat.prompt.Prompt;
- import org.springframework.ai.image.Image;
- import org.springframework.ai.image.ImageMessage;
- import org.springframework.ai.image.ImagePrompt;
- import org.springframework.stereotype.Service;
-
- import java.io.IOException;
- import java.nio.ByteBuffer;
- import java.util.Collections;
-
- @Service
- @RequiredArgsConstructor
- public class TongYiSimpleServiceImpl{
-
- /**
- * 对话问答Client
- */
- private final TongYiChatClient chatClient;
-
- /**
- * 文本生成图片Client
- */
- private final TongYiImagesClient imagesClient;
-
- /**
- * 文本转音频Client
- */
- private final TongYiAudioSpeechClient speechClient;
-
- private final HttpServletResponse response;
-
- /**
- * 对话问答
- * @param message
- * @return
- */
- public String completion(String message) {
- // 创建Prompt
- Prompt prompt = new Prompt(new UserMessage(message));
- // 调用对话问答Client
- return chatClient.call(prompt).getResult().getOutput().getContent();
- }
-
- /**
- * 文本生成图片
- * @param message
- * @return
- */
- public String image(String message) {
- // 封装信息
- ImageMessage imageMessage = new ImageMessage(message);
- // 创建Prompt
- ImagePrompt imagePrompt = new ImagePrompt(Collections.singletonList(imageMessage));
- // 调用文本生成图片Client
- Image output = imagesClient.call(imagePrompt).getResult().getOutput();
- // 生成图片URL
- return output.getUrl();
- }
-
- /**
- * 文本转音频
- * @param message
- * @return
- */
- public void speech(String message) throws IOException {
- // 封装信息
- SpeechPrompt speechPrompt = new SpeechPrompt(message);
- // 调用文本转音频Client
- ByteBuffer output = speechClient.call(speechPrompt).getResult().getOutput();
-
- // 设置响应头以触发文件下载
- response.setContentType("audio/wav"); // 根据音频实际类型调整,例如 "audio/wav", "audio/mp4" 等
- String fileName = "speech_output.wav"; // 自定义下载文件名及扩展名
- response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
-
- ServletOutputStream outputStream = response.getOutputStream();
-
- outputStream.write(output.array());
- outputStream.flush();
- outputStream.close(); // 关闭输出流,释放资源
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。