Josh Posted June 18, 2023 Share Posted June 18, 2023 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>'); } ?> Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Recommended Posts
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.