mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
Add a virtual model id `auto` (alias `zen-router`) to the /v1 chat path. When
routing is enabled for the deployment, the requested model is resolved to a
concrete servable id BEFORE provider/pricing/billing resolution, so the request
bills and reports the model that actually served it.
- router/: pure-Go RouterClient. Two strategies: (a) engine — POST {prompt,
tasks?,slo} to {endpoint}/route (learned zen-router via hanzo-engine), hard
150ms timeout; (b) heuristic — faithful port of hanzo-router's Rust
Heuristic::classify + a task->model preference table. Any engine error falls
through to the heuristic, so auto works with zero extra infrastructure.
- controllers/auto_route.go: resolveAutoModel glue + X-Routed-Model header +
X-Max-Cost/X-Max-Latency-Ms SLO headers. One insertion point in
ChatCompletions rewrites request.Model so the entire existing path (ModelRoute
fallbacks, zen identity, reserve/settle, usage record, response echo) binds to
the resolved id.
- conf/models.yaml: router: block {enabled:false, endpoint, prefer, cost_ceiling}
with sensible defaults; env overrides ROUTER_ENABLED / ROUTER_ENDPOINT.
- Tests: classifier table tests (mirror the Rust cases), RouterClient fallback
(engine 500/timeout/unservable -> heuristic), controller resolution + the
end-to-end HTTP contract (rewrite + header + body echo).
- README: enabling, model:auto, SLO headers, transparency header, and that the
learned zen-router (hf.co/zenlm/zen-router) is the upgrade over the heuristic.
Per-org: routing enable is deployment-global (no per-org settings object yet);
the resolved id still flows through per-org ModelRoute resolution + pricing.
77 lines
3.5 KiB
Go
77 lines
3.5 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package router
|
|
|
|
import "testing"
|
|
|
|
// TestClassify mirrors the cases exercised by hanzo-router's Rust Heuristic so
|
|
// the Go port stays behaviorally identical to engine/hanzo-router/src/classify.rs.
|
|
func TestClassify(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
req Request
|
|
want Task
|
|
}{
|
|
{"task hint wins", Request{Text: "write a poem", TaskHint: TaskMath}, TaskMath},
|
|
{"media implies vision", Request{Text: "what is this", HasMedia: true}, TaskVision},
|
|
{"long context by tokens", Request{Text: "summarize", ApproxTokens: 40000}, TaskLongContext},
|
|
{"code fence", Request{Text: "```go\nfmt.Println()\n```", ApproxTokens: 50}, TaskCode},
|
|
{"code keyword", Request{Text: "please refactor this function", ApproxTokens: 50}, TaskCode},
|
|
{"code bug keyword", Request{Text: "there is a bug in the compile step", ApproxTokens: 50}, TaskCode},
|
|
{"math keyword", Request{Text: "prove this theorem about the integral", ApproxTokens: 50}, TaskMath},
|
|
{"math solve for", Request{Text: "solve for x in this equation", ApproxTokens: 50}, TaskMath},
|
|
{"reasoning keyword", Request{Text: "explain how this works step by step", ApproxTokens: 50}, TaskReasoning},
|
|
{"reasoning why", Request{Text: "why does the trade-off matter", ApproxTokens: 50}, TaskReasoning},
|
|
{"creative keyword", Request{Text: "write a story about a dragon", ApproxTokens: 50}, TaskCreative},
|
|
{"creative brainstorm", Request{Text: "brainstorm some names", ApproxTokens: 50}, TaskCreative},
|
|
{"cheap chat short", Request{Text: "hi", ApproxTokens: 2}, TaskCheapChat},
|
|
{"general fallback", Request{Text: "tell me about the weather today please", ApproxTokens: 100}, TaskGeneral},
|
|
// Ordering: code is checked before math, so a message with both
|
|
// classifies as code (matches the Rust switch order).
|
|
{"code before math", Request{Text: "there is a bug, prove the theorem", ApproxTokens: 50}, TaskCode},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := Classify(tc.req); got != tc.want {
|
|
t.Errorf("Classify(%q) = %q, want %q", tc.req.Text, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPolicyForTask(t *testing.T) {
|
|
p := Policy{Prefer: map[string][]string{
|
|
"code": {"zen4-coder", "gpt-5.3-codex"},
|
|
"default": {"zen4", "gpt-4o"},
|
|
}}
|
|
|
|
if got := p.ForTask(TaskCode, nil); got != "zen4-coder" {
|
|
t.Errorf("ForTask(code, nil) = %q, want zen4-coder", got)
|
|
}
|
|
// Unknown task falls to the default list.
|
|
if got := p.ForTask(TaskCreative, nil); got != "zen4" {
|
|
t.Errorf("ForTask(creative, nil) = %q, want zen4 (default)", got)
|
|
}
|
|
// known predicate skips the first (unservable) code model.
|
|
known := func(id string) bool { return id != "zen4-coder" }
|
|
if got := p.ForTask(TaskCode, known); got != "gpt-5.3-codex" {
|
|
t.Errorf("ForTask(code, known) = %q, want gpt-5.3-codex", got)
|
|
}
|
|
// Empty policy yields no model.
|
|
if got := (Policy{}).ForTask(TaskCode, nil); got != "" {
|
|
t.Errorf("empty policy ForTask = %q, want empty", got)
|
|
}
|
|
}
|