Hello,
I am currently working with PHP and successfully managed to send a request to Gemini AI. However, I am struggling to send a request to the Gemini + Vertex AI combination.
Here’s what I’ve done so far:
- Created a service in Google Cloud Console called
gemini-api
and assigned the following roles:
- Storage Object Viewer
- Vertex AI User
- Enabled the following APIs in Vertex AI:
- Vertex AI API
- Cloud Storage API
- Dataplex API
- Notebooks API
- Dataflow API
- Artifact Registry API
- Data Catalog API
- Compute Engine API
- Dataform API
- Vision AI API
- Generated a token and used it to access the service.
Despite all this, I haven’t been able to send the request successfully to the Gemini + Vertex AI. Could anyone guide me on what might be missing or if there are additional steps required?
Thank you in advance!
this is my php code -
namespace App\Services;
use Google\ApiCore\ApiException;
use Google\Cloud\AIPlatform\V1\Client\PredictionServiceClient;
use Google\Cloud\AIPlatform\V1\PredictRequest;
use Google\Protobuf\Value;
use Exception;
use GuzzleHttp\Exception\RequestException;
class GoogleGeminiService
{
protected $project_id;
protected $region;
protected $model_name;
protected $predictionServiceClient; // Correct Variable Name
public function __construct()
{
// Retrieve configuration from config/services.php
$this->project_id = config('services.google_gemini.project_id');
$this->region = config('services.google_gemini.location');
$this->model_name = "gemini-pro";
$keyFilePath = config('services.google_gemini.key_file_path');
$this->predictionServiceClient = new PredictionServiceClient([
'credentials' => $keyFilePath,
]);
}
public function generateText($prompt)
{
$resourceName = sprintf('projects/%s/locations/%s/publishers/google/models/%s', $this->project_id, $this->region, $this->model_name);
$value = new Value();
$value->setStringValue($prompt);
$predictRequest = (new PredictRequest())
->setEndpoint($resourceName)
->setInstances([$value]);
try {
$response = $this->predictionServiceClient->predict($predictRequest);
return $response->getPredictions();
} catch (ApiException $e) {
throw new Exception("Error generating text: " . $e->getMessage(), $e->getCode(), $e);
}
catch (RequestException $e) {
throw new Exception("Error generating content: " . $e->getMessage(), $e->getCode(), $e);
}
}
}