mirror of
https://github.com/mudler/LocalAI.git
synced 2026-10-31 18:49:22 -04:00
* fix(schema): preserve SystemOne image inputs Assisted-by: OpenAI * test(schema): follow Ginkgo conventions for decision inputs Assisted-by: OpenAI * feat(llama-cpp): dispatch native decisions through Score Upgrade the stock dependency and reconcile Score/TTS patches. Reuse native decision parsing, tasks, formatting and response-reader cleanup; preserve ordinary scoring admission and guard older dependencies. Assisted-by: OpenAI * refactor(systemone): share request and model validation Assisted-by: OpenAI:gpt-5 * fix(systemone): preserve HTTP wire-byte validation limit Keep structural validation separate from the serialized internal request bound so HTML escaping cannot reject valid HTTP payloads. Assisted-by: OpenAI:gpt-5 * feat(systemone): bound images and account native decisions Preserve public wire limits independently from router serialization. Reject unsupported NER images, map native request/capability errors, and stamp explicit usage once. Advertise decisions for stock llama-cpp. Assisted-by: OpenAI * fix(systemone): record usage on registered native route Exercise real registration and billing with a mock native backend. Reject empty native responses, malformed image URLs, trailing JSON, and wire overflow including whitespace. Assisted-by: OpenAI * feat(router): add lazy native decision transport Bind named models through internal ModelSystemOne calls with shared validation and bounded abandoned operations. Remove request and echoed-error contents from decision traces. Assisted-by: OpenAI:gpt-5 * feat(router): classify overlapping policies with native decisions Ask independent noul questions, validate probabilities and preserve first-superset routing. Wire the central factory with config-sensitive invalidation and cancellation-safe resolution. Document native framing and bounded operation limits. Assisted-by: OpenAI:gpt-5 * feat(gallery): add pinned Julia-1 native decision model Add a separate text-only llama-cpp Q8 entry with pinned Apache-2.0 source provenance and checksum. Installed using the gallery installer and exercised choice, score and noul on CPU. Assisted-by: OpenAI * test(router): verify native decisions through central factory Add an opt-in real-model Ginkgo integration covering the native Go loader and C++ transport, token usage, independent overlapping labels, and candidate selection. Document owned-server execution and the intentionally non-quality threshold. Assisted-by: Codex:gpt-5 * fix(llama-cpp): align upstream pin and preserve decision signatures Advance to bed0a856 without losing the automated upstream bump. Detect full-request fill_task support at compile time and forward every question for Nimble framing while retaining the earlier native signature. Preserve reconciled SCORE/TTS patches; add standalone compatibility coverage. Assisted-by: Codex:gpt-5 * feat(gallery): add native decision family defaults Pin Laya, Kev-4B, lev, OpenJev and Nimble artifacts. Verify Laya/Kev/lev gallery installs and CPU contracts on both native pins; clearly mark OpenJev/Nimble runtime validation pending and their noncommercial licenses. Assisted-by: OpenAI * docs(decisions): clarify integrated Nimble prerequisite Record the exact combined backend pin while retaining pending OpenJev and Nimble installation/runtime validation status. Assisted-by: Codex:gpt-5 * fix(gallery): indent native decision model sequences Match repository yamllint indentation for Laya, Kev, lev and OpenJev list fields. Parsed gallery data is unchanged; reproduce CI gallery lint failure before the whitespace-only fix and pass the same command afterward. Assisted-by: Codex:gpt-5 * docs(decisions): record OpenJev and Nimble CPU validation Record gallery installation, checksum/metadata verification and multiquestion native smoke results on bed0a856. Retain noncommercial and text-only limitations without accuracy or deterministic-output claims. Assisted-by: OpenAI * fix(ui): expose native Decisions router classifiers Select classifier models using metadata-driven capability routing, retain tuned thresholds, and validate native decision selections before saving. Cover both native backends and create/save/reopen in the real React editor. Assisted-by: Codex:gpt-5 * fix(router): exclude aliases from native decision discovery Check the originally named config before advertising native Decisions eligibility. Retain target capability inheritance for ordinary generation aliases. Exercise the actual capabilities endpoint with native models on both backends, aliases, and disabled models. Assisted-by: Codex:gpt-5 * feat(systemone): share bounded multimodal input validation Preserve text wire limits while admitting bounded PNG/JPEG decision input. Share collection and header validation across internal and public callers and keep the native runner response budget independent. Assisted-by: OpenAI:API-assistant * fix(systemone): bound admission lifetimes and validate complete images Retain shared admission leases through actual work completion, including abandoned internal operations. Decode bounded image pixels, cap public native responses before usage stamping, and preserve oversized malformed text status precedence. Assisted-by: OpenAI:API-assistant * fix(router): classify images before media fetching Preserve ordered structured probes for native decisions. Defer OpenAI media preparation until routing selects the served model, so rejected decision URLs cannot trigger downloads before shared validation. Guard direct image collection with context-aware shared admission. Keep text classifiers and embedding caches from discarding image input. Retain fail-closed classifier configuration and runtime fallback policy. Add middleware, typed-content, admission, cancellation and cache tests. Assisted-by: OpenAI:API-assistant * fix(router): bound extraction before serialization Check probe budgets before copying text or marshaling message state. Count JSON escaping so oversized internal inputs fail before allocation. Preserve typed Anthropic blocks through selected-model conversion and fallback. Keep retry coverage in Ginkgo without global test registration. Assisted-by: OpenAI * fix(router): bound supported probe serialization Arbitrary structs can bypass the probe budget through pointer marshalers, string tags, and promoted fields. Accept concrete chat schema types and plain JSON values instead of emulating arbitrary struct serialization. Budget escaped direct prompts before marshaling so raw length cannot hide serialized expansion. Preserve runtime fallback and reject oversized input before invoking the decision runner. Add Ginkgo allocation, boundary, and marshaler invocation regressions. Six-package tests, three-package race tests, and full-T2 delta lint pass. Assisted-by: OpenAI:GPT-5 golangci-lint * feat(decisions): enable bounded OpenJev images Validate native decision images before permissive media parsing and pixel allocation. Require both decision image support and a vision projector; missing or audio-only projectors cannot silently become text decisions. Pin the OpenJev Q8 projector and document its license and disk footprint. Add native safety tests, canonical limit parity, gallery and load-option checks, and a reproducible CPU direct-RPC contrasting-image smoke. Assisted-by: OpenAI:GPT-5 * fix(decisions): reject incomplete image streams stb accepts corrupt PNG Adler checksums and truncated JPEG scans. Use bounded zlib validation and strict libjpeg decoding before parsing. Keep dimension and aggregate pixel checks ahead of decoder allocations. Wire decoder dependencies into native builds and runtime packaging. Add regressions for appended EOI and embedded marker bypasses. Assisted-by: OpenAI:GPT-5 * fix(ci): gate native decision image validation Run the decoder security tests outside the stdlib-only native suite. Fetch vendor headers at the backend pin and provision decoder dependencies. Gate Go limit parity and production CMake wiring without model downloads. Assisted-by: OpenAI:GPT-5 * test(decisions): cover multimodal public API paths Exercise shared image contracts through the registered HTTP routes and external mock backend. Add opt-in cached gallery installation and real OpenJev image decisions through SystemOne and both routing APIs. Assisted-by: Codex:gpt-5 * test(decisions): assert isolation and cache bypass Observe external RPC calls and compare complete classifier history. Winner-only and cache-miss checks could hide dropped history or cache use. Give real inference its own application and model directory so shared backend mappings and loaded processes cannot affect mixed suite order. Assisted-by: OpenAI:ChatGPT * test(decisions): isolate fixture globals Disable optional global services in the isolated HTTP fixture and register cleanup before setup assertions. Verify meter provider identity survives fixture creation and destruction. Snapshot observed usage before assertions so failures cannot retain the mutex. Require a successful usage stamp before checking error responses. Assisted-by: Codex:gpt-5 golangci-lint * fix(application): honor optional telemetry controls Skip failover gauge registration when metrics are disabled. Register against the application meter rather than looking up the global provider. Allow embedders to retain the bounded routing log without billing stats. Keep the existing default when stats are disabled. The isolated HTTP fixture uses this option without losing its native router assertions. Assisted-by: Codex:gpt-5 golangci-lint --------- Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
213 lines
7.8 KiB
Go
213 lines
7.8 KiB
Go
package router
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/mudler/LocalAI/core/config"
|
|
)
|
|
|
|
// CandidateLoader resolves a candidate's model name to its parsed
|
|
// ModelConfig. The router calls it after MatchCandidate to load the
|
|
// resolved target so the caller (HTTP middleware or realtime handler)
|
|
// can dispatch against it.
|
|
//
|
|
// Defined as a function value rather than tied to *config.ModelConfigLoader
|
|
// so callers in tests can stub it without spinning up a real loader.
|
|
type CandidateLoader func(name string) (*config.ModelConfig, error)
|
|
|
|
// ResolveResult is the output of Resolve. It captures everything a
|
|
// caller needs to (a) dispatch the request against the chosen candidate,
|
|
// (b) record an audit row, and (c) decide whether to fall back to the
|
|
// classifier-error path.
|
|
type ResolveResult struct {
|
|
// RouterModel is the router config's own name (the model the client asked for).
|
|
RouterModel string
|
|
|
|
// ChosenModel is the candidate the classifier picked, or
|
|
// cfg.Router.Fallback when the classifier errored / no candidate
|
|
// covered the active labels.
|
|
ChosenModel string
|
|
|
|
// ChosenConfig is the loaded ModelConfig for ChosenModel. The
|
|
// caller dispatches against this — it has the right backend,
|
|
// pipeline, etc.
|
|
ChosenConfig *config.ModelConfig
|
|
|
|
// Decision carries the classifier's labels/score/latency/cache
|
|
// info. When UsedFallback is true, Decision.Labels is
|
|
// []string{LabelFallback}.
|
|
Decision Decision
|
|
|
|
// Labels are the labels recorded against this decision — either
|
|
// Decision.Labels or []string{LabelFallback} when the classifier
|
|
// failed. Pulled out so callers don't have to special-case the
|
|
// fallback path.
|
|
Labels []string
|
|
|
|
// ClassifierName is the Name() of the classifier that produced the
|
|
// decision, or LabelFallback when classifier setup itself failed
|
|
// and the fallback path ran without a working classifier.
|
|
ClassifierName string
|
|
|
|
// UsedFallback is true when the result came from cfg.Router.Fallback
|
|
// rather than a classifier-picked candidate (classifier
|
|
// build/Classify error or no candidate covered the active labels).
|
|
UsedFallback bool
|
|
}
|
|
|
|
// Resolve runs the full classify → match → load pipeline for a router
|
|
// model config. It is transport-agnostic: callers pass a built
|
|
// classifier, a candidate loader, and a probe; Resolve returns a
|
|
// ResolveResult or an error if the resolved config violates invariants
|
|
// or the fallback can't be loaded.
|
|
//
|
|
// Errors returned here are *terminal* — the caller should surface them
|
|
// to the client. Classifier-error fallbacks are non-terminal and folded
|
|
// into ResolveResult.UsedFallback.
|
|
//
|
|
// classifier may be nil; that signals "classifier build failed" and
|
|
// pushes resolution straight to the fallback path (mirrors the
|
|
// classifier-build-error branch in the historical RouteModel middleware).
|
|
func Resolve(ctx context.Context, routerCfg *config.ModelConfig, classifier Classifier, loader CandidateLoader, probe Probe) (*ResolveResult, error) {
|
|
if routerCfg == nil || !routerCfg.HasRouter() {
|
|
return nil, fmt.Errorf("router.Resolve: config has no router block")
|
|
}
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if classifier == nil {
|
|
return resolveFallback(routerCfg, loader, Decision{}, LabelFallback, "classifier unavailable")
|
|
}
|
|
|
|
start := time.Now()
|
|
decision, err := classifier.Classify(ctx, probe)
|
|
if ctx.Err() != nil {
|
|
return nil, ctx.Err()
|
|
}
|
|
if err != nil {
|
|
return resolveFallback(routerCfg, loader, Decision{Latency: time.Since(start)}, classifier.Name(), "classifier error: "+err.Error())
|
|
}
|
|
|
|
candidate := MatchCandidate(routerCfg.Router.Candidates, decision.Labels)
|
|
if candidate == "" {
|
|
return resolveFallback(routerCfg, loader, decision, classifier.Name(), "no candidate covers labels: "+strings.Join(decision.Labels, ","))
|
|
}
|
|
|
|
candidateCfg, err := loader(candidate)
|
|
if err != nil || candidateCfg == nil {
|
|
return nil, fmt.Errorf("router candidate %q not loadable: %w", candidate, err)
|
|
}
|
|
if candidateCfg.HasRouter() {
|
|
return nil, fmt.Errorf("router candidate %q is itself a router (depth-1 invariant)", candidate)
|
|
}
|
|
|
|
return &ResolveResult{
|
|
RouterModel: routerCfg.Name,
|
|
ChosenModel: candidate,
|
|
ChosenConfig: candidateCfg,
|
|
Decision: decision,
|
|
Labels: decision.Labels,
|
|
ClassifierName: classifier.Name(),
|
|
UsedFallback: false,
|
|
}, nil
|
|
}
|
|
|
|
// resolveFallback handles the three failure modes that fall through to
|
|
// cfg.Router.Fallback: classifier build failed, Classify returned an
|
|
// error, or no candidate covered the active labels. Returns an error
|
|
// when no fallback is configured — those translate to 503/500 at the
|
|
// HTTP layer.
|
|
//
|
|
// reason is included in the wrapped error for debugging; it's not
|
|
// surfaced to the client.
|
|
func resolveFallback(routerCfg *config.ModelConfig, loader CandidateLoader, decision Decision, classifierName, reason string) (*ResolveResult, error) {
|
|
if routerCfg.Router.Fallback == "" {
|
|
return nil, fmt.Errorf("router: %s and no fallback configured", reason)
|
|
}
|
|
candidateCfg, err := loader(routerCfg.Router.Fallback)
|
|
if err != nil || candidateCfg == nil {
|
|
return nil, fmt.Errorf("router fallback %q not loadable: %w", routerCfg.Router.Fallback, err)
|
|
}
|
|
if candidateCfg.HasRouter() {
|
|
return nil, fmt.Errorf("router fallback %q is itself a router (depth-1 invariant)", routerCfg.Router.Fallback)
|
|
}
|
|
decision.Labels = []string{LabelFallback}
|
|
return &ResolveResult{
|
|
RouterModel: routerCfg.Name,
|
|
ChosenModel: routerCfg.Router.Fallback,
|
|
ChosenConfig: candidateCfg,
|
|
Decision: decision,
|
|
Labels: []string{LabelFallback},
|
|
ClassifierName: classifierName,
|
|
UsedFallback: true,
|
|
}, nil
|
|
}
|
|
|
|
// ToDecisionRecord projects a ResolveResult into the persisted
|
|
// DecisionRecord shape. Centralised so the chat-side recordHTTPDecision
|
|
// and the realtime-side recorder can't drift in which Decision fields
|
|
// they copy through — a new field added to Decision only needs to be
|
|
// remembered here, not at every call site.
|
|
//
|
|
// id, correlationID, userID, and source are caller-supplied because
|
|
// they originate outside the routing pipeline (request ID generator,
|
|
// auth, entry-point dispatch).
|
|
func (r *ResolveResult) ToDecisionRecord(id, correlationID, userID, source string) DecisionRecord {
|
|
return DecisionRecord{
|
|
ID: id,
|
|
CorrelationID: correlationID,
|
|
UserID: userID,
|
|
RouterModel: r.RouterModel,
|
|
RequestedModel: r.RouterModel,
|
|
ServedModel: r.ChosenModel,
|
|
Classifier: r.ClassifierName,
|
|
Label: strings.Join(r.Labels, ","),
|
|
Score: r.Decision.Score,
|
|
LatencyMs: r.Decision.Latency.Milliseconds(),
|
|
Cached: r.Decision.Cached,
|
|
CacheSimilarity: r.Decision.CacheSimilarity,
|
|
NearestSimilarity: r.Decision.NearestSimilarity,
|
|
LabelScores: r.Decision.LabelScores,
|
|
ActivationThreshold: r.Decision.ActivationThreshold,
|
|
Neighbors: r.Decision.Neighbors,
|
|
Source: source,
|
|
CreatedAt: time.Now().UTC(),
|
|
}
|
|
}
|
|
|
|
// MatchCandidate picks the FIRST candidate whose Labels are a
|
|
// superset of the active label set. Admins order the candidates list
|
|
// smallest → largest, so a request that needs one label routes to
|
|
// the smallest capable model and one that needs multiple falls to
|
|
// the first bigger candidate that covers them all. Returns empty
|
|
// string when no candidate matches; the caller falls back.
|
|
func MatchCandidate(candidates []config.RouterCandidate, active []string) string {
|
|
if len(active) == 0 {
|
|
return ""
|
|
}
|
|
for _, c := range candidates {
|
|
if labelSetCovers(c.Labels, active) {
|
|
return c.Model
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// labelSetCovers returns true when every element of needed appears
|
|
// in have. Label sets are typically <10 entries so the linear scan
|
|
// is fine.
|
|
func labelSetCovers(have, needed []string) bool {
|
|
for _, n := range needed {
|
|
if !slices.Contains(have, n) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|