Mit dem folgenden Code kann man per PHP auf GPT-3 via API zugreifen. Vorraussetzung ist ein valider API Key.
<?php
// Setzen Sie Ihren OpenAI API-Zugriffsschlüssel
$api_key = '';
// API-Anfrage senden
$response = send_api_request("Wer war Pablo Picasso?", $api_key);
print_r($response);
// Funktion zum Senden einer API-Anfrage
function send_api_request($prompt, $api_key)
{
$data = array(
'messages' => [
[
"role" => 'user',
"content" => $prompt,
],
],
'max_tokens' => 100,
'temperature' => 0.7,
'model' => 'gpt-3.5-turbo'
);
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
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, $headers);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}

