Files
magpie/internal/memembed/memembed.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

496 lines
11 KiB
Go

// Package memembed is memory's search by meaning:
// paraphrase-multilingual-MiniLM-L12-v2 (sentence-transformers, Apache
// 2.0; Xenova's int8 ONNX of it), run in magpie's own process by
// internal/pii/guard, gives each memory a place in a space where texts
// that say the same are near, in any of 50 languages and across them,
// and memory.Search
// fuses how near each is to what was searched with its word ranking
// (memory.Semantic).
//
// It is off until switched on, since the model is a 135 MB download.
// Switched on, the files are fetched from Hugging Face at the revision
// their sha256 were pinned at; switched off, they are deleted. Memories
// are read in the background (Keep) and their vectors kept in the model's
// folder, keyed by the text read, so an edited memory is read again and a
// new revision starts afresh. The model is loaded when first needed and
// let go after idleAfter with none.
package memembed
import (
"context"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/filelock"
"github.com/yetone/magpie/internal/memory"
"github.com/yetone/magpie/internal/pii"
"github.com/yetone/magpie/internal/pii/guard"
)
// rev is the repo's revision the files below were hashed at.
const rev = "2c4055b12046f11709e9df2c122e59ffbdc2f900"
type pinned struct {
path string // in the repo; saved under its base name
size int64
sha string
}
var files = []pinned{
{"onnx/model_int8.onnx", 118054609, "d6ea442ff6a891daefed7c83b2f596fc5dc66bf697e4d006236f64f34bbcf4c8"},
{"tokenizer.json", 17082913, "b60b6b43406a48bf3638526314f3d232d97058bc93472ff2de930d43686fa441"},
}
// base is where the files are fetched from; a variable for tests.
var base = "https://huggingface.co/Xenova/paraphrase-multilingual-MiniLM-L12-v2/resolve/"
// Root is where the model is kept, in magpie's cache: a folder per
// revision. A variable for tests.
var Root = func() string { return filepath.Join(appdir.Cache(), "paraphrase-multilingual-MiniLM-L12-v2") }
func dir() string { return filepath.Join(Root(), rev[:12]) }
// StatePath is the switch's file; a variable for tests.
var StatePath = func() string { return appdir.Join(appdir.Config(), "memory-semantic.json") }
// Size is the bytes the model downloads.
func Size() int64 {
var n int64
for _, f := range files {
n += f.size
}
return n
}
// Installed says every file is on disk at its size.
func Installed() bool {
for _, f := range files {
fi, err := os.Stat(filepath.Join(dir(), filepath.Base(f.path)))
if err != nil || fi.Size() != f.size {
return false
}
}
return true
}
type state struct {
On bool `json:"on"`
}
var stateMu sync.Mutex
func load() (state, error) {
var s state
b, err := os.ReadFile(StatePath())
if errors.Is(err, fs.ErrNotExist) {
return s, nil
}
if err != nil {
return s, err
}
return s, json.Unmarshal(b, &s)
}
// On says search by meaning is switched on. A switch file that can't be
// read is off: the model isn't loaded on a guess.
func On() bool {
s, err := load()
return err == nil && s.On
}
// Status is what the Memory tab shows of it.
type Status struct {
On bool `json:"on"`
Installed bool `json:"installed"`
Size int64 `json:"size"`
Downloading bool `json:"downloading"`
Done int64 `json:"done"`
Error string `json:"error,omitempty"`
// Read of Of are the memories read (with a vector), of all there are
Read int `json:"read"`
Of int `json:"of"`
// Reading is a background read going
Reading bool `json:"reading"`
// Failed is why the last read failed, "" when it worked
Failed string `json:"failed,omitempty"`
}
// Now is its status.
func Now() Status {
st := Status{On: On(), Installed: Installed(), Size: Size()}
dl.mu.Lock()
st.Downloading, st.Error = dl.cancel != nil, dl.err
dl.mu.Unlock()
st.Done = dl.done.Load()
if st.Installed {
st.Done = st.Size
}
st.Reading = reading.Load()
idx.mu.Lock()
st.Failed = idx.failed
idx.mu.Unlock()
if st.On && st.Installed {
if all, _, err := memory.All(); err == nil {
vs := vectors()
for _, m := range all {
if !searched(m) {
continue
}
st.Of++
if _, ok := vs.get(key(m)); ok {
st.Read++
}
}
}
}
return st
}
// Set switches it on, fetching the model when it isn't on disk, or off,
// stopping a download and deleting the model and what it read.
func Set(on bool) error {
stateMu.Lock()
defer stateMu.Unlock()
p := StatePath()
if err := os.MkdirAll(filepath.Dir(p), 0o700); err != nil {
return err
}
b, _ := json.Marshal(state{On: on})
if err := edit.WriteAtomic(p, b); err != nil {
return err
}
if on {
if !Installed() {
dl.start()
} else {
Poke()
}
return nil
}
dl.stop()
unload()
store.mu.Lock()
store.v = nil
store.mu.Unlock()
return os.RemoveAll(Root())
}
// ---------- the download ----------
type download struct {
mu sync.Mutex
cancel context.CancelFunc // nil when none is under way
err string // the last one's, "" when it worked
done atomic.Int64
}
var dl download
// start fetches the model in the background unless a download is under
// way, and reads the memories once it is on disk.
func (d *download) start() {
d.mu.Lock()
defer d.mu.Unlock()
if d.cancel != nil {
return
}
ctx, cancel := context.WithCancel(context.Background())
d.cancel, d.err = cancel, ""
d.done.Store(0)
go func() {
err := fetchModel(ctx)
d.mu.Lock()
d.cancel = nil
if err != nil && ctx.Err() == nil {
d.err = err.Error()
}
d.mu.Unlock()
cancel()
if err == nil {
Poke()
}
}()
}
// stop ends the download under way.
func (d *download) stop() {
d.mu.Lock()
if d.cancel != nil {
d.cancel()
}
d.mu.Unlock()
}
// fetchModel fetches the files not on disk yet, each checked against
// its sha256.
func fetchModel(ctx context.Context) error {
if err := os.MkdirAll(dir(), 0o755); err != nil {
return err
}
var at int64
for _, f := range files {
path := filepath.Join(dir(), filepath.Base(f.path))
if fi, err := os.Stat(path); err != nil || fi.Size() != f.size {
var n atomic.Int64
stop := make(chan struct{})
go func() {
t := time.NewTicker(200 * time.Millisecond)
defer t.Stop()
for {
select {
case <-stop:
return
case <-t.C:
dl.done.Store(at + n.Load())
}
}
}()
err := pii.Fetch(ctx, base+rev+"/"+f.path, path, f.size, f.sha, &n)
close(stop)
if err != nil {
return fmt.Errorf("downloading %s: %w", filepath.Base(f.path), err)
}
}
at += f.size
dl.done.Store(at)
}
// the folders of revisions pinned before
es, _ := os.ReadDir(Root())
for _, e := range es {
if e.IsDir() && e.Name() != filepath.Base(dir()) {
os.RemoveAll(filepath.Join(Root(), e.Name()))
}
}
return nil
}
// ---------- the model ----------
// idleAfter is how long the model stays loaded with nothing to read.
var idleAfter = 10 * time.Minute
// loadModel reads the model; a variable for tests, which give a fake.
var loadModel = func(dir string) (embedder, error) { return guard.LoadEmbedder(dir) }
type embedder interface {
Embed(text string) []float32
}
var mod struct {
mu sync.Mutex // held while it loads
e embedder
idle *time.Timer
}
// model is the model, loaded unless it is, kept for idleAfter more.
func model() (embedder, error) {
mod.mu.Lock()
defer mod.mu.Unlock()
if mod.e == nil {
if !Installed() {
return nil, errors.New("the model isn't downloaded")
}
e, err := loadModel(dir())
if err != nil {
return nil, err
}
mod.e = e
}
if mod.idle != nil {
mod.idle.Stop()
}
mod.idle = time.AfterFunc(idleAfter, unload)
return mod.e, nil
}
// unload lets the model go; a read under way finishes with the one it has.
func unload() {
mod.mu.Lock()
if mod.idle != nil {
mod.idle.Stop()
mod.idle = nil
}
mod.e = nil
mod.mu.Unlock()
}
// ---------- what was read ----------
// searched says m is one a search can return, and is read.
func searched(m *memory.Memory) bool {
return m.Status == memory.Active || m.Status == memory.Candidate
}
// passage is what is read of m: its text, and why when it says.
func passage(m *memory.Memory) string {
if m.Why == "" {
return m.Text
}
return m.Text + "\n" + m.Why
}
func key(m *memory.Memory) [32]byte { return sha256.Sum256([]byte(passage(m))) }
var reading atomic.Bool
var idx struct {
mu sync.Mutex // one read at a time
failed string
}
// pokes wakes Keep for a read now.
var pokes = make(chan struct{}, 1)
// Poke asks Keep to read what is new now, not on its next tick.
func Poke() {
select {
case pokes <- struct{}{}:
default:
}
}
// Keep reads memories not read yet while ctx lasts: every tick and when
// poked, while switched on with the model on disk.
func Keep(ctx context.Context, tick time.Duration) {
t := time.NewTicker(tick)
defer t.Stop()
for {
if On() && Installed() {
if _, err := Index(ctx); err != nil && ctx.Err() == nil {
idx.mu.Lock()
idx.failed = err.Error()
idx.mu.Unlock()
}
}
select {
case <-ctx.Done():
return
case <-t.C:
case <-pokes:
}
}
}
// Index reads every memory a search can return that has no vector for its
// text yet, keeps the vectors (dropping those of texts gone) and says how
// many it read. One goes at a time, in this magpie and across them.
func Index(ctx context.Context) (int, error) {
idx.mu.Lock()
defer idx.mu.Unlock()
unlock, held := filelock.Lock(filepath.Join(dir(), "vectors.lock"), 0)
if !held {
return 0, nil
}
defer unlock()
all, _, err := memory.All()
if err != nil {
return 0, err
}
vs := vectors()
var todo []*memory.Memory
keep := map[[32]byte]bool{}
for _, m := range all {
if !searched(m) {
continue
}
k := key(m)
keep[k] = true
if _, ok := vs.get(k); !ok {
todo = append(todo, m)
}
}
if len(todo) == 0 {
if vs.prune(keep) {
return 0, vs.save()
}
idx.failed = ""
return 0, nil
}
reading.Store(true)
defer reading.Store(false)
e, err := model()
if err != nil {
return 0, err
}
n := 0
for _, m := range todo {
if ctx.Err() != nil {
break
}
vs.put(key(m), e.Embed(passage(m)))
n++
if n%50 == 0 {
if err := vs.save(); err != nil {
return n, err
}
}
}
vs.prune(keep)
if err := vs.save(); err != nil {
return n, err
}
idx.failed = ""
return n, ctx.Err()
}
// similar is memory.Semantic: each memory of pool with a vector, and its
// cosine to text. A memory read since the last Index (a
// few at most) is read here, for a search right after it was saved.
func similar(text string, pool []*memory.Memory) map[string]float64 {
if len(pool) == 0 || !On() || !Installed() {
return nil
}
vs := vectors()
var missing []*memory.Memory
for _, m := range pool {
if _, ok := vs.get(key(m)); !ok {
missing = append(missing, m)
}
}
if len(missing) == len(pool) && len(pool) > inline {
// nothing read yet: words alone, while Keep reads them
Poke()
return nil
}
e, err := model()
if err != nil {
return nil
}
if len(missing) > inline {
Poke()
missing = missing[:inline]
}
for _, m := range missing {
vs.put(key(m), e.Embed(passage(m)))
}
q := e.Embed(text)
out := map[string]float64{}
for _, m := range pool {
v, ok := vs.get(key(m))
if !ok {
continue
}
var dot float64
for i := range q {
dot += float64(q[i]) * float64(v[i])
}
out[m.ID] = dot
}
return out
}
// inline is how many memories not read yet a search reads itself.
const inline = 16
func init() { memory.Semantic = similar }