• laravel对接百度智能云 实现智能机器人


    创建API Key和 Secret Key进入网址:百度智能云千帆大模型平台

    如下图操作:

    填写完毕点击确认后,即可得到sk和ak

     后端接口实现代码:

    1. //调用百度智能云第三方机器人接口
    2. public function run($text) {
    3. $curl = curl_init();
    4. curl_setopt_array($curl, array(
    5. CURLOPT_URL => "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-4.0-8k-0104?access_token={$this->getAccessToken()}",
    6. CURLOPT_TIMEOUT => 30,
    7. CURLOPT_RETURNTRANSFER => true,
    8. CURLOPT_SSL_VERIFYPEER => false,
    9. CURLOPT_SSL_VERIFYHOST => false,
    10. CURLOPT_CUSTOMREQUEST => 'POST',
    11. CURLOPT_POSTFIELDS =>'{
    12. "messages":
    13. [
    14. {
    15. "role":"user",
    16. "content":'.'"'.$text.'"'.'
    17. }
    18. ],
    19. "temperature":0.8,
    20. "top_p":0.8,
    21. "penalty_score":1,
    22. "disable_search":false,
    23. "enable_citation":false,
    24. "enable_trace":false
    25. }',
    26. CURLOPT_HTTPHEADER => array(
    27. 'Content-Type: application/json'
    28. ),
    29. ));
    30. $response = curl_exec($curl);
    31. curl_close($curl);
    32. return $response;
    33. }
    34. /**
    35. * 使用 AK,SK 生成鉴权签名(Access Token)
    36. * @return string 鉴权签名信息(Access Token)
    37. */
    38. private function getAccessToken(){
    39. $config = config('services.lev');
    40. $curl = curl_init();
    41. $postData = array(
    42. 'grant_type' => 'client_credentials',
    43. 'client_id' => '你的ak',
    44. 'client_secret' => '你的sk'
    45. );
    46. curl_setopt_array($curl, array(
    47. CURLOPT_URL => 'https://aip.baidubce.com/oauth/2.0/token',
    48. CURLOPT_CUSTOMREQUEST => 'POST',
    49. CURLOPT_SSL_VERIFYPEER => false,
    50. CURLOPT_SSL_VERIFYHOST => false,
    51. CURLOPT_RETURNTRANSFER => true,
    52. CURLOPT_POSTFIELDS => http_build_query($postData)
    53. ));
    54. $response = curl_exec($curl);
    55. curl_close($curl);
    56. $rtn = json_decode($response);
    57. return $rtn->access_token;
    58. }
    59. public function info_request()
    60. {
    61. $text = request()->get('content');
    62. $rtn = (new LevitatefSphereController())->run($text);
    63. return $rtn;
    64. }
    65. public function levitated()
    66. {
    67. return view('levitated_view');
    68. }

     页面HTML代码:

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Simple Chat Box</title>
    6. <link rel="stylesheet" href="{{ asset('static/levitated-css.css') }}" />
    7. <style>
    8. body {
    9. overflow-y: hidden; /* 隐藏垂直滚动条 */
    10. }
    11. #box {
    12. width: 1000px;
    13. height: 100%;
    14. margin: 0px auto;
    15. /*position: relative;*/
    16. }
    17. #chat-box {
    18. width: 900px;
    19. height: 670px;
    20. overflow: auto;
    21. border: 1px solid #ccc;
    22. border-radius: 10px 10px 0px 0px;
    23. padding: 8px;
    24. background-color: #ffffff;
    25. }
    26. #message-input{
    27. width: 916px;
    28. height: 110px;
    29. border: 1px solid #ccc;
    30. /*pointer-events:none;*/
    31. border-top:none;
    32. /*position: absolute;*/
    33. border-radius: 0px 0px 10px 10px;
    34. background-color: #ffffff;
    35. }
    36. #btn{
    37. /*width: 80px;*/
    38. border-radius: 10px;
    39. border: none;
    40. background-color: #8a57ea;
    41. color: #ffffff;
    42. margin-left: 820px;
    43. /*position: absolute;*/
    44. position: relative;
    45. bottom: 45px;
    46. padding: 10px 20px;
    47. }
    48. .input-div{
    49. width: max-content;
    50. padding: 10px 20px;
    51. border-radius: 8px 8px 0px 8px;
    52. float : right;
    53. color: #ffffff;
    54. margin: 10px;
    55. }
    56. .answer-div{
    57. width: 700px;
    58. padding: 10px 20px;
    59. border-radius: 0px 10px 10px 10px;
    60. /*float : left;*/
    61. clear: both;
    62. margin: 10px;
    63. }
    64. .loading {
    65. display: none;
    66. position: fixed;
    67. top: 0;
    68. left: 0;
    69. width: 100%;
    70. height: 100%;
    71. background-color: rgba(255, 255, 255, 0.8);
    72. z-index: 9999;
    73. text-align: center;
    74. padding-top: 20%;
    75. font-size: 24px;
    76. color: #333;
    77. }
    78. .logo{
    79. width: 40px !important;
    80. height: 40px !important;
    81. }
    82. </style>
    83. </head>
    84. <body>
    85. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    86. <div id="box">
    87. <div id="chat-box"></div>
    88. <div onfocus="divFocusMess()" onclick="divClickMess()" onblur="divBlurMess()" contenteditable = "true" tabindex="0" id="message-input" onkeydown="handleEnter(event)" onkeyup="keyup(event)" style="word-break: break-word;"></div>
    89. <button onclick="sendMessage()" id="btn" >提问</button>
    90. <div class="loading" id="loading">正在加载中 请耐心等待...</div>
    91. </div>
    92. <script>
    93. var chatbox = document.getElementById("chat-box");
    94. var message = document.getElementById('message-input').innerHTML;
    95. if(message == ''){
    96. document.getElementById('message-input').innerHTML = '输入消息';
    97. }
    98. function divClickMess(){ //点击时为空
    99. document.getElementById('message-input').innerHTML = '';
    100. }
    101. function divBlurMess(){ //失焦时设置为输入信息
    102. document.getElementById('message-input').innerHTML = '输入消息';
    103. }
    104. function divFocusMess(){ //聚焦时为空
    105. document.getElementById('message-input').innerHTML = '';
    106. }
    107. // function divSelMess(){
    108. // document.getElementById('message-input').innerHTML = '';
    109. // } onselect="divSelMess()"
    110. function scrollToBottom() {
    111. chatbox.scrollTop = chatbox.scrollHeight;
    112. }
    113. window.onload = function() {
    114. var chatbox = document.getElementById("chat-box");
    115. chatbox.scrollTop = chatbox.scrollHeight;
    116. }
    117. function sendMessage() {
    118. //获取输入框内容
    119. var message = document.getElementById('message-input').innerHTML;
    120. var chatBox = document.getElementById('chat-box');
    121. // 清除输入框的内容
    122. document.getElementById('message-input').innerHTML = '';
    123. // 在聊天框中添加消息
    124. chatBox.innerHTML += '
      ' + message + '
      '
      ;
    125. document.getElementById('loading').style.display = 'block';
    126. document.getElementById('message-input').innerHTML = '';
    127. // 在这里处理加载完成后的操作,例如显示答案等
    128. $.ajax({
    129. url:'info_request',//请求的路由接口
    130. data:{content:message},//传值
    131. type:'get',//接口类型
    132. dataType:'json',
    133. success:function (res){
    134. var result = res.result;
    135. document.getElementById('loading').style.display = 'none';
    136. //将返回的答案展示在页面上
    137. chatBox.innerHTML += '/images/levitated.png')}}">
      ' + result.replace(/\n/g, '
      '
      ) + '
      '
      ;
    138. //自动展示最新,将滚轮滑动至最下方
    139. if (chatbox.scrollHeight - chatbox.scrollTop - chatbox.clientHeight > 10) {
    140. scrollToBottom();
    141. }
    142. }
    143. })
    144. }
    145. function handleEnter(event) {
    146. // 检查按下的键是否是回车键
    147. if(event.key === "Enter") {
    148. // 可以在这里添加其他的逻辑处理
    149. var message = document.getElementById('message-input').innerHTML;
    150. var chatBox = document.getElementById('chat-box');
    151. // 清除输入框的内容
    152. document.getElementById('message-input').innerHTML = '';
    153. // 在聊天框中添加消息
    154. chatBox.innerHTML += '
      ' + message + '
      '
      ;
    155. document.getElementById('loading').style.display = 'block';
    156. document.getElementById('message-input').innerHTML = '';
    157. // 在这里处理加载完成后的操作,例如显示答案等
    158. $.ajax({
    159. url:'info_request',//请求的路由接口
    160. data:{content:message},//传值
    161. type:'get',//接口类型
    162. dataType:'json',
    163. success:function (res){
    164. var result = res.result;
    165. // 停止加载
    166. document.getElementById('loading').style.display = 'none';
    167. //将返回的答案展示在页面上
    168. chatBox.innerHTML += '/images/levitated.png')}}">
      ' + result.replace(/\n/g, '
      '
      ) + '
      '
      ;
    169. //自动展示最新,将滚轮滑动至最下方
    170. if (chatbox.scrollHeight - chatbox.scrollTop - chatbox.clientHeight > 10) {
    171. scrollToBottom();
    172. }
    173. }
    174. })
    175. event.preventDefault(); // 阻止 Enter 键的默认行为
    176. return false;
    177. }
    178. }
    179. </script>
    180. </body>
    181. </html>

  • 相关阅读:
    【K8S 九】使用docker作为CRI
    探索精彩世界,畅享短视频直播平台
    ES6之Set集合(通俗易懂,含实践)
    你知道ChatGPT里面的G、P、T分别代表什么吗?
    前端请求后台接口失败处理逻辑
    Oracle-Ogg经典模式升级为集成模式步骤
    【Python深度学习】Python全栈体系(三十二)
    【知识网络分析】研究者合作网络(co-investigator)
    Linux系统的常见变种、发行版有哪些 Debian,CentOS等
    Web前端大作业——基于HTML+CSS+JavaScript仿英雄联盟LOL游戏网站
  • 原文地址:https://blog.csdn.net/QiZong__BK/article/details/140036049