How to use the GPT-3.5-Turbo OpenAI Model:
<?php
$apiKey = "Your-API-Key";
$url = 'https://api.openai.com/v1/chat/completions';
$headers = array(
"Authorization: Bearer {$apiKey}",
"OpenAI-Organization: YOUR-ORG-STRING",
"Content-Type: application/json"
);
// Define messages
$messages = array();
$message = array();
$message["role"] = "user";
$message["content"] = "Hello future overlord!";
$messages[] = $message;
// Define data
$data = array();
$data["model"] = "gpt-3.5-turbo";
$data["messages"] = $messages;
$data["max_tokens"] = 50;
$data["temperature"] = 0.7;
// init curl
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Error:' . curl_error($curl);
} else {
echo $result;
}
curl_close($curl);
?>
The above is simple PHP / CURL example. Lets take it to the next level by adding the ability to enter a custom field for the prompt / message:
<?php
$apiKey = "Your-API-Key";
$url = 'https://api.openai.com/v1/chat/completions';
$headers = array(
"Authorization: Bearer {$apiKey}",
"OpenAI-Organization: YOUR-ORG-STRING",
"Content-Type: application/json"
);
// Retrieve message from input field
$message = $_POST['message']; //get data from html form
// Define messages
$messages = array();
$message = array();
$message["role"] = "user";
$message["content"] = $message;
$messages[] = $message;
// Define data
$data = array();
$data["model"] = "gpt-3.5-turbo";
$data["messages"] = $messages; //custom message prompt
$data["max_tokens"] = 50;
$data["temperature"] = 0.8;
// init curl
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Error:' . curl_error($curl);
} else {
echo $result;
}
curl_close($curl);
?>
IN PROGRESS TO COMPLETE...