相关推荐recommended
阿里通义千问API(Java)使用教程,基于Springboot后端
作者:mmseoamin日期:2024-04-01

一、先申请key

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

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

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

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

1.3 创建key

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

二、导入依赖坐标

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

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