[ 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: 387 lines (17 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

AiClient:: (16 methods):
  defaultRegistry()
  setEventDispatcher()
  getEventDispatcher()
  setCache()
  getCache()
  isConfigured()
  prompt()
  generateResult()
  generateTextResult()
  generateImageResult()
  convertTextToSpeechResult()
  generateSpeechResult()
  generateVideoResult()
  message()
  validateModelOrConfigParameter()
  getConfiguredPromptBuilder()


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.

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.

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.



Generated : Sat Jun 13 09:38:55 2026 Cross-referenced by PHPXref