gateway: Chinese that says the input is too long is an overflow, Chinese pace words a rate limit (#4420)
A group member answered 400 "内容太长了,请精简输入或开启新对话" once a turn passed ~730K on a model it declares at 1M. tooLongRe's Chinese only matched wordings that name 上下文, so the try was filed as a plain failure that rested the member, and the group's other members were never asked. Reported by MYCF711 (Discord, issue #4420). tooLongRe gets a third line for Chinese that names what was sent (内容, 输入, 文本, 提示词/prompt, 问题, 对话, 消息) beside its length: "…太长/过长/ 超长", "…超过/超出…长度/token/字数", "输入长度/token…超过", and "字数太多/过多". Tied to: the reporter's vendor, z.ai's 1261 "Prompt 超长", iFlytek Spark's 10907 "token数量超过上限。对话历史+问题的字数太多,需要精简 输入". Left out on purpose: bare "超出最大长度" (a field's length), bare "token数量超过上限" (a day's allowance is said so too), "输出过长" (the reply), and "请求体过大" (a 413 byte limit, already handled by status). paceWords gets Chinese pace words (速率, 频繁, 频率, 限流, 并发, 每秒/ 分钟/小时/天/日, 稍后再试/重试), so "内容太长,已达到速率限制" is a rate limit, not a size. rateWords gains 过于频繁/速率 and quotaWords 频繁/速率, so such a 400 is asked of the next account and such a 429 is failRate. Tests: - TestEveryVendorsOverflow: 8 Chinese rows (red on the old code, each). - TestChineseNotTooLong (new): pace-with-size, a field's length, the reply's length, a day's token cap and plain errors aren't overflows; the pace 400 is retryable. - TestChineseInputTooLongGoesToTheNextMember (new): two-member group, the reporter's literal words, raw and \u-escaped: member b answers, failOverflow, nobody rests (red on the old code: a 1, b 0, 400 to the agent). - docs/subsystems/gateway-routing.md names the new wordings. Run (mote, via mote-run, at this tree): - go test ./internal/gateway -count=1 -timeout 45m: ok 500.9s - go vet ./internal/gateway, GOOS=linux and GOOS=windows: clean; gofmt -l clean - Broken by hand: old gateway.go/routing.go/fallback.go with the new tests: all three tests red (11 failures). paceWords alone reverted: TestChineseNotTooLong red on 4 pace messages and the retryable check. Not tried: the reporter's real vendor (unknown which member it is), nor a real z.ai or iFlytek account; the wordings come from their docs. Only internal/gateway was run, not the whole repo; the change is inside that package's classifiers.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -1236,7 +1236,7 @@ func restOf(id string) (Rest, bool) {
|
||||
|
||||
// quotaWords are how vendors say "out of quota" or "slow down" when their
|
||||
// status code doesn't: some answer 400 or 403 with it.
|
||||
var quotaWords = regexp.MustCompile(`(?i)quota|insufficient|balance|credit|billing|out of budget|budget (exceeded|exhausted)|exceeded|rate.?limit|usage.?limit|limit.?reached|hit your .*limit|limit.{0,24}resets|too many requests|overloaded|余额|额度|欠费|限流|频率|套餐|用量|上限`)
|
||||
var quotaWords = regexp.MustCompile(`(?i)quota|insufficient|balance|credit|billing|out of budget|budget (exceeded|exhausted)|exceeded|rate.?limit|usage.?limit|limit.?reached|hit your .*limit|limit.{0,24}resets|too many requests|overloaded|余额|额度|欠费|限流|频率|频繁|速率|套餐|用量|上限`)
|
||||
|
||||
// quotaSaid is whether an error's words, with no status to go by, say
|
||||
// out of quota or slow down: quotaWords, unless they say the conversation
|
||||
|
||||
@@ -8017,8 +8017,19 @@ func writeErrorSaid(w http.ResponseWriter, proto provider.Protocol, status int,
|
||||
// DashScope's "Range of input length should be", "token limit exceeded",
|
||||
// a relay's own cap on one request's input, "Input token exceed the limit"
|
||||
// (#2445: B.ai, under the model's window, which another member holds).
|
||||
// The third line is Chinese that names the input rather than the context
|
||||
// (#4420, MYCF711): a group member's 400 "内容太长了,请精简输入或开启新对话",
|
||||
// z.ai's code 1261 "Prompt 超长", iFlytek Spark's 10907 "token数量超过上限。
|
||||
// 对话历史+问题的字数太多,需要精简输入", and "输入长度超过限制" /
|
||||
// "输入内容超过模型最大token数". Each names what was sent (内容, 输入,
|
||||
// 提示词, prompt…) beside its length, so "名称超出最大长度" (a field) or
|
||||
// "输出过长" (the reply) isn't taken; bare "token数量超过上限" isn't either,
|
||||
// as a day's allowance is said so too.
|
||||
var tooLongRe = regexp.MustCompile(`(?i)context_length_exceeded|prompt is too long|input is too long|(exceeds?|exceeded|over|beyond)( the)?( model'?s?)?( maximum)? context|context (length|limit|window) (exceeded|is exceeded)|maximum context length|too many (input |prompt )?tokens|上下文(长度)?(超|过长)|超(过|出)(了)?(模型)?(的)?(最大)?上下文` +
|
||||
`|input token count.*exceeds the maximum|maximum prompt length is \d|reduce the length of the messages|maximum allowed input length|longer than the model'?s context length|available context size|greater than the context length|prompt token count of [\d,]+ exceeds the limit|context window exceeds limit|exceeded model token limit|configured context size|prompt too long|prompt exceeds max length|context_window_exceeded|exceeded (max |maximum )?context length|range of input length should be|token limit exceeded|input tokens? (count )?(exceeds?|exceeded) (the )?(max(imum)? )?limit`)
|
||||
`|input token count.*exceeds the maximum|maximum prompt length is \d|reduce the length of the messages|maximum allowed input length|longer than the model'?s context length|available context size|greater than the context length|prompt token count of [\d,]+ exceeds the limit|context window exceeds limit|exceeded model token limit|configured context size|prompt too long|prompt exceeds max length|context_window_exceeded|exceeded (max |maximum )?context length|range of input length should be|token limit exceeded|input tokens? (count )?(exceeds?|exceeded) (the )?(max(imum)? )?limit` +
|
||||
`|(内容|输入|文本|提示词?|prompt|问题|对话|消息)(内容|文本)?(长度)?\s*(太长|过长|超长)|字数(太多|过多)` +
|
||||
`|(内容|输入|文本|提示词?|prompt|问题|对话|消息)[^,。,.;;!!]{0,12}超(过|出)[^,。,.;;!!]{0,12}(长度|token|令牌|字数)` +
|
||||
`|(输入|提示词?|prompt)\s*的?\s*(长度|token|令牌|字数)[^,。,.;;!!]{0,8}超(过|出)`)
|
||||
|
||||
// overflowed says a refused request was too long for the model — a
|
||||
// request error (400, 413, 422) whose words say so.
|
||||
@@ -8047,8 +8058,9 @@ func tooLong(status int, msg string) bool {
|
||||
return tooLongRe.MatchString(msg)
|
||||
}
|
||||
|
||||
// paceWords say a limit on how fast or how much, not on one prompt's size.
|
||||
var paceWords = regexp.MustCompile(`(?i)rate.?limit|per (minute|hour|day)|\bTP[MD]\b|please wait|try again later|throttl`)
|
||||
// paceWords say a limit on how fast or how much, not on one prompt's size,
|
||||
// in Chinese too (#4420): "内容太长,已达到速率限制" is the momentary cap.
|
||||
var paceWords = regexp.MustCompile(`(?i)rate.?limit|per (minute|hour|day)|\bTP[MD]\b|please wait|try again later|throttl|速率|频繁|频率|限流|并发|每(秒|分钟|小时|天|日)|稍后(再试|重试)`)
|
||||
|
||||
// splitSent tells what of sent, the ms from the request (start) to its
|
||||
// answering try's body written, was magpie's own work, the tries that
|
||||
|
||||
@@ -62,3 +62,34 @@ func TestRelayInputCapGoesToTheNextMember(t *testing.T) {
|
||||
t.Fatalf("bad request: %d %s (a %d, b %d)", code, body, a.n, b.n)
|
||||
}
|
||||
}
|
||||
|
||||
// #4420 (MYCF711 on Discord): a group member's 400 "内容太长了,请精简输入
|
||||
// 或开启新对话", past ~730K on a model it declares at 1M, names the input
|
||||
// rather than the context. It was filed as a failure that rested the
|
||||
// member, and the group's other members were never asked. It goes the way
|
||||
// an overflow goes: the next member is asked and nobody rests, in the
|
||||
// vendor's words and \u-escaped alike.
|
||||
func TestChineseInputTooLongGoesToTheNextMember(t *testing.T) {
|
||||
for _, said := range []string{
|
||||
`{"error":{"message":"内容太长了,请精简输入或开启新对话","type":"invalid_request_error"}}`,
|
||||
`{"error":{"message":"内容太长了,请精简输入或开启新对话"}}`,
|
||||
} {
|
||||
fresh(t)
|
||||
a := &scripted{replies: []reply{{400, "", said}}}
|
||||
b := &scripted{replies: []reply{{200, "", chatOK}}}
|
||||
scriptedOn(t, "a", provider.Chat, a)
|
||||
scriptedOn(t, "b", provider.Chat, b)
|
||||
if err := provider.SaveGroup(provider.Group{Name: "Top", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := New()
|
||||
code, body := postAs(t, s, "", `{"model":"group/top","messages":[{"role":"user","content":"hi"}]}`)
|
||||
if code != 200 || !strings.Contains(body, "hello") || a.n != 1 || b.n != 1 {
|
||||
t.Fatalf("%s: %d %s (a %d, b %d)", said, code, body, a.n, b.n)
|
||||
}
|
||||
r := s.trace.routes[len(s.trace.routes)-1]
|
||||
if len(r.Tries) != 2 || r.Tries[0].Fail != failOverflow || r.Tries[0].Rest != nil || r.Tries[1].Rest != nil {
|
||||
t.Fatalf("%s: tries %+v", said, r.Tries)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -274,7 +274,7 @@ var (
|
||||
// "rate_limit_error" for its type, every such 429 was, and rested a
|
||||
// quarter of an hour rather than a minute (#153).
|
||||
// "Frequency limit" is the English of 频率 (zoecats on Discord).
|
||||
rateWords = regexp.MustCompile(`(?i)rate.?limit|frequency.?limit|too many requests|per.?(second|sec|minute|min)\b|\b[rt]pm\b|频率|太频繁`)
|
||||
rateWords = regexp.MustCompile(`(?i)rate.?limit|frequency.?limit|too many requests|per.?(second|sec|minute|min)\b|\b[rt]pm\b|频率|太频繁|过于频繁|速率`)
|
||||
// plannedWords: a 429 that says the plan's own allowance is used, rate
|
||||
// words or not — a day's free requests, say, or a sub2api key's own
|
||||
// 5-hour, day or 7-day limit ("api key 7天限额已用完", sent as
|
||||
|
||||
@@ -83,6 +83,15 @@ func TestEveryVendorsOverflow(t *testing.T) {
|
||||
{400, "prompt too long; exceeded max context length by 100 tokens"},
|
||||
{400, "Range of input length should be [1, 98304]"},
|
||||
{400, "token limit exceeded"},
|
||||
// Chinese that names the input, not the context (#4420, MYCF711)
|
||||
{400, "内容太长了,请精简输入或开启新对话"},
|
||||
{400, `{"error":{"code":"1261","message":"Prompt 超长"}}`},
|
||||
{400, "token数量超过上限。对话历史+问题的字数太多,需要精简输入"},
|
||||
{400, "输入过长,请缩短后重试"},
|
||||
{400, "输入长度超过限制"},
|
||||
{400, "输入内容超过模型最大token数"},
|
||||
{400, "提示词超出最大长度"},
|
||||
{413, "请求内容过长"},
|
||||
} {
|
||||
if !tooLong(tc.status, tc.msg) {
|
||||
t.Errorf("%q isn't taken for a conversation too long", tc.msg)
|
||||
@@ -100,6 +109,40 @@ func TestEveryVendorsOverflow(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A Chinese pace limit beside words of size, or a length that isn't the
|
||||
// conversation's, isn't taken for a conversation too long (#4420): told
|
||||
// so, the agent compacts for nothing, and a pace limit worded so would
|
||||
// never be asked of the next account.
|
||||
func TestChineseNotTooLong(t *testing.T) {
|
||||
for _, msg := range []string{
|
||||
"内容太长,已达到速率限制",
|
||||
"请求过于频繁,输入内容过长的请求请稍后再试",
|
||||
"输入token数超过每分钟限制",
|
||||
"输入过长:已超过并发上限",
|
||||
"每日token数量超过上限",
|
||||
"token数量超过上限",
|
||||
"名称超出最大长度",
|
||||
"输出过长,请调小 max_tokens",
|
||||
"文件名过长",
|
||||
"参数错误",
|
||||
"服务出现问题,请联系管理员",
|
||||
"模型不存在",
|
||||
} {
|
||||
if tooLong(400, msg) {
|
||||
t.Errorf("%q taken for a conversation too long", msg)
|
||||
}
|
||||
}
|
||||
// the pace limit said with size still goes to the next account
|
||||
if !retryable(400, []byte("内容太长,已达到速率限制")) {
|
||||
t.Error("a pace limit worded with size isn't asked of the next account")
|
||||
}
|
||||
for _, msg := range []string{"请求过于频繁", "已达到速率限制"} {
|
||||
if f := failure(429, []byte(msg)); f != failRate {
|
||||
t.Errorf("429 %q is a %s failure", msg, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A spent budget, like a used-up plan (pi's retry.ts lists both as not to
|
||||
// be retried), goes to the next account at once rather than being asked
|
||||
// again in place: "out of budget" was a rate limit, asked three times.
|
||||
|
||||
Reference in New Issue
Block a user