user3385258
user3385258

Reputation: 1

google-cloud/aiplatform vertex AI predictionserviceclient not working on php

Here is my code

$data = new \stdClass();

$data_obj = new \stdClass();
$message1 = new \stdClass();
$message1->author = 'user';
$message1->content = "Give me ten interview questions for the role of program manager.";
$messages[] = $message1;
$data_obj = new \stdClass();
$data_obj->messages = $messages;
$data->instances = [];
$data->instances[] = $data_obj;

$data->parameters = new \stdClass();
$data->parameters->temperature = 0.2;
$data->parameters->maxOutputTokens = 1024;
$data->parameters->topK = 40;
$data->parameters->topP = 0.95;



  $modelName = 'codechat-bison@001';
  $projectId = 'project_id'; //passed here project id
  $locationId = 'location_id'; //passed here location id
  $api_endpoint="location_id.googleapis.com"; //api_end_points





  $ai_client_file = '/path/to/file.json';
  $credentials_array = json_decode(file_get_contents($ai_client_file), true);
    // The AI Platform services require regional API endpoints.
    $client_options = [
        'credentials' => $credentials_array,
        "api_endpoint" => $api_endpoint,
    ];

    // Initialize client that will be used to create and send requests.
    // This client only needs to be created once, and can be reused for multiple requests.
    $client = new PredictionServiceClient($client_options);

    $instances_struct = [];
    foreach ($data->instances as $instance) {

        $value = new \Google\Protobuf\Value();
        $value->mergeFromJsonString(json_encode($instance));
        $instances_struct[] = $value;
    }
    $endpoint_client = new EndpointServiceClient($client_options);
    $formattedEndpoint = $endpoint_client->endpointName($projectId, $locationId, $api_endpoint);;
    $params_new = new \Google\Protobuf\Value();
    $params_new->mergeFromJsonString(json_encode($data->parameters));

    $response = $client->predict($formattedEndpoint, $instances_struct, ['parameters' => $params_new]);


The error received: "Stream removed"

I am getting the error Stream removed. I have tried changing the data structure and tried different functions, but I am still getting the error. Can you help me figure out what I am missing?

Upvotes: 0

Views: 1180

Answers (1)

Leo Galleguillos
Leo Galleguillos

Reputation: 2730

At the time of this writing, the format for prediction endpoints is:

projects/{project}/locations/{location}/endpoints/{endpoint} 

In php, you can use static method ::projectLocationPublisherModelName to get the formatted endpoint. For example:

$formattedEndpoint = PredictionServiceClient::projectLocationPublisherModelName(
    'my-project-001',
    'us-central1',
    'google',
    'text-bison',
);

The returned string will be:

projects/my-project-001/locations/us-central1/publishers/google/models/text-bison

Also, check out the predict() php examples provided by Google. The first argument that predict() accepts is a PredictRequest object.

Finally, when you instantiate your PredictionServiceClient object, make sure the apiEndpoint option is set to {location-id}-aiplatform.googleapis.com. For example, here in the US, the apiEndpoint would be us-central1-aiplatform.googleapis.com.

Looks like your code still has some way to go, but this should at least send your API calls to a valid URL and resolve the "Stream removed" message you've been receiving. I would kindly recommend including the namespaces of your objects if you have any followup questions. Cheers.

Upvotes: 0

Related Questions