A framework-agnostic PHP 8.2+ library for communicating with AI providers. Supports text generation, image generation, embeddings, speech synthesis, transcription, streaming, tool calling, and structured output — through a unified, PSR-18 compliant interface.
OpenAI · Anthropic · Google Gemini · Ollama · DeepSeek · Groq · Mistral · xAI · Perplexity
- Text generation with full conversation history support
- Image generation (DALL-E 3, Imagen)
- Text embeddings with batch support
- Text-to-Speech and Speech-to-Text (Whisper)
- Streaming via Server-Sent Events with typed chunks
- Structured output — maps LLM responses directly to PHP DTOs
- Tool calling with multi-step execution loops
- Chat history — in-memory and file-based persistence
- Middleware pipeline — Retry, Cache, Cost tracking, Rate limiting, Circuit breaker, Logging
- Observability — EventBus with pluggable observers and metrics
- FakeDriver for unit testing without API calls
composer require token27/nexus-aiRequires a PSR-18 HTTP client:
composer require guzzlehttp/guzzleuse Token27\NexusAI\NexusAI;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
$factory = new HttpFactory();
NexusAI::setHttpClient(new Client());
NexusAI::setFactories($factory, $factory);
NexusAI::configure([
'openai' => ['api_key' => $_ENV['OPENAI_API_KEY']],
]);
$response = NexusAI::using('openai', 'gpt-4o')
->withSystemPrompt('You are a helpful assistant.')
->withPrompt('What is PHP?')
->asText();
echo $response->text;Full documentation is available in the docs/ directory:
- Installation & Setup
- Configuration
- Text Generation
- Image Generation
- Embeddings
- Audio — TTS & STT
- Structured Output
- Streaming
- Tool Calling
- Chat History
- Middleware Pipeline
- Observability
- Testing
- Troubleshooting
- PHP 8.2 or higher
- A PSR-18 HTTP client
- PSR-17 HTTP factories
Please see docs/contributing.md for details.
MIT. Please see LICENSE for more information.