赞
踩
- <!--阿里巴巴大模型-->
- <!-- 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>
这里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;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。