Skip to content

Commit d6a40f4

Browse files
committed
AI: Introduce support for generating embeddings
Update the bundled PHP AI Client from 1.3.1 to 1.5.0 and introduce `wp_ai_client_embedding()` and `WP_AI_Client_Embedding_Builder` for generating embedding vectors from text and file inputs. Accept multiple inputs as variadic arguments or a single list, and require an explicit model to keep vectors comparable. Extract shared prompt and embedding builder behavior into the internal `WP_AI_Client_Builder` base class, including fluent method proxying, `WP_Error` handling, request timeouts, and clone isolation. Add the `wp_ai_client_prevent_embedding` filter and embedding lifecycle actions, and pass the builder type to the existing timeout filter. Include tests for embedding input handling, model selection, error handling, filters, lifecycle actions, and clone isolation. Developed in: #12530. Props jason_the_adams, gziolo, dkotter, jorgefilipecosta, extrachill, ugyensupport, jeffpaul, jorbin, saadtajik, vedantere, arkaprabhachowdhury, jamesbregenzer. Fixes #65638. git-svn-id: https://develop.svn.wordpress.org/trunk@64327 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 10d3940 commit d6a40f4

27 files changed

Lines changed: 3826 additions & 642 deletions

‎src/wp-includes/ai-client.php‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
use WordPress\AiClient\AiClient;
11+
use WordPress\AiClient\Files\DTO\File;
1112
use WordPress\AiClient\Messages\DTO\Message;
1213
use WordPress\AiClient\Messages\DTO\MessagePart;
1314

@@ -60,3 +61,40 @@ function wp_supports_ai(): bool {
6061
function wp_ai_client_prompt( $prompt = null ): WP_AI_Client_Prompt_Builder {
6162
return new WP_AI_Client_Prompt_Builder( AiClient::defaultRegistry(), $prompt );
6263
}
64+
65+
/**
66+
* Creates a new AI embedding builder using the default provider registry.
67+
*
68+
* This is the main entry point for generating embeddings in WordPress. It returns
69+
* a fluent builder that can be used to configure and generate embedding vectors.
70+
*
71+
* Each input can be provided as a simple string for basic text inputs, or as more
72+
* complex types. Pass multiple arguments, or a single list of inputs, to embed
73+
* multiple inputs at once, each producing its own embedding vector.
74+
*
75+
* A model must be specified on the returned builder via `using_model()` or
76+
* `using_provider_model()`. Embedding vectors are only comparable to other
77+
* vectors produced by the same model, so no model is selected automatically.
78+
*
79+
* @since 7.2.0
80+
*
81+
* @param string|MessagePart|File|array ...$input Optional. Initial input(s) to embed.
82+
* A string for simple text inputs,
83+
* a MessagePart or File object for
84+
* structured content, or an array for
85+
* a message part array shape.
86+
* Alternatively, a single list of any
87+
* of these to embed multiple inputs.
88+
* @return WP_AI_Client_Embedding_Builder The embedding builder instance.
89+
*/
90+
function wp_ai_client_embedding( ...$input ): WP_AI_Client_Embedding_Builder {
91+
/*
92+
* A single list argument is a list of inputs. It cannot be a single input, since
93+
* the only array input is a message part array shape, which is never a list.
94+
*/
95+
if ( 1 === count( $input ) && isset( $input[0] ) && is_array( $input[0] ) && array_is_list( $input[0] ) ) {
96+
$input = $input[0];
97+
}
98+
99+
return new WP_AI_Client_Embedding_Builder( AiClient::defaultRegistry(), array() === $input ? null : $input );
100+
}

‎src/wp-includes/ai-client/adapters/class-wp-ai-client-event-dispatcher.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ public function dispatch( object $event ): object {
4848
*
4949
* - wp_ai_client_before_generate_result
5050
* - wp_ai_client_after_generate_result
51+
* - wp_ai_client_before_generate_embedding
52+
* - wp_ai_client_after_generate_embedding
5153
*
5254
* @since 7.0.0
55+
* @since 7.2.0 Added the `wp_ai_client_before_generate_embedding` and
56+
* `wp_ai_client_after_generate_embedding` action hooks.
5357
*
5458
* @param object $event The event object.
5559
*/

0 commit comments

Comments
 (0)