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有辅助器吗,(WePoKer)总是存在有挂(透视)安装教程(有挂黑科技);1...
透视app!sohoo竞技联盟... 透视app!sohoo竞技联盟辅助器,佛手在线是不是有挂,扑克教程(有挂细节)1、佛手在线是不是有挂...
透视数据!德扑圈透视挂, (德... 透视数据!德扑圈透视挂, (德扑之心)竟然真的有挂(透视)免费透视(有挂规律)1、每一步都需要思考,...
透视脚本!wepoker私人局... 透视脚本!wepoker私人局有透视吗,(wepoker)果然是真的有挂(透视)私人局俱乐部辅助(有...
透视规律!werplan透视挂... 透视规律!werplan透视挂,德州局怎么透视,必赢方法(有挂方法)1、透视规律!werplan透视...
透视玄学!德普之星透视辅助插件... 透视玄学!德普之星透视辅助插件, (德普之星)总是有挂(透视)透视免费(有挂方法)进入游戏-大厅左侧...
透视国外版!wepoker辅助... 透视国外版!wepoker辅助透视,(WEPOKER)好像有挂(透视)外挂(有挂解说)1、任何wep...
透视插件!福建大菠萝万能辅助器... 透视插件!福建大菠萝万能辅助器,约局吧游戏挂,解密教程(有挂方法);1、福建大菠萝万能辅助器系统规律...
透视游戏!德普之星怎么开辅助,... 透视游戏!德普之星怎么开辅助, (德普)都是是真的有挂(透视)辅助软件(有挂细节)1、在德普之星怎么...
透视底牌!wepoker辅助是... 透视底牌!wepoker辅助是真的吗,(WePoKer)其实是有挂(透视)透视脚本(有挂揭秘)在进入...