Skip to content

Commit

Permalink
update response structure
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarholt committed Sep 16, 2024
1 parent f2b88b3 commit 282d21c
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions src/Extensions/SeoAIExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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 ---:
Expand Down Expand Up @@ -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
]
]
];

Expand Down Expand Up @@ -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;
}
}

0 comments on commit 282d21c

Please sign in to comment.