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

avatar
作者
筋斗云
阅读量: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 导入以下俩个坐标

 <!--阿里巴巴大模型-->         <!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java -->         <dependency>             <groupId>com.alibaba</groupId>             <artifactId>dashscope-sdk-java</artifactId>             <exclusions>                 <exclusion>                     <groupId>org.slf4j</groupId>                     <artifactId>slf4j-simple</artifactId>                 </exclusion>             </exclusions>             <version>2.8.3</version>         </dependency>
 <dependency>             <groupId>com.squareup.okhttp3</groupId>             <artifactId>okhttp</artifactId>             <version>4.10.0</version>         </dependency>

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 在浏览器中输入后端本地,并请求接口,响应如下

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!