PHPackages                             wordpress/php-ai-client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. wordpress/php-ai-client

ActiveLibrary[API Development](/categories/api)

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.

1.3.1(3mo ago)26550.6k↓60.8%73[31 issues](https://github.com/WordPress/php-ai-client/issues)[20 PRs](https://github.com/WordPress/php-ai-client/pulls)13GPL-2.0-or-laterPHPPHP &gt;=7.4

Since Jul 21Pushed 3w ago14 watchersCompare

[ Source](https://github.com/WordPress/php-ai-client)[ Packagist](https://packagist.org/packages/wordpress/php-ai-client)[ Docs](https://github.com/WordPress/php-ai-client)[ RSS](/packages/wordpress-php-ai-client/feed)WikiDiscussions trunk Synced 2d ago

READMEChangelog (10)Dependencies (42)Versions (31)Used By (13)

PHP AI Client
=============

[](#php-ai-client)

[*Part of the **AI Building Blocks for WordPress** initiative*](https://make.wordpress.org/ai/2025/07/17/ai-building-blocks)

A provider agnostic PHP AI client SDK to communicate with any generative AI models of various capabilities using a uniform API.

General information
-------------------

[](#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](https://make.wordpress.org/ai/) 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
------------

[](#installation)

```
composer require wordpress/php-ai-client

```

Code examples
-------------

[](#code-examples)

### Text generation using a specific model

[](#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

[](#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

[](#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

[](#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

[](#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);
```

### Text generation using max tokens

[](#text-generation-using-max-tokens)

```
use WordPress\AiClient\AiClient;

$text = AiClient::prompt('Write a 80-verse poem with long stanzas about PHP.')
    ->usingSystemInstruction('You are a famous poet from the 17th century.')
    ->usingTemperature(0.8)
    ->usingMaxTokens(8000);
    ->generateText();
```

### Image generation using any compatible model

[](#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](https://github.com/WordPress/php-ai-client/blob/trunk/src/Builders/PromptBuilder.php) and its public methods for all the ways you can configure the prompt.

**More documentation is coming soon.**

Event Dispatching
-----------------

[](#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

[](#available-events)

- `BeforeGenerateResultEvent` - Dispatched before a prompt is sent to the model
- `AfterGenerateResultEvent` - 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

[](#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

[](#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
---------------

[](#further-reading)

For more information on the requirements and guiding principles, please review:

- [Glossary](./docs/GLOSSARY.md)
- [Requirements](./docs/REQUIREMENTS.md)
- [Architecture](./docs/ARCHITECTURE.md)
- [Prepublish Checklist](./docs/PREPUBLISH-CHECKLIST.md)

See the [contributing documentation](./CONTRIBUTING.md) for more information on how to get involved.

###  Health Score

57

—

FairBetter than 98% of packages

Maintenance86

Actively maintained with recent releases

Popularity51

Moderate usage in the ecosystem

Community43

Growing community involvement

Maturity47

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~14 days

Recently: every ~6 days

Total

15

Last Release

108d ago

Major Versions

0.4.3 → 1.0.02026-02-16

### Community

Maintainers

![](https://www.gravatar.com/avatar/a60abe9a2c42fe5cf2117b88aac2eab186f0ea3c969f1f4a29d4a09d831153b8?d=identicon)[felixarntz](/maintainers/felixarntz)

![](https://www.gravatar.com/avatar/d99a52e3a323417da88045cb7754e884899c54d42ab51287a2e8e4da51b8b1b0?d=identicon)[jeffpaul](/maintainers/jeffpaul)

---

Top Contributors

[![felixarntz](https://avatars.githubusercontent.com/u/3531426?v=4)](https://github.com/felixarntz "felixarntz (343 commits)")[![mokhaled-9h](https://avatars.githubusercontent.com/u/291022198?v=4)](https://github.com/mokhaled-9h "mokhaled-9h (141 commits)")[![JasonTheAdams](https://avatars.githubusercontent.com/u/2024145?v=4)](https://github.com/JasonTheAdams "JasonTheAdams (140 commits)")[![borkweb](https://avatars.githubusercontent.com/u/430385?v=4)](https://github.com/borkweb "borkweb (43 commits)")[![saarnilauri](https://avatars.githubusercontent.com/u/36833033?v=4)](https://github.com/saarnilauri "saarnilauri (10 commits)")[![JosephGabito](https://avatars.githubusercontent.com/u/81974552?v=4)](https://github.com/JosephGabito "JosephGabito (8 commits)")[![dkotter](https://avatars.githubusercontent.com/u/916738?v=4)](https://github.com/dkotter "dkotter (6 commits)")[![aaronjorbin](https://avatars.githubusercontent.com/u/622599?v=4)](https://github.com/aaronjorbin "aaronjorbin (5 commits)")[![desrosj](https://avatars.githubusercontent.com/u/359867?v=4)](https://github.com/desrosj "desrosj (3 commits)")[![raftaar1191](https://avatars.githubusercontent.com/u/22215595?v=4)](https://github.com/raftaar1191 "raftaar1191 (3 commits)")[![johnbillion](https://avatars.githubusercontent.com/u/208434?v=4)](https://github.com/johnbillion "johnbillion (2 commits)")[![mpeshev](https://avatars.githubusercontent.com/u/328189?v=4)](https://github.com/mpeshev "mpeshev (2 commits)")[![Copilot](https://avatars.githubusercontent.com/in/1143301?v=4)](https://github.com/Copilot "Copilot (2 commits)")[![Ref34t](https://avatars.githubusercontent.com/u/20759581?v=4)](https://github.com/Ref34t "Ref34t (2 commits)")[![akkspros](https://avatars.githubusercontent.com/u/56331609?v=4)](https://github.com/akkspros "akkspros (1 commits)")[![sarthak-19](https://avatars.githubusercontent.com/u/69689387?v=4)](https://github.com/sarthak-19 "sarthak-19 (1 commits)")[![shail-mehta](https://avatars.githubusercontent.com/u/22027190?v=4)](https://github.com/shail-mehta "shail-mehta (1 commits)")[![swissspidy](https://avatars.githubusercontent.com/u/841956?v=4)](https://github.com/swissspidy "swissspidy (1 commits)")[![szepeviktor](https://avatars.githubusercontent.com/u/952007?v=4)](https://github.com/szepeviktor "szepeviktor (1 commits)")[![huzaifaalmesbah](https://avatars.githubusercontent.com/u/63150399?v=4)](https://github.com/huzaifaalmesbah "huzaifaalmesbah (1 commits)")

---

Tags

apiaillm

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/wordpress-php-ai-client/health.svg)

```
[![Health](https://phpackages.com/badges/wordpress-php-ai-client/health.svg)](https://phpackages.com/packages/wordpress-php-ai-client)
```

###  Alternatives

[telnyx/telnyx-php

Official Telnyx PHP SDK — APIs for Voice, SMS, MMS, WhatsApp, Fax, SIP Trunking, Wireless IoT, Call Control, and more. Build global communications on Telnyx's private carrier-grade network.

35789.4k2](/packages/telnyx-telnyx-php)[flow-php/flow

PHP ETL - Extract Transform Load - Data processing framework

85036.3k](/packages/flow-php-flow)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

585.6M572](/packages/shopware-core)[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.5k5.9M738](/packages/sylius-sylius)[mollie/mollie-api-php

Mollie API client library for PHP. Mollie is a European Payment Service provider and offers international payment methods such as Mastercard, VISA, American Express and PayPal, and local payment methods such as iDEAL, Bancontact, SOFORT Banking, SEPA direct debit, Belfius Direct Net, KBC Payment Button and various gift cards such as Podiumcadeaukaart and fashioncheque.

60216.0M85](/packages/mollie-mollie-api-php)[shopware/platform

The Shopware e-commerce core

3.4k1.5M3](/packages/shopware-platform)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
