mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
feat(router): smart routers and per-member rate limits (#9)
Add smart routers: virtual models that score each request and route it to admin-defined model tiers. A retryable failure moves the request to a higher tier. The classifier v2 extracts prose, finds the original ask in long tool loops, adds an uncertainty margin and reads tool-output failures. Sessions, outcome signals, bounded calibration, adaptive selection within a tier, gated saturation downgrade, a first-byte timeout and an opt-in timeout fallback are included. Every new feature is off by default. Add per-member group limits, a tokensPerMinute window that counts uncached input and output, rate-limit headers in three dialects, soft pacing before the hard limit, and a limitExemptKinds list so embedding and reranking traffic never counts toward or hits a limit. Fix yaegi named-result state that made smartContentSize grow between calls.
This commit is contained in:
+45
-16
@@ -52,7 +52,9 @@ type adminCatalogModel struct {
|
||||
Kind string `json:"kind,omitempty"`
|
||||
PriceSource string `json:"priceSource"`
|
||||
Aliases []string `json:"aliases,omitempty"`
|
||||
DisplayFree bool `json:"displayFree,omitempty"`
|
||||
// Routers names the smart routers that list this model as a target.
|
||||
Routers []string `json:"routers,omitempty"`
|
||||
DisplayFree bool `json:"displayFree,omitempty"`
|
||||
// Endpoint marks a pseudo-model row for a pathBaseUrls key: shown in
|
||||
// the catalog, but never routable as a model and never priced.
|
||||
Endpoint bool `json:"endpoint,omitempty"`
|
||||
@@ -84,6 +86,7 @@ type adminCatalogProvider struct {
|
||||
type adminCatalogResponse struct {
|
||||
Providers []adminCatalogProvider `json:"providers"`
|
||||
Aliases []adminAliasView `json:"aliases"`
|
||||
Routers []adminRouterView `json:"routers"`
|
||||
}
|
||||
|
||||
// buildAdminCatalog assembles adminCatalogResponse from the SAME
|
||||
@@ -104,6 +107,9 @@ func (g *Gateway) buildAdminCatalog() adminCatalogResponse {
|
||||
aliasesByTarget[a.Target] = append(aliasesByTarget[a.Target], a.Alias)
|
||||
}
|
||||
|
||||
routers := g.buildAdminRouterViews()
|
||||
routersFor := routersByTarget(routers)
|
||||
|
||||
providers := make([]adminCatalogProvider, len(snaps))
|
||||
for i, s := range snaps {
|
||||
var kinds, pathBases map[string]string
|
||||
@@ -151,6 +157,7 @@ func (g *Gateway) buildAdminCatalog() adminCatalogResponse {
|
||||
PriceSource: source,
|
||||
DisplayFree: displayFree,
|
||||
Aliases: aliases,
|
||||
Routers: routerNamesFor(routersFor, canonical, model),
|
||||
Kind: kinds[model],
|
||||
}
|
||||
}
|
||||
@@ -174,7 +181,7 @@ func (g *Gateway) buildAdminCatalog() adminCatalogResponse {
|
||||
}
|
||||
}
|
||||
|
||||
return adminCatalogResponse{Providers: providers, Aliases: g.buildAdminAliasViews()}
|
||||
return adminCatalogResponse{Providers: providers, Aliases: g.buildAdminAliasViews(), Routers: routers}
|
||||
}
|
||||
|
||||
// sortedKeys returns m's keys in ascending order, nil for an empty map.
|
||||
@@ -201,13 +208,16 @@ func (g *Gateway) serveAdminCatalog(w http.ResponseWriter) {
|
||||
// userSummary's own new fields (auth.go's doc comment has the full
|
||||
// source/personal-grant contract).
|
||||
type adminConsumerUser struct {
|
||||
Limits *LimitsConfig `json:"limits,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Source string `json:"source"` // inline|file
|
||||
Groups []string `json:"groups"`
|
||||
Providers []string `json:"providers,omitempty"`
|
||||
Models []string `json:"models,omitempty"`
|
||||
Admin bool `json:"admin,omitempty"`
|
||||
Limits *LimitsConfig `json:"limits,omitempty"`
|
||||
// LimitSources names the origin of each set limits field ("user" or
|
||||
// "memberLimits:<group>").
|
||||
LimitSources map[string]string `json:"limitSources,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Source string `json:"source"` // inline|file
|
||||
Groups []string `json:"groups"`
|
||||
Providers []string `json:"providers,omitempty"`
|
||||
Models []string `json:"models,omitempty"`
|
||||
Admin bool `json:"admin,omitempty"`
|
||||
}
|
||||
|
||||
// adminModelAccessCount is one group's per-provider model-access tally in
|
||||
@@ -233,6 +243,7 @@ type adminModelAccessCount struct {
|
||||
// configured glob list the way Providers/Models below do.
|
||||
type adminConsumerGroup struct {
|
||||
Limits *LimitsConfig `json:"limits,omitempty"`
|
||||
MemberLimits *LimitsConfig `json:"memberLimits,omitempty"`
|
||||
ModelAccess map[string]adminModelAccessCount `json:"modelAccess"`
|
||||
Name string `json:"name"`
|
||||
Providers []string `json:"providers,omitempty"`
|
||||
@@ -278,13 +289,14 @@ func (g *Gateway) buildAdminConsumers() adminConsumersResponse {
|
||||
users := make([]adminConsumerUser, len(userSummaries))
|
||||
for i, us := range userSummaries {
|
||||
users[i] = adminConsumerUser{
|
||||
Limits: us.limits,
|
||||
Name: us.name,
|
||||
Source: us.source,
|
||||
Groups: us.groups,
|
||||
Providers: us.personalProviders,
|
||||
Models: us.personalModels,
|
||||
Admin: us.admin,
|
||||
Limits: us.limits,
|
||||
LimitSources: us.limitSources,
|
||||
Name: us.name,
|
||||
Source: us.source,
|
||||
Groups: us.groups,
|
||||
Providers: us.personalProviders,
|
||||
Models: us.personalModels,
|
||||
Admin: us.admin,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -308,6 +320,7 @@ func (g *Gateway) buildAdminConsumers() adminConsumersResponse {
|
||||
sort.Strings(allowedProviders)
|
||||
groups[i] = adminConsumerGroup{
|
||||
Limits: gs.limits,
|
||||
MemberLimits: gs.memberLimits,
|
||||
Name: gs.name,
|
||||
Providers: gs.providers,
|
||||
Models: gs.models,
|
||||
@@ -333,3 +346,19 @@ func (g *Gateway) serveAdminConsumers(w http.ResponseWriter) {
|
||||
setAdminJSONHeaders(w)
|
||||
_ = json.NewEncoder(w).Encode(g.buildAdminConsumers())
|
||||
}
|
||||
|
||||
// routerNamesFor returns the routers that target a model by canonical id or bare name.
|
||||
func routerNamesFor(by map[string][]string, canonical, model string) []string {
|
||||
names := append(append([]string{}, by[canonical]...), by[model]...)
|
||||
if len(names) == 0 {
|
||||
return nil
|
||||
}
|
||||
sort.Strings(names)
|
||||
out := names[:1]
|
||||
for _, n := range names[1:] {
|
||||
if n != out[len(out)-1] {
|
||||
out = append(out, n)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user