adachsoft / ai-image-xai
AI image explanation library
v0.3.1
2026-11-06 21:18 UTC
Requires
- adachsoft/ai-image-contract: ^0.3
- adachsoft/collection: ^3.0
- guzzlehttp/guzzle: ^7.10
Requires (Dev)
- adachsoft/php-code-style: ^0.4.2
- friendsofphp/php-cs-fixer: ^3.94
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^13.0
- rector/rector: ^2.3
- vlucas/phpdotenv: ^5.6
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
AI image generator implementation for the X.AI (Grok) API.
Installation
composer require adachsoft/ai-image-xai
Plugin auto-discovery
This package declares the xai plugin under the adachsoft/ai-image-contract discovery key in composer.json. Libraries that implement the contract's auto-discovery can detect the plugin automatically after this package is installed with Composer.
The plugin requires the api_key parameter.
use AdachSoft\AiImageContract\Plugins\PluginParametersDto;
use AdachSoft\AiImageXai\XaiImagePlugin;
$generator = (new XaiImagePlugin())->create(
new PluginParametersDto(['api_key' => 'your-xai-api-key']),
);
Usage
use AdachSoft\AiImageContract\Collections\ImageInputVoCollection;
use AdachSoft\AiImageContract\Exceptions\AiImageExceptionInterface;
use AdachSoft\AiImageContract\Models\GenerationRequestDto;
use AdachSoft\AiImageContract\ValueObjects\ImageSizeVo;
use AdachSoft\AiImageContract\ValueObjects\PromptVo;
use AdachSoft\AiImageXai\XaiImageGeneratorFactory;
$generator = XaiImageGeneratorFactory::create('your-xai-api-key');
$request = new GenerationRequestDto(
prompt: new PromptVo('A cute dog with a lion mane'),
imageInputs: new ImageInputVoCollection([]),
size: new ImageSizeVo(1024, 1024),
options: ['model' => 'grok-imagine-image'],
);
try {
$response = $generator->generate($request);
foreach ($response->images as $image) {
if ($image->url !== null) {
echo $image->url . PHP_EOL;
}
if ($image->b64Json !== null) {
echo $image->b64Json . PHP_EOL;
}
}
} catch (AiImageExceptionInterface $exception) {
echo $exception->getMessage();
}
Exception model
This adapter follows the adachsoft/ai-image-contract exception model.
ContentModerationExceptionis used for moderation and safety filter rejections.RetryableExceptionis used for temporary provider failures such as rate limits, connectivity issues, and transient server errors.GenerationFailedExceptionis used for non-retryable generation failures.