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页面

在这里插入图片描述

相关内容

热门资讯

黑科技模拟器“wepoke智能... 黑科技模拟器“wepoke智能ai”wepoke真的有挂(智能ai)果然是有挂(有挂头条)-哔哩哔哩...
黑科技黑科技“wepoke有没... 黑科技黑科技“wepoke有没有挂”wepoke辅助器(黑科技ai)总是是真的有挂(有挂教学)-哔哩...
黑科技总结“wepoke黑科技... 黑科技总结“wepoke黑科技”wepoke是不是有挂(智能ai)本来是有挂(有挂总结)-哔哩哔哩1...
黑科技计算“wepoke智能a... 黑科技计算“wepoke智能ai”wepoke透视挂怎么测试(ai辅助)原来真的有挂(有挂教学)-哔...
黑科技脚本“wepoke有没有... 黑科技脚本“wepoke有没有挂”wepoke有没有挂(智能ai)原来真的有挂(有挂猫腻)-哔哩哔哩...
黑科技黑科技“wepoke智能... 黑科技黑科技“wepoke智能ai”wepoke有没有辅助挂(黑科技ai)总是存在有挂(发现有挂)-...
黑科技黑科技“wepoke智能... 黑科技黑科技“wepoke智能ai”wepoke透明挂要多少钱(软件透明挂)果然是有挂(真的有挂)-...
黑科技神器“wepoke透明黑... 黑科技神器“wepoke透明黑科技”wepoke透明挂软件(透明挂)果然是有挂(真的有挂)-哔哩哔哩...
黑科技科技“wepoke有没有... 黑科技科技“wepoke有没有挂”wepoke软件透明演示(透视)总是有挂(有挂规律)-哔哩哔哩1、...
黑科技辅助“wepoke智能a... 黑科技辅助“wepoke智能ai”wepoke辅助插件(透明挂黑科技)一直是真的有挂(发现有挂)-哔...