mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
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.
61 lines
2.8 KiB
Go
61 lines
2.8 KiB
Go
package traefikllmgateway
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestLimitExempt_ManyEmbeddingsLeaveChatLimitsUntouched(t *testing.T) {
|
|
h, gw := exemptGateway(t, []string{"embedding"}, &LimitsConfig{RequestsPerMinute: 5, RequestsPerDay: 10, TokensPerMinute: 100, TokensPerDay: 100}, nil)
|
|
for i := 0; i < 1000; i++ {
|
|
require.Equal(t, http.StatusOK, exemptPost(h, "/v1/embeddings", embedBody).Code, "embedding %d", i+1)
|
|
}
|
|
rec := memberLimitsChat(t, h, "sk-a")
|
|
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
|
|
assert.Equal(t, "4", rec.Header().Get("x-ratelimit-remaining-requests"), "RPM untouched by embeddings")
|
|
assert.Equal(t, "100", rec.Header().Get("x-ratelimit-limit-tokens"))
|
|
remaining, err := strconv.Atoi(rec.Header().Get("x-ratelimit-remaining-tokens"))
|
|
require.NoError(t, err)
|
|
assert.Greater(t, remaining, 50, "TPM holds only the chat request's own pre-charge, not 1000 embeddings")
|
|
day, _ := gw.limiter.getCounter("user", "a", metricReq, windowDay, gw.limiter.now())
|
|
assert.EqualValues(t, 1, day, "RPD counts the chat request only")
|
|
}
|
|
|
|
func TestLimitExempt_NoLimitWindowCounterMoves(t *testing.T) {
|
|
h, gw := exemptGateway(t, []string{"embedding"}, &LimitsConfig{RequestsPerMinute: 5, TokensPerMinute: 100}, nil)
|
|
now := gw.limiter.now()
|
|
for i := 0; i < 3; i++ {
|
|
require.Equal(t, http.StatusOK, exemptPost(h, "/v1/embeddings", embedBody).Code)
|
|
}
|
|
for _, sc := range []struct{ kind, id string }{{"user", "a"}, {"group", "friends"}, {totalScopeKind, totalScopeID}} {
|
|
for _, m := range []string{metricReq, metricTokIn, metricTokOut, metricCost} {
|
|
for _, w := range []string{windowMin, windowHour, windowDay, windowMonth} {
|
|
v, _ := gw.limiter.getCounter(sc.kind, sc.id, m, w, now)
|
|
assert.EqualValues(t, 0, v, "%s/%s %s %s", sc.kind, sc.id, m, w)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLimitExempt_PerModelUsageAndExemptCounterStillRecorded(t *testing.T) {
|
|
h, gw := exemptGateway(t, []string{"embedding"}, &LimitsConfig{RequestsPerMinute: 1}, nil)
|
|
for i := 0; i < 4; i++ {
|
|
require.Equal(t, http.StatusOK, exemptPost(h, "/v1/embeddings", embedBody).Code)
|
|
}
|
|
now := gw.limiter.now()
|
|
reqs, _ := gw.limiter.getCounter(kindModel, "openai/gpt-test", metricReq, windowDay, now)
|
|
assert.EqualValues(t, 4, reqs, "per-model usage still counts exempt requests")
|
|
tok, _ := gw.limiter.getCounter(kindModel, "openai/gpt-test", metricTokIn, windowDay, now)
|
|
assert.EqualValues(t, 4*30, tok)
|
|
|
|
x, _ := gw.limiter.getCounter("user", "a", metricExempt, windowMonth, now)
|
|
assert.EqualValues(t, 4, x, "exempt counter")
|
|
got := gw.buildAdminUsage()
|
|
assert.EqualValues(t, 4, findUsageEntry(t, got.Users, "a").ExemptRequestsPerMonth)
|
|
assert.EqualValues(t, 0, findUsageEntry(t, got.Users, "a").RequestsPerDay)
|
|
}
|