当前位置:   article > 正文

SpringBoot实现简单AI问答(百度千帆)

SpringBoot实现简单AI问答(百度千帆)

第一步:注册并登录百度智能云,创建应用并获取自己的APIKey与SecretKey,参考网址:

点击去百度智能云
在这里插入图片描述

第二步:引入千帆的pom依赖

		<dependency>
            <groupId>com.baidubce</groupId>
            <artifactId>qianfan</artifactId>
            <version>0.0.9</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

第三步:创建前端需要的controller

import com.baidubce.qianfan.core.auth.Auth;
import com.lx.vue.common.resp.ResultData;
import com.lx.vue.common.resp.ReturnCodeEnum;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.core.builder.ChatBuilder;
import com.baidubce.qianfan.model.chat.ChatResponse;

@RestController
public class QianFanController {

    private static final String APIKey = "你的APIKey";
    private static final String SecretKey = "你的SecretKey";

    private static Qianfan qianfan = new Qianfan(Auth.TYPE_OAUTH,APIKey, SecretKey);


    @PostMapping("/ai/sendMsg")
    public ResultData sendMsg(@RequestBody String problem) {
        String result = null;
        try {
            result = chat(problem);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData(ReturnCodeEnum.RC500.getCode(),"服务暂不可用",null);
        }
        return new ResultData(ReturnCodeEnum.RC200.getCode(),ReturnCodeEnum.RC200.getMessage(),result);
    }

    private static String chat(String problem) {
        ChatBuilder bulder = qianfan.chatCompletion()
                .model("ERNIE-Speed-8K");//你要使用的大模型款式,最好和我一样,其他的很有可能是收费的
            bulder.addMessage("user",problem);//你的问题  
        ChatResponse response = bulder.execute();
        return response.getResult();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

第四步:前端进行调用,并动态将自己的问题与AI的回答填入Vue页面

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/809006?site
推荐阅读
相关标签
  

闽ICP备14008679号