Skip to content

Instantly share code, notes, and snippets.

@Pelirrojo
Last active October 15, 2024 10:25
Show Gist options
  • Save Pelirrojo/b1c642f74a04a3363df703f9f1303560 to your computer and use it in GitHub Desktop.
Save Pelirrojo/b1c642f74a04a3363df703f9f1303560 to your computer and use it in GitHub Desktop.
PHP Script to augment the page content through AWS Bedrock with Claude Sonnet
<?php
// Ensure this script its used by consola or exit process
if (php_sapi_name() !== 'cli') {
die('This script can only be run from the command line.');
}
// Database config (get all this data from wp-config.php)
$db_host = '<db_ip>:3306';
$db_name = '<db_name>';
$db_user = '<db_user>';
$db_pass = '<db_password>';
$table_prefix = 'wp_';
// AWS config (make sure to follow least privilege principle)
$aws_key = '********************';
$aws_secret = '****************************************';
$aws_region = 'us-east-1';
// Connect to datatase
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Load SDK de AWS
require 'vendor/autoload.php';
use Aws\Exception\AwsException;
use Aws\BedrockRuntime\BedrockRuntimeClient;
// Setup Bedrock client
$bedrockClient = new BedrockRuntimeClient([
'version' => 'latest',
'region' => $aws_region,
'credentials' => [
'key' => $aws_key,
'secret' => $aws_secret,
],
]);
// Optional function to filter the pages to interact
function isPageWithSomeCriteria($content, $language) {
// custom implementation
return true;
}
// Función para generar contenido con Claude Sonnet
function generateContent($prompt) {
global $bedrockClient;
$messages = [
[
'role' => 'user',
'content' => $prompt
]
];
try {
$result = $bedrockClient->invokeModel([
'modelId' => 'anthropic.claude-3-sonnet-20240229-v1:0',
'contentType' => 'application/json',
'accept' => 'application/json',
'body' => json_encode([
'anthropic_version' => 'bedrock-2023-05-31',
'max_tokens' => 1000,
'messages' => $messages
])
]);
$response = json_decode($result->get('body')->getContents(), true);
return $response['content'][0]['text'];
} catch (AwsException $e) {
echo "Error al invocar el modelo de Bedrock: " . $e->getMessage() . "\n";
return false;
}
}
// SQL query to get all published pages in worpdress (omitting drafts and hidden)
$sql = "SELECT p.ID, p.post_title, p.post_content, p.post_name, p.post_parent,
(SELECT meta_value FROM {$table_prefix}postmeta WHERE post_id = p.ID AND meta_key = '_wp_page_template') as template,
(SELECT t.slug FROM {$table_prefix}terms t
JOIN {$table_prefix}term_taxonomy tt ON t.term_id = tt.term_id
JOIN {$table_prefix}term_relationships tr ON tt.term_taxonomy_id = tr.term_taxonomy_id
WHERE tr.object_id = p.ID AND tt.taxonomy = 'language') as language
FROM {$table_prefix}posts p
WHERE p.post_type = 'page' AND p.post_status = 'publish'
ORDER BY p.ID";
$result = $conn->query($sql);
$processed_pages = 0;
// Iterate over the pages
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id=" . $row["ID"] . " -----------------------------------------------\n";
echo "link: /" . $row["post_name"] . "\n";
echo "title: " . $row["post_title"] . "\n";
echo "parent: " . ($row["post_parent"] != 0 ? $row["post_parent"] : "none") . "\n";
echo "language: " . ($row["language"] ? $row["language"] : "N/A") . "\n\n";
if (isPageWithSomeCriteria($row["post_content"], $row["language"])) {
$prompt = "Extend the content of this page in a fancy way that bla bla bla....:\n\n" .
"Title: " . $row["post_title"] . "\n" .
"Content: " . strip_tags($row["post_content"]) . "\n\n" .
"The structure must be ...\n" .
"Do not forget to include ...\n" .
"The output language must be: " . ($row["language"] == 'es' ? "español" : "inglés") . ".\n";
$response = generateContent($prompt);
// Outputs model response
if ($response) {
echo $response . "\n\n";
$processed_pages++;
} else {
echo "Unable to generate content for this page.\n\n";
}
// Manual Throttling
sleep(5);
} else {
echo "The page doesn't follow criteria, so it's discarded\n\n";
}
}
} else {
echo "No pages found.";
}
// Close connection and report
$conn->close();
echo "Total number of pages processed: " . $processed_pages . "\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment