devbx / ai-hub-client
There is no license information available for the latest version (v1.0.0) of this package.
API client for Grok and OpenRouter AI services. Independent Composer library.
v1.0.0
2026-04-04 11:27 UTC
Requires (Dev)
- guzzlehttp/guzzle: ^7.5
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
Suggests
- guzzlehttp/guzzle: Required if you want to use the Guzzle HTTP adapter out-of-the-box instead of providing your own custom adapter.
README
API client for Grok and OpenRouter AI services. Independent Composer library built with robust DTOs for seamless integration.
Installation
You can install the package via composer:
composer require devbx/ai-hub-client
Note: This library requires a PSR-18 compliant HTTP client. If you don't have one, you can install Guzzle:
composer require guzzlehttp/guzzle
Usage
OpenRouter API
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"; } }
Grok Batch API
The library includes convenient factories to build batch requests for Grok.
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);
Testing
composer test
License
The MIT License (MIT).