This commit is contained in:
2026-06-22 08:55:57 +08:00
parent dbb87823e8
commit 6ade6e8fa9
325 changed files with 41131 additions and 855 deletions
+59
View File
@@ -64,3 +64,62 @@ func (f *FakeClient) Stream(ctx context.Context, modelID string, req Request) (<
func (f *FakeClient) CountTokens(ctx context.Context, modelID string, req Request) (int, error) {
return f.TokenCount, nil
}
// FakeEmbedder is a deterministic Embedder for tests. Each input maps to a
// fixed-length vector derived from its bytes, so identical text always yields the
// same vector (lets tests assert idempotency / ranking without a real endpoint).
type FakeEmbedder struct {
Dims int // output dimension; defaults to PlatformEmbeddingDims when 0
Err error // if non-nil, Embed returns it
mu sync.Mutex
Calls int
Inputs []string // accumulated inputs across calls (for assertions)
}
// NewFakeEmbedder constructs a FakeEmbedder with the platform dimension.
func NewFakeEmbedder() *FakeEmbedder { return &FakeEmbedder{Dims: PlatformEmbeddingDims} }
var _ Embedder = (*FakeEmbedder)(nil)
func (e *FakeEmbedder) dims() int {
if e.Dims > 0 {
return e.Dims
}
return PlatformEmbeddingDims
}
// EmbedDims returns the configured dimension.
func (e *FakeEmbedder) EmbedDims(string) int { return e.dims() }
// Embed returns one deterministic vector per input. The vector is a normalized
// projection of a small hash of the input across the dimension, so distinct
// inputs are distinguishable and identical inputs are identical.
func (e *FakeEmbedder) Embed(_ context.Context, _ string, inputs []string) ([][]float32, error) {
e.mu.Lock()
e.Calls++
e.Inputs = append(e.Inputs, inputs...)
err := e.Err
e.mu.Unlock()
if err != nil {
return nil, err
}
d := e.dims()
out := make([][]float32, len(inputs))
for i, s := range inputs {
v := make([]float32, d)
// Seed from a stable rolling hash of the bytes.
var h uint32 = 2166136261
for _, b := range []byte(s) {
h ^= uint32(b)
h *= 16777619
}
for j := 0; j < d; j++ {
h ^= uint32(j) * 2654435761
h *= 16777619
// Map to [-1,1).
v[j] = float32(int32(h)) / float32(1<<31)
}
out[i] = v
}
return out, nil
}