wordpress / php-ai-client
A provider agnostic PHP AI client SDK to communicate with any generative AI models of various capabilities using a uniform API.
Installs: 10 200
Dependents: 1
Suggesters: 0
Security: 0
Stars: 204
Watchers: 12
Forks: 41
Open Issues: 23
pkg:composer/wordpress/php-ai-client
Requires
- php: >=7.4
- ext-json: *
- php-http/discovery: ^1.0
- php-http/httplug: ^2.0
- php-http/message-factory: ^1.0
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0 || ^2.0
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: ^1.0
- guzzlehttp/psr7: ^1.0 || ^2.0
- php-http/curl-client: ^2.0
- php-http/mock-client: ^1.0
- phpcompatibility/php-compatibility: dev-develop
- phpstan/phpstan: ~2.1
- phpunit/phpunit: ^9.5 || ^10.0
- slevomat/coding-standard: ^8.20
- squizlabs/php_codesniffer: ^3.7
- dev-trunk
- 0.3.1
- 0.3.0
- 0.2.1
- 0.2.0
- 0.1.0
- dev-add/provider-authentication-awareness
- dev-fix/incorrect-anthropic-model-option
- dev-event-dispatching
- dev-fix/provider-class-name-return-type
- dev-enhance/error-message-extractor
- dev-add/embeddings-support
- dev-feature/embeddings-support
- dev-typescript-generation-prompt
- dev-ai-client
- dev-test/install-provider-sdks
This package is auto-updated.
Last update: 2025-12-13 00:26:37 UTC
README
Part of the AI Building Blocks for WordPress initiative
A provider agnostic PHP AI client SDK to communicate with any generative AI models of various capabilities using a uniform API.
General information
This project is a PHP SDK, which can be installed as a Composer package. In WordPress, it could be bundled in plugins. It is however not a plugin itself.
While this project is stewarded by WordPress AI Team members and contributors, it is technically WordPress agnostic. The gap the project addresses is relevant for not only the WordPress ecosystem, but the overall PHP ecosystem, so any PHP project could benefit from it. There is also no technical reason to scope it to WordPress, as communicating with AI models and their providers is independent of WordPress's built-in APIs and paradigms.
Installation
composer require wordpress/php-ai-client
Code examples
Text generation using a specific model
use WordPress\AiClient\AiClient; $text = AiClient::prompt('Write a 2-verse poem about PHP.') ->usingModel(Google::model('gemini-2.5-flash')) ->generateText();
Text generation using any compatible model from a specific provider
use WordPress\AiClient\AiClient; $text = AiClient::prompt('Write a 2-verse poem about PHP.') ->usingProvider('openai') ->generateText();
Text generation using any compatible model
use WordPress\AiClient\AiClient; $text = AiClient::prompt('Write a 2-verse poem about PHP.') ->generateText();
Text generation with additional parameters
use WordPress\AiClient\AiClient; $text = AiClient::prompt('Write a 2-verse poem about PHP.') ->usingSystemInstruction('You are a famous poet from the 17th century.') ->usingTemperature(0.8) ->generateText();
Text generation with multiple candidates using any compatible model
use WordPress\AiClient\AiClient; $texts = AiClient::prompt('Write a 2-verse poem about PHP.') ->generateTexts(4);
Image generation using any compatible model
use WordPress\AiClient\AiClient; $imageFile = AiClient::prompt('Generate an illustration of the PHP elephant in the Caribbean sea.') ->generateImage();
See the PromptBuilder class and its public methods for all the ways you can configure the prompt.
More documentation is coming soon.
Event Dispatching
The AI Client supports PSR-14 event dispatching for prompt lifecycle events. This allows you to hook into the generation process for logging, monitoring, or other integrations.
Available Events
BeforeGenerateResultEvent- Dispatched before a prompt is sent to the modelAfterGenerateResultEvent- Dispatched after a result is received from the model
Important: Event listeners should not return a value, as they will be ignored. In order to modify data that is passed with the event object, you need to rely on setters on the event object. Any event data for which there are no setters on the event object is meant to be immutable or, in other words, read-only for the event listener.
Connecting Your Event Dispatcher
To enable event dispatching, pass any PSR-14 compatible EventDispatcherInterface to the client:
use WordPress\AiClient\AiClient; // Set your PSR-14 event dispatcher AiClient::setEventDispatcher($yourEventDispatcher); // Events will now be dispatched during generation $text = AiClient::prompt('Hello, world!') ->generateText();
Example: Logging Events
use WordPress\AiClient\Events\BeforeGenerateResultEvent; use WordPress\AiClient\Events\AfterGenerateResultEvent; // In your event listener/subscriber class AiEventListener { public function onBeforeGenerate(BeforeGenerateResultEvent $event): void { $model = $event->getModel(); $messages = $event->getMessages(); $capability = $event->getCapability(); // Log, monitor, or perform other actions } public function onAfterGenerate(AfterGenerateResultEvent $event): void { $result = $event->getResult(); // Log the result, track usage, etc. } }
Further reading
For more information on the requirements and guiding principles, please review:
See the contributing documentation for more information on how to get involved.