|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Cortex\ModelInfo\Providers; |
| 6 | + |
| 7 | +use SensitiveParameter; |
| 8 | +use Cortex\ModelInfo\Data\ModelInfo; |
| 9 | +use Psr\Http\Client\ClientInterface; |
| 10 | +use Cortex\ModelInfo\Enums\ModelType; |
| 11 | +use Cortex\ModelInfo\Enums\ModelFeature; |
| 12 | +use Cortex\ModelInfo\Enums\ModelProvider; |
| 13 | +use Cortex\ModelInfo\Contracts\ModelInfoProvider; |
| 14 | +use Cortex\ModelInfo\Providers\Concerns\ChecksSupport; |
| 15 | +use Cortex\ModelInfo\Providers\Concerns\MakesRequests; |
| 16 | + |
| 17 | +/** |
| 18 | + * @phpstan-type XAILanguageModelResponse array{id: string, fingerprint: string, created: int, object: string, owned_by: string, version: string, input_modalities: array<array-key, string>, output_modalities: array<array-key, string>, prompt_text_token_price: int, cached_prompt_text_token_price: int, prompt_image_token_price: int, completion_text_token_price: int, aliases: array<array-key, string>} |
| 19 | + */ |
| 20 | +class XAIModelInfoProvider implements ModelInfoProvider |
| 21 | +{ |
| 22 | + use ChecksSupport; |
| 23 | + use MakesRequests; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + #[SensitiveParameter] |
| 27 | + protected ?string $apiKey = null, |
| 28 | + ?ClientInterface $httpClient = null, |
| 29 | + ) { |
| 30 | + $this->httpClient = $httpClient ?? self::discoverHttpClientOrFail(); |
| 31 | + } |
| 32 | + |
| 33 | + public function supportedModelProviders(): array |
| 34 | + { |
| 35 | + return [ |
| 36 | + ModelProvider::XAI, |
| 37 | + ]; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @throws \Cortex\ModelInfo\Exceptions\ModelInfoException |
| 42 | + * |
| 43 | + * @return array<array-key, \Cortex\ModelInfo\Data\ModelInfo> |
| 44 | + */ |
| 45 | + public function getModels(ModelProvider $modelProvider): array |
| 46 | + { |
| 47 | + $this->checkSupportOrFail($modelProvider); |
| 48 | + |
| 49 | + // Only supports chat models at the moment |
| 50 | + $body = $this->getLanguageModelsResponse(); |
| 51 | + |
| 52 | + return array_values(array_map( |
| 53 | + fn(array $model): ModelInfo => self::mapModelInfo($model), |
| 54 | + $body['models'], |
| 55 | + )); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @throws \Cortex\ModelInfo\Exceptions\ModelInfoException |
| 60 | + */ |
| 61 | + public function getModelInfo(ModelProvider $modelProvider, string $model): ModelInfo |
| 62 | + { |
| 63 | + $this->checkSupportOrFail($modelProvider); |
| 64 | + |
| 65 | + return self::mapModelInfo( |
| 66 | + $this->getLanguageModelResponse($model), |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @param XAILanguageModelResponse $body |
| 72 | + */ |
| 73 | + protected static function mapModelInfo(array $body): ModelInfo |
| 74 | + { |
| 75 | + return new ModelInfo( |
| 76 | + name: $body['id'], |
| 77 | + provider: ModelProvider::XAI, |
| 78 | + type: self::getModelType($body), |
| 79 | + maxInputTokens: self::getMaxInputTokens($body), |
| 80 | + maxOutputTokens: null, |
| 81 | + inputCostPerToken: self::getCostPerToken($body['prompt_text_token_price']), |
| 82 | + outputCostPerToken: self::getCostPerToken($body['completion_text_token_price']), |
| 83 | + features: self::getFeatures($body), |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @param XAILanguageModelResponse $body |
| 89 | + * |
| 90 | + * @return array<int, \Cortex\ModelInfo\Enums\ModelFeature> |
| 91 | + */ |
| 92 | + protected static function getFeatures(array $body): array |
| 93 | + { |
| 94 | + $features = [ |
| 95 | + ModelFeature::JsonOutput, |
| 96 | + ModelFeature::ToolCalling, |
| 97 | + ModelFeature::ToolChoice, |
| 98 | + ModelFeature::StructuredOutput, |
| 99 | + ]; |
| 100 | + |
| 101 | + if (in_array('image', $body['input_modalities'], true)) { |
| 102 | + $features[] = ModelFeature::Vision; |
| 103 | + } |
| 104 | + |
| 105 | + return $features; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param XAILanguageModelResponse $body |
| 110 | + */ |
| 111 | + protected static function getModelType(array $body): ModelType |
| 112 | + { |
| 113 | + if (in_array('text', $body['input_modalities'], true)) { |
| 114 | + return ModelType::Chat; |
| 115 | + } |
| 116 | + |
| 117 | + return ModelType::Unknown; |
| 118 | + } |
| 119 | + |
| 120 | + protected static function getCostPerToken(int $pricePerMillionTokens): float |
| 121 | + { |
| 122 | + // Convert from cents per million tokens to dollars per token |
| 123 | + return ($pricePerMillionTokens / 100.0) / 1_000_000; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @param XAILanguageModelResponse $body |
| 128 | + */ |
| 129 | + protected static function getMaxInputTokens(array $body): ?int |
| 130 | + { |
| 131 | + return str_starts_with($body['id'], 'grok-2-vision') |
| 132 | + ? 32768 |
| 133 | + : 131072; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @throws \Cortex\ModelInfo\Exceptions\ModelInfoException |
| 138 | + * |
| 139 | + * @return array{models: array<array-key, XAILanguageModelResponse>} |
| 140 | + */ |
| 141 | + protected function getLanguageModelsResponse(): array |
| 142 | + { |
| 143 | + $request = self::discoverHttpRequestFactoryOrFail() |
| 144 | + ->createRequest('GET', 'https://api.x.ai/v1/language-models') |
| 145 | + ->withHeader('Authorization', 'Bearer ' . $this->apiKey); |
| 146 | + |
| 147 | + // @phpstan-ignore return.type |
| 148 | + return $this->getJsonResponse($request); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * @throws \Cortex\ModelInfo\Exceptions\ModelInfoException |
| 153 | + * |
| 154 | + * @return XAILanguageModelResponse |
| 155 | + */ |
| 156 | + protected function getLanguageModelResponse(string $id): array |
| 157 | + { |
| 158 | + $request = self::discoverHttpRequestFactoryOrFail() |
| 159 | + ->createRequest('GET', 'https://api.x.ai/v1/language-models/' . $id) |
| 160 | + ->withHeader('Authorization', 'Bearer ' . $this->apiKey); |
| 161 | + |
| 162 | + // @phpstan-ignore return.type |
| 163 | + return $this->getJsonResponse($request); |
| 164 | + } |
| 165 | +} |
0 commit comments