当前位置:   article > 正文

阿里通义千问API(Java)使用教程,基于Springboot后端_springboot接入通义千问api

springboot接入通义千问api

一、先申请key

1.1 登录阿里云官网并搜索模型服务灵积,开通key

1.2 点击上图中的控制台并进入

1.3 创建key

二、导入依赖坐标

2.1 导入以下俩个坐标

  1. <!--阿里巴巴大模型-->
  2. <!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java -->
  3. <dependency>
  4. <groupId>com.alibaba</groupId>
  5. <artifactId>dashscope-sdk-java</artifactId>
  6. <exclusions>
  7. <exclusion>
  8. <groupId>org.slf4j</groupId>
  9. <artifactId>slf4j-simple</artifactId>
  10. </exclusion>
  11. </exclusions>
  12. <version>2.8.3</version>
  13. </dependency>
  1. <dependency>
  2. <groupId>com.squareup.okhttp3</groupId>
  3. <artifactId>okhttp</artifactId>
  4. <version>4.10.0</version>
  5. </dependency>

2.2 编写Controller层 

这里controller层并未将前端的请求数据传入给api中的参数,有需求的可以自己加一下,这里就使用浏览器测试一下看是否返回数据即可

  1. import com.alibaba.dashscope.aigc.generation.GenerationResult;
  2. import com.alibaba.dashscope.exception.InputRequiredException;
  3. import com.alibaba.dashscope.exception.NoApiKeyException;
  4. import com.ruoyi.aliyunchat.service.ChatService;
  5. import com.ruoyi.common.annotation.Anonymous;
  6. import com.ruoyi.pojo.Result;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. @Slf4j
  12. @RestController
  13. @Anonymous
  14. public class ChatController {
  15. @Autowired
  16. private ChatService chatService;
  17. @RequestMapping("/chat")
  18. public Result chatAi() throws NoApiKeyException, InputRequiredException {
  19. GenerationResult generationResult = chatService.chatAi();
  20. return Result.success(generationResult);
  21. }
  22. }

 2.3 Servic层及实现类

接口

  1. import com.alibaba.dashscope.aigc.generation.GenerationResult;
  2. import com.alibaba.dashscope.exception.InputRequiredException;
  3. import com.alibaba.dashscope.exception.NoApiKeyException;
  4. public interface ChatService {
  5. GenerationResult chatAi() throws NoApiKeyException, InputRequiredException;
  6. }

实现类

  1. import com.alibaba.dashscope.aigc.generation.GenerationResult;
  2. import com.alibaba.dashscope.exception.InputRequiredException;
  3. import com.alibaba.dashscope.exception.NoApiKeyException;
  4. import com.ruoyi.aliyunchat.aliqianwen.QianWenChat;
  5. import com.ruoyi.aliyunchat.service.ChatService;
  6. import org.springframework.stereotype.Service;
  7. @Service
  8. public class ChatServiceImpl implements ChatService {
  9. @Override
  10. public GenerationResult chatAi() throws NoApiKeyException, InputRequiredException {
  11. return QianWenChat.callWithMessage();
  12. }
  13. }

接着就是官网上的Demo了,我们这里封装一下

  1. import com.alibaba.dashscope.aigc.generation.Generation;
  2. import com.alibaba.dashscope.aigc.generation.GenerationResult;
  3. import com.alibaba.dashscope.aigc.generation.models.QwenParam;
  4. import com.alibaba.dashscope.common.Message;
  5. import com.alibaba.dashscope.common.MessageManager;
  6. import com.alibaba.dashscope.common.Role;
  7. import com.alibaba.dashscope.exception.ApiException;
  8. import com.alibaba.dashscope.exception.InputRequiredException;
  9. import com.alibaba.dashscope.exception.NoApiKeyException;
  10. import com.alibaba.dashscope.utils.Constants;
  11. import lombok.extern.slf4j.Slf4j;
  12. @Slf4j
  13. public class QianWenChat {
  14. public static GenerationResult callWithMessage() throws NoApiKeyException, ApiException, InputRequiredException
  15. {
  16. Constants.apiKey="你的key";
  17. Generation gen = new Generation();
  18. MessageManager msgManager = new MessageManager(10);
  19. Message systemMsg =
  20. Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build();
  21. Message userMsg = Message.builder().role(Role.USER.getValue()).content("请问杭州阿里巴巴工作环境咋样?").build();
  22. msgManager.add(systemMsg);
  23. msgManager.add(userMsg);
  24. QwenParam param =
  25. QwenParam.builder().model(Generation.Models.QWEN_TURBO).messages(msgManager.get())
  26. .resultFormat(QwenParam.ResultFormat.MESSAGE)
  27. .topP(0.8)
  28. .enableSearch(true)
  29. .build();
  30. GenerationResult result = gen.call(param);
  31. log.info("千问api回答的话语为:{}",result);
  32. System.out.println(result);
  33. return result;
  34. }
  35. }

三、测试

3.1 在浏览器中输入后端本地,并请求接口,响应如下

 

 

 

 

 

 

 

 

 

 

 

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

闽ICP备14008679号