wppaste
WordPress

PromptBuilder

Since
0.1.0
Source
wp-includes/php-ai-client/src/Builders/PromptBuilder.php:50
Fluent builder for constructing AI prompts.

Description

This class provides a fluent interface for building prompts with various content types and model configurations. It automatically infers model requirements based on the features used in the prompt.

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 · 8

$registryWordPress\AiClient\Providers\ProviderRegistryprivate
$messagesWordPress\AiClient\Builders\list<Message>protected
$modelWordPress\AiClient\Providers\Models\Contracts\ModelInterface|nullprotected
$modelPreferenceKeysWordPress\AiClient\Builders\list<string>protected
$providerIdOrClassNamestring|nullprotected
$modelConfigWordPress\AiClient\Providers\Models\DTO\ModelConfigprotected
$requestOptionsWordPress\AiClient\Providers\Http\DTO\RequestOptions|nullprotected
$eventDispatcherWordPress\AiClientDependencies\Psr\EventDispatcher\EventDispatcherInterface|nullprivate

Methods · 72

Source code

class PromptBuilder{    /**     * @var ProviderRegistry The provider registry for finding suitable models.     */    private ProviderRegistry $registry;    /**     * @var list<Message> The messages in the conversation.     */    protected array $messages = [];    /**     * @var ModelInterface|null The model to use for generation.     */    protected ?ModelInterface $model = null;    /**     * @var list<string> Ordered list of preference keys to check when selecting a model.     */    protected array $modelPreferenceKeys = [];    /**     * @var string|null The provider ID or class name.     */    protected ?string $providerIdOrClassName = null;    /**     * @var ModelConfig The model configuration.     */    protected ModelConfig $modelConfig;    /**     * @var RequestOptions|null The request options for HTTP transport.     */    protected ?RequestOptions $requestOptions = null;    /**     * @var EventDispatcherInterface|null The event dispatcher for prompt lifecycle events.     */    private ?EventDispatcherInterface $eventDispatcher = null;    // phpcs:disable Generic.Files.LineLength.TooLong    /**     * Constructor.     *     * @since 0.1.0     *     * @param ProviderRegistry $registry The provider registry for finding suitable models.     * @param Prompt $prompt Optional initial prompt content.     * @param EventDispatcherInterface|null $eventDispatcher Optional event dispatcher for lifecycle events.     */    // phpcs:enable Generic.Files.LineLength.TooLong    public function __construct(ProviderRegistry $registry, $prompt = null, ?EventDispatcherInterface $eventDispatcher = null)    {        $this->registry = $registry;        $this->modelConfig = new ModelConfig();        $this->eventDispatcher = $eventDispatcher;        if ($prompt === null) {            return;        }        // Check if it's a list of Messages - set as messages        if ($this->isMessagesList($prompt)) {            $this->messages = $prompt;            return;        }        // Parse it as a user message        $userMessage = $this->parseMessage($prompt, MessageRoleEnum::user());        $this->messages[] = $userMessage;    }    /**     * Creates a deep clone of this builder.     *     * Clones all mutable state including messages, model configuration, and request options.     * Service objects (registry, model, event dispatcher) are intentionally NOT cloned     * as they are shared dependencies.     *     * @since 0.4.2     */    public function __clone()    {        // Deep clone messages array (Message has __clone)        $clonedMessages = [];        foreach ($this->messages as $message) {            $clonedMessages[] = clone $message;        }        $this->messages = $clonedMessages;        // Clone model config (ModelConfig has __clone)

Changelog

Introduced in 0.1.0. Unchanged from 7.0.4 through 7.1.0.

  1. 7.0.4
  2. 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/Builders/PromptBuilder.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.