mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
feat(admin): decision model grid in router panel
This commit is contained in:
@@ -138,6 +138,8 @@ function configWarnings(path: string, body: Obj) {
|
||||
|
||||
const ROUTER_MIN_REQUESTS = 120
|
||||
const SHADOW_CLASSIFIER = 'v3'
|
||||
const DECISION_PROVIDER = 'gx10'
|
||||
const DECISION_OUTCOMES = ['agree', 'override', 'lowconf', 'timeout', 'error', 'breaker', 'cache']
|
||||
|
||||
/** Scales counts by `factor`; with `total`, the rounding remainder lands on the largest row so the rows still sum to it. */
|
||||
function scaleCounts(rows: Obj[], factor: number, total?: number) {
|
||||
@@ -149,6 +151,7 @@ function scaleCounts(rows: Obj[], factor: number, total?: number) {
|
||||
|
||||
function routerEscalations(path: string, body: Obj) {
|
||||
if (path !== '/admin/api/overview' && path !== '/admin/api/catalog') return
|
||||
let decided = false
|
||||
eachRouter(body, (r) => {
|
||||
const tiers: string[] = (r.tiers ?? []).map((t: Obj) => t.name)
|
||||
if (tiers.length < 2) return
|
||||
@@ -161,6 +164,12 @@ function routerEscalations(path: string, body: Obj) {
|
||||
stats.requests = target
|
||||
for (const list of [stats.tiers, stats.reasons]) scaleCounts(list ?? [], factor, target)
|
||||
scaleCounts(stats.outcomes ?? [], factor)
|
||||
if (!decided) {
|
||||
decided = true
|
||||
r.decision = { provider: DECISION_PROVIDER, path: '/v1/decide', mode: 'live', timeout: '500ms', minProb: 700, band: 100 }
|
||||
stats.decision = tiers.flatMap((heuristic, i) => tiers.map((model, j) => ({ heuristic, model, count: i === j ? 30 - i * 6 : Math.abs(i - j) === 1 ? 12 : 0 }))).filter((c) => c.count > 0)
|
||||
stats.decisionOutcomes = DECISION_OUTCOMES.map((name, i) => ({ name, count: 60 - i * 9 }))
|
||||
}
|
||||
r.shadowEnabled = true
|
||||
r.shadowClassifier = SHADOW_CLASSIFIER
|
||||
r.fellBack = 7
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { decisionView, hasDecision } from '@/lib/decision-view'
|
||||
import type { AdminRouterView } from '@/types/api'
|
||||
|
||||
function view(over: Partial<AdminRouterView> = {}): AdminRouterView {
|
||||
return {
|
||||
name: 'smartrouter/uni',
|
||||
defaultTier: 'low',
|
||||
shadowEnabled: false,
|
||||
classifier: 'v2',
|
||||
riskZ: 128,
|
||||
selection: 'ordered',
|
||||
calibration: { mode: 'off', delta: [], maxLower: 0, maxRaise: 0, freeze: false },
|
||||
saturationDowngrade: false,
|
||||
downgradeBand: 100,
|
||||
downgraded: 0,
|
||||
timeoutFallback: false,
|
||||
fellBack: 0,
|
||||
tiers: [
|
||||
{ name: 'low', models: ['a'], minScore: 0 },
|
||||
{ name: 'mid', models: ['b'], minScore: 150 },
|
||||
{ name: 'top', models: ['c'], minScore: 350 },
|
||||
],
|
||||
keywordRules: [],
|
||||
maxAttempts: 2,
|
||||
contextEscalation: false,
|
||||
decision: { provider: 'gx10', path: '/v1/decide', mode: 'live', timeout: '500ms', minProb: 700, band: 100 },
|
||||
stats: {
|
||||
scope: 'shared',
|
||||
requests: 10,
|
||||
tiers: [],
|
||||
reasons: [],
|
||||
escalations: [],
|
||||
shadow: [],
|
||||
outcomes: [],
|
||||
decision: [{ heuristic: 'mid', model: 'top', count: 40 }],
|
||||
decisionOutcomes: [
|
||||
{ name: 'agree', count: 5 },
|
||||
{ name: 'override', count: 40 },
|
||||
],
|
||||
},
|
||||
...over,
|
||||
}
|
||||
}
|
||||
|
||||
describe('decisionView', () => {
|
||||
it('puts mid to top count in the grid by tier order', () => {
|
||||
const grid = decisionView(view())!.grid!
|
||||
expect(grid.rows).toEqual(['low', 'mid', 'top'])
|
||||
expect(grid.values[grid.rows.indexOf('mid')][grid.cols.indexOf('top')]).toBe(40)
|
||||
})
|
||||
|
||||
it('lists outcomes by name', () => {
|
||||
const d = decisionView(view())!
|
||||
expect(d.outcomes).toContainEqual({ name: 'override', count: 40 })
|
||||
})
|
||||
|
||||
it('summarises the config', () => {
|
||||
expect(decisionView(view())!.summary).toBe('gx10 · live · 500ms · ≥70%')
|
||||
})
|
||||
|
||||
it('hides the section without a decision block', () => {
|
||||
const v = view({ decision: undefined })
|
||||
expect(hasDecision(v)).toBe(false)
|
||||
expect(decisionView(v)).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,40 @@
|
||||
import type { AdminRouterView } from '@/types/api'
|
||||
|
||||
const PER_MILLE_PER_PCT = 10
|
||||
|
||||
export interface DecisionGrid {
|
||||
rows: string[]
|
||||
cols: string[]
|
||||
values: number[][]
|
||||
}
|
||||
export interface DecisionView {
|
||||
summary: string
|
||||
grid: DecisionGrid | null
|
||||
outcomes: Array<{ name: string; count: number }>
|
||||
}
|
||||
|
||||
export const hasDecision = (v: AdminRouterView): boolean => v.decision !== undefined
|
||||
|
||||
/** Decision-model section of the router panel; null hides it. Grid rows are heuristic tiers, columns model tiers. */
|
||||
export function decisionView(v: AdminRouterView): DecisionView | null {
|
||||
const d = v.decision
|
||||
if (!d) return null
|
||||
const cells = v.stats?.decision ?? []
|
||||
let grid: DecisionGrid | null = null
|
||||
if (cells.length > 0) {
|
||||
const names = new Set(v.tiers.map((t) => t.name))
|
||||
for (const c of cells) {
|
||||
names.add(c.heuristic)
|
||||
names.add(c.model)
|
||||
}
|
||||
const order = [...names]
|
||||
const values = order.map(() => order.map(() => 0))
|
||||
for (const c of cells) values[order.indexOf(c.heuristic)][order.indexOf(c.model)] += c.count
|
||||
grid = { rows: order, cols: order, values }
|
||||
}
|
||||
return {
|
||||
summary: [d.provider, d.mode, d.timeout, `≥${Math.round(d.minProb / PER_MILLE_PER_PCT)}%`].join(' · '),
|
||||
grid,
|
||||
outcomes: (v.stats?.decisionOutcomes ?? []).map((o) => ({ name: o.name, count: o.count })),
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { Badge, Button, DescriptionItem, DescriptionList, Heatmap, StatBar } fro
|
||||
import { computed, ref, watch } from 'vue'
|
||||
|
||||
import RouterForm from '@/components/forms/RouterForm.vue'
|
||||
import { decisionView } from '@/lib/decision-view'
|
||||
import { formatCompactCount, formatExactInt } from '@/lib/format'
|
||||
import { formatSel } from '@/lib/panel-selection'
|
||||
import { routerPanelModel } from '@/lib/router-panel'
|
||||
@@ -22,6 +23,7 @@ const nav = useNavStore()
|
||||
|
||||
const view = computed(() => models.routers.find((r) => r.name === props.id))
|
||||
const model = computed(() => (view.value ? routerPanelModel(view.value) : null))
|
||||
const decision = computed(() => (view.value ? decisionView(view.value) : null))
|
||||
const known = computed(() => new Set(models.rows.map((r) => r.id)))
|
||||
const requests = computed(() => view.value?.stats?.requests ?? 0)
|
||||
const scope = computed(() => (view.value?.stats?.scope === 'shared' ? 'all replicas, this month (UTC)' : 'this gateway instance, since start'))
|
||||
@@ -146,6 +148,31 @@ watch(() => props.id, () => (snippetOpen.value = false))
|
||||
</div>
|
||||
</PanelSection>
|
||||
|
||||
<PanelSection v-if="decision" title="Decision model">
|
||||
<p class="text-muted-foreground m-0 font-mono text-xs" data-testid="decision-summary">{{ decision.summary }}</p>
|
||||
<p v-if="decision.grid" class="text-muted-foreground m-0 text-xs">Rows: heuristic tier. Columns: model tier.</p>
|
||||
<div v-if="decision.grid" data-wb-ignore-numeric>
|
||||
<Heatmap
|
||||
:values="decision.grid.values"
|
||||
:row-labels="decision.grid.rows"
|
||||
:col-labels="decision.grid.cols"
|
||||
tone="info"
|
||||
:levels="5"
|
||||
fit
|
||||
:min="0"
|
||||
show-legend
|
||||
label="Heuristic tier by model tier request counts"
|
||||
:cell-title="(v: number | null, r: string, c: string) => `heuristic ${r}, model ${c}: ${formatExactInt(v ?? 0)}`"
|
||||
/>
|
||||
</div>
|
||||
<p v-else class="text-muted-foreground m-0 text-sm">No decisions recorded yet.</p>
|
||||
<DescriptionList variant="inline" size="sm" density="compact">
|
||||
<DescriptionItem v-for="o in decision.outcomes" :key="o.name" :term="o.name">
|
||||
<span class="font-mono tabular-nums">{{ formatExactInt(o.count) }}</span>
|
||||
</DescriptionItem>
|
||||
</DescriptionList>
|
||||
</PanelSection>
|
||||
|
||||
<PanelSection title="Settings">
|
||||
<DescriptionList variant="inline" size="sm" density="compact">
|
||||
<DescriptionItem v-for="s in model.settings" :key="s.key" :term="s.label">
|
||||
|
||||
@@ -267,6 +267,25 @@ export interface AdminRouterOutcomeView {
|
||||
count: number
|
||||
}
|
||||
|
||||
/** One heuristic-tier by model-tier decision cell (admin_routers.go: adminRouterDecisionTierView). */
|
||||
export interface AdminRouterDecisionTierView {
|
||||
heuristic: string
|
||||
model: string
|
||||
count: number
|
||||
}
|
||||
|
||||
/** Decision-model settings of a router (admin_routers.go: adminRouterDecisionView); absent when not configured. */
|
||||
export interface AdminRouterDecisionView {
|
||||
provider: string
|
||||
path: string
|
||||
mode: 'live' | 'shadow'
|
||||
timeout: string
|
||||
/** Minimum model probability in per-mille. */
|
||||
minProb: number
|
||||
/** Score distance from a tier boundary in per-mille that triggers a call. */
|
||||
band: number
|
||||
}
|
||||
|
||||
/** Router counters (admin_routers.go: adminRouterStatsView); scope "shared" sums all gateway instances this month, "instance" is this process since start. */
|
||||
export interface AdminRouterStatsView {
|
||||
scope: 'shared' | 'instance'
|
||||
@@ -277,6 +296,10 @@ export interface AdminRouterStatsView {
|
||||
/** Non-zero cells of the live-by-shadow tier matrix; empty without shadowWeights. */
|
||||
shadow: AdminRouterShadowView[]
|
||||
outcomes: AdminRouterOutcomeView[]
|
||||
/** Non-zero heuristic-by-model cells; empty without a decision block. */
|
||||
decision?: AdminRouterDecisionTierView[]
|
||||
/** One row per decision outcome, zeros included; empty without a decision block. */
|
||||
decisionOutcomes?: AdminRouterCountView[]
|
||||
}
|
||||
|
||||
/** One tier's calibration offset: points its threshold sits below the configured minScore. */
|
||||
@@ -335,6 +358,7 @@ export interface AdminRouterView {
|
||||
contextEscalation: boolean
|
||||
/** Advertised context window in tokens; absent when no target declares one. */
|
||||
contextTokens?: number
|
||||
decision?: AdminRouterDecisionView
|
||||
stats?: AdminRouterStatsView
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user