Files
magpie/internal/memory/search.go
T
yetone d77eb6d236 memory: search by meaning, on a small model on this computer
A memory search now finds what means the same in other words or another
language ("什么时候上线" finds "Releases go out on Tuesdays"), for the
MCP server's memory_search and the Library's Memory tab alike. It is off
until switched on under Memory → Search by meaning, since it downloads
paraphrase-multilingual-MiniLM-L12-v2 (Xenova's int8 ONNX, ~135 MB,
pinned revision, size and sha256 checked); switched off, the model is
deleted.

- internal/pii/guard: the BERT code runs the embedder too (LoadEmbedder,
  mean pooling; the tokenizer reads 250,002 pieces and WhitespaceSplit).
  Its vectors match ONNX Runtime's int8 to cosine >= 0.995 and are no
  further from fp32 than ORT's own; testdata carries both.
- internal/memembed: the switch, the download, vectors.bin keyed by each
  memory's sha256 (read once, again when the text changes, a cut-short
  file read as none), Keep reading in the backend, the model let go after
  10 minutes unused.
- internal/memory: memory.Semantic fuses the meaning ranking with the word
  ranking by reciprocal rank; a tie goes to the nearer in meaning, since
  a first word hit may hold one common word ("when do we release" had
  "Never delete images when..." first). Near 0.30 / Within 0.15 come from
  97 real memories and 16 queries, half Chinese for English memories:
  12 of 16 sought found with ~4 others each. Rank searches without noting
  memories used.
- GUI: the Search by meaning row (download %, memories read, failures),
  and the tab's search lists what the backend ranked first
  (POST /api/memory/rank), then word matches. zh, zh-TW, ja, de.
- docs/subsystems/memory.md: the section, out of "Not built yet".

Why not multilingual-e5-small, which this was built on first: run for
real, its cosines sat at 0.78-0.89 for everything, and a Chinese search
ranked Chinese memories over the English one it meant. e5-small, e5-base
and MiniLM were compared in ONNX Runtime on the same 97 memories; MiniLM
put the sought one first 9 of 16 and in the top 3 12 of 16, with the
rest at a median cosine of 0.16.

Run before the rebases (mote, on origin/main as of the work):
- gofmt -l internal/ clean; go vet of memembed, memory, pii/..., gui,
  library; GOOS=linux and GOOS=windows vet of memembed and pii/guard.
- MAGPIE_EMBED_DIR=~/models/pml go test -run Embedder ./internal/pii/guard:
  7 texts (CJK, full width, emoji, digits before runs of spaces) at
  0.9962-1.0000 with ORT int8.
