feat(ml): fuse two embedding models and protect the ML service with a token
This commit is contained in:
1 parent
bc00d6901f
commit
b24e5ff906
63 files changed
+2287
-276
No files matched your search
@@ -0,0 +1,72 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"math"
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEnsembleSemanticsPolicyIsValidAndKeepsTheGigaExplanation(t *testing.T) {
|
||||
ensemble := DemoMatchingSemanticsEnsemblePolicy()
|
||||
if err := ensemble.Validate(); err != nil {
|
||||
t.Fatalf("ensemble Validate() = %v", err)
|
||||
}
|
||||
single := DemoMatchingSemanticsPolicy()
|
||||
if ensemble.ExplanationModel() != single.ExplanationModel() || ensemble.ExplanationModel().ID != GigaEmbeddingsModelID {
|
||||
t.Fatalf("explanation model = %+v, want Giga", ensemble.ExplanationModel())
|
||||
}
|
||||
if ensemble.SkillMedium != single.SkillMedium || ensemble.SkillHigh != single.SkillHigh ||
|
||||
ensemble.TaskMedium != single.TaskMedium || ensemble.TaskHigh != single.TaskHigh {
|
||||
t.Fatal("the explanation thresholds must stay those of Giga")
|
||||
}
|
||||
if ensemble.Members[1].Model != (SemanticModel{ID: Qwen3EmbeddingModelID, Revision: Qwen3EmbeddingModelRevision, PromptVersion: Qwen3EmbeddingPromptVersion}) {
|
||||
t.Fatalf("second member = %+v, want the pinned Qwen3-Embedding", ensemble.Members[1].Model)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsembleSemanticsMapsTheFusedScoreMonotonically(t *testing.T) {
|
||||
ensemble := DemoMatchingSemanticsEnsemblePolicy()
|
||||
// At the member means every standardized cosine, and so the fused score, is 0.
|
||||
score, ok := ensemble.FusedScore([]float64{0.2497, 0.3636})
|
||||
if !ok || math.Abs(score) > 1e-12 {
|
||||
t.Fatalf("score at the means = %v, %v, want 0", score, ok)
|
||||
}
|
||||
if low, high := ensemble.Probability(-1), ensemble.Probability(10); low != 0 || high != 1_000_000 {
|
||||
t.Fatalf("clipped ends = %d and %d, want 0 and 1", low, high)
|
||||
}
|
||||
previous := int64(-1)
|
||||
for giga := 0.0; giga <= 0.8; giga += 0.02 {
|
||||
score, ok := ensemble.FusedScore([]float64{giga, giga + 0.1})
|
||||
if !ok {
|
||||
t.Fatal("the fused score needs both members")
|
||||
}
|
||||
value := ensemble.Probability(score)
|
||||
if value < previous {
|
||||
t.Fatalf("Sem decreased at cosine %v: %d after %d", giga, value, previous)
|
||||
}
|
||||
previous = value
|
||||
}
|
||||
}
|
||||
|
||||
func TestProductPolicyV6CarriesTheEnsembleAndRoundTrips(t *testing.T) {
|
||||
v6 := DemoProductPolicyV6()
|
||||
if err := v6.Validate(); err != nil {
|
||||
t.Fatalf("V6 Validate() = %v", err)
|
||||
}
|
||||
v5 := DemoProductPolicyV5()
|
||||
if v6.Matching.FitSemanticsWeight != v5.Matching.FitSemanticsWeight || len(v5.Matching.Semantics.Members) != 1 {
|
||||
t.Fatal("V6 must keep the Fit weights of V5, and V5 must stay the single-model version")
|
||||
}
|
||||
encoded, _, err := encodeProductPolicy(v6)
|
||||
if err != nil {
|
||||
t.Fatalf("encodeProductPolicy(V6) error = %v", err)
|
||||
}
|
||||
decoded, err := decodeProductPolicy(encoded)
|
||||
if err != nil {
|
||||
t.Fatalf("decodeProductPolicy(V6) error = %v", err)
|
||||
}
|
||||
if !slices.Equal(decoded.Matching.Semantics.Members, v6.Matching.Semantics.Members) ||
|
||||
!slices.Equal(decoded.Matching.Semantics.Calibration, v6.Matching.Semantics.Calibration) {
|
||||
t.Fatal("the ensemble section did not survive the document round trip")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user