Код: Выделить всё
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.";
}
}
Что работает:
Код: Выделить всё
$serviceAccountFile
Код: Выделить всё
$accessToken
Код: Выделить всё
$text_to_summarize
Я получаю следующие ошибки 403:
Код: Выделить всё
"message": "Permission denied on resource project '.my-posts-data-optimization.'.",
Код: Выделить всё
"status": "PERMISSION_DENIED",
Код: Выделить всё
"reason": "CONSUMER_INVALID",
Подробнее здесь: https://stackoverflow.com/questions/792 ... xt-summari