ChatGpt API PHP Kullanımı – Örnek Proje Kodlaması

Postman api kullanımı, chatgpt postman kullanımı ve örnek bir proje nasıl geliştirilir süreçleri izleyerek öğrenebilirsiniz ayrıca chatgpt php curl kodları içinde değişken nasıl kullanılır bunun yöntemini de vermiş oldum:

projenin kodları:

hesapla.php

<?php
if($_POST) {
 
$mesaj = $_POST['mesaj'];
 
$curl = curl_init();
 
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.openai.com/v1/chat/completions',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  $postData = [
    "model" => "gpt-3.5-turbo",
    "messages" => [
        [
            "role" => "system",
            "content" => "Sana verilecek şehirler arasındaki mesafeyi km cinsinden hesapla"
        ],
        [
            "role" => "user",
            "content" => $mesaj
        ]
    ],
    "temperature" => 1,
    "top_p" => 1,
    "n" => 1,
    "stream" => false,
    "max_tokens" => 250,
    "presence_penalty" => 0,
    "frequency_penalty" => 0
],
CURLOPT_POSTFIELDS => json_encode($postData),
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Accept: application/json',
    'Authorization: Bearer CHAT-GPT-API-KEY',
    'Cookie: __cf_bm=vZOEmnB.iBoARA5jNlzl_3fmVT5kf7XS5XGVJIULbzU-1707949206-1.0-Ab4yfu/SgkWvujszh3GgRmWFp+Yrn08466/17QdbMNzsExMjBK+iMYSXzmFjSaSxije1GChN+znSDrZVbagYXIw=; _cfuvid=4EBhWeYHBHCUPtDL8MCwyNKaINeeK3AkDRyEEqhKcAo-1707942992182-0.0-604800000'
  ),
));
 
$response = curl_exec($curl);
 
curl_close($curl);
 
$response = json_decode($response);
 
echo $response->choices[0]->message->content;
 
 
//print_r($response);
//echo $response;
 
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <form method="post">
    <textarea name="mesaj" rows="6"></textarea>
<button type="submit">Hesapla</button>
  </form>
 
</body>
</html>

ChatGpt Api Kullanarak Masal Yazdırma Projesi – ChatGpt For PHP Code

işte ChatGpt Api kullanarak bir hikaye/masal yazdırma projesi basit bir input alanından gelen veriyi bir değişkene atayıp prompt içinde kullanarak ekrana bir masal yazdırıyoruz.

<?php
 
//$input = "anahtar kelime, kelime2, başkabirşey";
 
if($_POST) {
 
 
  $input = $_POST['prompt'];
 
$curl = curl_init();
 
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.openai.com/v1/completions',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  $postData = [
  "model" => "gpt-3.5-turbo-instruct",
  "prompt" => "Şu anahtar kelimelerle alakalı bir masal yaz: $input",
  "max_tokens" => 500,
  "temperature" => 0.7
],
  CURLOPT_POSTFIELDS => json_encode($postData),
  CURLOPT_HTTPHEADER => array(
    'Authorization: Bearer CHAT-GPT-API-KEY',
    'Content-Type: application/json',
    'Cookie: __cf_bm=y5nFmlyys1DPwTtjCpj.pBevVryHN0TF6YLT1GWupkM-1706396921-1-AfYdSPVEQD1klX01+A+pFOeBJa1Q6CEeTN7fD99l7XJZu74miS5Ga4wAMuPgD/LaSIr0yjI6NT3Sp86lXVMfvAQ=; _cfuvid=DlcMs.nAdBoxgWFBSesVC0W8VMU8sZNGz2rkNOsG0xg-1706396921015-0-604800000'
  ),
));
 
$response = curl_exec($curl);
 
curl_close($curl);
 
$response = json_decode($response);
 
echo $response->choices[0]->text;
//print_r($response);
 
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
 
  <form method="post">
 
<textarea name="prompt" placeholder="Lütfen yazmamı istediğiniz masalınızın içeriğiyle alakalı anahtar kelimeler girin" rows="6"></textarea>
 
<button name="submit" type="submit">Gönder</button>
 
  </form>
 
</body>
</html>