adachsoft / ai-integration-xai
xAI SPI provider for adachsoft/ai-integration: tool-calling chat integration with Grok models (OpenAI-compatible endpoint).
Requires
- php: ^8.3
- adachsoft/ai-integration: ^0.7
- guzzlehttp/guzzle: ^7.0
Requires (Dev)
- adachsoft/php-code-style: ^0.4
- friendsofphp/php-cs-fixer: ^3.94
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^13.0
- rector/rector: ^2.3
- vlucas/phpdotenv: ^5.6
This package is not auto-updated.
Last update: 2026-03-28 15:00:49 UTC
README
adachsoft/ai-integration-xai is a small integration library that plugs the x.ai chat completions API into the adachsoft/ai-integration package using a tool-calling capable SPI.
It provides a tiny, focused adapter that exposes x.ai as an OpenAI-compatible tool-calling chat provider.
Features
- Minimal integration layer for x.ai chat completions
- OpenAI-compatible tool-calling SPI using
OpenAiCompatibleToolCallingChatSpi - Simple configuration via the
XaiConfigvalue object - Factory
XaiToolCallingChatSpiFactorythat wires configuration into the SPI implementation - Sensible defaults for base URI and model ID
Requirements
- PHP 8.3 or higher
adachsoft/ai-integrationpackage (for SPI interfaces and OpenAI-compatible implementation)- An active x.ai API key
Installation
Install via Composer:
composer require adachsoft/ai-integration-xai
Make sure you also have adachsoft/ai-integration installed; Composer will usually handle this automatically via dependencies.
Configuration
The library exposes a single configuration value object: AdachSoft\AiIntegrationXai\Config\XaiConfig.
use AdachSoft\AiIntegrationXai\Config\XaiConfig;
$xaiConfig = new XaiConfig(
apiKey: 'your-xai-api-key',
defaultModelId: 'grok-4', // optional, default is 'grok-4'
baseUri: 'https://api.x.ai/v1/chat/completions', // optional, default x.ai chat endpoint
providerId: 'xai', // optional, identifier used by ai-integration
);
You can keep this configuration in your dependency injection container or any other configuration layer used in your application.
Usage
The main entry point of this library is the factory AdachSoft\AiIntegrationXai\XaiToolCallingChatSpiFactory, which produces a ToolCallingChatSpiInterface compatible instance backed by x.ai.
use AdachSoft\AiIntegrationXai\Config\XaiConfig;
use AdachSoft\AiIntegrationXai\XaiToolCallingChatSpiFactory;
use AdachSoft\AiIntegration\Spi\ToolCalling\ToolCallingChatSpiInterface;
$xaiConfig = new XaiConfig(apiKey: 'your-xai-api-key');
$factory = new XaiToolCallingChatSpiFactory($xaiConfig);
/** @var ToolCallingChatSpiInterface $chatSpi */
$chatSpi = $factory->create();
// You can now pass $chatSpi to the ai-integration tooling that expects a ToolCallingChatSpiInterface
From this point on, you should use the standard APIs exposed by adachsoft/ai-integration for building prompts, registering tools, and executing tool-calling chat interactions. This library is intentionally thin and only concerns itself with wiring x.ai into that SPI.
Default Model
The XaiConfig exposes a defaultModelId property (default grok-4). Depending on your usage of adachsoft/ai-integration, you may:
- Use this value as the default model when constructing higher-level services
- Override it per-request when your domain logic requires a different model
Versioning and Changelog
This library follows semantic versioning. All notable changes are documented in CHANGELOG.md.
The first tagged version is 0.1.0, which provides the initial x.ai tool-calling integration.
Testing
This project includes unit tests and a dedicated production test for the actual x.ai integration. To run the test suite, use:
vendor/bin/phpunit
Production tests that hit the real x.ai API are grouped separately (for example under a production group); consult the tests under tests/Production before running them against live services.
License
This library is open-sourced under the MIT license.