-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider_stream_test.go
More file actions
166 lines (157 loc) · 5.12 KB
/
provider_stream_test.go
File metadata and controls
166 lines (157 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package ai
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestProviders_SendStream_ParsesChunks(t *testing.T) {
tests := []struct {
name string
makeProv func(baseURL string) Provider
path string
writeBody func(w http.ResponseWriter)
wantFull string
wantTokens func(full string) (prompt, completion, total int)
}{
{
name: "openai",
makeProv: func(baseURL string) Provider {
return NewOpenAIProvider(ProviderConfig{APIKey: "k", BaseURL: baseURL})
},
path: "/chat/completions",
writeBody: func(w http.ResponseWriter) {
_, _ = w.Write([]byte("data: {\"choices\":[{\"delta\":{\"content\":\"Hel\"}}]}\n"))
_, _ = w.Write([]byte("data: {\"choices\":[{\"delta\":{\"content\":\"lo\"}}]}\n"))
_, _ = w.Write([]byte("data: [DONE]\n"))
},
wantFull: "Hello",
wantTokens: func(full string) (int, int, int) {
// streaming estimates completion tokens as len/4
ct := len(full) / 4
return 0, ct, ct
},
},
{
name: "openrouter",
makeProv: func(baseURL string) Provider {
return NewOpenRouterProvider(ProviderConfig{APIKey: "k", BaseURL: baseURL})
},
path: "/chat/completions",
writeBody: func(w http.ResponseWriter) {
_, _ = w.Write([]byte("data: {\"choices\":[{\"delta\":{\"content\":\"A\"}}]}\n"))
_, _ = w.Write([]byte("data: {\"choices\":[{\"delta\":{\"content\":\"B\"}}]}\n"))
_, _ = w.Write([]byte("data: [DONE]\n"))
},
wantFull: "AB",
wantTokens: func(full string) (int, int, int) {
ct := len(full) / 4
return 0, ct, ct
},
},
{
name: "anthropic",
makeProv: func(baseURL string) Provider {
return NewAnthropicProvider(ProviderConfig{APIKey: "k", BaseURL: baseURL})
},
path: "/messages",
writeBody: func(w http.ResponseWriter) {
_, _ = w.Write([]byte("data: {\"type\":\"content_block_delta\",\"delta\":{\"type\":\"text_delta\",\"text\":\"Hi\"}}\n"))
_, _ = w.Write([]byte("data: {\"type\":\"message_stop\"}\n"))
},
wantFull: "Hi",
wantTokens: func(full string) (int, int, int) {
ct := len(full) / 4
return 0, ct, ct
},
},
{
name: "google",
makeProv: func(baseURL string) Provider {
return NewGoogleProvider(ProviderConfig{APIKey: "k", BaseURL: baseURL})
},
// google uses full URL with query params; we'll match by prefix.
path: "/models/gemini-3-flash-preview:streamGenerateContent",
writeBody: func(w http.ResponseWriter) {
_, _ = w.Write([]byte("data: {\"candidates\":[{\"content\":{\"parts\":[{\"text\":\"Yo\"}]}}]}\n"))
_, _ = w.Write([]byte("data: {\"candidates\":[{\"content\":{\"parts\":[{\"text\":\"!\"}]}}]}\n"))
},
wantFull: "Yo!",
wantTokens: func(full string) (int, int, int) {
ct := len(full) / 4
return 0, ct, ct
},
},
{
name: "ollama",
makeProv: func(baseURL string) Provider {
return NewOllamaProvider(ProviderConfig{BaseURL: baseURL})
},
path: "/api/chat",
writeBody: func(w http.ResponseWriter) {
_, _ = w.Write([]byte("{\"model\":\"m\",\"message\":{\"role\":\"assistant\",\"content\":\"He\"},\"done\":false}\n"))
_, _ = w.Write([]byte("{\"model\":\"m\",\"message\":{\"role\":\"assistant\",\"content\":\"y\"},\"done\":true,\"prompt_eval_count\":2,\"eval_count\":3}\n"))
},
wantFull: "Hey",
wantTokens: func(full string) (int, int, int) {
return 2, 3, 5
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cleanup := withTestGlobals(t)
defer cleanup()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch tt.name {
case "google":
if !strings.HasPrefix(r.URL.Path, tt.path) {
t.Fatalf("expected path prefix %q, got %q", tt.path, r.URL.Path)
}
default:
if r.URL.Path != tt.path {
t.Fatalf("expected path %q, got %q", tt.path, r.URL.Path)
}
}
w.Header().Set("Content-Type", "text/event-stream")
tt.writeBody(w)
}))
defer srv.Close()
p := tt.makeProv(srv.URL)
var chunks []string
resp, err := p.SendStream(context.Background(), &ProviderRequest{
Model: string(ModelGemini3Flash),
Messages: []Message{{Role: "user", Content: "hi"}},
Stream: true,
}, func(chunk string) {
chunks = append(chunks, chunk)
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resp.Content != tt.wantFull {
t.Fatalf("expected full content %q, got %q", tt.wantFull, resp.Content)
}
joined := strings.Join(chunks, "")
if joined != tt.wantFull {
t.Fatalf("expected streamed chunks %q, got %q", tt.wantFull, joined)
}
// Ensure callback ordering is preserved.
if bytes.Join(func() [][]byte {
out := make([][]byte, 0, len(chunks))
for _, c := range chunks {
out = append(out, []byte(c))
}
return out
}(), nil); false {
// no-op: keeps imports stable if you tweak chunk checks later
}
pt, ct, ttok := tt.wantTokens(tt.wantFull)
if resp.PromptTokens != pt || resp.CompletionTokens != ct || resp.TotalTokens != ttok {
t.Fatalf("unexpected tokens: prompt=%d completion=%d total=%d", resp.PromptTokens, resp.CompletionTokens, resp.TotalTokens)
}
})
}
}