阿里通义千问API(Java)使用教程,基于Springboot后端
创始人
2024-12-18 05:05:45
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 在浏览器中输入后端本地,并请求接口,响应如下

相关内容

热门资讯

分享!微信群炸金花房间怎么开/... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享...
推荐一款!微信金花房卡怎么弄天... 推荐一款!微信金花房卡怎么弄天道联盟/房卡购买房间怎么开天道联盟是一款非常受欢迎的游戏,咨询房/卡添...
玩家攻略,牛牛房卡制作链接复仇... 微信游戏中心:复仇者联盟房卡在哪里买打开微信,添加客服微信【88355042】,进入游戏中心或相关小...
玩家攻略,牛牛房卡怎么获得精灵... 精灵大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:【3329006910】或QQ:332900...
一分钟推荐“金花客服代理房卡获... 先锋大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来...
科普!金花房卡链接跟谁买,微信... 微信游戏中心:拼三张房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信拼三张房...
科普!微信里炸金花房卡卖家联系... 微信游戏中心:炸金花房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信炸金花房...
金花房卡购买正规渠道/创建金花... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来享...
分享!微信怎样开炸金房间卡/新... 牌九是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享受...
科普!微信群开金花房卡在哪购买... 微信游戏中心:牛牛房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信牛牛房卡”,...
安卓系统怎么截视频,安卓系统视... 你是不是也和我一样,在使用安卓手机的时候,总想记录下那些精彩瞬间呢?比如,孩子第一次学会走路,还是朋...
金花房卡找谁买划算/微信炸金花... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡...
正版授权!牛牛房卡怎么购买新详... 微信游戏中心:新详心/天王大厅房卡在哪里买打开微信,添加客服微信【88355042】,进入游戏中心或...
科普!微信玩金花游戏房卡在哪弄... 微信游戏中心:拼三张房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信拼三张房...
头条推荐!微信金花房卡怎么弄玄... 今 日消息,玄灵大厅房卡添加微信33549083 苹果今日发布了 iOS 16.1 正式版更新,简单...
玩家攻略,牛牛房卡游戏代理新蜜... 新蜜瓜大厅/随意玩房卡更多详情添加微:33549083、 2、在商城页面中选择房卡选项。 ...
分享!微信玩炸金花房卡链接/牛... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享...
科普!炸金花怎么买房卡,创建拼... 微信游戏中心:拼三张房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信拼三张房...
ia实测“哪里有卖微信炸金花房... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡...
科技实测!金花房间怎么创建新全... 科技实测!金花房间怎么创建新全游/飞鹰互娱/房卡是在哪里买的新全游/飞鹰互娱是一款非常受欢迎的游戏,...