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>