| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 3 declare (strict_types=1); 4 namespace WordPress\AiClient\Providers\ApiBasedImplementation; 5 6 use Exception; 7 use WordPress\AiClient\Messages\DTO\Message; 8 use WordPress\AiClient\Messages\DTO\MessagePart; 9 use WordPress\AiClient\Messages\Enums\MessageRoleEnum; 10 use WordPress\AiClient\Providers\Contracts\ProviderAvailabilityInterface; 11 use WordPress\AiClient\Providers\Models\Contracts\ModelInterface; 12 use WordPress\AiClient\Providers\Models\DTO\ModelConfig; 13 use WordPress\AiClient\Providers\Models\TextGeneration\Contracts\TextGenerationModelInterface; 14 /** 15 * Class to check availability for an API-based provider via a test request to the endpoint to generate text. 16 * 17 * This class should be used for cloud-based providers that do not offer a model listing endpoint, but do offer a 18 * text generation endpoint which requires authentication. A minimal request to this endpoint is used to determine 19 * if the provider is properly configured with valid credentials. 20 * 21 * @since 0.1.0 22 */ 23 class GenerateTextApiBasedProviderAvailability implements ProviderAvailabilityInterface 24 { 25 /** 26 * @var ModelInterface&TextGenerationModelInterface The model to use for checking availability. 27 */ 28 private ModelInterface $model; 29 /** 30 * Constructor. 31 * 32 * @since 0.1.0 33 * 34 * @param ModelInterface $model The model to use for checking availability. 35 */ 36 public function __construct(ModelInterface $model) 37 { 38 if (!$model instanceof TextGenerationModelInterface) { 39 throw new Exception('The model class to check provider availability must implement TextGenerationModelInterface.'); 40 } 41 $this->model = $model; 42 } 43 /** 44 * {@inheritDoc} 45 * 46 * @since 0.1.0 47 */ 48 public function isConfigured(): bool 49 { 50 // Set config to use as few resources as possible for the test. 51 $modelConfig = ModelConfig::fromArray([ModelConfig::KEY_MAX_TOKENS => 1]); 52 $this->model->setConfig($modelConfig); 53 try { 54 // Attempt to generate text to check if the provider is available. 55 $this->model->generateTextResult([new Message(MessageRoleEnum::user(), [new MessagePart('a')])]); 56 return \true; 57 } catch (Exception $e) { 58 // If an exception occurs, the provider is not available. 59 return \false; 60 } 61 } 62 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sat Jun 13 09:38:55 2026 | Cross-referenced by PHPXref |