阿里通义千问API(Java)使用教程,基于Springboot后端
创始人
2024-12-18 05:33:54
0

目录

一、先申请key

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

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

​编辑

1.3 创建key

二、导入依赖坐标

2.1 导入以下俩个坐标

2.2 编写Controller层 

 2.3 Servic层及实现类

三、测试

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


一、先申请key

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

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

1.3 创建key

二、导入依赖坐标

2.1 导入以下俩个坐标

                                com.alibaba             dashscope-sdk-java                                                   org.slf4j                     slf4j-simple                                           2.8.3         
              com.squareup.okhttp3             okhttp             4.10.0         

2.2 编写Controller层 

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

​ import com.alibaba.dashscope.aigc.generation.GenerationResult; import com.alibaba.dashscope.exception.InputRequiredException; import com.alibaba.dashscope.exception.NoApiKeyException; import com.ruoyi.aliyunchat.service.ChatService; import com.ruoyi.common.annotation.Anonymous; import com.ruoyi.pojo.Result; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;  @Slf4j @RestController @Anonymous public class ChatController {      @Autowired     private ChatService chatService;      @RequestMapping("/chat")     public Result chatAi() throws NoApiKeyException, InputRequiredException {         GenerationResult generationResult = chatService.chatAi();                  return Result.success(generationResult);     } }  ​

 2.3 Servic层及实现类

接口

import com.alibaba.dashscope.aigc.generation.GenerationResult; import com.alibaba.dashscope.exception.InputRequiredException; import com.alibaba.dashscope.exception.NoApiKeyException;  public interface ChatService {     GenerationResult chatAi() throws NoApiKeyException, InputRequiredException; }

实现类

import com.alibaba.dashscope.aigc.generation.GenerationResult; import com.alibaba.dashscope.exception.InputRequiredException; import com.alibaba.dashscope.exception.NoApiKeyException; import com.ruoyi.aliyunchat.aliqianwen.QianWenChat; import com.ruoyi.aliyunchat.service.ChatService; import org.springframework.stereotype.Service;  @Service public class ChatServiceImpl implements ChatService {     @Override     public GenerationResult chatAi() throws NoApiKeyException, InputRequiredException {         return  QianWenChat.callWithMessage();     } }

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

import com.alibaba.dashscope.aigc.generation.Generation; import com.alibaba.dashscope.aigc.generation.GenerationResult; import com.alibaba.dashscope.aigc.generation.models.QwenParam; import com.alibaba.dashscope.common.Message; import com.alibaba.dashscope.common.MessageManager; import com.alibaba.dashscope.common.Role; import com.alibaba.dashscope.exception.ApiException; import com.alibaba.dashscope.exception.InputRequiredException; import com.alibaba.dashscope.exception.NoApiKeyException; import com.alibaba.dashscope.utils.Constants; import lombok.extern.slf4j.Slf4j;  @Slf4j public class QianWenChat {     public static GenerationResult callWithMessage() throws NoApiKeyException, ApiException, InputRequiredException     {         Constants.apiKey="你的key";         Generation gen = new Generation();         MessageManager msgManager = new MessageManager(10);         Message systemMsg =                 Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build();         Message userMsg = Message.builder().role(Role.USER.getValue()).content("请问杭州阿里巴巴工作环境咋样?").build();         msgManager.add(systemMsg);         msgManager.add(userMsg);         QwenParam param =                 QwenParam.builder().model(Generation.Models.QWEN_TURBO).messages(msgManager.get())                         .resultFormat(QwenParam.ResultFormat.MESSAGE)                         .topP(0.8)                         .enableSearch(true)                         .build();         GenerationResult result = gen.call(param);         log.info("千问api回答的话语为:{}",result);         System.out.println(result);         return result;     } }

三、测试

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

相关内容

热门资讯

辅助透视!aapoker破解侠... 辅助透视!aapoker破解侠是真的吗,aapoker透视插件,2025新版(有挂解说)1、辅助透视...
透视黑科技!wepoker底牌... 透视黑科技!wepoker底牌透视(透视)一直是有挂(系统教程);1、wepoker底牌透视ai辅助...
透视肯定!aapoker插件,... 透视肯定!aapoker插件,aapoker透视脚本入口,微扑克教程(有挂脚本);1、进入到aapo...
透视辅助!aapoker公共底... 透视辅助!aapoker公共底牌,aapoker透视脚本入口,2025新版教程(有挂教程)1、打开软...
透视游戏!wepoker永久免... 透视游戏!wepoker永久免费脚本(透视)原来存在有挂(必赢教程)1、构建自己的wepoker永久...
广东雀神智能辅助照片,兴化打两... 广东雀神智能辅助照片,兴化打两圈辅助器,微信小程序边锋辅助攻略1、完成广东雀神智能辅助照片的残局,帮...
透视ai代打!aapoker辅... 透视ai代打!aapoker辅助器是真的吗,aapoker插件下载,插件教程(有挂方法)1、让任何用...
透视能赢!wepoker作弊方... 透视能赢!wepoker作弊方法(透视)确实是有挂(总结教程)1、超多福利:超高返利,海量正版游戏,...
广东雀神挂件去那买,广西友乐辅... 广东雀神挂件去那买,广西友乐辅助app,财神13张辅助系统解密1、在广东雀神挂件去那买ai机器人技巧...
透视透视!aapoker发牌逻... 透视透视!aapoker发牌逻辑,aapoker万能辅助器,微扑克教程(有挂解密)1、aapoker...