You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By default, the SDK automatically chooses a suitable model based on the prompt's requirements (e.g., text vs. image) and the configured providers on the site. This makes your plugin **provider-agnostic**, allowing it to work on any site regardless of which AI provider the admin has configured.
134
+
135
+
### Using Model Preferences
136
+
137
+
If you prefer specific models for better performance or capabilities, use `using_model_preference()`. The SDK will try to use the first available model from your list. If none are available (e.g., provider not configured), it falls back to automatic selection.
138
+
139
+
Pass preferences as an array of `[ provider_id, model_id ]` to ensure the correct provider is targeted.
63
140
64
141
```php
65
142
use WordPress\AI_Client\AI_Client;
66
143
67
-
$text = AI_Client::prompt( 'Write a 2-verse poem about PHP.' )
68
-
->using_system_instruction( 'You are a famous poet from the 17th century.' )
69
-
->using_temperature( 0.8 )
144
+
$summary = AI_Client::prompt( 'Summarize the history of the printing press.' )
145
+
->using_temperature( 0.1 )
146
+
->using_model_preference(
147
+
array( 'anthropic', 'claude-sonnet-4-5' ),
148
+
array( 'google', 'gemini-3-pro-preview' ),
149
+
array( 'openai', 'gpt-5.1' )
150
+
)
70
151
->generate_text();
71
152
```
72
153
73
-
### Text generation with multiple candidates using any compatible model
154
+
### Using a Specific Model
155
+
156
+
Enforcing a single specific model using `using_model()` restricts your feature to sites that have that specific provider configured. For most scenarios, this is unnecessarily opinionated. Only use this approach if you really only want to offer the feature in combination with that model.
74
157
75
158
```php
76
159
use WordPress\AI_Client\AI_Client;
160
+
use WordPress\AiClient\ProviderImplementations\Anthropic\AnthropicProvider as Anthropic;
77
161
78
-
$texts = AI_Client::prompt( 'Write a 2-verse poem about PHP.' )
79
-
->generate_texts( 4 );
162
+
$text = AI_Client::prompt( 'Explain quantum computing in simple terms.' )
Before actually sending an AI prompt and getting a response, always check if the prompt is supported before execution.
170
+
171
+
This is always recommended, but especially crucial if you require the use of a specific model.
83
172
84
173
```php
85
174
use WordPress\AI_Client\AI_Client;
86
175
87
-
$imageFile = AI_Client::prompt( 'Generate an illustration of the PHP elephant in the Caribbean sea.' )
88
-
->generate_image();
176
+
$prompt = AI_Client::prompt( 'Explain quantum computing in simple terms.' )
177
+
->using_temperature( 0.2 );
178
+
179
+
if ( $prompt->is_supported_for_text_generation() ) {
180
+
// Safe to generate.
181
+
$text = $prompt->generate_text();
182
+
} else {
183
+
// Fallback: Hide feature or show setup instructions.
184
+
}
89
185
```
90
186
91
-
See the [`Prompt_Builder` class](https://github.com/WordPress/wp-ai-client/blob/trunk/includes/Builders/Prompt_Builder.php) and its public methods for all the ways you can configure the prompt.
187
+
The above condition will only evaluate to `true` if the site has one or more providers configured with models that support text generation including a temperature configuration.
188
+
189
+
Generally, using `is_supported_for_text_generation()` (or `is_supported_for_image_generation()`, etc.) ensures you only expose AI features that can actually run on the current site configuration.
190
+
191
+
## Error Handling
192
+
193
+
The SDK offers two ways to handle errors.
194
+
195
+
### 1. Exception Based
196
+
197
+
`AI_Client::prompt()` throws exceptions on failure.
`AI_Client::prompt_with_wp_error()` returns a `WP_Error` object on failure.
212
+
213
+
> **Note:** This error handling mechanism is experimental. We are gathering feedback to decide whether the SDK should primarily focus on exceptions or `WP_Error` objects.
This library is a WordPress-specific wrapper around the [PHP AI Client](https://github.com/WordPress/php-ai-client).
229
+
230
+
***`WordPress\AI_Client\AI_Client`**: The main entry point.
231
+
***`WordPress\AI_Client\Builders\Prompt_Builder`**: A fluent builder for constructing AI requests. It maps WordPress-style `snake_case` methods to the underlying SDK's `camelCase` methods.
232
+
***`WordPress\AI_Client\Builders\Prompt_Builder_With_WP_Error`**: A wrapper around `Prompt_Builder` that catches exceptions and returns `WP_Error` objects.
94
233
95
234
## Further reading
96
235
236
+
See the [`Prompt_Builder` class](https://github.com/WordPress/wp-ai-client/blob/trunk/includes/Builders/Prompt_Builder.php) and its public methods for all the ways you can configure the prompt.
237
+
97
238
See the [contributing documentation](./CONTRIBUTING.md) for more information on how to get involved.
0 commit comments