From 8d020a3eb940fb2cc2465e2d2eab415b382c26ab Mon Sep 17 00:00:00 2001 From: CXM <chenzixin202209@163.com> Date: Thu, 14 Mar 2024 11:27:21 +0000 Subject: [PATCH] implement the chatbot, this chatbot can automatically responses user. --- Presentation/ChatbotConfig.php | 59 ++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Presentation/ChatbotConfig.php diff --git a/Presentation/ChatbotConfig.php b/Presentation/ChatbotConfig.php new file mode 100644 index 0000000..f2a55ea --- /dev/null +++ b/Presentation/ChatbotConfig.php @@ -0,0 +1,59 @@ +<?php + +$apiKey = "sk-fNCmqQ3Te5cEuj6a2w0IFmnvrMMX6TpVdVVuOYT0aPA4dJcG"; +$baseUrl = "https://api.chatanywhere.cn/v1"; + +function gpt35Api($messages) { + global $apiKey, $baseUrl; + + $url = $baseUrl . "/chat/completions"; + $headers = [ + "Authorization: Bearer " . $apiKey, + "Content-Type: application/json" + ]; + + $data = [ + "model" => "gpt-3.5-turbo", + "messages" => $messages, + ]; + + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); + + // 禁用SSL证书验证 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + + + $response = curl_exec($ch); + if (curl_errno($ch)) { + echo 'Curl error: ' . curl_error($ch); + } + + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + echo "HTTP Status Code: " . $httpCode . "\n"; + + curl_close($ch); + + echo "Raw response: " . $response . "\n"; // 打印原始响应 + + $responseDecoded = json_decode($response, true); + if (isset($responseDecoded['choices'][0]['message']['content'])) { + echo $responseDecoded['choices'][0]['message']['content']; + } else { + // 处理错误或无响应的情况 + echo "No response or an error occurred."; + } +} + +$messages = [ + ['role' => 'user', 'content' => '请你告诉我融创有哪些好玩的'], +]; + +gpt35Api($messages); + +?> + + -- GitLab