蒸馏嵌入接线:Distiller/Agent 注入共享 embedder,修复配置缺失时蒸馏零产出

This commit is contained in:
root
2026-08-05 09:59:33 +08:00
parent 04ce809525
commit 9f66d055b6
7 changed files with 42 additions and 14 deletions

View File

@ -42,8 +42,11 @@ type Distiller struct {
ctx context.Context
cancel context.CancelFunc
onMemory func(input, response string)
embedder nlp.Vectorizer
}
func (d *Distiller) SetEmbedder(ev nlp.Vectorizer) { d.embedder = ev }
func NewDistiller(db *memory.GraphDB, dataDir string, cfg DistillerConfig) *Distiller {
ctx, cancel := context.WithCancel(context.Background())
return &Distiller{
@ -233,7 +236,7 @@ func (d *Distiller) distillBatch(batch []RawRecord) {
assistantContent += r.Content + " "
}
}
triples := extractKeyTriples(userContent, assistantContent)
triples := extractKeyTriples(userContent, assistantContent, d.embedder)
if len(triples) > 0 {
sessionID := ""
for sid := range sessionIDs {
@ -263,10 +266,13 @@ func (d *Distiller) cleanupRawFiles() {
}
}
func extractKeyTriples(userContent, assistantContent string) []memory.Triple {
func extractKeyTriples(userContent, assistantContent string, embedder nlp.Vectorizer) []memory.Triple {
var triples []memory.Triple
e := nlp.NewExtractor(nil)
if embedder != nil {
e.SetEmbedder(embedder)
}
text := userContent
if assistantContent != "" {
text += assistantContent

View File

@ -7,8 +7,16 @@ import (
"time"
"gitcode.com/JianFeeeee/HomeAgent/internal/memory"
"gitcode.com/JianFeeeee/HomeAgent/internal/memory/vector"
)
// constVectorizer 恒等向量器:所有候选统一过融合阈值,验证嵌入接线是否生效。
type constVectorizer struct{}
func (constVectorizer) Vectorize(text string) vector.Vector {
return vector.Vector{"0": 1.0, "1": 0.5}
}
func TestNewDistiller(t *testing.T) {
d := NewDistiller(nil, t.TempDir(), DistillerConfig{
Interval: 10 * time.Minute,
@ -147,7 +155,7 @@ func TestExtractKeyTriples(t *testing.T) {
}
for _, tt := range tests {
triples := extractKeyTriples(tt.user, tt.assistant)
triples := extractKeyTriples(tt.user, tt.assistant, constVectorizer{})
if tt.check != nil && !tt.check(triples) {
t.Errorf("extractKeyTriples(%q) = %v, check failed", tt.user, triples)
}