ruslanmv commited on
Commit
8f3f3ca
·
verified ·
1 Parent(s): 4b2167e

Upload examples/run_memory.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. examples/run_memory.py +10 -6
examples/run_memory.py CHANGED
@@ -1,12 +1,13 @@
1
  """Matrix-BIOS-Memory-0.1 — grounded, citation-faithful recall (RAG).
2
- Ships a FAISS index + a small corpus; answers cite the source ids they used.
3
  pip install torch transformers sentence-transformers faiss-cpu huggingface_hub
4
  """
5
  import json
6
  import faiss
 
7
  from huggingface_hub import snapshot_download
8
  from sentence_transformers import SentenceTransformer
9
- from transformers import pipeline
10
 
11
  REPO = "ruslanmv/Matrix-BIOS-Memory-0.1"
12
  path = snapshot_download(REPO)
@@ -14,8 +15,9 @@ cfg = json.load(open(f"{path}/memory_config.json")) # embedder / generator /
14
  docs = json.load(open(f"{path}/docs.json")) # [{"id": ..., "text": ...}]
15
  index = faiss.read_index(f"{path}/index.faiss")
16
 
17
- embedder = SentenceTransformer(cfg["embedder"])
18
- generator = pipeline("text2text-generation", model=cfg["generator"])
 
19
 
20
  def answer(question: str):
21
  qv = embedder.encode([question], normalize_embeddings=True).astype("float32")
@@ -24,8 +26,10 @@ def answer(question: str):
24
  context = "\n".join(f"[{d['id']}] {d['text']}" for d in hits)
25
  prompt = ("Answer the question using ONLY the context, and cite the [id] you used.\n"
26
  f"Context:\n{context}\n\nQuestion: {question}\nAnswer:")
27
- out = generator(prompt, max_new_tokens=64)[0]["generated_text"]
28
- return out, [d["id"] for d in hits]
 
 
29
 
30
  if __name__ == "__main__":
31
  for q in ["What does every effectful action in Matrix OS emit?",
 
1
  """Matrix-BIOS-Memory-0.1 — grounded, citation-faithful recall (RAG).
2
+ Ships a FAISS index + a small corpus; every answer cites the source ids it used.
3
  pip install torch transformers sentence-transformers faiss-cpu huggingface_hub
4
  """
5
  import json
6
  import faiss
7
+ import torch
8
  from huggingface_hub import snapshot_download
9
  from sentence_transformers import SentenceTransformer
10
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
11
 
12
  REPO = "ruslanmv/Matrix-BIOS-Memory-0.1"
13
  path = snapshot_download(REPO)
 
15
  docs = json.load(open(f"{path}/docs.json")) # [{"id": ..., "text": ...}]
16
  index = faiss.read_index(f"{path}/index.faiss")
17
 
18
+ embedder = SentenceTransformer(cfg["embedder"])
19
+ gen_tok = AutoTokenizer.from_pretrained(cfg["generator"])
20
+ gen_model = AutoModelForSeq2SeqLM.from_pretrained(cfg["generator"]).eval()
21
 
22
  def answer(question: str):
23
  qv = embedder.encode([question], normalize_embeddings=True).astype("float32")
 
26
  context = "\n".join(f"[{d['id']}] {d['text']}" for d in hits)
27
  prompt = ("Answer the question using ONLY the context, and cite the [id] you used.\n"
28
  f"Context:\n{context}\n\nQuestion: {question}\nAnswer:")
29
+ ids = gen_tok(prompt, return_tensors="pt", truncation=True).input_ids
30
+ with torch.no_grad():
31
+ out = gen_model.generate(ids, max_new_tokens=64)
32
+ return gen_tok.decode(out[0], skip_special_tokens=True), [d["id"] for d in hits]
33
 
34
  if __name__ == "__main__":
35
  for q in ["What does every effectful action in Matrix OS emit?",