- md5
- cad2d604a7af2f37040b7b733d50ebfc
- sha1
- ae6da0de91d6cef14caf99c458ac2b460a8a8c98
- sha256
- d55208d6033034a11535f68f38bdc6b3732d3319ff1d9019d9ea58e8b75e97e7
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
<?php
declare (strict_types=1);
namespace WordPress\AiClient\Providers\OpenAiCompatibleImplementation;
use WordPress\AiClient\Providers\ApiBasedImplementation\AbstractApiBasedModelMetadataDirectory;
use WordPress\AiClient\Providers\Http\DTO\Request;
use WordPress\AiClient\Providers\Http\DTO\Response;
use WordPress\AiClient\Providers\Http\Enums\HttpMethodEnum;
use WordPress\AiClient\Providers\Http\Exception\ResponseException;
use WordPress\AiClient\Providers\Http\Util\ResponseUtil;
use WordPress\AiClient\Providers\Models\DTO\ModelMetadata;
/**
* Base class for a model metadata directory for providers that implement OpenAI's API format.
*
* This abstract class is designed to work with any AI provider that offers an OpenAI-compatible
* models listing endpoint, including but not limited to Anthropic, Google, and other
* providers that have adopted OpenAI's models API specification as a standard interface.
*
* @since 0.1.0
*/
abstract class AbstractOpenAiCompatibleModelMetadataDirectory extends AbstractApiBasedModelMetadataDirectory
{
/**
* {@inheritDoc}
*
* @since 0.1.0
*/
protected function sendListModelsRequest(): array
{
$httpTransporter = $this->getHttpTransporter();
$request = $this->createRequest(HttpMethodEnum::GET(), 'models');
$request = $this->getRequestAuthentication()->authenticateRequest($request);
$response = $httpTransporter->send($request);
$this->throwIfNotSuccessful($response);
$modelsMetadataList = $this->parseResponseToModelMetadataList($response);
$modelMetadataMap = [];
foreach ($modelsMetadataList as $modelMetadata) {
$modelMetadataMap[$modelMetadata->getId()] = $modelMetadata;
}
return $modelMetadataMap;
}
/**
* Creates a request object for the provider's API.
*
* @since 0.1.0
*
* @param HttpMethodEnum $method The HTTP method.
* @param string $path The API endpoint path, relative to the base URI.
* @param array<string, string|list<string>> $headers The request headers.
* @param string|array<string, mixed>|null $data The request data.
* @return Request The request object.
*/
abstract protected function createRequest(HttpMethodEnum $method, string $path, array $headers = [], $data = null): Request;
/**
* Throws an exception if the response is not successful.
*
* @since 0.1.0
*
* @param Response $response The HTTP response to check.
* @throws ResponseException If the response is not successful.
*/
protected function throwIfNotSuccessful(Response $response): void
{
/*
* While this method only calls the utility method, it's important to have it here as a protected method so
* that child classes can override it if needed.
*/
ResponseUtil::throwIfNotSuccessful($response);
}
/**
* Parses the response from the API endpoint to list models into a list of model metadata objects.
*
* @since 0.1.0
*
* @param Response $response The response from the API endpoint to list models.
* @return list<ModelMetadata> List of model metadata objects.
*/
abstract protected function parseResponseToModelMetadataList(Response $response): array;
}
The same content is in 40 releases, WordPress 7.0-beta1 to 7.2-beta5. See them all, or every version of this file.