PHP code example of devbx / ai-hub-client

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

    

devbx / ai-hub-client example snippets


use GuzzleHttp\Client;
use DevBX\AIHub\Services\OpenRouterApiClient;
use DevBX\AIHub\Http\Adapters\GuzzleHttpClientAdapter;
use DevBX\AIHub\DTO\OpenRouter\ChatCompletionRequestDTO;
use DevBX\AIHub\DTO\OpenRouter\MessageDTO;

// 1. Initialize HTTP Adapter
$guzzleClient = new Client();
$adapter = new GuzzleHttpClientAdapter($guzzleClient);

// 2. Initialize API Client
$apiKey = 'your-openrouter-api-key';
$siteUrl = 'https://your-site.com'; // Optional
$siteName = 'YourSiteName'; // Optional
$apiClient = new OpenRouterApiClient($apiKey, $adapter, $siteUrl, $siteName);

// 3. Prepare Request via DTO
$requestDto = new ChatCompletionRequestDTO();
$requestDto->model = 'google/gemini-2.5-flash';

$messageDto = new MessageDTO();
$messageDto->role = 'user';
$messageDto->content = 'Hello, AI!';
$requestDto->messages = [$messageDto];

// 4. Send Request
try {
    $response = $apiClient->chatCompletion($requestDto);
    echo $response->choices[0]->message->content;
} catch (\DevBX\AIHub\Exceptions\OpenRouterApiException $e) {
    // Handle API/Network errors
    $errors = $e->getErrors();
    foreach ($errors as $error) {
        echo "Error [{$error->code}]: {$error->message}\n";
    }
}

use GuzzleHttp\Client;
use DevBX\AIHub\Services\GrokBatchApiClient;
use DevBX\AIHub\Http\Adapters\GuzzleHttpClientAdapter;
use DevBX\AIHub\DTO\Grok\Request\AddBatchRequestsDTO;
use DevBX\AIHub\Helpers\GrokBatchRequestFactory;

$adapter = new GuzzleHttpClientAdapter(new Client());
$grokClient = new GrokBatchApiClient('your-grok-api-key', $adapter);

// Create a new batch
$batch = $grokClient->createBatch('My Translation Batch');

// Add requests to the batch
$requestsDto = new AddBatchRequestsDTO();
$requestsDto->batchRequests = [
    GrokBatchRequestFactory::createChatCompletion(
        'request-1', 
        [['role' => 'user', 'content' => 'Translate to French: Hello World']], 
        'grok-4'
    )
];

$grokClient->addBatchRequests($batch->batchId, $requestsDto);