-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch.go
More file actions
393 lines (335 loc) · 11.2 KB
/
batch.go
File metadata and controls
393 lines (335 loc) · 11.2 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package ai
import (
"context"
"fmt"
"sync"
"time"
)
// ═══════════════════════════════════════════════════════════════════════════
// Batch Processing
// ═══════════════════════════════════════════════════════════════════════════
// BatchResult holds a single result from batch processing.
type BatchResult struct {
Index int // original index in batch
Content string // response content
Error error // error if any
Model Model // model used
Tokens int // tokens used
Latency time.Duration // request latency
}
// BatchConfig configures batch processing.
type BatchConfig struct {
MaxConcurrency int // max parallel requests (default: 5)
Timeout time.Duration // per-request timeout (0 = no timeout)
StopOnError bool // stop all on first error
RetryConfig *RetryConfig // retry config for failed requests
}
// DefaultBatchConfig returns sensible defaults.
func DefaultBatchConfig() *BatchConfig {
return &BatchConfig{
MaxConcurrency: 5,
Timeout: 0,
StopOnError: false,
RetryConfig: nil,
}
}
// ═══════════════════════════════════════════════════════════════════════════
// Batch Builder - Fluent API
// ═══════════════════════════════════════════════════════════════════════════
// BatchBuilder provides a fluent API for batch processing.
type BatchBuilder struct {
builders []*Builder
config *BatchConfig
ctx context.Context
}
// Batch creates a new batch builder.
func Batch(builders ...*Builder) *BatchBuilder {
return &BatchBuilder{
builders: builders,
config: DefaultBatchConfig(),
ctx: context.Background(),
}
}
// Add adds more builders to the batch.
func (b *BatchBuilder) Add(builders ...*Builder) *BatchBuilder {
b.builders = append(b.builders, builders...)
return b
}
// Concurrency sets max parallel requests.
func (b *BatchBuilder) Concurrency(n int) *BatchBuilder {
if n < 1 {
n = 1
}
b.config.MaxConcurrency = n
return b
}
// Timeout sets per-request timeout.
func (b *BatchBuilder) Timeout(d time.Duration) *BatchBuilder {
b.config.Timeout = d
return b
}
// StopOnError stops all processing on first error.
func (b *BatchBuilder) StopOnError() *BatchBuilder {
b.config.StopOnError = true
return b
}
// WithRetry sets retry config for failed requests.
func (b *BatchBuilder) WithRetry(config *RetryConfig) *BatchBuilder {
b.config.RetryConfig = config
return b
}
// WithContext sets the context for all requests.
func (b *BatchBuilder) WithContext(ctx context.Context) *BatchBuilder {
b.ctx = ctx
return b
}
// Config sets a custom batch config.
func (b *BatchBuilder) Config(config *BatchConfig) *BatchBuilder {
b.config = config
return b
}
// ═══════════════════════════════════════════════════════════════════════════
// Execution
// ═══════════════════════════════════════════════════════════════════════════
// Do executes all requests and returns results.
func (b *BatchBuilder) Do() []BatchResult {
if len(b.builders) == 0 {
return nil
}
results := make([]BatchResult, len(b.builders))
var wg sync.WaitGroup
var mu sync.Mutex
var stopped bool
// Create semaphore for concurrency control
sem := make(chan struct{}, b.config.MaxConcurrency)
// Create cancelable context if StopOnError is set
ctx := b.ctx
var cancel context.CancelFunc
if b.config.StopOnError {
ctx, cancel = context.WithCancel(b.ctx)
defer cancel()
}
if Debug {
fmt.Printf("%s Batch: processing %d requests (concurrency=%d)\n",
colorCyan("→"), len(b.builders), b.config.MaxConcurrency)
}
start := time.Now()
for i, builder := range b.builders {
wg.Add(1)
go func(idx int, bldr *Builder) {
defer wg.Done()
// Check if stopped
mu.Lock()
if stopped {
mu.Unlock()
return
}
mu.Unlock()
// Acquire semaphore
select {
case sem <- struct{}{}:
defer func() { <-sem }()
case <-ctx.Done():
results[idx] = BatchResult{
Index: idx,
Error: ctx.Err(),
Model: bldr.model,
}
return
}
// Apply timeout if configured
reqCtx := ctx
if b.config.Timeout > 0 {
var reqCancel context.CancelFunc
reqCtx, reqCancel = context.WithTimeout(ctx, b.config.Timeout)
defer reqCancel()
}
// Apply retry config if set
if b.config.RetryConfig != nil && bldr.retryConfig == nil {
bldr = bldr.Clone()
bldr.retryConfig = b.config.RetryConfig
}
// Execute request
reqStart := time.Now()
bldr.ctx = reqCtx
meta := bldr.SendWithMeta()
results[idx] = BatchResult{
Index: idx,
Content: meta.Content,
Error: meta.Error,
Model: meta.Model,
Tokens: meta.Tokens,
Latency: time.Since(reqStart),
}
// Stop on error if configured
if meta.Error != nil && b.config.StopOnError {
mu.Lock()
stopped = true
mu.Unlock()
if cancel != nil {
cancel()
}
}
}(i, builder)
}
wg.Wait()
if Debug {
successCount := 0
for _, r := range results {
if r.Error == nil {
successCount++
}
}
fmt.Printf("%s Batch complete: %d/%d succeeded in %v\n",
colorGreen("✓"), successCount, len(results), time.Since(start).Round(time.Millisecond))
}
return results
}
// DoStrings executes and returns just the content strings.
func (b *BatchBuilder) DoStrings() ([]string, error) {
results := b.Do()
var firstErr error
strings := make([]string, len(results))
for i, r := range results {
strings[i] = r.Content
if r.Error != nil && firstErr == nil {
firstErr = r.Error
}
}
return strings, firstErr
}
// ═══════════════════════════════════════════════════════════════════════════
// Quick Batch Helpers
// ═══════════════════════════════════════════════════════════════════════════
// BatchPrompts creates a batch from multiple prompts using the same model.
func BatchPrompts(model Model, prompts ...string) *BatchBuilder {
builders := make([]*Builder, len(prompts))
for i, prompt := range prompts {
builders[i] = New(model).User(prompt)
}
return Batch(builders...)
}
// BatchPromptsWithSystem creates a batch with a shared system prompt.
func BatchPromptsWithSystem(model Model, system string, prompts ...string) *BatchBuilder {
builders := make([]*Builder, len(prompts))
for i, prompt := range prompts {
builders[i] = New(model).System(system).User(prompt)
}
return Batch(builders...)
}
// BatchModels sends the same prompt to multiple models.
func BatchModels(prompt string, models ...Model) *BatchBuilder {
builders := make([]*Builder, len(models))
for i, model := range models {
builders[i] = New(model).User(prompt)
}
return Batch(builders...)
}
// ═══════════════════════════════════════════════════════════════════════════
// Batch Result Helpers
// ═══════════════════════════════════════════════════════════════════════════
// BatchResults wraps []BatchResult with helper methods.
type BatchResults []BatchResult
// Successful returns only successful results.
func (r BatchResults) Successful() BatchResults {
var results BatchResults
for _, res := range r {
if res.Error == nil {
results = append(results, res)
}
}
return results
}
// Failed returns only failed results.
func (r BatchResults) Failed() BatchResults {
var results BatchResults
for _, res := range r {
if res.Error != nil {
results = append(results, res)
}
}
return results
}
// Errors returns all errors.
func (r BatchResults) Errors() []error {
var errors []error
for _, res := range r {
if res.Error != nil {
errors = append(errors, res.Error)
}
}
return errors
}
// Contents returns all content strings (empty for errors).
func (r BatchResults) Contents() []string {
contents := make([]string, len(r))
for i, res := range r {
contents[i] = res.Content
}
return contents
}
// TotalTokens returns sum of all tokens used.
func (r BatchResults) TotalTokens() int {
var total int
for _, res := range r {
total += res.Tokens
}
return total
}
// TotalLatency returns sum of all latencies.
func (r BatchResults) TotalLatency() time.Duration {
var total time.Duration
for _, res := range r {
total += res.Latency
}
return total
}
// SuccessRate returns the success rate (0-1).
func (r BatchResults) SuccessRate() float64 {
if len(r) == 0 {
return 0
}
return float64(len(r.Successful())) / float64(len(r))
}
// ═══════════════════════════════════════════════════════════════════════════
// Fan-Out / Fan-In Patterns
// ═══════════════════════════════════════════════════════════════════════════
// FanOut sends a prompt to multiple models and returns the first success.
func FanOut(prompt string, models ...Model) (string, Model, error) {
if len(models) == 0 {
return "", "", fmt.Errorf("no models provided")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
type result struct {
content string
model Model
err error
}
resultChan := make(chan result, len(models))
for _, model := range models {
go func(m Model) {
builder := New(m).User(prompt).WithContext(ctx)
meta := builder.SendWithMeta()
select {
case resultChan <- result{meta.Content, m, meta.Error}:
case <-ctx.Done():
}
}(model)
}
// Wait for first success or all failures
var errors []error
for i := 0; i < len(models); i++ {
res := <-resultChan
if res.err == nil {
cancel() // Cancel remaining requests
return res.content, res.model, nil
}
errors = append(errors, res.err)
}
return "", "", fmt.Errorf("all %d models failed: %v", len(models), errors)
}
// Race is an alias for FanOut.
func Race(prompt string, models ...Model) (string, Model, error) {
return FanOut(prompt, models...)
}