• <small id='hCPGe'></small><noframes id='hCPGe'>

          <bdo id='hCPGe'></bdo><ul id='hCPGe'></ul>
        <i id='hCPGe'><tr id='hCPGe'><dt id='hCPGe'><q id='hCPGe'><span id='hCPGe'><b id='hCPGe'><form id='hCPGe'><ins id='hCPGe'></ins><ul id='hCPGe'></ul><sub id='hCPGe'></sub></form><legend id='hCPGe'></legend><bdo id='hCPGe'><pre id='hCPGe'><center id='hCPGe'></center></pre></bdo></b><th id='hCPGe'></th></span></q></dt></tr></i><div id='hCPGe'><tfoot id='hCPGe'></tfoot><dl id='hCPGe'><fieldset id='hCPGe'></fieldset></dl></div>
        <legend id='hCPGe'><style id='hCPGe'><dir id='hCPGe'><q id='hCPGe'></q></dir></style></legend>
      1. <tfoot id='hCPGe'></tfoot>

      2. PHP+HTML集成DeepSeek API,实现一个简单的聊天对话项目

        PHP+HTML集成DeepSeek API,实现一个简单的聊天对话项目,最终实现效果如下: 准备工作 PHP环境 :确保你的开发环境中安装了PHP。 DeepSeek API密钥 :注册并获取DeepSeek API的访问密钥。 创建api调用接口 创建一个PHP脚本(api.php),用于调用DeepSeek API

        <legend id='UnYe4'><style id='UnYe4'><dir id='UnYe4'><q id='UnYe4'></q></dir></style></legend>

            <i id='UnYe4'><tr id='UnYe4'><dt id='UnYe4'><q id='UnYe4'><span id='UnYe4'><b id='UnYe4'><form id='UnYe4'><ins id='UnYe4'></ins><ul id='UnYe4'></ul><sub id='UnYe4'></sub></form><legend id='UnYe4'></legend><bdo id='UnYe4'><pre id='UnYe4'><center id='UnYe4'></center></pre></bdo></b><th id='UnYe4'></th></span></q></dt></tr></i><div id='UnYe4'><tfoot id='UnYe4'></tfoot><dl id='UnYe4'><fieldset id='UnYe4'></fieldset></dl></div>

                <tbody id='UnYe4'></tbody>

                <small id='UnYe4'></small><noframes id='UnYe4'>

                  <bdo id='UnYe4'></bdo><ul id='UnYe4'></ul>
                • <tfoot id='UnYe4'></tfoot>

                  PHP+HTML集成DeepSeek API,实现一个简单的聊天对话项目,最终实现效果如下:

                  准备工作

                  • PHP环境:确保你的开发环境中安装了PHP。
                  • DeepSeek API密钥:注册并获取DeepSeek API的访问密钥。

                  创建api调用接口

                  创建一个PHP脚本(api.php),用于调用DeepSeek API。假设API密钥为YOUR_API_KEY。代码如下:
                  <?php
                  header('Content-Type: application/json');
                  
                  function callDeepSeekAPI($message) {
                      $apiKey = 'YOUR_API_KEY';
                      $url = 'https://api.deepseek.com/chat/completions';
                  
                      $data = [
                          "model" => "deepseek-chat",
                          'messages' => [
                              ["role" => "system", "content" => "You are a helpful assistant."],
                              ["role" => "user", "content" => $message]
                          ],
                          'stream'=>false
                      ];
                  
                      $ch = curl_init($url);
                      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                      curl_setopt($ch, CURLOPT_POST, true);
                      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
                      curl_setopt($ch, CURLOPT_HTTPHEADER, [
                          'Content-Type: application/json',
                          'Authorization: Bearer ' . $apiKey
                      ]);
                  
                      $response = curl_exec($ch);
                      if (curl_errno($ch)) {
                          $error_msg = curl_error($ch);
                          curl_close($ch);
                          return json_encode(['error' => $error_msg]);
                      }
                      curl_close($ch);
                  
                      $response = json_decode($response, true);
                      return json_encode(['content'=>$response['choices'][0]['message']['content']]);
                  }
                  
                  if ($_SERVER['REQUEST_METHOD'] === 'POST') {
                      $input = json_decode(file_get_contents('php://input'), true);
                      if (isset($input['message'])) {
                          echo callDeepSeekAPI($input['message']);
                      } else {
                          echo json_encode(['error' => '参数错误']);
                      }
                  }
                  

                  HTML文件

                  创建一个简单的HTML文件(index.html),用于显示聊天界面。
                  <!DOCTYPE html>
                  <html lang="en">
                  <head>
                    <meta charset="UTF-8">
                    <meta name="viewport" content="width=device-width, initial-scale=1.0">
                    <title>智能客服系统</title>
                    <style>
                      body {
                        font-family: Arial, sans-serif;
                        display: flex;
                        justify-content: center;
                        align-items: center;
                        height: 100vh;
                        margin: 0;
                        background-color: #f4f4f9;
                      }
                      .chat-container {
                        width: 800px;
                        background-color: #fff;
                        border-radius: 10px;
                        box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                        padding: 20px;
                      }
                      .messages {
                        height: 500px;
                        overflow-y: auto;
                        border-bottom: 1px solid #ddd;
                        padding-bottom: 10px;
                      }
                      .message {
                        margin: 10px 0;
                      }
                      .user {
                        text-align: right;
                      }
                      .bot {
                        text-align: left;
                      }
                      .input-container {
                        display: flex;
                        margin-top: 10px;
                      }
                      .input-container input {
                        flex: 1;
                        padding: 10px;
                        border: 1px solid #ddd;
                        border-radius: 5px;
                      }
                      .input-container button {
                        padding: 10px 20px;
                        border: none;
                        background-color: #007bff;
                        color: #fff;
                        border-radius: 5px;
                        cursor: pointer;
                      }
                      .input-container button:hover {
                        background-color: #0056b3;
                      }
                    </style>
                  </head>
                  <body>
                  <div class="chat-container">
                    <div class="messages" id="messages"></div>
                    <div class="input-container">
                      <input type="text" id="userInput" placeholder="输入消息...">
                      <button onclick="sendMessage()">发送</button>
                    </div>
                  </div>
                  
                  <script>
                    function sendMessage() {
                      const userInput = document.getElementById('userInput').value;
                      if (userInput.trim() === '') return;
                  
                      const messagesContainer = document.getElementById('messages');
                      const userMessage = document.createElement('div');
                      userMessage.className = 'message user';
                      userMessage.textContent = userInput;
                      messagesContainer.appendChild(userMessage);
                  
                      fetch('api.php', {
                        method: 'POST',
                        headers: {
                          'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({ message: userInput })
                      }).then(response => response.json())
                              .then(data => {
                                console.log('Success:', data)
                                const botMessage = document.createElement('div');
                                botMessage.className = 'message bot';
                                botMessage.textContent = data.content || '无法获取回复';
                                messagesContainer.appendChild(botMessage);
                              })
                              .catch(error => {
                                console.error('Error:', error);
                                const botMessage = document.createElement('div');
                                botMessage.className = 'message bot';
                                botMessage.textContent = '发生错误,请重试';
                                messagesContainer.appendChild(botMessage);
                              });
                  
                      document.getElementById('userInput').value = '';
                      messagesContainer.scrollTop = messagesContainer.scrollHeight;
                    }
                  </script>
                  </body>
                  </html>
                  


                   
                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  • <bdo id='gqHgT'></bdo><ul id='gqHgT'></ul>

                              <tbody id='gqHgT'></tbody>
                            <legend id='gqHgT'><style id='gqHgT'><dir id='gqHgT'><q id='gqHgT'></q></dir></style></legend>
                          • <i id='gqHgT'><tr id='gqHgT'><dt id='gqHgT'><q id='gqHgT'><span id='gqHgT'><b id='gqHgT'><form id='gqHgT'><ins id='gqHgT'></ins><ul id='gqHgT'></ul><sub id='gqHgT'></sub></form><legend id='gqHgT'></legend><bdo id='gqHgT'><pre id='gqHgT'><center id='gqHgT'></center></pre></bdo></b><th id='gqHgT'></th></span></q></dt></tr></i><div id='gqHgT'><tfoot id='gqHgT'></tfoot><dl id='gqHgT'><fieldset id='gqHgT'></fieldset></dl></div>

                          • <tfoot id='gqHgT'></tfoot>
                          • <small id='gqHgT'></small><noframes id='gqHgT'>