AiClient
- Since
- 0.1.0
- Source
wp-includes/php-ai-client/src/AiClient.php:82
Main AI Client class providing both fluent and traditional APIs for AI operations.
Description
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:
$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:
$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:
// System analyzes prompt and selects appropriate model automatically
$result = AiClient::generateTextResult('What is PHP?');
$imageResult = AiClient::generateImageResult('A sunset over mountains'); Fluent API Examples
// 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();Compatibility
- WordPress
- since 0.1.0
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in 2 of the 5 tracked releases, added in 0.1.0.
Properties · 3
$defaultRegistryWordPress\AiClient\Providers\ProviderRegistry|nullprivate static$eventDispatcherWordPress\AiClientDependencies\Psr\EventDispatcher\EventDispatcherInterface|nullprivate static$cacheWordPress\AiClientDependencies\Psr\SimpleCache\CacheInterface|nullprivate static
Methods · 16
- defaultRegistry()Gets the default provider registry instance.
- setEventDispatcher()Sets the event dispatcher for prompt lifecycle events.
- getEventDispatcher()Gets the event dispatcher for prompt lifecycle events.
- setCache()Sets the PSR-16 cache for storing and retrieving cached data.
- getCache()Gets the PSR-16 cache instance.
- isConfigured()Checks if a provider is configured and available for use.
- prompt()Creates a new prompt builder for fluent API usage.
- generateResult()Generates content using a unified API that automatically detects model capabilities.
- generateTextResult()Generates text using the traditional API approach.
- generateImageResult()Generates an image using the traditional API approach.
- convertTextToSpeechResult()Converts text to speech using the traditional API approach.
- generateSpeechResult()Generates speech using the traditional API approach.
- generateVideoResult()Generates a video using the traditional API approach.
- message()Creates a new message builder for fluent API usage.
- validateModelOrConfigParameter()Validates that parameter is ModelInterface, ModelConfig, or null.
- getConfiguredPromptBuilder()Configures PromptBuilder based on model/config parameter type.
Source code
class AiClient{ /** * @var string The version of the AI Client. */ public const VERSION = '1.3.1'; /** * @var ProviderRegistry|null The default provider registry instance. */ private static ?ProviderRegistry $defaultRegistry = null; /** * @var EventDispatcherInterface|null The event dispatcher for prompt lifecycle events. */ private static ?EventDispatcherInterface $eventDispatcher = null; /** * @var CacheInterface|null The PSR-16 cache for storing and retrieving cached data. */ private static ?CacheInterface $cache = null; /** * Gets the default provider registry instance. * * @since 0.1.0 * * @return ProviderRegistry The default provider registry. */ public static function defaultRegistry(): ProviderRegistry { if (self::$defaultRegistry === null) { self::$defaultRegistry = new ProviderRegistry(); } return self::$defaultRegistry; } /** * Sets the event dispatcher for prompt lifecycle events. * * The event dispatcher will be used to dispatch BeforeGenerateResultEvent and * AfterGenerateResultEvent during prompt generation. * * @since 0.4.0 * * @param EventDispatcherInterface|null $dispatcher The event dispatcher, or null to disable. * @return void */ public static function setEventDispatcher(?EventDispatcherInterface $dispatcher): void { self::$eventDispatcher = $dispatcher; } /** * Gets the event dispatcher for prompt lifecycle events. * * @since 0.4.0 * * @return EventDispatcherInterface|null The event dispatcher, or null if not set. */ public static function getEventDispatcher(): ?EventDispatcherInterface { return self::$eventDispatcher; } /** * 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. * * @since 0.4.0 * * @param CacheInterface|null $cache The PSR-16 cache instance, or null to disable caching. * @return void */ public static function setCache(?CacheInterface $cache): void { self::$cache = $cache; } /** * Gets the PSR-16 cache instance. * * @since 0.4.0 * * @return CacheInterface|null The cache instance, or null if not set. */Changelog
Introduced in 0.1.0. Unchanged from 7.0.4 through 7.1.0.
Signature, return type and hooks compared across 2 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/php-ai-client/src/AiClient.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.