webfiori / ai
Provider-agnostic AI library for PHP. Supports chat completions, embeddings, image generation, tool calling, and streaming across multiple providers.
0.2.0
2026-07-15 21:26 UTC
Requires
- php: >=8.1
- ext-curl: *
- ext-json: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.92
- phpunit/phpunit: ^10.0
README
A provider-agnostic AI library for PHP. Supports chat completions, embeddings, image generation, tool calling, and streaming across multiple providers (OpenAI, Google, Anthropic, AWS Bedrock).
Key Features
- Provider-Agnostic — Common interface across OpenAI, Google, Anthropic, and AWS Bedrock
- Chat Completions — Send messages and receive AI-generated responses
- Streaming — Token-by-token streaming via Server-Sent Events
- Embeddings — Generate vector embeddings for semantic search
- Image Generation — Generate images from text prompts
- Tool/Function Calling — Define tools the AI can invoke during conversation
- Conversation Management — Built-in conversation history with swappable storage
- Enterprise Ready — Retry logic, rate limiting, caching, health checks, metrics, audit logging
Supported PHP Versions
| Build Status |
|---|
Installation
composer require webfiori/ai
Quick Start
<?php use WebFiori\Ai\Provider\OpenAI\OpenAIClient; use WebFiori\Ai\Message; $client = new OpenAIClient([ 'api_key' => 'sk-...', 'model' => 'gpt-4o', ]); $response = $client->chat([ new Message('system', 'You are a helpful assistant.'), new Message('user', 'What is PHP?'), ]); echo $response->getMessage()->getContent();
Streaming
$client->streamChat( messages: [ new Message('user', 'Write a story about PHP'), ], onToken: function (string $token) { echo $token; flush(); }, );
Multiple Providers
use WebFiori\Ai\Provider\Google\GoogleClient; $client = new GoogleClient([ 'api_key' => 'your-gemini-api-key', 'model' => 'gemini-2.5-flash', ]); $response = $client->chat([ new Message('user', 'What is PHP?'), ]);
Documentation
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
See CHANGELOG.md for a list of changes and version history.