[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/php-ai-client/src/Providers/ApiBasedImplementation/ -> AbstractApiBasedModel.php (source)

   1  <?php
   2  
   3  declare (strict_types=1);
   4  namespace WordPress\AiClient\Providers\ApiBasedImplementation;
   5  
   6  use WordPress\AiClient\Providers\ApiBasedImplementation\Contracts\ApiBasedModelInterface;
   7  use WordPress\AiClient\Providers\DTO\ProviderMetadata;
   8  use WordPress\AiClient\Providers\Http\Contracts\WithHttpTransporterInterface;
   9  use WordPress\AiClient\Providers\Http\Contracts\WithRequestAuthenticationInterface;
  10  use WordPress\AiClient\Providers\Http\DTO\RequestOptions;
  11  use WordPress\AiClient\Providers\Http\Traits\WithHttpTransporterTrait;
  12  use WordPress\AiClient\Providers\Http\Traits\WithRequestAuthenticationTrait;
  13  use WordPress\AiClient\Providers\Models\DTO\ModelConfig;
  14  use WordPress\AiClient\Providers\Models\DTO\ModelMetadata;
  15  use WordPress\AiClient\Tools\DTO\FunctionDeclaration;
  16  /**
  17   * Base class for an API-based model for a provider.
  18   *
  19   * While this class contains no abstract methods, it is still abstract to ensure that each model class can actually
  20   * perform generative AI tasks by implementing the corresponding interfaces.
  21   *
  22   * @since 0.1.0
  23   */
  24  abstract class AbstractApiBasedModel implements ApiBasedModelInterface, WithHttpTransporterInterface, WithRequestAuthenticationInterface
  25  {
  26      use WithHttpTransporterTrait;
  27      use WithRequestAuthenticationTrait;
  28      /**
  29       * @var ModelMetadata The metadata for the model.
  30       */
  31      private ModelMetadata $metadata;
  32      /**
  33       * @var ProviderMetadata The metadata for the model's provider.
  34       */
  35      private ProviderMetadata $providerMetadata;
  36      /**
  37       * @var ModelConfig The configuration for the model.
  38       */
  39      private ModelConfig $config;
  40      /**
  41       * @var RequestOptions|null The request options for HTTP transport.
  42       */
  43      private ?RequestOptions $requestOptions = null;
  44      /**
  45       * Constructor.
  46       *
  47       * @since 0.1.0
  48       *
  49       * @param ModelMetadata $metadata The metadata for the model.
  50       * @param ProviderMetadata $providerMetadata The metadata for the model's provider.
  51       */
  52      public function __construct(ModelMetadata $metadata, ProviderMetadata $providerMetadata)
  53      {
  54          $this->metadata = $metadata;
  55          $this->providerMetadata = $providerMetadata;
  56          $this->config = ModelConfig::fromArray([]);
  57      }
  58      /**
  59       * {@inheritDoc}
  60       *
  61       * @since 0.1.0
  62       */
  63      final public function metadata(): ModelMetadata
  64      {
  65          return $this->metadata;
  66      }
  67      /**
  68       * {@inheritDoc}
  69       *
  70       * @since 0.1.0
  71       */
  72      final public function providerMetadata(): ProviderMetadata
  73      {
  74          return $this->providerMetadata;
  75      }
  76      /**
  77       * {@inheritDoc}
  78       *
  79       * @since 0.1.0
  80       */
  81      final public function setConfig(ModelConfig $config): void
  82      {
  83          $this->config = $config;
  84      }
  85      /**
  86       * {@inheritDoc}
  87       *
  88       * @since 0.1.0
  89       */
  90      final public function getConfig(): ModelConfig
  91      {
  92          return $this->config;
  93      }
  94      /**
  95       * {@inheritDoc}
  96       *
  97       * @since 0.3.0
  98       */
  99      final public function setRequestOptions(RequestOptions $requestOptions): void
 100      {
 101          $this->requestOptions = $requestOptions;
 102      }
 103      /**
 104       * {@inheritDoc}
 105       *
 106       * @since 0.3.0
 107       */
 108      final public function getRequestOptions(): ?RequestOptions
 109      {
 110          return $this->requestOptions;
 111      }
 112      /**
 113       * Prepares a function parameter schema for a provider request.
 114       *
 115       * Returns the original parameters by default. Providers must call this hook
 116       * when building their tool payload. Overrides can read the declaration annotations
 117       * and return an adjusted schema without changing the original declaration.
 118       * Providers still choose the API fields and encode the request.
 119       *
 120       * @since 1.5.1
 121       *
 122       * @param FunctionDeclaration $functionDeclaration The original function declaration.
 123       * @return array<string, mixed>|null The parameter schema to use in this request.
 124       */
 125      protected function prepareFunctionParametersForRequest(FunctionDeclaration $functionDeclaration): ?array
 126      {
 127          return $functionDeclaration->getParameters();
 128      }
 129  }


Generated : Mon Nov 2 08:20:36 2026 Cross-referenced by PHPXref