WordPress\AiClient\Providers\ApiBasedImplementation
GenerateTextApiBasedProviderAvailability{}└─ ProviderAvailabilityInterface
Class to check availability for an API-based provider via a test request to the endpoint to generate text.
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.
No Hooks.
Usage
$GenerateTextApiBasedProviderAvailability = new GenerateTextApiBasedProviderAvailability(); // use class methods
Methods
- public __construct(ModelInterface $model)
- public isConfigured()
Changelog
| Since 0.1.0 | Introduced. |
GenerateTextApiBasedProviderAvailability{} GenerateTextApiBasedProviderAvailability{} code WP 7.0
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;
}
}
}