PHP code example of latentkit / latentkit-php

1. Go to this page and download the library: Download latentkit/latentkit-php 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/ */

    

latentkit / latentkit-php example snippets




atentKit\LatentKit;

$client = new LatentKit(); // Reads LATENTKIT_API_KEY and LATENTKIT_BASE_URL.

$response = $client->chat->create(
    messages: [
        ['role' => 'user', 'content' => 'Say hello from LatentKit.'],
    ],
    maxTokens: 100,
    responseProfile: 'balanced',
);

echo $response['content'];

$client = new LatentKit(
    apiKey: $_ENV['LATENTKIT_API_KEY'],
    baseUrl: 'https://ai.latentkit.com',
    timeoutSeconds: 120,
    toolSlug: 'latentkit-wordpress',
    toolVersion: '0.1.0',
    appId: null,
);

$context = $client->me->retrieveContext();

echo $context->app?->name;
echo $context->route?->name;
echo $context->route?->modelCount;

foreach ($context->route?->models ?? [] as $model) {
    echo $model->rank . ': ' . $model->provider . ' / ' . $model->model;
}

echo $context->latestRequest?->model;

$client->me->retrieveContext();

$client->chat->create(
    messages: [['role' => 'user', 'content' => 'Hello']],
);

$client->vision->create(
    prompt: 'Describe this image.',
    imageUrl: 'https://example.com/image.png',
);

$client->embeddings->create(
    input: ['first document', 'second document'],
    dimensions: 256,
);

$client->image->generate(
    prompt: 'A clean product icon',
    size: '1024x1024',
);

$queued = $client->queue->create(
    endpoint: 'chat',
    payload: [
        'messages' => [['role' => 'user', 'content' => 'Summarize this.']],
    ],
    idempotencyKey: 'cms-job-42',
);

use LatentKit\Exceptions\ApiException;
use LatentKit\Exceptions\AuthenticationException;
use LatentKit\Exceptions\RateLimitException;
use LatentKit\Exceptions\ValidationException;

try {
    $response = $client->me->retrieve();
} catch (ApiException $error) {
    error_log(json_encode([
        'status' => $error->statusCode,
        'code' => $error->apiCode,
        'request_id' => $error->requestId,
    ]));
}

use LatentKit\LatentKit;
use LatentKit\Transport\Psr18Transport;

$transport = new Psr18Transport(
    client: $psr18Client,
    requestFactory: $requestFactory,
    streamFactory: $streamFactory,
);

$client = new LatentKit(
    apiKey: $_ENV['LATENTKIT_API_KEY'],
    transport: $transport,
);
bash
composer