wordpress / ai-provider-for-openai
AI Provider for OpenAI for the PHP AI Client SDK. Works as both a Composer package and WordPress plugin.
Package info
github.com/WordPress/ai-provider-for-openai
pkg:composer/wordpress/ai-provider-for-openai
Requires
- php: >=7.4
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: ^1.0
- phpcompatibility/php-compatibility: dev-develop
- phpstan/phpstan: ~2.1
- phpunit/phpunit: ^9.6
- slevomat/coding-standard: ^8.20
- squizlabs/php_codesniffer: ^3.7 || ^4.0
- wordpress/php-ai-client: ^1.3.1
Suggests
- wordpress/php-ai-client: Required. The core PHP AI Client SDK that this provider extends.
Provides
None
Conflicts
None
Replaces
None
README
An AI Provider for OpenAI for the PHP AI Client SDK. Works as both a Composer package and a WordPress plugin.
Requirements
- PHP 7.4 or higher
- When using with WordPress, requires WordPress 7.0 or higher
- If using an older WordPress release, the wordpress/php-ai-client package must be installed
Installation
As a Composer Package
composer require wordpress/ai-provider-for-openai
As a WordPress Plugin
- Download the plugin files
- Upload to
/wp-content/plugins/ai-provider-for-openai/ - Ensure the PHP AI Client plugin is installed and activated
- Activate the plugin through the WordPress admin
Usage
With WordPress
The provider automatically registers itself with the PHP AI Client on the init hook. Simply ensure both plugins are active and configure your API key:
// Set your OpenAI API key (or use the OPENAI_API_KEY environment variable) putenv('OPENAI_API_KEY=your-api-key'); // Use the provider $result = AiClient::prompt('Hello, world!') ->usingProvider('openai') ->generateTextResult();
As a Standalone Package
use WordPress\AiClient\AiClient; use WordPress\OpenAiAiProvider\Provider\OpenAiProvider; // Register the provider $registry = AiClient::defaultRegistry(); $registry->registerProvider(OpenAiProvider::class); // Set your API key putenv('OPENAI_API_KEY=your-api-key'); // Generate text $result = AiClient::prompt('Explain quantum computing') ->usingProvider('openai') ->generateTextResult(); echo $result->toText();
Supported Models
Available models are dynamically discovered from the OpenAI API. This includes GPT models for text generation, DALL-E and GPT Image models for image generation, and TTS models for text-to-speech. See the OpenAI documentation for the full list of available models.
Configuration
The provider uses the OPENAI_API_KEY environment variable for authentication. You can set this in your environment or via PHP:
putenv('OPENAI_API_KEY=your-api-key');
Extending OpenAI API profiles
The provider supports alternate OpenAI API dialects without requiring a fork. A custom request authentication implementation can implement OpenAiApiProfileAwareAuthenticationInterface and return an OpenAiApiProfileInterface instance. The profile can adapt operation support, endpoint URLs, requests, responses, model metadata, and model-cache isolation while the authentication object remains responsible for credentials.
The PHP AI Client currently validates replacement OpenAI authentication against its API-key DTO. Pass profile-aware authentication through OpenAiApiProfileRequestAuthenticationAdapter before registering it with setProviderRequestAuthentication(). The adapter only satisfies that registry compatibility check; request authentication and profile selection are delegated to the wrapped implementation.
WordPress integrations may also supply or replace a profile with the ai_provider_for_openai_api_profile filter. Profile request changes are applied before authentication, and response normalization is applied only after a successful HTTP response. Because a profile can change where authenticated requests are sent, only trusted code should provide one.
When no profile-aware authentication is bound and no filter supplies a profile, request and cache behavior is unchanged. The filter also runs for plain API-key authentication, so a supplied profile can route that API key to its chosen endpoint.
Profiles must support OpenAiApiOperation::LIST_MODELS: provider availability and model discovery use the model catalog, including on cache misses. Other operations (text, images, embeddings, and text-to-speech) may be rejected individually before credentials are applied or a request is sent.
Maintainers
Maintainers for this repo are David Stone (@superdav42), Felix Arntz (@felixarntz), Jason Adams (@JasonTheAdams), Jeffrey Paul (@jeffpaul), and Lauri Saarni (@saarnilauri); all can be reached using the @WordPress/ai-provider-for-openai-maintainers team.
License
GPL-2.0-or-later