File manager - Edit - /home/autoph/public_html/pms_v1/chatGPTv1/chatGPTv2.php
Back
<?php // Replace 'YOUR_API_KEY' with your actual OpenAI API key $api_key = "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"; // Function to send a message to ChatGPT and receive a response function chatWithGPT($message, $api_key) { $data = array( 'model' => 'davinci', // You can choose from different GPT-3 engines 'messages' => array( array( 'role' => 'system', 'content' => 'You are a helpful assistant.' ), array( 'role' => 'user', 'content' => $message ) ) ); $url = 'https://api.openai.com/v1/chat/completions'; $url = 'https://api.openai.com/v1/engines/davinci/completions'; $headers = array( 'Content-Type: application/json', 'Authorization: Bearer ' . $api_key ); $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); $decoded_response = json_decode($response, true); $message = $decoded_response['choices'][0]['message']['content']; return $message; } // Example usage if ($_SERVER['REQUEST_METHOD'] === 'POST') { $user_input = $_POST['user_input']; $response = chatWithGPT($user_input, $api_key); echo json_encode(array('message' => $response)); } ?>
| ver. 1.4 |
.
| PHP 7.3.33 | Generation time: 0 |
proxy
|
phpinfo
|
Settings