| [ 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\OpenAiCompatibleImplementation; 5 6 use WordPress\AiClient\Providers\ApiBasedImplementation\AbstractApiBasedModelMetadataDirectory; 7 use WordPress\AiClient\Providers\Http\DTO\Request; 8 use WordPress\AiClient\Providers\Http\DTO\Response; 9 use WordPress\AiClient\Providers\Http\Enums\HttpMethodEnum; 10 use WordPress\AiClient\Providers\Http\Exception\ResponseException; 11 use WordPress\AiClient\Providers\Http\Util\ResponseUtil; 12 use WordPress\AiClient\Providers\Models\DTO\ModelMetadata; 13 /** 14 * Base class for a model metadata directory for providers that implement OpenAI's API format. 15 * 16 * This abstract class is designed to work with any AI provider that offers an OpenAI-compatible 17 * models listing endpoint, including but not limited to Anthropic, Google, and other 18 * providers that have adopted OpenAI's models API specification as a standard interface. 19 * 20 * @since 0.1.0 21 */ 22 abstract class AbstractOpenAiCompatibleModelMetadataDirectory extends AbstractApiBasedModelMetadataDirectory 23 { 24 /** 25 * {@inheritDoc} 26 * 27 * @since 0.1.0 28 */ 29 protected function sendListModelsRequest(): array 30 { 31 $httpTransporter = $this->getHttpTransporter(); 32 $request = $this->createRequest(HttpMethodEnum::GET(), 'models'); 33 $request = $this->getRequestAuthentication()->authenticateRequest($request); 34 $response = $httpTransporter->send($request); 35 $this->throwIfNotSuccessful($response); 36 $modelsMetadataList = $this->parseResponseToModelMetadataList($response); 37 $modelMetadataMap = []; 38 foreach ($modelsMetadataList as $modelMetadata) { 39 $modelMetadataMap[$modelMetadata->getId()] = $modelMetadata; 40 } 41 return $modelMetadataMap; 42 } 43 /** 44 * Creates a request object for the provider's API. 45 * 46 * @since 0.1.0 47 * 48 * @param HttpMethodEnum $method The HTTP method. 49 * @param string $path The API endpoint path, relative to the base URI. 50 * @param array<string, string|list<string>> $headers The request headers. 51 * @param string|array<string, mixed>|null $data The request data. 52 * @return Request The request object. 53 */ 54 abstract protected function createRequest(HttpMethodEnum $method, string $path, array $headers = [], $data = null): Request; 55 /** 56 * Throws an exception if the response is not successful. 57 * 58 * @since 0.1.0 59 * 60 * @param Response $response The HTTP response to check. 61 * @throws ResponseException If the response is not successful. 62 */ 63 protected function throwIfNotSuccessful(Response $response): void 64 { 65 /* 66 * While this method only calls the utility method, it's important to have it here as a protected method so 67 * that child classes can override it if needed. 68 */ 69 ResponseUtil::throwIfNotSuccessful($response); 70 } 71 /** 72 * Parses the response from the API endpoint to list models into a list of model metadata objects. 73 * 74 * @since 0.1.0 75 * 76 * @param Response $response The response from the API endpoint to list models. 77 * @return list<ModelMetadata> List of model metadata objects. 78 */ 79 abstract protected function parseResponseToModelMetadataList(Response $response): array; 80 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sat Jun 13 09:38:55 2026 | Cross-referenced by PHPXref |