wppaste
WordPress

GenerateTextApiBasedProviderAvailability

Since
0.1.0
Source
wp-includes/php-ai-client/src/Providers/ApiBasedImplementation/GenerateTextApiBasedProviderAvailability.php:23
Class to check availability for an API-based provider via a test request to the endpoint to generate text.

Description

This class should be used for cloud-based providers that do not offer a model listing endpoint, but do offer a text generation endpoint which requires authentication. A minimal request to this endpoint is used to determine if the provider is properly configured with valid credentials.

Compatibility

WordPress
since 0.1.0
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 2 of the 5 tracked releases, added in 0.1.0.

Properties · 1

$modelWordPress\AiClient\Providers\ApiBasedImplementation\ModelInterface&TextGenerationModelInterfaceprivate

Methods · 2

Source code

class GenerateTextApiBasedProviderAvailability implements ProviderAvailabilityInterface{    /**     * @var ModelInterface&TextGenerationModelInterface The model to use for checking availability.     */    private ModelInterface $model;    /**     * Constructor.     *     * @since 0.1.0     *     * @param ModelInterface $model The model to use for checking availability.     */    public function __construct(ModelInterface $model)    {        if (!$model instanceof TextGenerationModelInterface) {            throw new Exception('The model class to check provider availability must implement TextGenerationModelInterface.');        }        $this->model = $model;    }    /**     * {@inheritDoc}     *     * @since 0.1.0     */    public function isConfigured(): bool    {        // Set config to use as few resources as possible for the test.        $modelConfig = ModelConfig::fromArray([ModelConfig::KEY_MAX_TOKENS => 1]);        $this->model->setConfig($modelConfig);        try {            // Attempt to generate text to check if the provider is available.            $this->model->generateTextResult([new Message(MessageRoleEnum::user(), [new MessagePart('a')])]);            return \true;        } catch (Exception $e) {            // If an exception occurs, the provider is not available.            return \false;        }    }}

Changelog

Introduced in 0.1.0. Unchanged from 7.0.4 through 7.1.0.

  1. 7.0.4
  2. 7.1.0

Signature, return type and hooks compared across 2 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/php-ai-client/src/Providers/ApiBasedImplementation/GenerateTextApiBasedProviderAvailability.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.