refactor(recommendation): both candidate pools score through one listener context (#8002)
LoadCandidates and LoadCandidatesFromSimilarity each loaded contextual likes, the taste profile and context affinity, and each built ScoringInputs field by field, so a new scoring input added to one pool would be missing from the other. loadScoringContext loads them once and scoringContext.inputs builds every candidate's inputs; the similarity pool adds its similarity score on top. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -32,43 +32,19 @@ func LoadCandidates(
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
likes, err := loadContextualLikesByTrack(ctx, q, userID)
|
sc, err := loadScoringContext(ctx, q, userID, currentVector)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
profile, err := LoadTasteProfile(ctx, q, userID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
affinity, err := LoadContextAffinity(ctx, q, userID, currentVector.DeviceClass)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
out := make([]Candidate, 0, len(rows))
|
out := make([]Candidate, 0, len(rows))
|
||||||
for _, r := range rows {
|
for _, r := range rows {
|
||||||
var lpt *time.Time
|
// Fallback path: mood is scored only in the primary (similarity)
|
||||||
if r.LastPlayedAt.Valid {
|
// loader — loading per-candidate tags over this near-whole-library
|
||||||
t := r.LastPlayedAt.Time
|
// pool isn't worth it (nil moods → 0).
|
||||||
lpt = &t
|
|
||||||
}
|
|
||||||
ctxScore := ContextualMatchScore(currentVector, likes[r.Track.ID], DefaultSimilarityWeights)
|
|
||||||
out = append(out, Candidate{
|
out = append(out, Candidate{
|
||||||
Track: r.Track,
|
Track: r.Track,
|
||||||
Inputs: ScoringInputs{
|
Inputs: sc.inputs(r.Track, r.IsLiked, r.LastPlayedAt, r.PlayCount, r.SkipCount, r.ReleaseDate, nil),
|
||||||
IsGeneralLiked: r.IsLiked,
|
|
||||||
LastPlayedAt: lpt,
|
|
||||||
PlayCount: int(r.PlayCount),
|
|
||||||
SkipCount: int(r.SkipCount),
|
|
||||||
ContextualMatchScore: ctxScore,
|
|
||||||
// Fallback path: mood is scored only in the primary
|
|
||||||
// (similarity) loader — loading per-candidate tags over this
|
|
||||||
// near-whole-library pool isn't worth it (nil moods → 0).
|
|
||||||
TasteMatchScore: profile.Match(r.Track.ArtistID, r.Track.Genre, r.ReleaseDate, nil),
|
|
||||||
ContextAffinityScore: affinity.Affinity(r.Track.ArtistID),
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return out, nil
|
return out, nil
|
||||||
@@ -200,17 +176,7 @@ func LoadCandidatesFromSimilarity(
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
likes, err := loadContextualLikesByTrack(ctx, q, userID)
|
sc, err := loadScoringContext(ctx, q, userID, currentVector)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
profile, err := LoadTasteProfile(ctx, q, userID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
affinity, err := LoadContextAffinity(ctx, q, userID, currentVector.DeviceClass)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -226,11 +192,6 @@ func LoadCandidatesFromSimilarity(
|
|||||||
|
|
||||||
out := make([]Candidate, 0, len(rows))
|
out := make([]Candidate, 0, len(rows))
|
||||||
for _, r := range rows {
|
for _, r := range rows {
|
||||||
var lpt *time.Time
|
|
||||||
if r.LastPlayedAt.Valid {
|
|
||||||
t := r.LastPlayedAt.Time
|
|
||||||
lpt = &t
|
|
||||||
}
|
|
||||||
// sqlc returns SimilarityScore as interface{} (couldn't infer the
|
// sqlc returns SimilarityScore as interface{} (couldn't infer the
|
||||||
// type through max(...) over a UNION). Type-assert; default to 0
|
// type through max(...) over a UNION). Type-assert; default to 0
|
||||||
// on the (impossible-but-defensive) case where it's nil/wrong type.
|
// on the (impossible-but-defensive) case where it's nil/wrong type.
|
||||||
@@ -238,25 +199,64 @@ func LoadCandidatesFromSimilarity(
|
|||||||
if v, ok := r.SimilarityScore.(float64); ok {
|
if v, ok := r.SimilarityScore.(float64); ok {
|
||||||
simScore = v
|
simScore = v
|
||||||
}
|
}
|
||||||
ctxScore := ContextualMatchScore(currentVector, likes[r.Track.ID], DefaultSimilarityWeights)
|
inputs := sc.inputs(r.Track, r.IsLiked, r.LastPlayedAt, r.PlayCount, r.SkipCount, r.ReleaseDate,
|
||||||
out = append(out, Candidate{
|
moods[r.Track.ID])
|
||||||
Track: r.Track,
|
inputs.SimilarityScore = simScore
|
||||||
Inputs: ScoringInputs{
|
out = append(out, Candidate{Track: r.Track, Inputs: inputs})
|
||||||
IsGeneralLiked: r.IsLiked,
|
|
||||||
LastPlayedAt: lpt,
|
|
||||||
PlayCount: int(r.PlayCount),
|
|
||||||
SkipCount: int(r.SkipCount),
|
|
||||||
ContextualMatchScore: ctxScore,
|
|
||||||
SimilarityScore: simScore,
|
|
||||||
TasteMatchScore: profile.Match(
|
|
||||||
r.Track.ArtistID, r.Track.Genre, r.ReleaseDate, moods[r.Track.ID]),
|
|
||||||
ContextAffinityScore: affinity.Affinity(r.Track.ArtistID),
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// scoringContext is what scoring needs to know about the listener, loaded
|
||||||
|
// once per candidate pool. Both pool loaders build their inputs through it,
|
||||||
|
// so a scoring input added here reaches radio, Songs-like and the mixes
|
||||||
|
// alike.
|
||||||
|
type scoringContext struct {
|
||||||
|
vector SessionVector
|
||||||
|
likes map[pgtype.UUID][]SessionVector
|
||||||
|
profile TasteProfile
|
||||||
|
affinity ContextAffinity
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadScoringContext(
|
||||||
|
ctx context.Context, q *dbq.Queries, userID pgtype.UUID, currentVector SessionVector,
|
||||||
|
) (scoringContext, error) {
|
||||||
|
likes, err := loadContextualLikesByTrack(ctx, q, userID)
|
||||||
|
if err != nil {
|
||||||
|
return scoringContext{}, err
|
||||||
|
}
|
||||||
|
profile, err := LoadTasteProfile(ctx, q, userID)
|
||||||
|
if err != nil {
|
||||||
|
return scoringContext{}, err
|
||||||
|
}
|
||||||
|
affinity, err := LoadContextAffinity(ctx, q, userID, currentVector.DeviceClass)
|
||||||
|
if err != nil {
|
||||||
|
return scoringContext{}, err
|
||||||
|
}
|
||||||
|
return scoringContext{vector: currentVector, likes: likes, profile: profile, affinity: affinity}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// inputs scores one candidate row against the listener. moods may be nil.
|
||||||
|
func (s scoringContext) inputs(
|
||||||
|
track dbq.Track, liked bool, lastPlayed pgtype.Timestamptz, plays, skips int64,
|
||||||
|
released pgtype.Date, moods []string,
|
||||||
|
) ScoringInputs {
|
||||||
|
var lpt *time.Time
|
||||||
|
if lastPlayed.Valid {
|
||||||
|
t := lastPlayed.Time
|
||||||
|
lpt = &t
|
||||||
|
}
|
||||||
|
return ScoringInputs{
|
||||||
|
IsGeneralLiked: liked,
|
||||||
|
LastPlayedAt: lpt,
|
||||||
|
PlayCount: int(plays),
|
||||||
|
SkipCount: int(skips),
|
||||||
|
ContextualMatchScore: ContextualMatchScore(s.vector, s.likes[track.ID], DefaultSimilarityWeights),
|
||||||
|
TasteMatchScore: s.profile.Match(track.ArtistID, track.Genre, released, moods),
|
||||||
|
ContextAffinityScore: s.affinity.Affinity(track.ArtistID),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// loadCandidateMoods fetches the enriched tags for the given candidate tracks
|
// loadCandidateMoods fetches the enriched tags for the given candidate tracks
|
||||||
// and reduces each to its canonical mood buckets (internal/mood), so the
|
// and reduces each to its canonical mood buckets (internal/mood), so the
|
||||||
// scorer can apply the mood facet per candidate. Tracks with no mood-word tags
|
// scorer can apply the mood facet per candidate. Tracks with no mood-word tags
|
||||||
|
|||||||
Reference in New Issue
Block a user