Reputation: 844
In My WordPress, I am creating a text summarization function using the below function:
function call_vertex_ai($text_to_summarize) {
// Get the access token using the service account
$serviceAccountFile = (PLUGIN_PATH . 'vertex_sa.json');
$accessToken = getAccessToken($serviceAccountFile);
// Google Cloud project details
$project = 'my-posts-data-optimization';
$location = 'us-central1';
$publisher = 'google';
$model = 'gemini-1.5-flash-002';
// Prepare the API endpoint and payload
$endpoint = "https://us-central1-aiplatform.googleapis.com/v1/projects/'.$project.'/locations/'.$location.'/publishers/'.$publisher.'/models/'.$model.':predict";
$requestBody = [
"instances" => [
["content" => $text_to_summarize]
],
"parameters" => [
"temperature" => 0.7,
"maxOutputTokens" => 100, // Ensure a 100-word limit or equivalent in tokens
"topP" => 0.8,
"topK" => 40
]
];
// Make the cURL request to the Gemini model
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $accessToken",
"Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestBody));
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode == 200) {
$responseBody = json_decode($response, true);
return $responseBody['predictions'][0]['content']; // Return the summarized text
} else {
error_log("Error with the request. HTTP Code: $httpCode");
return "Error: Unable to summarize text.";
}
}
In IAM, principal [email protected]
has been assigned AI Platform Admin
& Vertex AI user
roles.
What's working are:
$serviceAccountFile
path is correct, and the credentials in the file are valid.
$accessToken
returns a valid token.
$text_to_summarize
is passed to this function from another function and has valid content.
I am getting the following 403 errors:
"message": "Permission denied on resource project '.my-posts-data-optimization.'.",
"status": "PERMISSION_DENIED",
"reason": "CONSUMER_INVALID",
What could be the potential mistakes I need to correct to make this function work?
Upvotes: 0
Views: 84
Reputation: 844
After debugging, I found how I constructed the $endpoint
with variables is troubling.
Not the permission issues.
Below is the correct one without single quotes:
$endpoint = "https://us-central1-aiplatform.googleapis.com/v1/projects/$project/locations/$location/publishers/$publisher/models/$model:predict";
Upvotes: 0