Models: which Workers AI model writes, does short tasks, reads and makes images. */ final class ModelsScreen { public static function render(): string { if (get_account_id() === '' || get_api_key() === '') { return Ui::card(__('Models', 'aipcf-ai-provider-for-cloudflare'), Ui::notice(__('Connect to Cloudflare first, then choose the models here.', 'aipcf-ai-provider-for-cloudflare'), 'info') . Ui::actions([['label' => __('Connect', 'aipcf-ai-provider-for-cloudflare'), 'url' => SettingsPage::settingsUrl('connection'), 'kind' => 'primary']])); } return self::text() . self::images(); } /** * The fallback when no model is chosen: a wp-config.php or environment value, else * Cloudflare's general-purpose default. */ public static function fallbackModel(): string { if (defined('CLOUDFLARE_WORKERS_AI_DEFAULT_MODEL') && is_string(\CLOUDFLARE_WORKERS_AI_DEFAULT_MODEL) && \CLOUDFLARE_WORKERS_AI_DEFAULT_MODEL !== '') { return \CLOUDFLARE_WORKERS_AI_DEFAULT_MODEL; } $env = getenv('CLOUDFLARE_WORKERS_AI_DEFAULT_MODEL'); return is_string($env) && $env !== '' ? $env : '@cf/meta/llama-4-scout-17b-16e-instruct'; } private static function text(): string { $s = get_plugin_settings(); $current = (string) $s['preferred_model']; $pick = (string) Setup::DEFAULTS['preferred_model']; $live = fetch_available_models(); $models = $live !== [] ? $live : static_model_catalog(); if ($current !== '' && !in_array($current, array_column($models, 'id'), true)) { // Chosen in code (wp-config.php) or no longer listed: still shown, so it is not lost. array_unshift($models, ['id' => $current, 'name' => $current]); } $groups = []; foreach ($models as $m) { $groups[SettingsPage::modelGroupLabel((string) $m['id'])][(string) $m['id']] = format_model_label($m, (string) $m['id'] === $pick); } ksort($groups); $fallback = self::fallbackModel(); $help = $current === $pick ? __('Recommended: tested on live sites.', 'aipcf-ai-provider-for-cloudflare') : ($current === '' /* translators: %s: model name */ ? sprintf(__('Nothing chosen, so %s is used.', 'aipcf-ai-provider-for-cloudflare'), pretty_model_name($fallback)) : __('The list marks the recommended model.', 'aipcf-ai-provider-for-cloudflare')); $body = Ui::facts(array_map(static fn(array $r): array => [$r[0], $r[1], $r[2]], ModelMap::rows()), __('Who does what now', 'aipcf-ai-provider-for-cloudflare')); $body .= Ui::row( __('Writing', 'aipcf-ai-provider-for-cloudflare'), /* translators: %s: model name */ Ui::selectGroups('preferred_model', $groups, $current, 'aipcf_preferred_model', sprintf(__('Cloudflare\'s default (%s)', 'aipcf-ai-provider-for-cloudflare'), pretty_model_name($fallback))), ['for' => 'aipcf_preferred_model', 'help' => $help, 'more' => self::listNote($live !== [], count($models))] ); $light = (string) ($s['light_model'] ?? ''); $lightOptions = ['' => __('Off: use the writing model', 'aipcf-ai-provider-for-cloudflare')]; foreach (FreeTierSettings::textModels() as $m) { if (empty($m['vision'])) { $lightOptions[(string) $m['id']] = format_model_label($m, (string) $m['id'] === (string) Setup::DEFAULTS['light_model']); } } $body .= Ui::row( __('Short tasks', 'aipcf-ai-provider-for-cloudflare'), Ui::select('light_model', $lightOptions, $light, 'aipcf-light-model'), ['for' => 'aipcf-light-model', 'help' => __('Titles, excerpts and meta descriptions go to a smaller model that costs far fewer neurons.', 'aipcf-ai-provider-for-cloudflare')] ); $tasks = is_array($s['light_tasks'] ?? null) ? array_map('strval', $s['light_tasks']) : TaskContext::DEFAULT_LIGHT_TASKS; $all = array_unique(array_merge(TaskContext::DEFAULT_LIGHT_TASKS, ['summarization', 'content-resizing', 'suggest-reply', 'editorial-notes', 'content-translation'])); $choices = []; foreach ($all as $slug) { $choices[$slug] = ['label' => task_label($slug)]; } $body .= Ui::moreSettings(Ui::row( __('Short tasks are', 'aipcf-ai-provider-for-cloudflare'), Ui::checks('light_tasks', $choices, $tasks, __('Tasks that use the short-task model', 'aipcf-ai-provider-for-cloudflare'), 'aipcf-light-tasks'), ['for' => 'aipcf-light-tasks'] )); return Ui::card(__('Text', 'aipcf-ai-provider-for-cloudflare'), $body, [ 'text' => __('Which models write for you. The table shows the result of the choices below.', 'aipcf-ai-provider-for-cloudflare'), ]); } /** * Where the model list comes from, and a way to read it again. */ private static function listNote(bool $live, int $count): string { if (!$live) { return '
' . esc_html__('This is the built-in list. Test the connection to load the models on your account.', 'aipcf-ai-provider-for-cloudflare') . '
'; } $at = catalog_fetched_at(); return '' . esc_html($at > 0 /* translators: 1: number of models, 2: time since, e.g. "5 mins" */ ? sprintf(_n('%1$d model from your account, read %2$s ago.', '%1$d models from your account, read %2$s ago.', $count, 'aipcf-ai-provider-for-cloudflare'), $count, human_time_diff($at)) /* translators: %d: number of models */ : sprintf(_n('%d model from your account.', '%d models from your account.', $count, 'aipcf-ai-provider-for-cloudflare'), $count)) . ' ' . esc_html__('The neurons per 1,000 tokens beside each name come from your account\'s price list.', 'aipcf-ai-provider-for-cloudflare') . '
' . Ui::actions([[ 'label' => __('Read the list again', 'aipcf-ai-provider-for-cloudflare'), 'url' => wp_nonce_url(admin_url('admin-post.php?action=aipcf_refresh_models'), 'aipcf_refresh_models'), 'kind' => 'link', 'busy' => __('Reading...', 'aipcf-ai-provider-for-cloudflare'), ]]); } private static function images(): string { $s = get_plugin_settings(); $visions = array_values(array_filter(FreeTierSettings::textModels(), static fn($m): bool => !empty($m['vision']))); $auto = (string) (($visions[0]['id'] ?? '') ?: '@cf/meta/llama-3.2-11b-vision-instruct'); /* translators: %s: model name */ $visionOptions = ['' => sprintf(__('Automatic (%s)', 'aipcf-ai-provider-for-cloudflare'), pretty_model_name($auto))]; foreach ($visions as $m) { $visionOptions[(string) $m['id']] = pretty_model_name((string) $m['id']); } $imageOptions = []; foreach (FreeTierSettings::imageModels() as $m) { $imageOptions[(string) $m['id']] = (string) $m['name']; } $body = Ui::row( __('Reading images', 'aipcf-ai-provider-for-cloudflare'), Ui::select('vision_model', $visionOptions, (string) ($s['vision_model'] ?? ''), 'aipcf-vision-model'), ['for' => 'aipcf-vision-model', 'help' => __('Writes alt text and image descriptions. Text models cannot see images.', 'aipcf-ai-provider-for-cloudflare')] ); $body .= Ui::row( __('Making images', 'aipcf-ai-provider-for-cloudflare'), Ui::select('image_model', $imageOptions, (string) ($s['image_model'] ?? '@cf/black-forest-labs/flux-1-schnell'), 'aipcf-image-model'), ['for' => 'aipcf-image-model', 'help' => __('About 170 neurons per 1024 x 1024 image.', 'aipcf-ai-provider-for-cloudflare'), 'more' => self::imageNote()] ); return Ui::card(__('Images and translation', 'aipcf-ai-provider-for-cloudflare'), $body, [ 'text' => __('Requests with an image never go to the writing model.', 'aipcf-ai-provider-for-cloudflare'), ]); } private static function imageNote(): string { return '' . esc_html__('The AI plugin asks your writing model for an image prompt, sends it to the image model, then writes alt text with the image-reading model. The free daily allowance covers about 55 images.', 'aipcf-ai-provider-for-cloudflare') . '
' . '' . esc_html(sprintf( /* translators: %s: list of languages */ __('Translation uses IndicTrans2 for %s, and M2M100 for any other language.', 'aipcf-ai-provider-for-cloudflare'), implode(', ', array_map('ucfirst', CloudflareWorkersAiTranslationModel::indicLanguages())) )) . '
'; } }