Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add puzzle API #1372

Merged
merged 10 commits into from
May 17, 2024
202 changes: 202 additions & 0 deletions packages/playground/website/public/puzzle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?php
// error_reporting(0);
header('Content-Type: application/json');

$open_ai_api_key = "";
$api_key = "";

if (!isset($_POST["api_key"])) {
response("error", "Please provide an API key.");
}
if ($_POST["api_key"] !== $api_key) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where will our users be getting this API key? I apologize if I've missed discussion about this elsewhere.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hardcoded here.

It's only purpose is to allow us to disable the API in case we start having problems.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I will send you the details.

response("error", "Invalid API key.");
}

if (empty($open_ai_api_key)) {
response("error", "The API isn't configured properly. Please contact the administrator.");
}

if (!isset($_POST["action"])) {
response("error", "Please provide ac action.");
}

if ($_POST["action"] == "read-image") {
if (!isset($_POST["image"])) {
response("error", "Please provide an image.");
}
readImage($_POST["image"]);
} else if ($_POST["action"] == "site-name") {
siteName();
} else if ($_POST["action"] === 'post') {
post();
} else {
response("error", "Invalid action.");
}

function siteName()
{
global $open_ai_api_key;
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.openai.com/v1/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
"model" => "gpt-3.5-turbo-instruct",
"prompt" => "Please come up with a random name for a website.
That's related to playing, but don't use the word 'play' in the name.
Don't suggest names combining multiple words into one.
Return only the website name without any additional text or quotes surrounding it as one line.",
"temperature" => 1,
"max_tokens" => 256,
"top_p" => 1,
"frequency_penalty" => 0,
"presence_penalty" => 0,
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"Authorization: Bearer " . $open_ai_api_key,
],
]);
$response = curl_exec($curl);
curl_close($curl);

$response = json_decode($response, true);
if (empty($response["choices"])) {
response("error", "No response from OpenAI");
}
try {
$output = $response["choices"][0]["text"];
$output = trim($output);
$output = str_replace('"', "", $output);
$output = str_replace("'", "", $output);
response("success", $output);
} catch (Exception $e) {
response("error", "Invalid response from OpenAI");
}
}

function post()
{
global $open_ai_api_key;
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.openai.com/v1/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
"model" => "gpt-3.5-turbo-instruct",
"prompt" => "Please write a short blog post about a random topic.
Make sure the post is complete and doesn't end abruptly.
The topic should be fun and not too serious. It should be something that people would enjoy reading.
It should be safe for work and not contain any inappropriate content.
Start with a title for the post and then write the content. Add two new lines after the title.
Don't include any quotes or additional text in the output.
Don't prefix the title with 'Title:', or content with the word 'Content:'.",
"temperature" => 1,
"max_tokens" => 256,
"top_p" => 1,
"frequency_penalty" => 0,
"presence_penalty" => 0,
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"Authorization: Bearer " . $open_ai_api_key,
],
]);
$response = curl_exec($curl);
curl_close($curl);

$response = json_decode($response, true);
if (empty($response["choices"])) {
response("error", "No response from OpenAI");
}
try {
$output = $response["choices"][0]["text"];
response("success", $output);
} catch (Exception $e) {
response("error", "Invalid response from OpenAI");
}
}

function readImage($image)
{
global $open_ai_api_key;
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.openai.com/v1/chat/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
"model" => "gpt-4-vision-preview",
"messages" => [
[
"role" => "user",
"content" => [
[
"type" => "text",
"text" => "The image will contain puzzle pieces with text on them.
Please list all the puzzle pieces by only outputting the text on them, each in new line.
Valid lines of text are:
- Site name
- Post
- /wp-admin/
- Multisite
- Omnisend
- WooCommerce
- Google
- Jetpack
- Elementor
- YITH
- Dynamic.ooo
- PersonalizeWP
- JetFormBuilder
- Fastspring
- Cookiebot
- W3 Total Cache
- SiteGround
- Yoast
If the text is invalid, don't return it.
If you do not see puzzle pieces of paper with valid text on them, simply output 'NO'",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the "NO" be seen by humans, or is it a value for an app to interpret? If it will be seen by people, perhaps something like "Unrecognized" would sound more gentle.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is internal. We only show a message if a piece was successfully recognized.

],
[
"type" => "image",
"image_url" => [
"url" => $_POST["image"],
],
],
],
],
],
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"Authorization: Bearer " . $open_ai_api_key,
],
]);
$response = curl_exec($curl);
curl_close($curl);

$response = json_decode($response, true);
if (empty($response["choices"])) {
response("error", "No response from OpenAI");
}
try {
$output = $response["choices"][0]["message"]["content"];
$output = explode("\n", $output);
$output = array_map("trim", $output);
response("success", $output);
} catch (Exception $e) {
response("error", "Invalid response from OpenAI");
}
}



function response($status, $message)
{
if ($status == "error") {
http_response_code(400);
}
die(json_encode(array("status" => $status, "message" => $message)));
}
Loading