[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/php-ai-client/src/ -> AiClient.php (summary)

(no description)

File Size: 513 lines (24 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

AiClient:: (22 methods):
  defaultRegistry()
  setEventDispatcher()
  getEventDispatcher()
  setCache()
  getCache()
  isConfigured()
  prompt()
  input()
  generateResult()
  generateTextResult()
  generateImageResult()
  convertTextToSpeechResult()
  generateSpeechResult()
  generateVideoResult()
  generateEmbeddingResult()
  generateEmbedding()
  generateEmbeddings()
  message()
  validateModelOrConfigParameter()
  getConfiguredPromptBuilder()
  getConfiguredEmbeddingBuilder()
  applyModelOrConfig()


Class: AiClient  - X-Ref

Main AI Client class providing both fluent and traditional APIs for AI operations.

This class serves as the primary entry point for AI operations, offering:
- Fluent API for easy-to-read chained method calls
- Traditional API for array-based configuration (WordPress style)
- Integration with provider registry for model discovery
- Support for three model specification approaches

All model requirements analysis and capability matching is handled
automatically by the PromptBuilder, which provides intelligent model
discovery based on prompt content and configuration.

## Model Specification Approaches

### 1. Specific Model Instance
Use a specific ModelInterface instance when you know exactly which model to use:
```php
$model = $registry->getProvider('openai')->getModel('gpt-4');
$result = AiClient::generateTextResult('What is PHP?', $model);
```

### 2. ModelConfig for Auto-Discovery
Use ModelConfig to specify requirements and let the system discover the best model:
```php
$config = new ModelConfig();
$config->setTemperature(0.7);
$config->setMaxTokens(150);

$result = AiClient::generateTextResult('What is PHP?', $config);
```

### 3. Automatic Discovery (Default)
Pass null or omit the parameter for intelligent model discovery based on prompt content:
```php
// System analyzes prompt and selects appropriate model automatically
$result = AiClient::generateTextResult('What is PHP?');
$imageResult = AiClient::generateImageResult('A sunset over mountains');
```

## Fluent API Examples
```php
// Fluent API with automatic model discovery
$result = AiClient::prompt('Generate an image of a sunset')
->usingTemperature(0.7)
->generateImageResult();

// Fluent API with specific model
$result = AiClient::prompt('What is PHP?')
->usingModel($specificModel)
->usingTemperature(0.5)
->generateTextResult();

// Fluent API with model configuration
$result = AiClient::prompt('Explain quantum physics')
->usingModelConfig($config)
->generateTextResult();
```

defaultRegistry()   X-Ref
Gets the default provider registry instance.

return: ProviderRegistry The default provider registry.

setEventDispatcher(?EventDispatcherInterface $dispatcher)   X-Ref
Sets the event dispatcher for prompt lifecycle events.

The event dispatcher will be used to dispatch BeforeGenerateResultEvent and
AfterGenerateResultEvent during prompt generation.

return: void
param: EventDispatcherInterface|null $dispatcher The event dispatcher, or null to disable.

getEventDispatcher()   X-Ref
Gets the event dispatcher for prompt lifecycle events.

return: EventDispatcherInterface|null The event dispatcher, or null if not set.

setCache(?CacheInterface $cache)   X-Ref
Sets the PSR-16 cache for storing and retrieving cached data.

The cache can be used to store AI responses and other data to avoid
redundant API calls and improve performance.

return: void
param: CacheInterface|null $cache The PSR-16 cache instance, or null to disable caching.

getCache()   X-Ref
Gets the PSR-16 cache instance.

return: CacheInterface|null The cache instance, or null if not set.

isConfigured($availabilityOrIdOrClassName)   X-Ref
Checks if a provider is configured and available for use.

Supports multiple input formats for developer convenience:
- ProviderAvailabilityInterface: Direct availability check
- string (provider ID): e.g., AiClient::isConfigured('openai')
- string (class name): e.g., AiClient::isConfigured(OpenAiProvider::class)

When using string input, this method leverages the ProviderRegistry's centralized
dependency management, ensuring HttpTransporter and authentication are properly
injected into availability instances.

return: bool True if the provider is configured and available, false otherwise.
param: ProviderAvailabilityInterface|string|class-string<ProviderInterface> $availabilityOrIdOrClassName

prompt($prompt = null, ?ProviderRegistry $registry = null)   X-Ref
Creates a new prompt builder for fluent API usage.

Returns a PromptBuilder instance configured with the specified or default registry.
The traditional API methods in this class delegate to PromptBuilder
for all generation logic.

return: PromptBuilder The prompt builder instance.
param: Prompt $prompt Optional initial prompt content.
param: ProviderRegistry|null $registry Optional custom registry. If null, uses default.

input($input = null, ?ProviderRegistry $registry = null)   X-Ref
Creates an embedding builder for fluent embedding generation.

Accepts a single input or a list of inputs; each input is embedded independently. Chain an
embedding generation method such as generateEmbedding() or generateEmbeddings() to produce
one vector per input.

A model must be specified on the returned builder via usingModel() or usingProviderModel().
Embedding vectors are only comparable to other vectors from the same model, so no model is
selected automatically.

return: EmbeddingBuilder The embedding builder instance.
param: EmbeddingInput|list<EmbeddingInput>|null $input Optional initial input(s) to embed.
param: ProviderRegistry|null $registry Optional custom registry. If null, uses default.

generateResult($prompt, $modelOrConfig, ?ProviderRegistry $registry = null)   X-Ref
Generates content using a unified API that automatically detects model capabilities.

When no model is provided, this method delegates to PromptBuilder for intelligent
model discovery based on prompt content and configuration. When a model is provided,
it infers the capability from the model's interfaces and delegates to the capability-based method.

return: GenerativeAiResult The generation result.
param: Prompt $prompt The prompt content.
param: ModelInterface|ModelConfig $modelOrConfig Specific model to use, or model configuration
param: ProviderRegistry|null $registry Optional custom registry. If null, uses default.

generateTextResult($prompt, $modelOrConfig = null, ?ProviderRegistry $registry = null)   X-Ref
Generates text using the traditional API approach.

return: GenerativeAiResult The generation result.
param: Prompt $prompt The prompt content.
param: ModelInterface|ModelConfig|null $modelOrConfig Optional specific model to use,
param: ProviderRegistry|null $registry Optional custom registry. If null, uses default.

generateImageResult($prompt, $modelOrConfig = null, ?ProviderRegistry $registry = null)   X-Ref
Generates an image using the traditional API approach.

return: GenerativeAiResult The generation result.
param: Prompt $prompt The prompt content.
param: ModelInterface|ModelConfig|null $modelOrConfig Optional specific model to use,
param: ProviderRegistry|null $registry Optional custom registry. If null, uses default.

convertTextToSpeechResult($prompt, $modelOrConfig = null, ?ProviderRegistry $registry = null)   X-Ref
Converts text to speech using the traditional API approach.

return: GenerativeAiResult The generation result.
param: Prompt $prompt The prompt content.
param: ModelInterface|ModelConfig|null $modelOrConfig Optional specific model to use,
param: ProviderRegistry|null $registry Optional custom registry. If null, uses default.

generateSpeechResult($prompt, $modelOrConfig = null, ?ProviderRegistry $registry = null)   X-Ref
Generates speech using the traditional API approach.

return: GenerativeAiResult The generation result.
param: Prompt $prompt The prompt content.
param: ModelInterface|ModelConfig|null $modelOrConfig Optional specific model to use,
param: ProviderRegistry|null $registry Optional custom registry. If null, uses default.

generateVideoResult($prompt, $modelOrConfig = null, ?ProviderRegistry $registry = null)   X-Ref
Generates a video using the traditional API approach.

return: GenerativeAiResult The generation result.
param: Prompt $prompt The prompt content.
param: ModelInterface|ModelConfig|null $modelOrConfig Optional specific model to use,
param: ProviderRegistry|null $registry Optional custom registry. If null, uses default.

generateEmbeddingResult($input, $model, ?ModelConfig $modelConfig = null, ?ProviderRegistry $registry = null)   X-Ref
Generates embeddings using the traditional API approach.

A model is required. Embedding vectors are only comparable to other vectors from the same
model, so no model is selected automatically.

return: EmbeddingResult The embedding result.
param: EmbeddingInput|list<EmbeddingInput> $input The input(s) to embed.
param: ModelInterface|ProviderModelTuple $model The model to use.
param: ModelConfig|null $modelConfig Optional model configuration to apply.
param: ProviderRegistry|null $registry Optional custom registry. If null, uses default.

generateEmbedding($input, $model, ?ModelConfig $modelConfig = null, ?ProviderRegistry $registry = null)   X-Ref
Generates an embedding using the traditional API approach.

A model is required. Embedding vectors are only comparable to other vectors from the same
model, so no model is selected automatically.

return: Embedding The generated embedding vector.
param: EmbeddingInput $input The input to embed.
param: ModelInterface|ProviderModelTuple $model The model to use.
param: ModelConfig|null $modelConfig Optional model configuration to apply.
param: ProviderRegistry|null $registry Optional custom registry. If null, uses default.

generateEmbeddings(array $inputs, $model, ?ModelConfig $modelConfig = null, ?ProviderRegistry $registry = null)   X-Ref
Generates embeddings for a list of inputs using the traditional API approach.

A model is required. Embedding vectors are only comparable to other vectors from the same
model, so no model is selected automatically.

return: list<Embedding> The generated embedding vectors.
param: list<EmbeddingInput> $inputs The inputs to embed.
param: ModelInterface|ProviderModelTuple $model The model to use.
param: ModelConfig|null $modelConfig Optional model configuration to apply.
param: ProviderRegistry|null $registry Optional custom registry. If null, uses default.

message(?string $text = null)   X-Ref
Creates a new message builder for fluent API usage.

This method will be implemented once MessageBuilder is available.
MessageBuilder will provide a fluent interface for constructing complex
messages with multiple parts, attachments, and metadata.

return: object MessageBuilder instance (type will be updated when MessageBuilder is available).
param: string|null $text Optional initial message text.

validateModelOrConfigParameter($modelOrConfig)   X-Ref
Validates that parameter is ModelInterface, ModelConfig, or null.

return: void
param: mixed $modelOrConfig The parameter to validate.

getConfiguredPromptBuilder($prompt, $modelOrConfig, ?ProviderRegistry $registry = null)   X-Ref
Configures PromptBuilder based on model/config parameter type.

return: PromptBuilder Configured prompt builder.
param: Prompt $prompt The prompt content.
param: ModelInterface|ModelConfig|null $modelOrConfig The model or config parameter.
param: ProviderRegistry|null $registry Optional custom registry to use.

getConfiguredEmbeddingBuilder($input, $model, ?ModelConfig $modelConfig = null, ?ProviderRegistry $registry = null)   X-Ref
Configures an EmbeddingBuilder with the required model and optional configuration.

return: EmbeddingBuilder Configured embedding builder.
param: EmbeddingInput|list<EmbeddingInput> $input The input(s) to embed.
param: mixed $model The model to use, expected to be a ModelInterface instance or a
param: ModelConfig|null $modelConfig Optional model configuration to apply.
param: ProviderRegistry|null $registry Optional custom registry to use.

applyModelOrConfig($builder, $modelOrConfig)   X-Ref
Applies a model or model configuration to a builder.

Works with any builder that exposes the shared model resolution methods
(see {@see \WordPress\AiClient\Builders\Traits\ModelResolutionTrait}).

return: T The configured builder.
param: T $builder The builder to configure.
param: ModelInterface|ModelConfig|null $modelOrConfig Specific model, model configuration,



Generated : Sun Nov 1 08:20:35 2026 Cross-referenced by PHPXref