ChatGPT API Integration




Antwort von ChatGPT: “ . htmlspecialchars($response) . „

„;
}

function send_to_chatgpt($message) {
$api_key = ‚your_api_key_here‘;
$url = ‚https://api.openai.com/v1/engines/davinci-codex/completions‘;
$prompt = „ChatGPT: “ . $message;

$data = json_encode([
‚prompt‘ => $prompt,
‚max_tokens‘ => 50,
‚temperature‘ => 0.7,
’n‘ => 1
]);

$headers = [
‚Content-Type: application/json‘,
‚Authorization: Bearer ‚ . $api_key
];

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$result = curl_exec($ch);
curl_close($ch);

$response_data = json_decode($result, true);
return $response_data[‚choices‘][0][‚text‘];
}
?>