Merhaba chat gpt api bağlantısı ve sonuç almanız için sizlere örnek bir class yazdım umarım işinizi görür. Kod içerisindeki prompt ve api anahtarını değiştirmeniz yeterlidir.

Çıktı:


<?php
class gpt
{
    public function index($prompt,$apiKey)
    {
  
        $model = 'gpt-3.5-turbo';
        // ChatGPT ile etkileşim örneği
        $response = $this->chat_with_gpt($prompt, $apiKey, $model);
        return $response;
    }
    public function chat_with_gpt($prompt, $apiKey, $model)
    {
        $data = array(
            'messages' => array(
                array('role' => 'system', 'content' => 'You: ' . $prompt),
            ),
            'max_tokens' => 3500,
            'temperature' => 0.7,
            'top_p' => 1,
            'n' => 1,
            'stop' => null,
            'model' => $model,
        );
        $headers = array(
            'Content-Type: application/json',
            'Authorization: Bearer ' . $apiKey,
        );
        $url = 'https://api.openai.com/v1/chat/completions';

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        $response = curl_exec($ch);
        curl_close($ch);
        $response = json_decode($response, true);
        
        $response = $response["choices"][0];
        $message = $response["message"]["content"];
        return $message;
    }
}
/////
$apiKey = 'Bu alana openai api anahtarını giriniz.';
$prompt = 'r10 Forumuna selam metni yaz.';
/////
$gptapi = new gpt();
echo $gptapi->index($prompt,$apiKey);