-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting_helpers.go
More file actions
74 lines (65 loc) · 1.77 KB
/
Copy pathtesting_helpers.go
File metadata and controls
74 lines (65 loc) · 1.77 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
package contexty
import (
"context"
"errors"
"fmt"
)
// FixedEstimator returns deterministic token counts for tests.
type FixedEstimator struct {
TokensPerMessage int
TokensPerContentPart int
TokensPerToolCall int
}
// Estimate returns total token weight.
func (c *FixedEstimator) Estimate(ctx context.Context, msgs []Message) (int, error) {
weights, err := c.EstimatePerMessage(ctx, msgs)
if err != nil {
return 0, err
}
total := 0
for _, w := range weights {
total += w
}
return total, nil
}
// EstimatePerMessage returns per-message weights.
func (c *FixedEstimator) EstimatePerMessage(ctx context.Context, msgs []Message) ([]int, error) {
if err := ctx.Err(); err != nil {
return nil, fmt.Errorf("contexty: fixed estimator: %w", err)
}
out := make([]int, len(msgs))
for i, m := range msgs {
if err := ctx.Err(); err != nil {
return nil, fmt.Errorf("contexty: fixed estimator: %w", err)
}
w := c.TokensPerMessage
if c.TokensPerContentPart != 0 {
w += len(m.Parts) * c.TokensPerContentPart
}
if c.TokensPerToolCall != 0 {
w += len(m.ToolCallParts()) * c.TokensPerToolCall
}
out[i] = w
}
return out, nil
}
var _ TokenEstimator = (*FixedEstimator)(nil)
// FailingEstimator always returns Err from Estimate calls (tests).
type FailingEstimator struct {
Err error
}
// Estimate returns the configured error.
func (f *FailingEstimator) Estimate(context.Context, []Message) (int, error) {
if f.Err == nil {
return 0, errors.New("estimate failed")
}
return 0, f.Err
}
// EstimatePerMessage returns the configured error.
func (f *FailingEstimator) EstimatePerMessage(context.Context, []Message) ([]int, error) {
if f.Err == nil {
return nil, errors.New("estimate failed")
}
return nil, f.Err
}
var _ TokenEstimator = (*FailingEstimator)(nil)