getApiKey(); [$accountId, $token] = self::parseCredential($rawKey); // Nothing was split off the key; reuse the original authentication object. if ($token === trim($rawKey)) { return [$accountId, $auth]; } return [$accountId, new ApiKeyRequestAuthentication($token)]; } /** * {@inheritDoc} * * @since 1.0.0 */ protected static function createModel( ModelMetadata $modelMetadata, ProviderMetadata $providerMetadata ): ModelInterface { $capabilities = $modelMetadata->getSupportedCapabilities(); foreach ($capabilities as $capability) { if ($capability->isTextGeneration()) { return new CloudflareTextGenerationModel($modelMetadata, $providerMetadata); } if ($capability->isImageGeneration()) { return new CloudflareImageGenerationModel($modelMetadata, $providerMetadata); } } throw new RuntimeException( 'Unsupported model capabilities: ' . implode(', ', $capabilities) ); } /** * {@inheritDoc} * * @since 1.0.0 */ protected static function createProviderMetadata(): ProviderMetadata { $providerMetadataArgs = [ 'cloudflare', 'Cloudflare Workers AI', ProviderTypeEnum::cloud(), 'https://dash.cloudflare.com/?to=/:account/ai/workers-ai', RequestAuthenticationMethod::apiKey() ]; // Provider description support was added in 1.2.0. if (version_compare(AiClient::VERSION, '1.2.0', '>=')) { // For WordPress, we should translate the description. if (function_exists('__')) { // phpcs:ignore Generic.Files.LineLength.TooLong $providerMetadataArgs[] = __('Text and image generation with Cloudflare Workers AI (Llama, Mistral, Qwen, FLUX). Enter your API key as account_id:token (your Cloudflare account ID and API token joined by a colon).', 'ai-provider-for-cloudflare'); } else { // phpcs:ignore Generic.Files.LineLength.TooLong -- Description literal, kept in sync with the translated string above. $providerMetadataArgs[] = 'Text and image generation with Cloudflare Workers AI (Llama, Mistral, Qwen, FLUX). Enter your API key as account_id:token (your Cloudflare account ID and API token joined by a colon).'; } } // Provider logoPath support was added in 1.3.0. if (version_compare(AiClient::VERSION, '1.3.0', '>=')) { $providerMetadataArgs[] = dirname(__DIR__, 2) . '/assets/images/cloudflare.svg'; } return new ProviderMetadata(...$providerMetadataArgs); } /** * {@inheritDoc} * * @since 1.0.0 */ protected static function createProviderAvailability(): ProviderAvailabilityInterface { // Check valid API access by attempting to list models. return new ListModelsApiBasedProviderAvailability( static::modelMetadataDirectory() ); } /** * {@inheritDoc} * * @since 1.0.0 */ protected static function createModelMetadataDirectory(): ModelMetadataDirectoryInterface { return new CloudflareModelMetadataDirectory(); } }