Download the PHP package ez-php/ai without Composer
On this page you can find all versions of the php package ez-php/ai. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package ai
ez-php/ai
Multi-provider AI client for ez-php. Supports chat completions, streaming, tool calling, and embeddings across OpenAI, Anthropic, Gemini, and Mistral.
Installation
Requires PHP 8.5 and ez-php/http-client.
Configuration
Register AiServiceProvider in your application and add config/ai.php:
Driver options
AI_DRIVER value |
Description |
|---|---|
openai |
OpenAI chat completions API |
anthropic |
Anthropic Messages API |
gemini |
Google Gemini generateContent API |
mistral |
Mistral AI (OpenAI-compatible) |
log |
Decorates another driver with error_log output |
null |
Returns empty responses; useful in tests |
Environment variables
| Variable | Default | Description |
|---|---|---|
AI_DRIVER |
null |
Active driver |
OPENAI_API_KEY |
— | OpenAI API key |
OPENAI_MODEL |
gpt-4o-mini |
Default OpenAI model |
OPENAI_BASE_URL |
https://api.openai.com |
Base URL (Azure / proxy support) |
ANTHROPIC_API_KEY |
— | Anthropic API key |
ANTHROPIC_MODEL |
claude-sonnet-4-6 |
Default Anthropic model |
ANTHROPIC_API_VERSION |
2023-06-01 |
anthropic-version header value |
GEMINI_API_KEY |
— | Google AI API key |
GEMINI_MODEL |
gemini-2.0-flash |
Default Gemini model |
MISTRAL_API_KEY |
— | Mistral API key |
MISTRAL_MODEL |
mistral-small-latest |
Default Mistral model |
MISTRAL_BASE_URL |
https://api.mistral.ai |
Mistral base URL |
AI_LOG_INNER_DRIVER |
openai |
Driver wrapped by the log driver |
Basic usage
Static facade
Direct driver injection
Building requests
AiRequest is immutable. All wither methods return new instances.
Messages
Streaming
Drivers that implement StreamingAiClientInterface support streaming responses.
All four production drivers (OpenAI, Anthropic, Gemini, Mistral) implement StreamingAiClientInterface.
Note: Streaming uses SSE post-hoc parsing — the full response body is buffered, then parsed line-by-line. True chunked transfer is not supported.
Tool calling
Define tools, attach them to the request, and handle tool calls in a loop.
Gemini note: Gemini does not assign separate IDs to tool calls. The function name is used as the call ID. Use the function name as
toolCallIdin tool result messages for Gemini conversations.Streaming + tool calling: Tool calls are only parsed in
complete(). Thestream()path yields text chunks only.
Embeddings
Use OpenAiEmbeddingDriver or GeminiEmbeddingDriver directly — embeddings are not wired through AiServiceProvider or the Ai facade.
| Driver | Default model | Endpoint |
|---|---|---|
OpenAiEmbeddingDriver |
text-embedding-3-small |
POST /v1/embeddings |
GeminiEmbeddingDriver |
text-embedding-004 |
POST /v1beta/models/{model}:embedContent |
Response object
Logging decorator
Wrap any driver to log every request and response via error_log:
Or construct LogDriver manually with a custom logger closure:
OpenAI-compatible proxies and Azure
OpenAiDriver and MistralDriver accept a base_url config key, making them compatible with Azure OpenAI and any OpenAI-compatible proxy:
Testing
In unit tests, inject NullDriver or use FakeTransport from ez-php/http-client:
Use Ai::resetClient() in tearDown() when tests touch the static facade to prevent state leaking between test cases.
Quality suite
Start the development shell: