This commit is contained in:
ct
2025-06-17 16:37:19 +08:00
parent e378245554
commit 950202c1fb
3 changed files with 21 additions and 31 deletions

View File

@@ -4,7 +4,6 @@
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
class OpenAI class OpenAI
{ {
public static function getSingleMemeGenerator($user_prompt) public static function getSingleMemeGenerator($user_prompt)
@@ -12,7 +11,7 @@ public static function getSingleMemeGenerator($user_prompt)
$response = Http::withHeaders([ $response = Http::withHeaders([
'Content-Type' => 'application/json', 'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . env('OPENAI_API_KEY'), 'Authorization' => 'Bearer '.env('OPENAI_API_KEY'),
]) ])
->post('https://api.openai.com/v1/responses', [ ->post('https://api.openai.com/v1/responses', [
'model' => 'gpt-4.1-nano', 'model' => 'gpt-4.1-nano',
@@ -23,18 +22,18 @@ public static function getSingleMemeGenerator($user_prompt)
[ [
'type' => 'input_text', 'type' => 'input_text',
'text' => '"You are an AI meme writer for a video meme generator.\n\nYour task is to generate a JSON array containing a unique, short-form workplace-related video meme ideas.\n\nEach meme object must include the following fields, with the format shown below. Each field contains an inline description of the type of content it must generate.\n\nReturn **only** a Markdown code block containing valid JSON with inline content guidance (as strings). Do not add any commentary or explanation outside the code block.\n\n- All memes must be funny, relatable, and clearly connected to the provided topic.\n- Use realistic but exaggerated expressions and settings to match meme-style content.\n- Style should suit video formats like TikTok, Instagram Reels, or YouTube Shorts."', 'text' => '"You are an AI meme writer for a video meme generator.\n\nYour task is to generate a JSON array containing a unique, short-form workplace-related video meme ideas.\n\nEach meme object must include the following fields, with the format shown below. Each field contains an inline description of the type of content it must generate.\n\nReturn **only** a Markdown code block containing valid JSON with inline content guidance (as strings). Do not add any commentary or explanation outside the code block.\n\n- All memes must be funny, relatable, and clearly connected to the provided topic.\n- Use realistic but exaggerated expressions and settings to match meme-style content.\n- Style should suit video formats like TikTok, Instagram Reels, or YouTube Shorts."',
] ],
] ],
], ],
[ [
'role' => 'user', 'role' => 'user',
'content' => [ 'content' => [
[ [
'type' => 'input_text', 'type' => 'input_text',
'text' => $user_prompt 'text' => $user_prompt,
] ],
] ],
] ],
], ],
'text' => [ 'text' => [
'format' => [ 'format' => [
@@ -46,40 +45,40 @@ public static function getSingleMemeGenerator($user_prompt)
'properties' => [ 'properties' => [
'caption' => [ 'caption' => [
'type' => 'string', 'type' => 'string',
'description' => 'A humorous, one-liner or POV meme (under 15 words, no punctuation)' 'description' => 'A humorous, one-liner or POV meme (under 15 words, no punctuation)',
], ],
'meme_overlay' => [ 'meme_overlay' => [
'type' => 'string', 'type' => 'string',
'description' => 'A short visual reaction that pairs with the caption (e.g., "guy blinking in disbelief")' 'description' => 'A short visual reaction that pairs with the caption (e.g., "guy blinking in disbelief")',
], ],
'background' => [ 'background' => [
'type' => 'string', 'type' => 'string',
'description' => 'The setting or location of the meme (e.g., "Zoom call grid", "office cubicle")' 'description' => 'The setting or location of the meme (e.g., "Zoom call grid", "office cubicle")',
], ],
'keywords' => [ 'keywords' => [
'type' => 'array', 'type' => 'array',
'description' => 'Relevant lowercase tags for the meme (e.g., "burnout", "email fail", "meetings")', 'description' => 'Relevant lowercase tags for the meme (e.g., "burnout", "email fail", "meetings")',
'items' => [ 'items' => [
'type' => 'string' 'type' => 'string',
] ],
] ],
], ],
'required' => [ 'required' => [
'caption', 'caption',
'meme_overlay', 'meme_overlay',
'background', 'background',
'keywords' 'keywords',
], ],
'additionalProperties' => false 'additionalProperties' => false,
] ],
] ],
], ],
'reasoning' => (object)[], 'reasoning' => (object) [],
'tools' => [], 'tools' => [],
'temperature' => 1, 'temperature' => 1,
'max_output_tokens' => 2048, 'max_output_tokens' => 2048,
'top_p' => 1, 'top_p' => 1,
'store' => true 'store' => true,
]); ]);
// Get the response data // Get the response data
@@ -91,21 +90,12 @@ public static function getSingleMemeGenerator($user_prompt)
return $data; return $data;
} else { } else {
// Handle error // Handle error
throw new \Exception('API request failed: ' . $response->body()); throw new \Exception('API request failed: '.$response->body());
} }
} }
public static function getOpenAIOutput($data) public static function getOpenAIOutput($data)
{ {
if (isset($data['output'])) { return data_get($data, 'output.0.content.0.text', null);
if (count($data['output']) > 0) {
if (isset($data['output'][0]['content'])) {
if (isset($data['output'][0]['content'][0]['text'])) {
return $data['output'][0]['content'][0]['text'];
}
}
}
}
} }
} }

Binary file not shown.

Binary file not shown.