• 06-06-2023, 17:34:48
    #1
    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);
  • 06-06-2023, 17:38:19
    #2
    Elinize sağlık hocam
  • 06-06-2023, 17:43:55
    #3
    www.sabamedya.com
    Elinize sağlık hocam.
  • 06-06-2023, 17:46:20
    #4
    celalyesil adlı üyeden alıntı: mesajı görüntüle
    Elinize sağlık hocam
    saba_medya adlı üyeden alıntı: mesajı görüntüle
    Elinize sağlık hocam.
    Teşekkür ederim, umarım faydalı olmuştur.
  • 06-06-2023, 19:55:15
    #5
    Teşekkürler hocam, elimize sağlık. API kullanmamız için Plus üyelik mi almamız gerekiyor?
  • 06-06-2023, 20:17:13
    #6
    umutyildiz adlı üyeden alıntı: mesajı görüntüle
    Teşekkürler hocam, elimize sağlık. API kullanmamız için Plus üyelik mi almamız gerekiyor?
    İlk kayıttan sonra kart eklerseniz 120 dolar bakiye hediye ediyor
  • 10-06-2023, 17:56:07
    #7
    Sefaakcihan adlı üyeden alıntı: mesajı görüntüle
    İlk kayıttan sonra kart eklerseniz 120 dolar bakiye hediye ediyor
    bu bilgi için teşekkürler
  • 24-01-2024, 18:11:12
    #8
    Kurumsal Üye
    Teşekkürler deneyeceğim
  • 21-06-2024, 05:00:21
    #9
    bu kod artık güncel değil mi ? ben calıstıramadım da