Download the PHP package alex-tikhomirova/ai-client without Composer
On this page you can find all versions of the php package alex-tikhomirova/ai-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alex-tikhomirova/ai-client
More information about alex-tikhomirova/ai-client
Files in alex-tikhomirova/ai-client
Package ai-client
Short Description Простой PHP-клиент для OpenAI Responses API на базе Guzzle.
License MIT
Informations about the package ai-client
AI Client
Русская версия
Simple PHP 7.4+ client for the OpenAI Responses API.
The package provides:
- a transport layer based on
guzzlehttp/guzzle; - retry handling for retryable HTTP errors;
- a high-level
AiClient; - prompt objects via
PromptInterface; - response objects via
ResponseInterface.
Quick Start
Requirements
- PHP 7.4+
guzzlehttp/guzzle
Installation
As a local package in the current project
If the package lives inside external/packages/ai-client, add it to the root Composer project as a path repository.
In the current project setup the package is already connected through a Composer path repository and symlink.
Main classes
AlexTikhomirova\Ai\Transport\GuzzleTransport— low-level HTTP transportAlexTikhomirova\Ai\AiClient— high-level clientAlexTikhomirova\Ai\Prompt\PromptInterface— prompt contractAlexTikhomirova\Ai\Prompt\UniversalPrompt— generic prompt implementationAlexTikhomirova\Ai\Response\AiResponse— default response object
Quick start
1. Create transport and client
2. Send a raw payload and get text
3. Send a raw payload and get JSON
Using UniversalPrompt
UniversalPrompt is useful when you want to keep request building outside of the client.
AiClient methods
respondPayload(array $payload): AiResponse
Returns the default response object.
textFromPayload(array $payload): string
Returns text directly.
jsonFromPayload(array $payload): array
Returns decoded JSON directly.
respondPrompt(PromptInterface $prompt): ResponseInterface
Uses a prompt object and returns either the prompt-specific response class or the default AiResponse.
AiResponse
Default response wrapper supports:
toArray()getText()getJson()getId()getModel()getUsage()
Example:
Retry and errors
Retryable errors
GuzzleTransport throws RetryableRequestException for:
- network errors;
- HTTP
429; - HTTP
5xx.
If the API returns Retry-After, the exception stores the recommended delay in milliseconds.
Non-retryable errors
AiRequestException is used for:
- invalid payload;
- invalid JSON in the API response;
- unsupported HTTP method;
- response mapping errors.
Custom prompts
To create a custom prompt, implement PromptInterface:
Custom response classes
If a prompt needs a custom response object, return its class name from responseClass().
The class must implement AlexTikhomirova\Ai\Response\ResponseInterface and accept array $data in the constructor.
Running tests
In the current project the tests are executed from the root external environment.
Example command used in Docker:
Current package status:
- unit tests for responses, prompts, retry, exceptions, client and transport;
- all tests are green in the current Docker setup.
License
MIT