Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [AiPlatform] add PredictionService.ServerStreamingPredict method #6508

Merged
merged 2 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions AiPlatform/metadata/V1/PredictionService.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified AiPlatform/metadata/V1/Types.php
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* GENERATED CODE WARNING
* This file was automatically generated - do not edit!
*/

require_once __DIR__ . '/../../../vendor/autoload.php';

// [START aiplatform_v1_generated_PredictionService_ServerStreamingPredict_sync]
use Google\ApiCore\ApiException;
use Google\ApiCore\ServerStream;
use Google\Cloud\AIPlatform\V1\Client\PredictionServiceClient;
use Google\Cloud\AIPlatform\V1\StreamingPredictRequest;
use Google\Cloud\AIPlatform\V1\StreamingPredictResponse;

/**
* Perform a server-side streaming online prediction request for Vertex
* LLM streaming.
*
* @param string $formattedEndpoint The name of the Endpoint requested to serve the prediction.
* Format:
* `projects/{project}/locations/{location}/endpoints/{endpoint}`
* Please see {@see PredictionServiceClient::endpointName()} for help formatting this field.
*/
function server_streaming_predict_sample(string $formattedEndpoint): void
{
// Create a client.
$predictionServiceClient = new PredictionServiceClient();

// Prepare the request message.
$request = (new StreamingPredictRequest())
->setEndpoint($formattedEndpoint);

// Call the API and handle any network failures.
try {
/** @var ServerStream $stream */
$stream = $predictionServiceClient->serverStreamingPredict($request);

/** @var StreamingPredictResponse $element */
foreach ($stream->readAll() as $element) {
printf('Element data: %s' . PHP_EOL, $element->serializeToJsonString());
}
} catch (ApiException $ex) {
printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
}
}

/**
* Helper to execute the sample.
*
* This sample has been automatically generated and should be regarded as a code
* template only. It will require modifications to work:
* - It may require correct/in-range values for request initialization.
* - It may require specifying regional endpoints when creating the service client,
* please see the apiEndpoint client configuration option for more details.
*/
function callSample(): void
{
$formattedEndpoint = PredictionServiceClient::endpointName('[PROJECT]', '[LOCATION]', '[ENDPOINT]');

server_streaming_predict_sample($formattedEndpoint);
}
// [END aiplatform_v1_generated_PredictionService_ServerStreamingPredict_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Google\ApiCore\PagedListResponse;
use Google\ApiCore\ResourceHelperTrait;
use Google\ApiCore\RetrySettings;
use Google\ApiCore\ServerStream;
use Google\ApiCore\Transport\TransportInterface;
use Google\ApiCore\ValidationException;
use Google\Api\HttpBody;
Expand All @@ -39,6 +40,7 @@
use Google\Cloud\AIPlatform\V1\PredictRequest;
use Google\Cloud\AIPlatform\V1\PredictResponse;
use Google\Cloud\AIPlatform\V1\RawPredictRequest;
use Google\Cloud\AIPlatform\V1\StreamingPredictRequest;
use Google\Cloud\Iam\V1\GetIamPolicyRequest;
use Google\Cloud\Iam\V1\Policy;
use Google\Cloud\Iam\V1\SetIamPolicyRequest;
Expand Down Expand Up @@ -372,6 +374,29 @@ public function rawPredict(RawPredictRequest $request, array $callOptions = []):
return $this->startApiCall('RawPredict', $request, $callOptions)->wait();
}

/**
* Perform a server-side streaming online prediction request for Vertex
* LLM streaming.
*
* @example samples/V1/PredictionServiceClient/server_streaming_predict.php
*
* @param StreamingPredictRequest $request A request to house fields associated with the call.
* @param array $callOptions {
* Optional.
*
* @type int $timeoutMillis
* Timeout to use for this call.
* }
*
* @return ServerStream
*
* @throws ApiException Thrown if the API call fails.
*/
public function serverStreamingPredict(StreamingPredictRequest $request, array $callOptions = []): ServerStream
{
return $this->startApiCall('ServerStreamingPredict', $request, $callOptions);
}

/**
* Gets information about a location.
*
Expand Down
69 changes: 69 additions & 0 deletions AiPlatform/src/V1/Gapic/PredictionServiceGapicClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
use Google\Cloud\AIPlatform\V1\PredictRequest;
use Google\Cloud\AIPlatform\V1\PredictResponse;
use Google\Cloud\AIPlatform\V1\RawPredictRequest;
use Google\Cloud\AIPlatform\V1\StreamingPredictRequest;
use Google\Cloud\AIPlatform\V1\StreamingPredictResponse;
use Google\Cloud\AIPlatform\V1\Tensor;
use Google\Cloud\Iam\V1\GetIamPolicyRequest;
use Google\Cloud\Iam\V1\GetPolicyOptions;
use Google\Cloud\Iam\V1\Policy;
Expand Down Expand Up @@ -609,6 +612,72 @@ public function rawPredict($endpoint, array $optionalArgs = [])
)->wait();
}

/**
* Perform a server-side streaming online prediction request for Vertex
* LLM streaming.
*
* Sample code:
* ```
* $predictionServiceClient = new PredictionServiceClient();
* try {
* $formattedEndpoint = $predictionServiceClient->endpointName('[PROJECT]', '[LOCATION]', '[ENDPOINT]');
* // Read all responses until the stream is complete
* $stream = $predictionServiceClient->serverStreamingPredict($formattedEndpoint);
* foreach ($stream->readAll() as $element) {
* // doSomethingWith($element);
* }
* } finally {
* $predictionServiceClient->close();
* }
* ```
*
* @param string $endpoint Required. The name of the Endpoint requested to serve the prediction.
* Format:
* `projects/{project}/locations/{location}/endpoints/{endpoint}`
* @param array $optionalArgs {
* Optional.
*
* @type Tensor[] $inputs
* The prediction input.
* @type Tensor $parameters
* The parameters that govern the prediction.
* @type int $timeoutMillis
* Timeout to use for this call.
* }
*
* @return \Google\ApiCore\ServerStream
*
* @throws ApiException if the remote call fails
*/
public function serverStreamingPredict($endpoint, array $optionalArgs = [])
{
$request = new StreamingPredictRequest();
$requestParamHeaders = [];
$request->setEndpoint($endpoint);
$requestParamHeaders['endpoint'] = $endpoint;
if (isset($optionalArgs['inputs'])) {
$request->setInputs($optionalArgs['inputs']);
}

if (isset($optionalArgs['parameters'])) {
$request->setParameters($optionalArgs['parameters']);
}

$requestParams = new RequestParamsHeaderDescriptor(
$requestParamHeaders
);
$optionalArgs['headers'] = isset($optionalArgs['headers'])
? array_merge($requestParams->getHeader(), $optionalArgs['headers'])
: $requestParams->getHeader();
return $this->startCall(
'ServerStreamingPredict',
StreamingPredictResponse::class,
$optionalArgs,
$request,
Call::SERVER_STREAMING_CALL
);
}

/**
* Gets information about a location.
*
Expand Down
48 changes: 48 additions & 0 deletions AiPlatform/src/V1/PredictResponse.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading