build: 内置 norma 模块,构建不再依赖上游仓库
把 github.com/Autumn-27/norma v0.4.3(commit 320ae32)的源码复制到 third_party/norma,并用 go.mod 的 replace 指向本地副本。上游删除仓库或 改写 tag 都不再影响本仓库构建。 - 已验证 GOPROXY=off 下可完整编译并打出发行包 - go.sum 中该模块的两条校验记录随之删除(本地 replace 不需要哈希) - 升级方法与授权注意事项见 third_party/README.md
This commit is contained in:
1 parent
a819fa3230
commit
f782b612ec
213 files changed
+37401
-2
No files matched your search
+126
@@ -0,0 +1,126 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// emptyStop is a completed stream that produced no content (a normal `stop` with
|
||||
// nothing before it) — the degenerate empty completion the retry targets.
|
||||
var emptyStop = sse(`data: {"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}`, ``, `data: [DONE]`, ``)
|
||||
|
||||
// A streamed empty completion is re-requested; a subsequent non-empty attempt
|
||||
// surfaces normally. Exactly two requests: one empty, one recovered.
|
||||
func TestOpenAIStreamEmptyResponseRetries(t *testing.T) {
|
||||
var calls atomic.Int32
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if calls.Add(1) == 1 {
|
||||
w.Write([]byte(emptyStop))
|
||||
return
|
||||
}
|
||||
w.Write([]byte(sse(`data: {"choices":[{"index":0,"delta":{"content":"recovered"}}]}`, ``, `data: [DONE]`, ``)))
|
||||
}))
|
||||
defer srv.Close()
|
||||
p, _ := NewProvider(Config{Format: FormatOpenAI, BaseURL: srv.URL, APIKey: "k", Model: "m"})
|
||||
msg := collect(t, p, CompletionRequest{Messages: []Message{UserText("go")}})
|
||||
if msg.Text() != "recovered" {
|
||||
t.Fatalf("text=%q", msg.Text())
|
||||
}
|
||||
if calls.Load() != 2 {
|
||||
t.Fatalf("calls=%d, want 2 (one empty + one retry)", calls.Load())
|
||||
}
|
||||
}
|
||||
|
||||
// An always-empty stream is retried a bounded number of times, then surfaced as
|
||||
// an empty message rather than looping forever.
|
||||
func TestOpenAIStreamEmptyResponseExhausts(t *testing.T) {
|
||||
var calls atomic.Int32
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
calls.Add(1)
|
||||
w.Write([]byte(emptyStop))
|
||||
}))
|
||||
defer srv.Close()
|
||||
p, _ := NewProvider(Config{Format: FormatOpenAI, BaseURL: srv.URL, APIKey: "k", Model: "m"})
|
||||
msg := collect(t, p, CompletionRequest{Messages: []Message{UserText("go")}})
|
||||
if len(msg.Content) != 0 {
|
||||
t.Fatalf("want empty message after exhaustion, got %+v", msg.Content)
|
||||
}
|
||||
if calls.Load() != int32(emptyResponseRetries+1) {
|
||||
t.Fatalf("calls=%d, want %d", calls.Load(), emptyResponseRetries+1)
|
||||
}
|
||||
}
|
||||
|
||||
// A max_tokens truncation with no content must NOT retry — that recovery is the
|
||||
// harness's to drive, and re-requesting would fight it.
|
||||
func TestOpenAIStreamEmptyMaxTokensNoRetry(t *testing.T) {
|
||||
var calls atomic.Int32
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
calls.Add(1)
|
||||
w.Write([]byte(sse(`data: {"choices":[{"index":0,"delta":{},"finish_reason":"length"}]}`, ``, `data: [DONE]`, ``)))
|
||||
}))
|
||||
defer srv.Close()
|
||||
p, _ := NewProvider(Config{Format: FormatOpenAI, BaseURL: srv.URL, APIKey: "k", Model: "m"})
|
||||
_ = collect(t, p, CompletionRequest{Messages: []Message{UserText("go")}})
|
||||
if calls.Load() != 1 {
|
||||
t.Fatalf("calls=%d, want 1 (max_tokens must not retry)", calls.Load())
|
||||
}
|
||||
}
|
||||
|
||||
// A stream that produces content on the first attempt is never retried.
|
||||
func TestOpenAIStreamContentNoRetry(t *testing.T) {
|
||||
var calls atomic.Int32
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
calls.Add(1)
|
||||
w.Write([]byte(sse(`data: {"choices":[{"index":0,"delta":{"content":"hi"},"finish_reason":"stop"}]}`, ``, `data: [DONE]`, ``)))
|
||||
}))
|
||||
defer srv.Close()
|
||||
p, _ := NewProvider(Config{Format: FormatOpenAI, BaseURL: srv.URL, APIKey: "k", Model: "m"})
|
||||
msg := collect(t, p, CompletionRequest{Messages: []Message{UserText("go")}})
|
||||
if msg.Text() != "hi" || calls.Load() != 1 {
|
||||
t.Fatalf("text=%q calls=%d", msg.Text(), calls.Load())
|
||||
}
|
||||
}
|
||||
|
||||
// The non-streaming path retries an empty completion the same way.
|
||||
func TestOpenAICompleteEmptyResponseRetries(t *testing.T) {
|
||||
var calls atomic.Int32
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("content-type", "application/json")
|
||||
if calls.Add(1) == 1 {
|
||||
w.Write([]byte(`{"choices":[{"message":{"content":""},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":0}}`))
|
||||
return
|
||||
}
|
||||
w.Write([]byte(`{"choices":[{"message":{"content":"recovered"},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":2}}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
p, _ := NewProvider(Config{Format: FormatOpenAI, BaseURL: srv.URL, APIKey: "k", Model: "m"})
|
||||
msg, _, _, err := p.Complete(context.Background(), CompletionRequest{Messages: []Message{UserText("go")}})
|
||||
if err != nil {
|
||||
t.Fatalf("Complete err: %v", err)
|
||||
}
|
||||
if msg.Text() != "recovered" {
|
||||
t.Fatalf("text=%q", msg.Text())
|
||||
}
|
||||
if calls.Load() != 2 {
|
||||
t.Fatalf("calls=%d, want 2", calls.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsEmptyResponseRetryable(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
stop string
|
||||
want bool
|
||||
}{
|
||||
{"end_turn", true},
|
||||
{"", true},
|
||||
{"max_tokens", false},
|
||||
{"tool_use", false},
|
||||
} {
|
||||
if got := isEmptyResponseRetryable(tc.stop); got != tc.want {
|
||||
t.Fatalf("isEmptyResponseRetryable(%q)=%v, want %v", tc.stop, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user