Skip to content
Brok UI

Loading…

No results

AI Top-K

Open source

A next-token probability debugger listing candidate tokens with proportional bars and percentages.

Version
v1.0.5
Stability
stable
License
MIT
Related
AI Chain of Thought
AI Function Call
AI Prompt

Preview

Next token
␣the
42%
␣a
21%
␣an
12%
␣this
8%
␣that
5%
previews.components.ai-top-k.default.blade.php Blade
<x-ui.ai.top-k class="mx-auto w-full max-w-sm" />

Installation

terminal
php artisan ui:add ai-top-k

Registry contract

php artisan ui:add ai-top-k writes only the files below. The CLI validates each file hash before writing and asks before it replaces a local change, unless you pass --force.

  • blade resources/views/components/ui/ai/top-k.blade.php
Registry dependencies
None — installs on its own.
Packages
composer: jml/brok:^0.2

Use with AI

A brief for your coding agent: install command, usage, props, guidance and the rules. Copy it, or open a prompt about this component in an assistant.

ai-top-k.md
# Brok UI: AI Top-K (`ai-top-k`)

A next-token probability debugger listing candidate tokens with proportional bars and percentages.

Brok UI is a Laravel Blade component registry. Installed components are plain Blade files the app owns.

## Install

```bash
php artisan ui:add ai-top-k
```

## Usage

```blade
<x-ui.ai.top-k class="mx-auto w-full max-w-sm" />
```

## Props

- `tokens` (array, default `[]`)

## Use when

- Use to expose AI-specific prompts, states, or reasoning metadata when those details help users supervise or understand an AI workflow.
- Inspecting the candidate next tokens a model considered, with their probabilities.
- Building a playground or eval tool where sampling has to be explained.

## Avoid when

- Do not surface internal AI mechanics when they add confusion or implementation detail without user value.
- Charting general data; chart and partition-bar are the data-viz primitives.
- Showing a single probability, where a progress bar reads more simply.

## Anti-patterns

- Decorative AI chrome
- Exposing internal mechanics without user value

## Rules

- Use the `<brok:ai-top-k>` tag (or `<x-ui.ai-top-k>`) in Blade; do not rewrite the component.
- Prefer the documented props and variants over utility-class overrides; when a utility must win, use the `!` important modifier.
- Use semantic design tokens (`bg-primary`, `text-muted-foreground`), never raw colour utilities.
- Keep the `data-slot` attributes; they are the styling and test hooks.

## Links

- Docs: https://brokui.dev/docs/components/ai-top-k
- Registry JSON (files, props, contract): https://brokui.dev/r/open/ai-top-k.json

Working in Claude Code, Cursor or Codex? Give the agent the whole registry through the MCP server or the Brok UI skill.

API

manifest knowledge + registry-derived coverage

Props

Props accepted by this component: name, type, default value and description.
Prop Type Default Description
tokens array []

Slots

  • default —

Data slots

Stable hooks for CSS overrides and browser tests.

ai-top-k ai-top-k-title

Behavior

  • Each token renders a bar proportional to its probability beside a percentage, in the order given.
  • Bars animate to length once and stay static under prefers-reduced-motion.
  • Declares registry capability flags: a11y, responsive, rtl, darkMode, localized, reducedMotion.

Guidance

AI-assisted interaction

Help users prompt, inspect, or supervise an AI-assisted operation.

Use when

  • Use to expose AI-specific prompts, states, or reasoning metadata when those details help users supervise or understand an AI workflow.
  • Inspecting the candidate next tokens a model considered, with their probabilities.
  • Building a playground or eval tool where sampling has to be explained.

Avoid when

  • Do not surface internal AI mechanics when they add confusion or implementation detail without user value.
  • Charting general data; chart and partition-bar are the data-viz primitives.
  • Showing a single probability, where a progress bar reads more simply.

Use instead

  • Deterministic conventional workflow

Anti-patterns

  • Decorative AI chrome
  • Exposing internal mechanics without user value
Anatomy
root ai-top-k-title
Theming hooks
ai-top-k ai-top-k-title

Accessibility

