[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> ai-client.php (source)

   1  <?php
   2  /**
   3   * WordPress AI Client API.
   4   *
   5   * @package WordPress
   6   * @subpackage AI
   7   * @since 7.0.0
   8   */
   9  
  10  use WordPress\AiClient\AiClient;
  11  use WordPress\AiClient\Files\DTO\File;
  12  use WordPress\AiClient\Messages\DTO\Message;
  13  use WordPress\AiClient\Messages\DTO\MessagePart;
  14  
  15  /**
  16   * Returns whether AI features are supported in the current environment.
  17   *
  18   * @since 7.0.0
  19   *
  20   * @return bool Whether AI features are supported.
  21   */
  22  function wp_supports_ai(): bool {
  23      // Return early if AI is disabled by the current environment.
  24      if ( defined( 'WP_AI_SUPPORT' ) && ! WP_AI_SUPPORT ) {
  25          return false;
  26      }
  27  
  28      /**
  29       * Filters whether the current request can use AI.
  30       *
  31       * This allows plugins and 3rd-party code to disable AI features on a per-request basis, or to even override explicit
  32       * preferences defined by the site owner.
  33       *
  34       * @since 7.0.0
  35       *
  36       * @param bool $is_enabled Whether AI is available. Default to true.
  37       */
  38      return (bool) apply_filters( 'wp_supports_ai', true );
  39  }
  40  
  41  /**
  42   * Creates a new AI prompt builder using the default provider registry.
  43   *
  44   * This is the main entry point for generating AI content in WordPress. It returns
  45   * a fluent builder that can be used to configure and execute AI prompts.
  46   *
  47   * The prompt can be provided as a simple string for basic text prompts, or as more
  48   * complex types for advanced use cases like multi-modal content or conversation history.
  49   *
  50   * @since 7.0.0
  51   *
  52   * @param string|MessagePart|Message|array|list<string|MessagePart|array>|list<Message>|null $prompt Optional. Initial prompt content.
  53   *                                                                                                   A string for simple text prompts,
  54   *                                                                                                   a MessagePart or Message object for
  55   *                                                                                                   structured content, an array for a
  56   *                                                                                                   message array shape, or a list of
  57   *                                                                                                   parts or messages for multi-turn
  58   *                                                                                                   conversations. Default null.
  59   * @return WP_AI_Client_Prompt_Builder The prompt builder instance.
  60   */
  61  function wp_ai_client_prompt( $prompt = null ): WP_AI_Client_Prompt_Builder {
  62      return new WP_AI_Client_Prompt_Builder( AiClient::defaultRegistry(), $prompt );
  63  }
  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  }


Generated : Sun Nov 1 08:20:35 2026 Cross-referenced by PHPXref