PHP code example of alex-tikhomirova / ai-client

1. Go to this page and download the library: Download alex-tikhomirova/ai-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alex-tikhomirova / ai-client example snippets




use AlexTikhomirova\Ai\AiClient;
use AlexTikhomirova\Ai\Transport\GuzzleTransport;

$client = new AiClient(new GuzzleTransport('sk-...'));

$text = $client->textFromPayload([
    'model' => 'gpt-4o-mini',
    'input' => 'Write a short product description.',
]);



use AlexTikhomirova\Ai\AiClient;
use AlexTikhomirova\Ai\Transport\GuzzleTransport;

$transport = new GuzzleTransport('sk-...');
$client = new AiClient($transport);



$text = $client->textFromPayload([
    'model' => 'gpt-4o-mini',
    'input' => 'Write a short product description.',
]);



$data = $client->jsonFromPayload([
    'model' => 'gpt-4o-mini',
    'input' => 'Return JSON with fields title and description.',
    'text' => [
        'format' => [
            'type' => 'json_object',
        ],
    ],
]);



use AlexTikhomirova\Ai\Prompt\UniversalPrompt;

$prompt = new UniversalPrompt(
    'gpt-4o-mini',
    'You normalize product data and return strict JSON.',
    [
        'raw_description' => 'Small kettle, black, 1.2kg',
        'raw_attributes' => [
            'Weight: 1.2 kg',
            'Color: black',
            'Has light: yes',
        ],
    ],
    'json',
    0.2
);

$result = $client->jsonFromPrompt($prompt);



$response = $client->respondPayload([
    'model' => 'gpt-4o-mini',
    'input' => 'Hello',
]);

$raw = $response->toArray();



$text = $client->textFromPayload([
    'model' => 'gpt-4o-mini',
    'input' => 'Return one short sentence.',
]);



$data = $client->jsonFromPayload([
    'model' => 'gpt-4o-mini',
    'input' => 'Return JSON with keys name and price.',
    'text' => [
        'format' => [
            'type' => 'json_object',
        ],
    ],
]);



$response = $client->respondPrompt($prompt);



$response = $client->respondPayload([
    'model' => 'gpt-4o-mini',
    'input' => 'Return JSON with title.',
    'text' => [
        'format' => [
            'type' => 'json_object',
        ],
    ],
]);

$id = $response->getId();
$model = $response->getModel();
$data = $response->getJson();
$usage = $response->getUsage();



use AlexTikhomirova\Ai\Prompt\PromptInterface;

final class MyPrompt implements PromptInterface
{
    public function toPayload(): array
    {
        return [
            'model' => 'gpt-4o-mini',
            'input' => 'Hello',
        ];
    }

    public function outputType(): string
    {
        return 'text';
    }

    public function responseClass(): ?string
    {
        return null;
    }
}
bash
php /app/external/vendor/phpunit/phpunit/phpunit --bootstrap tests/bootstrap.php --no-configuration /app/external/packages/ai-client/tests