SpringBoot实现简单AI问答(百度千帆)
创始人
2025-01-15 05:37:26
0

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

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

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

		             com.baidubce             qianfan             0.0.9          

第三步:创建前端需要的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();     } } 

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

在这里插入图片描述

相关内容

热门资讯

突发!wepoker买钻石有用... 突发!wepoker买钻石有用吗,德扑之心免费透视,好像真的有挂(有挂教学)1)德扑之心免费透视免费...
相较于以往!wepoker私人... 相较于以往!wepoker私人局外卦,德普之星透视辅助,本来是真的有挂(有挂功能);1)德普之星透视...
长期以来!购买wepoker模... 长期以来!购买wepoker模拟器,德扑之心免费透视,一贯真的是有挂(竟然有挂)1、完成德扑之心免费...
这一问题亟待解决!wepoke... 这一问题亟待解决!wepoker轻量版有透视吗,如何下载德普之星辅助软件,本来确实有挂(有挂方针)1...
有消息称!wpk私人辅助,如何... 有消息称!wpk私人辅助,如何下载德普之星辅助软件,确实是真的有挂(有挂细节)1、如何下载德普之星辅...
技巧辅助挂!wepoker有辅... 技巧辅助挂!wepoker有辅助功能吗,德扑之星透视,切实真的有挂(有挂技巧)1、德扑之星透视辅助器...
为了进一步!hhpoker德州... 为了进一步!hhpoker德州机器人,德普之星辅助器,真是确实有挂(有挂技术)1、实时德普之星辅助器...
截至发稿!wpk真的有透视嘛,... 截至发稿!wpk真的有透视嘛,德普之星怎么开辅助,其实是有挂(有挂解惑)1、用户打开应用后不用登录就...
技巧辅助挂!xpoker辅助神... 技巧辅助挂!xpoker辅助神器,德扑软件,一贯存在有挂(真的有挂)1、进入到德扑软件是否有挂之后,...
受玩家影响!wepoker私人... 受玩家影响!wepoker私人辅助器,智星菠萝可以辅助吗,原来是真的有挂(有挂工具)小薇(辅助器软件...