Using vertex + gemini ai with PHP

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:

  1. Created a service in Google Cloud Console called gemini-api and assigned the following roles:
  • Storage Object Viewer
  • Vertex AI User
  1. 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
  1. 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);
        }
    }
}

Hi,

Welcome to the forum.

First, I’m absolutely not a PHP person, however, I saw similar issues in .NET regarding the PredictionServiceClient. To my knowledge, this is for the older PaLM (2) models and APIs and not relevant for the Gemini API. That client uses the predict method which isn’t offered by the Gemini models. Those models operate on the generateContent method.

Maybe this GitHub sample helps you.

Alternatively, I would suggest you have a look at the REST API and see how you could use it in PHP.

Otherwise, you might have a look at PHP Client Libraries  |  Google Cloud for a newer client library.

2 Likes