feat(matching): rank with semantic embeddings and explain matches by evidence
This commit is contained in:
1 parent
facc1789b1
commit
24b64d698a
215 files changed
+16951
-327
No files matched your search
@@ -0,0 +1,172 @@
|
||||
package matching
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
// unit returns a normalized two-dimensional vector at the given cosine to
|
||||
// the first axis.
|
||||
func unit(cosine float64) domain.SemanticVector {
|
||||
return domain.SemanticVector{float32(cosine), float32(math.Sqrt(1 - cosine*cosine))}
|
||||
}
|
||||
|
||||
func semanticFixture() needSemantics {
|
||||
policy := domain.DemoMatchingSemanticsPolicy()
|
||||
return needSemantics{
|
||||
policy: &policy,
|
||||
wholes: []domain.SemanticVector{unit(1)},
|
||||
requirements: []needRequirement{
|
||||
{item: domain.SemanticItem{Key: "required_skill:0", Kind: domain.SemanticItemRequiredSkill, Label: "Kafka"}, vector: unit(1), order: 0},
|
||||
{item: domain.SemanticItem{Key: "task:0", Kind: domain.SemanticItemTask, Label: "Make callbacks idempotent"}, vector: unit(1), order: 1},
|
||||
{item: domain.SemanticItem{Key: "task:1", Kind: domain.SemanticItemTask, Label: "Unrelated task"}, vector: domain.SemanticVector{-1, 0}, order: 2},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectEvidenceNamesOnlyWhatTheCardShowsAndOrdersStably(t *testing.T) {
|
||||
semantics := semanticFixture()
|
||||
card := domain.AnonymousCandidateViewDTO{
|
||||
Skills: []domain.CandidateViewSkillCode{domain.CandidateViewSkillKafka},
|
||||
Projects: []domain.CandidateViewProject{{Title: "Ledger", RepositoryVerified: true}},
|
||||
}
|
||||
stored := []outbound.SemanticItemVector{
|
||||
{Key: "stack", Vector: unit(0.40)},
|
||||
{Key: "project:0", LabelDigest: domain.SemanticLabelDigest("Ledger"), Vector: unit(0.50)},
|
||||
}
|
||||
evidence := selectEvidence(semantics, card, stored)
|
||||
if len(evidence) != 2 {
|
||||
t.Fatalf("evidence = %+v, want two items above the thresholds", evidence)
|
||||
}
|
||||
// The project is the closest item to both requirements and reaches the
|
||||
// high band; equal similarities keep the requirement order, and the
|
||||
// unrelated task produces nothing.
|
||||
first, second := evidence[0], evidence[1]
|
||||
if first.RequirementText != "Kafka" || first.Similarity != domain.SemanticSimilarityHigh ||
|
||||
first.ItemKind != domain.SemanticItemProject || first.ProjectTitle == nil || *first.ProjectTitle != "Ledger" || !first.RepositoryVerified {
|
||||
t.Fatalf("first = %+v", first)
|
||||
}
|
||||
if second.RequirementText != "Make callbacks idempotent" || second.RequirementKind != domain.SemanticItemTask {
|
||||
t.Fatalf("second = %+v", second)
|
||||
}
|
||||
if again := selectEvidence(semantics, card, slices.Clone(stored)); !slices.EqualFunc(evidence, again, func(a, b SemanticEvidence) bool {
|
||||
return a.RequirementText == b.RequirementText && a.ItemKind == b.ItemKind && a.Similarity == b.Similarity
|
||||
}) {
|
||||
t.Fatal("the same vectors gave another explanation")
|
||||
}
|
||||
|
||||
t.Run("a stale project title is never shown", func(t *testing.T) {
|
||||
renamed := card
|
||||
renamed.Projects = []domain.CandidateViewProject{{Title: "Renamed"}}
|
||||
for _, item := range selectEvidence(semantics, renamed, stored) {
|
||||
if item.ItemKind == domain.SemanticItemProject {
|
||||
t.Fatalf("evidence names a project whose title changed: %+v", item)
|
||||
}
|
||||
}
|
||||
})
|
||||
t.Run("unpublished projects and a generalized stack explain nothing", func(t *testing.T) {
|
||||
hidden := domain.AnonymousCandidateViewDTO{}
|
||||
if got := selectEvidence(semantics, hidden, stored); len(got) != 0 {
|
||||
t.Fatalf("evidence = %+v for a card that shows neither the stack nor projects", got)
|
||||
}
|
||||
})
|
||||
t.Run("the limit cuts the list", func(t *testing.T) {
|
||||
limited := semanticFixture()
|
||||
limited.policy.MaxEvidencePerEntry = 1
|
||||
if got := selectEvidence(limited, card, stored); len(got) != 1 {
|
||||
t.Fatalf("evidence = %d items, want 1", len(got))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestSemanticsEnterFitAndClearTheFlagOnlyWhenKnown(t *testing.T) {
|
||||
policy := domain.DemoSemanticMatchingPolicy()
|
||||
skills := []requestedSkill{{code: "go", required: true}}
|
||||
high, low := int64(900_000), int64(100_000)
|
||||
withHigh := computeScores(scoreInput{policy: policy, skills: skills, semantics: &high})
|
||||
withLow := computeScores(scoreInput{policy: policy, skills: skills, semantics: &low})
|
||||
unknown := computeScores(scoreInput{policy: policy, skills: skills})
|
||||
// Cov is 0, Dom neutral 0.5: Fit = 0.0833*0.5 + 0.5*Sem.
|
||||
if withHigh.fit != 491_650 || withLow.fit != 91_650 || unknown.fit != 291_650 {
|
||||
t.Fatalf("fit = %d / %d / %d", withHigh.fit, withLow.fit, unknown.fit)
|
||||
}
|
||||
flags := unknownFlags(explanationInput{semanticsKnown: true})
|
||||
for _, flag := range flags {
|
||||
if flag.Component == UnknownSemantics {
|
||||
t.Fatal("a known Sem is still flagged")
|
||||
}
|
||||
}
|
||||
if flags := unknownFlags(explanationInput{}); flags[0] != (UnknownFlag{Component: UnknownSemantics, Reason: ReasonMLUnavailable}) {
|
||||
t.Fatalf("flags = %v, want the ML flag first", flags)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProjectEvidenceIsRecordedAsTheProjectsField(t *testing.T) {
|
||||
title := "Ledger"
|
||||
fields := entryFields(Entry{SemanticEvidence: []SemanticEvidence{{ItemKind: domain.SemanticItemProject, ProjectTitle: &title}}})
|
||||
if !slices.Contains(fields, domain.CardFieldProjects) {
|
||||
t.Fatalf("fields = %v, want projects", fields)
|
||||
}
|
||||
if fields := entryFields(Entry{SemanticEvidence: []SemanticEvidence{{ItemKind: domain.SemanticItemStack}}}); slices.Contains(fields, domain.CardFieldProjects) {
|
||||
t.Fatalf("fields = %v, a stack item is not a project", fields)
|
||||
}
|
||||
}
|
||||
|
||||
type wholeVectorsFake struct {
|
||||
outbound.SemanticVectorRepository
|
||||
|
||||
byModel map[domain.SemanticModel]map[uuid.UUID]domain.SemanticVector
|
||||
}
|
||||
|
||||
func (fake wholeVectorsFake) CandidateWholeVectors(
|
||||
_ context.Context, model domain.SemanticModel, _ []uuid.UUID,
|
||||
) (map[uuid.UUID]domain.SemanticVector, error) {
|
||||
return fake.byModel[model], nil
|
||||
}
|
||||
|
||||
type semanticUnitFake struct {
|
||||
outbound.UnitOfWork
|
||||
|
||||
vectors wholeVectorsFake
|
||||
}
|
||||
|
||||
func (unit semanticUnitFake) SemanticVectors() outbound.SemanticVectorRepository { return unit.vectors }
|
||||
|
||||
func TestCandidateSemanticsFusesEveryMemberAndSkipsPartialCandidates(t *testing.T) {
|
||||
policy := domain.DemoMatchingSemanticsPolicy()
|
||||
second := domain.SemanticModel{ID: "example/second", Revision: "r1", PromptVersion: "v1"}
|
||||
policy.Members[0].Weight = 5000
|
||||
policy.Members = append(policy.Members, domain.MatchingSemanticMember{
|
||||
Model: second, Weight: 5000, Mean: 0, StandardDeviation: 10000,
|
||||
})
|
||||
first := policy.Members[0].Model
|
||||
complete, partial := uuid.New(), uuid.New()
|
||||
unitOfWork := semanticUnitFake{vectors: wholeVectorsFake{byModel: map[domain.SemanticModel]map[uuid.UUID]domain.SemanticVector{
|
||||
first: {complete: unit(0.2), partial: unit(0.9)},
|
||||
second: {complete: unit(0.6)},
|
||||
}}}
|
||||
semantics := needSemantics{policy: &policy, wholes: []domain.SemanticVector{unit(1), unit(1)}}
|
||||
values, err := candidateSemantics(context.Background(), unitOfWork, semantics,
|
||||
[]candidateEntry{{candidateID: complete}, {candidateID: partial}, {candidateID: complete}})
|
||||
if err != nil {
|
||||
t.Fatalf("candidateSemantics() error = %v", err)
|
||||
}
|
||||
if _, ok := values[partial]; ok {
|
||||
t.Fatal("a candidate without a vector of every member got Sem")
|
||||
}
|
||||
// 0.5 * 0.2 + 0.5 * 0.6 = 0.4 on the calibration of the fused score.
|
||||
if got, want := values[complete], policy.Probability(0.4); len(values) != 1 || got != want {
|
||||
t.Fatalf("Sem = %v, want %d for the complete candidate only", values, want)
|
||||
}
|
||||
semantics.wholes = semantics.wholes[:1]
|
||||
if values, _ := candidateSemantics(context.Background(), unitOfWork, semantics, []candidateEntry{{candidateID: complete}}); len(values) != 0 {
|
||||
t.Fatal("Sem was computed without the whole view of every member of the need")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user