当前位置:   article > 正文

基于alibaba cloud ai+通义千问实现对话问答、文字转图片、文字转音频功能_tongyiaudiotranscriptionclient 哪个jar

tongyiaudiotranscriptionclient 哪个jar

一、引入依赖spring-cloud-starter-alibaba-ai:

  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>com.alibaba.cloud</groupId>
  5. <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  6. <version>2023.0.1.0</version>
  7. <type>pom</type>
  8. <scope>import</scope>
  9. </dependency>
  10. </dependencies>
  11. </dependencyManagement>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-test</artifactId>
  20. <scope>test</scope>
  21. </dependency>
  22. <dependency>
  23. <groupId>com.alibaba.cloud</groupId>
  24. <artifactId>spring-cloud-starter-alibaba-ai</artifactId>
  25. </dependency>
  26. </dependencies>

这里遇到个坑,我用阿里云的Maven仓库拉不到里面关联的spring-ai-core依赖,可以使用其他Maven仓库试试,或者直接下载我上传的jar包。

二、配置文件:

  1. spring:
  2. application:
  3. name: spring-ai-demo
  4. cloud:
  5. ai:
  6. tongyi:
  7. api-key: ${your-api-key}

api-key申请方式:如何开通DashScope并创建API-KEY_模型服务灵积(DashScope)-阿里云帮助中心

三、Controller层代码:

  1. import com.example.springaidemo.pojo.Msg;
  2. import com.example.springaidemo.service.impl.TongYiSimpleServiceImpl;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import java.io.IOException;
  8. @RestController
  9. public class TongYiDemoController {
  10. @Autowired
  11. private TongYiSimpleServiceImpl tongYiSimpleService;
  12. /**
  13. * 对话问答
  14. * @param message
  15. * @return
  16. */
  17. @GetMapping("/completion")
  18. public Msg completion(
  19. @RequestParam(value = "message", defaultValue = "Tell me a joke")
  20. String message
  21. ) {
  22. return Msg.success().setResp(tongYiSimpleService.completion(message));
  23. }
  24. /**
  25. * 图像生成
  26. * @param message
  27. * @return
  28. */
  29. @GetMapping("/image")
  30. public Msg image(
  31. @RequestParam(value = "message", defaultValue = "")
  32. String message
  33. ) {
  34. return Msg.success().setResp(tongYiSimpleService.image(message));
  35. }
  36. /**
  37. * 文本转音频
  38. * @param message
  39. * @return
  40. */
  41. @GetMapping("/speech")
  42. public Msg speech(
  43. @RequestParam(value = "message", defaultValue = "")
  44. String message
  45. ) {
  46. try {
  47. tongYiSimpleService.speech(message);
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. }
  51. return Msg.success();
  52. }
  53. }

四、Service层代码:

  1. import com.alibaba.cloud.ai.tongyi.audio.TongYiAudioSpeechClient;
  2. import com.alibaba.cloud.ai.tongyi.audio.api.SpeechPrompt;
  3. import com.alibaba.cloud.ai.tongyi.chat.TongYiChatClient;
  4. import com.alibaba.cloud.ai.tongyi.image.TongYiImagesClient;
  5. import jakarta.servlet.ServletOutputStream;
  6. import jakarta.servlet.http.HttpServletResponse;
  7. import lombok.RequiredArgsConstructor;
  8. import org.springframework.ai.chat.messages.UserMessage;
  9. import org.springframework.ai.chat.prompt.Prompt;
  10. import org.springframework.ai.image.Image;
  11. import org.springframework.ai.image.ImageMessage;
  12. import org.springframework.ai.image.ImagePrompt;
  13. import org.springframework.stereotype.Service;
  14. import java.io.IOException;
  15. import java.nio.ByteBuffer;
  16. import java.util.Collections;
  17. @Service
  18. @RequiredArgsConstructor
  19. public class TongYiSimpleServiceImpl{
  20. /**
  21. * 对话问答Client
  22. */
  23. private final TongYiChatClient chatClient;
  24. /**
  25. * 文本生成图片Client
  26. */
  27. private final TongYiImagesClient imagesClient;
  28. /**
  29. * 文本转音频Client
  30. */
  31. private final TongYiAudioSpeechClient speechClient;
  32. private final HttpServletResponse response;
  33. /**
  34. * 对话问答
  35. * @param message
  36. * @return
  37. */
  38. public String completion(String message) {
  39. // 创建Prompt
  40. Prompt prompt = new Prompt(new UserMessage(message));
  41. // 调用对话问答Client
  42. return chatClient.call(prompt).getResult().getOutput().getContent();
  43. }
  44. /**
  45. * 文本生成图片
  46. * @param message
  47. * @return
  48. */
  49. public String image(String message) {
  50. // 封装信息
  51. ImageMessage imageMessage = new ImageMessage(message);
  52. // 创建Prompt
  53. ImagePrompt imagePrompt = new ImagePrompt(Collections.singletonList(imageMessage));
  54. // 调用文本生成图片Client
  55. Image output = imagesClient.call(imagePrompt).getResult().getOutput();
  56. // 生成图片URL
  57. return output.getUrl();
  58. }
  59. /**
  60. * 文本转音频
  61. * @param message
  62. * @return
  63. */
  64. public void speech(String message) throws IOException {
  65. // 封装信息
  66. SpeechPrompt speechPrompt = new SpeechPrompt(message);
  67. // 调用文本转音频Client
  68. ByteBuffer output = speechClient.call(speechPrompt).getResult().getOutput();
  69. // 设置响应头以触发文件下载
  70. response.setContentType("audio/wav"); // 根据音频实际类型调整,例如 "audio/wav", "audio/mp4" 等
  71. String fileName = "speech_output.wav"; // 自定义下载文件名及扩展名
  72. response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
  73. ServletOutputStream outputStream = response.getOutputStream();
  74. outputStream.write(output.array());
  75. outputStream.flush();
  76. outputStream.close(); // 关闭输出流,释放资源
  77. }
  78. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/761170
推荐阅读
相关标签
  

闽ICP备14008679号