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.4.0(1mo ago)26554.3k↓57.8%79[33 issues](https://github.com/WordPress/php-ai-client/issues)[24 PRs](https://github.com/WordPress/php-ai-client/pulls)14GPL-2.0-or-laterPHPPHP &gt;=7.4CI passing

Since Jul 21Pushed 3w ago15 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 2w ago

READMEChangelog (10)Dependencies (63)Versions (33)Used By (14)

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

### Embedding generation using any compatible model

[](#embedding-generation-using-any-compatible-model)

```
use WordPress\AiClient\AiClient;

$embedding = AiClient::input('PHP powers a large part of the web.')
    ->generateEmbedding();

$values = $embedding->getValues();
```

### Batch embedding generation

[](#batch-embedding-generation)

```
use WordPress\AiClient\AiClient;

$embeddings = AiClient::input([
        'PHP powers a large part of the web.',
        'WordPress makes publishing accessible.',
    ])
    ->usingProvider('openai')
    ->generateEmbeddings();
```

### Embedding generation with dimensions

[](#embedding-generation-with-dimensions)

```
use WordPress\AiClient\AiClient;

$embedding = AiClient::input('PHP powers a large part of the web.')
    ->usingDimensions(512)
    ->generateEmbedding();
```

Embedding inputs are independent [`MessagePart`](https://github.com/WordPress/php-ai-client/blob/trunk/src/Messages/DTO/MessagePart.php) values, not a conversation. `input()` accepts one input or a list of inputs. Variadic `withInput()` accepts one or more arguments, and lists can be passed using PHP's spread syntax (`withInput(...$inputs)`). Each input may be a string, `MessagePart`, `File`, or message-part array shape. Model selection accounts for their input modalities and embedding configuration.

See the [`PromptBuilder` class](https://github.com/WordPress/php-ai-client/blob/trunk/src/Builders/PromptBuilder.php) and the [`EmbeddingBuilder` class](https://github.com/WordPress/php-ai-client/blob/trunk/src/Builders/EmbeddingBuilder.php) and their public methods for all the ways you can configure generation.

**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

59

—

FairBetter than 98% of packages

Maintenance92

Actively maintained with recent releases

Popularity51

Moderate usage in the ecosystem

Community44

Growing community involvement

Maturity48

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 ~21 days

Recently: every ~34 days

Total

16

Last Release

35d 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)")[![JasonTheAdams](https://avatars.githubusercontent.com/u/2024145?v=4)](https://github.com/JasonTheAdams "JasonTheAdams (143 commits)")[![mokhaled-9h](https://avatars.githubusercontent.com/u/291022198?v=4)](https://github.com/mokhaled-9h "mokhaled-9h (141 commits)")[![borkweb](https://avatars.githubusercontent.com/u/430385?v=4)](https://github.com/borkweb "borkweb (43 commits)")[![dkotter](https://avatars.githubusercontent.com/u/916738?v=4)](https://github.com/dkotter "dkotter (21 commits)")[![chubes4](https://avatars.githubusercontent.com/u/44784595?v=4)](https://github.com/chubes4 "chubes4 (13 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)")[![aaronjorbin](https://avatars.githubusercontent.com/u/622599?v=4)](https://github.com/aaronjorbin "aaronjorbin (5 commits)")[![raftaar1191](https://avatars.githubusercontent.com/u/22215595?v=4)](https://github.com/raftaar1191 "raftaar1191 (3 commits)")[![desrosj](https://avatars.githubusercontent.com/u/359867?v=4)](https://github.com/desrosj "desrosj (3 commits)")[![mpeshev](https://avatars.githubusercontent.com/u/328189?v=4)](https://github.com/mpeshev "mpeshev (2 commits)")[![Ref34t](https://avatars.githubusercontent.com/u/20759581?v=4)](https://github.com/Ref34t "Ref34t (2 commits)")[![johnbillion](https://avatars.githubusercontent.com/u/208434?v=4)](https://github.com/johnbillion "johnbillion (2 commits)")[![Copilot](https://avatars.githubusercontent.com/in/1143301?v=4)](https://github.com/Copilot "Copilot (2 commits)")[![westonruter](https://avatars.githubusercontent.com/u/134745?v=4)](https://github.com/westonruter "westonruter (1 commits)")[![akkspros](https://avatars.githubusercontent.com/u/56331609?v=4)](https://github.com/akkspros "akkspros (1 commits)")[![chloe-pomegranate](https://avatars.githubusercontent.com/u/264397470?v=4)](https://github.com/chloe-pomegranate "chloe-pomegranate (1 commits)")[![huzaifaalmesbah](https://avatars.githubusercontent.com/u/63150399?v=4)](https://github.com/huzaifaalmesbah "huzaifaalmesbah (1 commits)")[![Jameswlepage](https://avatars.githubusercontent.com/u/36246732?v=4)](https://github.com/Jameswlepage "Jameswlepage (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

[flow-php/flow

PHP ETL - Extract Transform Load - Data processing framework

86337.5k](/packages/flow-php-flow)[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.

36826.2k2](/packages/telnyx-telnyx-php)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

595.8M674](/packages/shopware-core)[shopware/platform

The Shopware e-commerce core

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

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

8.5k6.0M778](/packages/sylius-sylius)[shopware/app-php-sdk

Shopware App SDK for PHP

15120.5k3](/packages/shopware-app-php-sdk)

PHPackages © 2026

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