WP_AI_Client_Prompt_Builder
- Since
- 7.0.0
- Source
wp-includes/ai-client/class-wp-ai-client-prompt-builder.php:107
Description
This class provides a fluent interface for building prompts with various content types and model configurations. It wraps the PHP AI Client SDK's PromptBuilder and adds WordPress-specific behavior including WP_Error handling instead of exceptions, snake_case method naming, and integration with the Abilities API.
Only the generating methods will return a WP_Error, to not break the fluent interface. As soon as any exception is caught in a chain of method calls, the returned instance will be in an error state, and all subsequent method calls will be no-ops that just return the same error state instance. Only when a generating method is called, the WP_Error will be returned.
Compatibility
- WordPress
- since 7.0.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 7.0.0.
Hooks and filters fired · 2
Every hook that fires from inside WP_AI_Client_Prompt_Builder, in the order it appears in the class, grouped by the method that fires it.
Properties · 4
$builderWordPress\AiClient\Builders\PromptBuilderprivate- Wrapped prompt builder instance from the PHP AI Client SDK.
$errorWP_Error|nullprivate- WordPress error instance, if any error occurred during method calls.
$generating_methodsarray<string,private static- List of methods that generate a result from the prompt.
$support_check_methodsarray<string,private static- List of methods that check whether the prompt is supported.
Methods · 8
- __construct()Constructor.
- using_abilities()Registers WordPress abilities as function declarations for the AI model.
- __call()Magic method to proxy snake_case method calls to their PHP AI Client camelCase counterparts.
- exception_to_wp_error()Converts an exception into a WP_Error with a structured error code and message.
- is_support_check_method()Checks if a method name is a support check method (is_supported*).
- is_generating_method()Checks if a method name is a generating method (generate_*, convert_text_to_speech*).
- get_builder_callable()Retrieves a callable for a given PHP AI Client SDK prompt builder method name.
- snake_to_camel_case()Converts snake_case to camelCase.
Source code
class WP_AI_Client_Prompt_Builder { /** * Wrapped prompt builder instance from the PHP AI Client SDK. * * @since 7.0.0 * @var PromptBuilder */ private PromptBuilder $builder; /** * WordPress error instance, if any error occurred during method calls. * * @since 7.0.0 * @var WP_Error|null */ private ?WP_Error $error = null; /** * List of methods that generate a result from the prompt. * * Structured as a map for faster lookups. * * @since 7.0.0 * @var array<string, bool> */ private static array $generating_methods = array( 'generate_result' => true, 'generate_text_result' => true, 'generate_image_result' => true, 'generate_speech_result' => true, 'convert_text_to_speech_result' => true, 'generate_video_result' => true, 'generate_text' => true, 'generate_texts' => true, 'generate_image' => true, 'generate_images' => true, 'convert_text_to_speech' => true, 'convert_text_to_speeches' => true, 'generate_speech' => true, 'generate_speeches' => true, 'generate_video' => true, 'generate_videos' => true, ); /** * List of methods that check whether the prompt is supported. * * Structured as a map for faster lookups. * * @since 7.0.0 * @var array<string, bool> */ private static array $support_check_methods = array( 'is_supported' => true, 'is_supported_for_text_generation' => true, 'is_supported_for_image_generation' => true, 'is_supported_for_text_to_speech_conversion' => true, 'is_supported_for_video_generation' => true, 'is_supported_for_speech_generation' => true, 'is_supported_for_music_generation' => true, 'is_supported_for_embedding_generation' => true, ); /** * Constructor. * * @since 7.0.0 * * @param ProviderRegistry $registry The provider registry for finding suitable models. * @param Prompt $prompt Optional. Initial prompt content. * A string for simple text prompts, * a MessagePart or Message object for * structured content, an array for a * message array shape, or a list of * parts or messages for multi-turn * conversations. Default null. */ public function __construct( ProviderRegistry $registry, $prompt = null ) { try {Changelog
Introduced in 7.0.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/ai-client/class-wp-ai-client-prompt-builder.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.