Spaces:
Running

File size: 3,545 Bytes
1d919a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Champ's local mind: MVCC thought versions, the DEC embedding module,
// and a library slot for each new weight or self-improvement note.

import { createStore } from "./mvcc.js";
import { buildLut, embedText, cosine, packEmbedModule } from "./embed.js";

const CHAT = { id: "champ", role: "chat", codec: "bnz" };
const EMBED = { id: "embed-dec", role: "embed", codec: "bnz" };

// opts.onCommit receives every new record, for persistence (persist.js).
export function createMind(opts) {
  const store = createStore(opts);
  const lut = buildLut();
  const embedBnz = packEmbedModule(lut);
  const mods = [CHAT, EMBED];
  return {
    store,
    lut,
    embedBnz,
    embed(text) { return embedText(text, lut); },
    cosine,
    commit(page, key, value, limit) { return store.commit(page, key, value, limit); },
    read(page, key, asOf) { return store.read(page, key, asOf); },
    history(page, key) { return store.history(page, key); },
    replay(txn, page, key, value) { store.replay(txn, page, key, value); },
    scan(page, prefix, asOf) { return store.scan(page, prefix, asOf); },
    head() { return store.head(); },
    modules() { return mods.map((m) => Object.assign({}, m)); },
    // After a replay: the library slots come back from their own records.
    restoreModules() {
      for (const rec of store.scan("library", "", store.head())) {
        if (mods.some((m) => m.id === rec.key)) continue;
        try { mods.push(JSON.parse(rec.value)); } catch { /* not a module record */ }
      }
    },
    addModule(mod) {
      mods.push(mod);
      store.commit("library", mod.id, JSON.stringify(mod));
      return mod;
    },
    learn(question, answer) {
      const text = (String(question) + "\n" + String(answer)).slice(0, 700);
      const txn = store.commit("learn", "note/" + (store.head() + 1), text);
      this.addModule({ id: "learn-" + txn, role: "memory", codec: "mvcc", page: "learn" });
      return txn;
    },
    // A page read to learn from: its text in pieces small enough to recall, each with its address.
    learnPage(source, text) {
      const body = String(text || "").replace(/\s+/g, " ").trim();
      let txn = 0;
      for (let at = 0; at < body.length && at < 2400; at += 600) {
        txn = store.commit("learn", "note/" + (store.head() + 1), (String(source) + "\n" + body.slice(at, at + 600)).slice(0, 700));
      }
      if (txn) this.addModule({ id: "learn-" + txn, role: "memory", codec: "mvcc", page: "learn" });
      return txn;
    },
    recall(query, minScore) {
      const floor = minScore == null ? 0.72 : minScore;
      const qv = embedText(query, lut);
      let best = "";
      let score = floor;
      for (const rec of store.scan("learn", "note/", store.head())) {
        const s = cosine(qv, embedText(rec.value, lut));
        if (s > score) {
          score = s;
          best = rec.value;
        }
      }
      return best ? best.slice(0, 400) : "";
    },
  };
}

export function thoughtPrompt(step, question, earlier) {
  const prior = earlier ? " So far: " + earlier.slice(0, 180) : "";
  return "One reasoning step only, in one sentence. Do not give the final answer. Do not use a tool." + prior + "\nQuestion: " + question + "\nStep " + step + ":";
}

export function explainPrompt(question, versions, notes) {
  const lines = versions.map((v, i) => (i + 1) + ". " + v).join("\n");
  const note = notes ? "\nNotes:\n" + notes : "";
  return "Explain the answer in plain words. Do not use a tool.\nThoughts:\n" + lines + note + "\nQuestion: " + question;
}