πŸ‡ΊπŸ‡ΈUnited States @aolivera

Account created on 24 March 2021, about 4 years ago
#

Recent comments

πŸ‡ΊπŸ‡ΈUnited States aolivera

Removed Use statements no longer needed
Remove
use Google\Cloud\AIPlatform\V1\Blob;
use Google\Cloud\AIPlatform\V1\Client\PredictionServiceClient;
use Google\Cloud\AIPlatform\V1\Content;
use Google\Cloud\AIPlatform\V1\GenerateContentRequest;
use Google\Cloud\AIPlatform\V1\Part;
use Google\Cloud\AIPlatform\V1\PredictRequest;

Add Use statements needed
Add

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Google\Auth\Credentials\ServiceAccountCredentials;

Change the client to use GuzzleHtttp instead of Google Cloud
/**
* The HTTP client.
*
* (Originally this was a PredictionServiceClient; now we use a Guzzle HTTP client.)
*
* @var \GuzzleHttp\Client|null
*/
protected $client;

Change from using Prediction for client
/**
* Gets the raw chat client.
*
* This is the client for inference.
*
* @return \Google\Cloud\AIPlatform\V1\Client\PredictionServiceClient
* The Google Vertex client.
*/
public function getClient(): PredictionServiceClient {
$this->loadClient();
return $this->client;
}

To using GuzzleHttp

/**
* Gets the raw chat client.
*
* This is the client for inference.
*
* @return \GuzzleHttp\Client
* The HTTP client.
*/
public function getClient(): Client {
$this->loadClient();
return $this->client;
}

Add Helper Methods GetAccessToken and GetAuthHeaders

/**
* Retrieves an OAuth2 access token using the service account credentials.
*
* @return string
* The access token.
*
* @throws \Exception
* Thrown if unable to retrieve an access token.
*/
protected function getAccessToken(): string {
$credentialsJson = $this->loadCredentialFile();
$credentialsArray = json_decode($credentialsJson, true);
if (!$credentialsArray) {
throw new \Exception('Invalid credentials JSON.');
}
$sc = new ServiceAccountCredentials(
['https://www.googleapis.com/auth/cloud-platform'],
$credentialsArray
);
$token = $sc->fetchAuthToken();
if (!isset($token['access_token'])) {
throw new \Exception('Unable to retrieve access token.');
}
return $token['access_token'];
}

/**
* Returns the authentication headers.
*
* @return array
* An array containing the Authorization header.
*/
protected function getAuthHeaders(): array {
return [
'Authorization' => 'Bearer ' . $this->getAccessToken(),
];
}

Create Endpoint
$endpoint = 'https://us-central1-aiplatform.googleapis.com/v1/' . $url . ':generateContent';

Production build 0.71.5 2024