WCAG 2.2 AA Keyboard focus-visible RTL-ready Localized labels Dark mode
Keyboard
No component-owned keyboard interaction; native element behavior applies.
Focus
none
  • The list is a labelled group, so its purpose is announced before its rows.
  • Every probability is stated as a percentage in text; the bar length is a second reading, not the only one.
  • Semantic HTML and a stable data-slot attribute for styling and scripting hooks.
  • Focus-visible rings use the ring token, so keyboard focus is always visible.
  • Disabled and invalid states are conveyed to assistive tech, not by color alone.
  • Targets WCAG 2.2 AA; verify contrast in light, dark, admin and customer surfaces in the preview.
  • Labels go through __() and layout uses logical properties (ms-*, text-start), so it mirrors under dir="rtl" — flip the preview to RTL to confirm.
  • Dark mode uses the same semantic tokens under the dark class; high contrast follows forced-color system tokens.

Livewire

Safe

Livewire can update this component through forwarded wire:* attributes.

Source

The exact, editable file ui:add writes into your app. Previews render this same code; there are no preview-only components.

resources/views/components/ui/ai/top-k.blade.php Blade
@props([
    'tokens' => [],
])

@php
    $tokens = is_array($tokens) && count($tokens)
        ? $tokens
        : [
            ['token' => ' the', 'prob' => 0.42],
            ['token' => ' a', 'prob' => 0.21],
            ['token' => ' an', 'prob' => 0.12],
            ['token' => ' this', 'prob' => 0.08],
            ['token' => ' that', 'prob' => 0.05],
        ];

    // Highest probability for subtle emphasis of the front-runner.
    $maxProb = 0.0;
    foreach ($tokens as $t) {
        $maxProb = max($maxProb, (float) ($t['prob'] ?? 0));
    }
@endphp

{{--
    AI top-k — a next-token probability debugger. Each candidate token renders as
    a labelled row: the raw token in a monospace chip (literal spaces shown as ␣
    so leading whitespace is visible), a proportional bar fed by the inline --prob
    CSS var (width = calc(--prob * 100%)), and the percentage in tabular figures.
    Modelled as a description list (dt = token, dd = probability) for a11y.
--}}
<div
    data-slot="ai-top-k"
    role="group"
    aria-label="{{ __('Next token probabilities') }}"
    {{ $attributes->merge(['class' => 'rounded-md border border-border bg-card p-3 text-foreground']) }}
>
    <div data-slot="ai-top-k-title" class="mb-2 text-sm font-medium">{{ __('Next token') }}</div>

    <dl class="space-y-2">
        @foreach ($tokens as $t)
            @php
                $raw = (string) ($t['token'] ?? '');
                $prob = max(0.0, min(1.0, (float) ($t['prob'] ?? 0)));
                $pct = round($prob * 100, 1);
                $display = str_replace(' ', '␣', $raw);
                $isTop = $prob >= $maxProb && $maxProb > 0;
            @endphp
            <div class="flex items-center gap-2">
                <dt class="w-20 shrink-0">
                    <code class="inline-block max-w-full truncate rounded bg-muted px-2 text-xs font-mono {{ $isTop ? 'text-foreground' : 'text-muted-foreground' }}">{{ $display }}</code>
                </dt>
                <dd class="flex flex-1 items-center gap-2">
                    <div class="h-2 flex-1 overflow-hidden rounded-full bg-muted" aria-hidden="true">
                        <div
                            @class([
                                'h-full rounded-full bg-primary transition-[width] duration-500 motion-reduce:transition-none',
                                'opacity-100' => $isTop,
                                'opacity-70' => ! $isTop,
                            ])
                            style="--prob:{{ round($prob, 4) }};width:calc(var(--prob) * 100%)"
                        ></div>
                    </div>
                    <span class="w-12 shrink-0 text-end text-xs tabular-nums {{ $isTop ? 'font-medium text-foreground' : 'text-muted-foreground' }}">{{ $pct }}%</span>
                </dd>
            </div>
        @endforeach
    </dl>
</div>

Ownership & lifecycle

Owner, release state, review evidence and adoption for this item.
Owner
Platform UI (@JoshJML)
Current version
1.0.5
Status
Stable
License
open
Accessibility reviewed
No review date recorded
Last breaking change
No date recorded
Deprecation
Not deprecated
Contract
v6
Foundation
≥ 1.0.0