p2.2: cap embed inputs at 3000 runes, retry too-large 500
Link-heavy chunks estimated under EmbedMaxTok still overflow Dixie's 2048-token embed slot. Cap every embed input at 3000 runes. A 500 whose body contains 'too large' halves inputs and retries once. Fallback logs the truncated upstream body.
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func sseHandler(t *testing.T, reply string, saw *atomic.Pointer[chatReq], disconnected *atomic.Bool) http.Handler {
|
||||
@@ -289,3 +290,47 @@ func TestEmbedUsesConfiguredModel(t *testing.T) {
|
||||
t.Fatalf("default model %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmbedTooLargeRetriesAndCompletes(t *testing.T) {
|
||||
var n500 atomic.Int32
|
||||
var saw atomic.Pointer[chatReq]
|
||||
var disc atomic.Bool
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/embed", func(w http.ResponseWriter, r *http.Request) {
|
||||
var body struct {
|
||||
Input []string `json:"input"`
|
||||
}
|
||||
_ = json.NewDecoder(r.Body).Decode(&body)
|
||||
for _, s := range body.Input {
|
||||
if utf8.RuneCountInString(s) > 1500 {
|
||||
n500.Add(1)
|
||||
w.WriteHeader(500)
|
||||
_, _ = w.Write([]byte("input (2064 tokens) is too large to process. (current batch size: 2048)"))
|
||||
return
|
||||
}
|
||||
}
|
||||
var data []map[string]any
|
||||
for i := range body.Input {
|
||||
data = append(data, map[string]any{"index": i, "embedding": []float64{1, float64(i)}})
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"data": data})
|
||||
})
|
||||
mux.Handle("/chat", sseHandler(t, "ok from doc", &saw, &disc))
|
||||
ts := httptest.NewServer(mux)
|
||||
defer ts.Close()
|
||||
e := &Engine{
|
||||
ChatURL: ts.URL + "/chat", EmbedURL: ts.URL + "/embed",
|
||||
Gate: NewGate(2, 8, time.Second), Client: ts.Client(),
|
||||
}
|
||||
cleaned := strings.Repeat("https://example.com/path ", 4000)
|
||||
r := e.Run(context.Background(), cleaned, "what is this?", "https://ex.test/pypi")
|
||||
if r.Fallback != "" {
|
||||
t.Fatalf("fallback %s text %q", r.Fallback, r.Text)
|
||||
}
|
||||
if n500.Load() < 1 {
|
||||
t.Fatal("want a too-large 500 then retry")
|
||||
}
|
||||
if !strings.Contains(r.Text, "ok") {
|
||||
t.Fatalf("text %q", r.Text)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user