- go test memembed, memory, memdistill, pii/..., library, browsermcp.
- Linux (magpie-desktop, -tags nogui, mote's disk was full): go test
  memembed, memory, pii/guard, gui (461s), cli (1228s, past the default
  10m timeout, so -timeout 30m); gateway -run 'MemoryMCP|OwnMCP'.
- Rebased on origin/main 3ebb1ba2c, on mote at this tree: gofmt -l
  internal/ clean; go vet of memembed, memory, pii/..., library, gui,
  gateway, and GOOS=linux and GOOS=windows vet of the first four; go
  test memembed, memory, pii/..., library, browsermcp (Embedder test on
  ~/models/pml); gateway -run 'MemoryMCP|OwnMCP'; cli -run
  'Settings|Parity|Budget'.
- Rebased again on origin/main 5d6935b11 (title bar, cli parity,
  account export, provider catalog): go vet of gui and cli; go test -run
  'Settings|Parity|Budget' of cli and gui, ok with the Bark test fix two
  commits up (TestSettingsSaveKeepsWhatItDoesNotSend, red on main).
- GUI on mote, this commit plus the next (90f4a377b, which fixes
  library-bar-more for #5982): every library-*.test.cjs, i18n-keys and
  i18n-en-no-cjk, BROWSER=chromium and webkit, 701/702 each. The red is
  library-loading's tab list, which has no "Claude plugins" (6af54dc9a)
  and is red alone at origin/main faffb1e95 (#5997). library-bili's
  webkit saved count times out about 1 run in 3, on main too (#6015).
- A real switch-on: the pinned files downloaded from Hugging Face and
  checked in 11s, 5 memories read; 什么时候上线, 端口被占用怎么办, 清理磁盘,
  回复用什么语言 and "when do we release" each find their memory first.
  缩进用什么 finds nothing (0.23 to the tabs memory, under Near).
- Broken by hand: without the tie rule, TestSearchFindsByMeaning's new
  case is red (images first).

Not run: the whole Go suite and the whole GUI suite; go test of
internal/gui and the whole of internal/cli after the rebases (WSL kept
restarting, and they passed on Linux before).
2026-11-11 04:14:58 +08:00

306 lines
7.5 KiB
Go

package memory
import (
"math"
"slices"
"strings"
"unicode"
)
// Query is what a search asks for. Scopes are the scopes searched (none:
// every scope), Kinds and Tags narrow it (a memory has one of the kinds
// and every tag), Status is the state looked in ("": active).
type Query struct {
Text string
Scopes []string
Kinds []string
Tags []string
Status string
Limit int
}
// Hit is a memory a search found, with how well it matched.
type Hit struct {
*Memory
Score float64 `json:"score"`
}
// Semantic, when set (memembed, with search by meaning switched on), is
// how near in meaning each memory of pool is to text, a cosine, for the
// ones it has read; nil when it can't say (switched off, the model not
// there, or failing), and the search is by words alone.
var Semantic func(text string, pool []*Memory) map[string]float64
// A memory counts as meaning what was searched for at a cosine of Near
// or more, and Within of the nearest. Over 97 real memories and 16
// searches (half in Chinese, for memories in English), the memory sought
// was at a median of 0.47, the rest at a median of 0.16 and a 95th
// percentile of 0.36. Near alone let ~11 of the rest in a search; with
// Within, 12 of the 16 were found and ~4 of the rest came along, behind
// them, and the word ranking finds most of the four it misses.
var (
Near = 0.30
Within = 0.15
)
// rrfK is reciprocal-rank fusion's constant: the word ranking and the
// meaning ranking each give a hit 1/(rrfK+rank).
const rrfK = 20
// Search finds memories matching q, best first: every word of the text
// counted by how rare it is among the memories, a memory holding the whole
// text ahead, pinned ones and a project's or agent's own a little ahead of
// the rest; with search by meaning on (Semantic), the ones that mean the
// same in other words too, the two rankings fused. With no text, it lists
// them by kind and newest change. The hits are noted as used.
func Search(q Query) ([]Hit, error) {
hits, err := Rank(q)
if err != nil {
return nil, err
}
ids := make([]string, len(hits))
for i, h := range hits {
ids[i] = h.ID
}
if strings.TrimSpace(q.Text) != "" {
touch(ids)
}
return hits, nil
}
// Rank is Search without noting the hits as used: for the Memory tab's
// search box, which the user types into.
func Rank(q Query) ([]Hit, error) {
f, err := view()
if err != nil {
return nil, err
}
return search(f.Memories, q), nil
}
func search(all []*Memory, q Query) []Hit {
status := q.Status
if status == "" {
status = Active
}
tags := CleanTags(q.Tags)
pool := []*Memory{}
for _, m := range all {
if m.Status != status ||
(len(q.Scopes) > 0 && !slices.Contains(q.Scopes, m.Scope)) ||
(len(q.Kinds) > 0 && !slices.Contains(q.Kinds, m.Kind)) ||
slices.ContainsFunc(tags, func(t string) bool { return !slices.Contains(m.Tags, t) }) {
continue
}
pool = append(pool, m)
}
limit := q.Limit
if limit <= 0 {
limit = 20
}
text := strings.TrimSpace(q.Text)
if text == "" {
slices.SortStableFunc(pool, func(a, b *Memory) int {
if a.Pinned != b.Pinned {
if a.Pinned {
return -1
}
return 1
}
if d := kindRank(a.Kind) - kindRank(b.Kind); d != 0 {
return d
}
return b.Updated.Compare(a.Updated)
})
out := []Hit{}
for _, m := range pool[:min(limit, len(pool))] {
out = append(out, Hit{Memory: m})
}
return out
}
out := wordHits(pool, text)
var sims map[string]float64
if Semantic != nil {
sims = Semantic(text, pool)
}
if sims != nil {
out = fuse(pool, out, sims)
}
return out[:min(limit, len(out))]
}
// wordHits are pool's memories holding words of text, best first.
func wordHits(pool []*Memory, text string) []Hit {
words := Words(text)
if len(words) == 0 {
return []Hit{}
}
docs := make([][]string, len(pool))
df := map[string]int{}
for i, m := range pool {
docs[i] = Words(m.Text + " " + m.Why + " " + strings.Join(m.Tags, " ") + " " + m.Kind)
seen := map[string]bool{}
for _, w := range docs[i] {
if !seen[w] {
seen[w] = true
df[w]++
}
}
}
whole := norm(text)
out := []Hit{}
for i, m := range pool {
has := map[string]bool{}
for _, w := range docs[i] {
has[w] = true
}
score, matched := 0.0, 0
for _, w := range words {
if has[w] {
matched++
score += 1 + math.Log(float64(len(pool)+1)/float64(df[w]+1))
}
}
if matched == 0 {
continue
}
// most of the words, not one common one, make a match
score *= float64(matched) / float64(len(words))
if strings.Contains(norm(m.Text), whole) {
score += 2
}
if m.Pinned {
score *= 1.2
}
if m.Scope != Global {
score *= 1.1
}
out = append(out, Hit{Memory: m, Score: math.Round(score*100) / 100})
}
slices.SortStableFunc(out, func(a, b Hit) int {
if a.Score != b.Score {
if a.Score > b.Score {
return -1
}
return 1
}
return b.Updated.Compare(a.Updated)
})
return out
}
// fuse is the word hits and the memories near text in meaning (sims, at
// Near or more and Within of the nearest) as one ranking: each list gives
// a hit 1/(rrfK+rank), and a pinned or a project's or agent's own memory
// is a little ahead, as in wordHits. A memory found both ways is ahead of
// one found either way, and of two that score the same, the nearer in
// meaning: the first word hit may hold one common word of the search.
func fuse(pool []*Memory, words []Hit, sims map[string]float64) []Hit {
bar := Near
for _, s := range sims {
bar = max(bar, s-Within)
}
near := []*Memory{}
for _, m := range pool {
if s, ok := sims[m.ID]; ok && s >= bar {
near = append(near, m)
}
}
slices.SortStableFunc(near, func(a, b *Memory) int {
if sims[a.ID] != sims[b.ID] {
if sims[a.ID] > sims[b.ID] {
return -1
}
return 1
}
return b.Updated.Compare(a.Updated)
})
score := map[string]float64{}
byID := map[string]*Memory{}
for i, h := range words {
score[h.ID] += 1 / float64(rrfK+i+1)
byID[h.ID] = h.Memory
}
for i, m := range near {
score[m.ID] += 1 / float64(rrfK+i+1)
byID[m.ID] = m
}
out := make([]Hit, 0, len(score))
for id, s := range score {
m := byID[id]
if m.Pinned {
s *= 1.2
}
if m.Scope != Global {
s *= 1.1
}
out = append(out, Hit{Memory: m, Score: math.Round(s*rrfK*100) / 100})
}
slices.SortStableFunc(out, func(a, b Hit) int {
if a.Score != b.Score {
if a.Score > b.Score {
return -1
}
return 1
}
if sa, sb := sims[a.ID], sims[b.ID]; sa != sb {
if sa > sb {
return -1
}
return 1
}
if c := b.Updated.Compare(a.Updated); c != 0 {
return c
}
return strings.Compare(a.ID, b.ID)
})
return out
}
func kindRank(k string) int {
if i := slices.Index(Kinds, k); i >= 0 {
return i
}
return len(Kinds)
}
// Words are the words of s as searched: letters and digits in lower case,
// and a run of Chinese, Japanese or Korean characters as each character
// and each pair of them, since those are written without spaces.
func Words(s string) []string {
out := []string{}
var word []rune
var cjk []rune
flushWord := func() {
if len(word) > 0 {
out = append(out, string(word))
word = word[:0]
}
}
flushCJK := func() {
for i := range cjk {
out = append(out, string(cjk[i]))
if i+1 < len(cjk) {
out = append(out, string(cjk[i:i+2]))
}
}
cjk = cjk[:0]
}
for _, r := range s {
switch {
case unicode.In(r, unicode.Han, unicode.Hiragana, unicode.Katakana, unicode.Hangul):
flushWord()
cjk = append(cjk, r)
case unicode.IsLetter(r) || unicode.IsDigit(r):
flushCJK()
word = append(word, unicode.ToLower(r))
default:
flushWord()
flushCJK()
}
}
flushWord()
flushCJK()
return out
}