a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
36 lines
1.4 KiB
Go
36 lines
1.4 KiB
Go
package embedding
|
|
|
|
import "fmt"
|
|
|
|
// validateBatch checks that an embedding backend returned exactly one vector per
|
|
// input, that no vector is empty, and that every vector has the expected width.
|
|
//
|
|
// dims is the expected width. Pass a provider's known dimension (e.g. a Hugot
|
|
// model's 384) for a strict check; pass 0 when the width is not yet known — an
|
|
// API provider learns its width lazily from the first response — and the width
|
|
// is taken from the first returned vector so the batch is still checked for
|
|
// internal consistency (a ragged batch) without asserting an absolute size.
|
|
//
|
|
// A violation returns a descriptive error naming the offending index so a
|
|
// provider regression that returns nil, short, or mis-counted vectors surfaces
|
|
// as a loud, attributable failure instead of a silently empty or mis-dimensioned
|
|
// index.
|
|
func validateBatch(name string, texts []string, vecs [][]float32, dims int) error {
|
|
if len(vecs) != len(texts) {
|
|
return fmt.Errorf("%s returned %d vectors for %d inputs", name, len(vecs), len(texts))
|
|
}
|
|
expected := dims
|
|
if expected <= 0 && len(vecs) > 0 {
|
|
expected = len(vecs[0])
|
|
}
|
|
for i, v := range vecs {
|
|
if len(v) == 0 {
|
|
return fmt.Errorf("%s returned an empty vector at index %d of %d", name, i, len(vecs))
|
|
}
|
|
if expected > 0 && len(v) != expected {
|
|
return fmt.Errorf("%s returned a width-%d vector at index %d, want %d", name, len(v), i, expected)
|
|
}
|
|
}
|
|
return nil
|
|
}
|