From 282d21c3c49c66f0b6234c9bcbd59e89b5d2b365 Mon Sep 17 00:00:00 2001 From: Oscar Date: Mon, 16 Sep 2024 15:29:50 +1200 Subject: [PATCH] update response structure --- src/Extensions/SeoAIExtension.php | 49 ++++++++++++++++++------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/Extensions/SeoAIExtension.php b/src/Extensions/SeoAIExtension.php index 1145ce2..feb9c44 100644 --- a/src/Extensions/SeoAIExtension.php +++ b/src/Extensions/SeoAIExtension.php @@ -30,12 +30,7 @@ public function onAfterWrite() // Do an OpenAI API call to generate meta tags $prompt = $this->generatePrompt(); $response = $this->promptAPICall($prompt); - $metaTags = json_decode($response, true); // Decode the response which is in JSON format - $this->populateMetaTags($metaTags); - - // Reset generate tags field - $this->owner->GenerateTags = false; - $this->owner->write(); + $this->populateMetaTagsFromAPI($response); } } @@ -77,12 +72,6 @@ public function generatePrompt() Your task is to scan the following content gathered from a web page, and generate the following meta-tags for it: - MetaTitle - MetaDescription - - You'll provide the response in JSON format every time, here's an example: - { - "metaTitle": "Meta Title", - "metaDescription": "This is an example of the meta description." - } Here is some background information on the brand which the web page belongs to, delimited by ---: @@ -119,6 +108,22 @@ public function promptAPICall($prompt) "role" => "user", "content" => $prompt ] + ], + "response_format" => [ + "type" => "json_schema", + "json_schema" => [ + "name" => "metadata", + "schema" => [ + "type" => "object", + "properties" => [ + "metaTitle" => ["type" => "string"], + "metaDescription" => ["type" => "string"] + ], + "required" => ["metaTitle", "metaDescription"], + "additionalProperties" => false + ], + "strict" => true + ] ] ]; @@ -146,16 +151,20 @@ public function promptAPICall($prompt) /** * Populate the page's meta tags with AI generated content * @param Array - * - * @return Void + * + * @return Boolean */ - public function populateMetaTags($metaTags) + public function populateMetaTagsFromAPI($response) { - $page = $this->owner; - - $page->MetaTitle = $metaTags["metaTitle"]; - $page->MetaDescription = $metaTags["metaDescription"]; + $metaTags = json_decode($response, true); + if ($metaTags) { + $this->owner->MetaTitle = $metaTags["metaTitle"] ?? ''; + $this->owner->MetaDescription = $metaTags["metaDescription"] ?? ''; + $this->owner->GenerateTags = false; + $this->owner->write(); + return true; + } - return; + return false; } } \ No newline at end of file