com.alibaba dashscope-sdk-javaorg.slf4j slf4j-simple2.8.3
com.squareup.okhttp3 okhttp4.10.0
这里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); } }
接口
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; } }