GoogleCloudPlatform / php-docs-samples

A collection of samples that demonstrate how to call Google Cloud services from PHP.
http://cloud.google.com/php
Apache License 2.0
966 stars 1.03k forks source link

Add Vertex AI samples #1956

Closed cselti closed 9 months ago

cselti commented 10 months ago

Please add some examples for the Vertex AI PHP client library.

Actually, I need a working example for the predict call in the PredictionServiceClient.

My problem is that it doesn't accept the instances input and I get the following error messages:

Fatal error: Uncaught UnexpectedValueException: Invalid message property: content in /Users/totha/Work/gcloud_samples/google-cloud-php-ai-platform/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:1042 Stack trace:

0 /Users/totha/Work/gcloud_samples/google-cloud-php-ai-platform/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php(57): Google\Protobuf\Internal\Message->mergeFromArray(Array)

1 /Users/totha/Work/gcloud_samples/google-cloud-php-ai-platform/vendor/google/protobuf/src/Google/Protobuf/Value.php(46): Google\Protobuf\Internal\Message->__construct(Array)

2 /Users/totha/Work/gcloud_samples/google-cloud-php-ai-platform/samples/V1/PredictionServiceClient/palm2-text.php(50): Google\Protobuf\Value->__construct(Array)

3 /Users/totha/Work/gcloud_samples/google-cloud-php-ai-platform/samples/V1/PredictionServiceClient/palm2-text.php(100): predict_sample('projects/rugged...')

4 /Users/totha/Work/gcloud_samples/google-cloud-php-ai-platform/samples/V1/PredictionServiceClient/palm2-text.php(104): callSample()

5 {main}

thrown in /Users/totha/Work/gcloud_samples/google-cloud-php-ai-platform/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php on line 1042

I have tried with a following instances input with several combinations similar to this:

$formattedEndpoint = PredictionServiceClient::projectLocationPublisherModelName('xxxxx-yyyyy-zzzzz', 'europe-west1', 'google', 'text-bison@001');
$predictionServiceClient = new PredictionServiceClient([
        'apiEndpoint' => 'europe-west1-aiplatform.googleapis.com',
]);

$instances = new Value(['prompt' => 'Give me ten interview questions for the role of program manager.']);
$request = (new PredictRequest())
        ->setEndpoint($formattedEndpoint)
        ->setInstances($instances);
$response = $predictionServiceClient->predict($request);

I usedcontent, prompt, text, input, input_text, input_string, message values, using a Value with multiple level, using StringValue or StructValue - nothing worked.

I have checked the "samples" in the client library, but it uses only empty [new Value()] that it throws an Empty instancis INVALID_ARGUMENT error response.

I have checked the NodeJS client library (which is quite similar to the PHP client library) and it's worked correctly. I searched on the internet, of course, but I found only 2 examples and none of them worked.

I know that I can use the REST API too, but I prefer the client library because of the built-in authentication, error handling, etc.

Thanks in advance.

shivgautam commented 10 months ago

Does this example help? https://github.com/googleapis/google-cloud-php-ai-platform/blob/main/src/V1/Gapic/PredictionServiceGapicClient.php#L614-L623

cselti commented 10 months ago

Sadly, no, I have already checked everything in the client library's source code. The problem is that the acceptable instance structure has not been specified. I have tried with the same structure as in the REST API or Node.js library (and those work, of course):

$instances = new Value(['prompt' => 'Give me ten interview questions for the role of program manager.']);
// or
$instances = new Value(['content' => 'Give me ten interview questions for the role of program manager.']);
// or
$instances = new Value(['text' => 'Give me ten interview questions for the role of program manager.']);
// or
$instances = new StringValue(['input' => 'Give me ten interview questions for the role of program manager.']);
// or
$instances = new Value(['input_text' => new StringValue('Give me ten interview questions for the role of program manager.')]);
// .....

I have got always like these:

Fatal error: Uncaught UnexpectedValueException: Invalid message property: content Fatal error: Uncaught UnexpectedValueException: Invalid message property: prompt Fatal error: Uncaught UnexpectedValueException: Invalid message property: text Fatal error: Uncaught UnexpectedValueException: Invalid message property: input ....

yash30201 commented 9 months ago

Hi @cselti , I see that the Value is imported as Google\Protobuf\Value (ref). Also observing that Ai Platform has defined it's own Value.php with possible options as string_value, int_value and double_value.

Can you check if the following works for you?

$instances = [new Value(['string_value' => 'Give me ten interview questions for the role of program manager.'])]
cselti commented 9 months ago

Hi @yash30201 , Sadly not:

Call failed with message: {
    "message": "Invalid instance: string_value: \t \"Give me ten interview questions for the role of program manager.\"\n",
    "code": 3,
    "status": "INVALID_ARGUMENT",
    "details": []
}

It was worked for you?

yash30201 commented 9 months ago

Hi @cselti , sorry for the delay.

So I tried to deploy a model and the send predict requests. Following the way a model was queried in ruby, I replicated the syntax for PHP and it worked. Following is the crude way of creating a prediction request. You can extract the logic so that request making becomes easy.

$serializer = new Serializer();
$struct = $serializer->decodeMessage(new Struct(), ['fields' => ['prompt' => ['string_value' => 'Hi Model']]]);
$request = (new PredictRequest())
    ->setEndpoint($client->endpointName($projectId, $locationId, $endpointId))
    ->setInstances([
        new Value(['struct_value' => $struct])

If you face connection timeout / deadline exceed error, you can just simply increase the timeout for predict rpc according to your model's average end to end request times in the vendor/google/cloud-ai-platform/src/V1/resources/prediction_service_client_config.json