Jump to content

Recommended Posts

Posted

Some stuff I was experimenting with a while back, just posting here before I delete it:
 

<?php

$mode = $_GET["mode"];

$OPENAI_API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

// Chat
if ($mode == "text")
{
    $URL = 'https://api.openai.com/v1/chat/completions';
    $content_type = 'application/json';

    $messages = array(
       "Hello!"
    );

    $data = array(
        'model' => 'gpt-3.5-turbo',
        "messages"=> $messages,
    );
}

// Image generation
else if ($mode == "image_new")
{
    $URL = 'https://api.openai.com/v1/images/generations';
    $content_type = 'application/json';

    $prompt = 'dirt rocks seamless tiling texture quixel';
    $num_images = 1;
    $size = '512x512';
    $response_format = 'url';
    $format = 'png';
    
    $data = array(
        'prompt' => $prompt,
        'num_images' => $num_images,
        'size' => $size,
        'response_format' => $response_format
    );
}

// Image variation
else if ($mode == "image_variations")
{
    $URL = 'https://api.openai.com/v1/images/variations';
    $image = curl_file_create(__DIR__ . './boberroof01.png');
    $n = 1;
    $size = '512x512';
    $response_format = 'url';
    $format = 'png';
    $data = array(
        'n' => $n,
        'size' => $size,
        'image' => $image,
        'response_format' => $response_format,
    );
    $content_type = 'multipart/form-data';
}

$curl = curl_init($URL);

$headers = array(
    'Content-Type: ' . $content_type,
    'Authorization: Bearer ' . $OPENAI_API_KEY
);

$payload = json_encode($data);

curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($curl);

echo $response;

$responseObj = json_decode($response, true);

if ($mode == "text")
{
    echo($responseObj['choices'][0]["text"]);
}
else if ($mode == "image_new" || $mode == "image_variations")
{
    $imageUrl = $responseObj['data'][0]['url'];
    echo('<html><body><img src="');
    echo($imageUrl);
    echo('" /></body></html>');
}

?>

 

Let's build cool stuff and have fun. :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...