• About Us
  • Disclaimer
  • Contact Us
  • Privacy Policy
Wednesday, July 22, 2026
mGrowTech
No Result
View All Result
  • Technology And Software
    • Account Based Marketing
    • Channel Marketing
    • Marketing Automation
      • Al, Analytics and Automation
      • Ad Management
  • Digital Marketing
    • Social Media Management
    • Google Marketing
  • Direct Marketing
    • Brand Management
    • Marketing Attribution and Consulting
  • Mobile Marketing
  • Event Management
  • PR Solutions
  • Technology And Software
    • Account Based Marketing
    • Channel Marketing
    • Marketing Automation
      • Al, Analytics and Automation
      • Ad Management
  • Digital Marketing
    • Social Media Management
    • Google Marketing
  • Direct Marketing
    • Brand Management
    • Marketing Attribution and Consulting
  • Mobile Marketing
  • Event Management
  • PR Solutions
No Result
View All Result
mGrowTech
No Result
View All Result
Home Al, Analytics and Automation

How to Build an EverMem-Style Persistent AI Agent OS with Hierarchical Memory, FAISS Vector Retrieval, SQLite Storage, and Automated Memory Consolidation

Josh by Josh
March 5, 2026
in Al, Analytics and Automation
0
How to Build an EverMem-Style Persistent AI Agent OS with Hierarchical Memory, FAISS Vector Retrieval, SQLite Storage, and Automated Memory Consolidation


class EverMemAgentOS:
   def __init__(
       self,
       workdir: str = "/content/evermem_agent_os",
       db_name: str = "evermem.sqlite",
       embedding_model: str = "sentence-transformers/all-MiniLM-L6-v2",
       gen_model: str = "google/flan-t5-small",
       stm_max_turns: int = 10,
       ltm_topk: int = 6,
       consolidate_every: int = 8,
       consolidate_trigger_tokens: int = 1400,
       compress_target_chars: int = 420,
       seed: int = 7,
   ):
       self.workdir = workdir
       _ensure_dir(self.workdir)
       self.db_path = os.path.join(self.workdir, db_name)


       self.embedder = SentenceTransformer(embedding_model)
       self.embed_dim = self.embedder.get_sentence_embedding_dimension()


       self.tokenizer = AutoTokenizer.from_pretrained(gen_model)
       self.model = AutoModelForSeq2SeqLM.from_pretrained(gen_model)
       self.model.to(self.device)
       self.model.eval()


       self.stm_max_turns = stm_max_turns
       self.ltm_topk = ltm_topk
       self.consolidate_every = consolidate_every
       self.consolidate_trigger_tokens = consolidate_trigger_tokens
       self.compress_target_chars = compress_target_chars


       np.random.seed(seed)


       self._init_db()
       self._init_faiss()


       self.stm: List[Dict[str, str]] = []
       self.turns = 0


   def _init_db(self):
       conn = sqlite3.connect(self.db_path)
       cur = conn.cursor()
       cur.execute(
           """
           CREATE TABLE IF NOT EXISTS memories (
               mid TEXT PRIMARY KEY,
               role TEXT,
               text TEXT,
               created_ts INTEGER,
               importance REAL,
               tokens_est INTEGER,
               meta_json TEXT
           )
           """
       )
       cur.execute(
           """
           CREATE TABLE IF NOT EXISTS kv_store (
               k TEXT PRIMARY KEY,
               v_json TEXT,
               updated_ts INTEGER
           )
           """
       )
       cur.execute(
           """
           CREATE TABLE IF NOT EXISTS consolidations (
               cid TEXT PRIMARY KEY,
               created_ts INTEGER,
               summary TEXT,
               source_mids_json TEXT
           )
           """
       )
       conn.commit()
       conn.close()


   def _init_faiss(self):
       self.faiss_index_path = os.path.join(self.workdir, "faiss.index")
       self.faiss_map_path = os.path.join(self.workdir, "faiss_map.json")


       if os.path.exists(self.faiss_index_path) and os.path.exists(self.faiss_map_path):
           self.index = faiss.read_index(self.faiss_index_path)
           with open(self.faiss_map_path, "r", encoding="utf-8") as f:
               self.id_map = json.load(f)
           self.id_map = {int(k): v for k, v in self.id_map.items()}
           self.next_faiss_id = (max(self.id_map.keys()) + 1) if self.id_map else 0
           return


       self.index = faiss.IndexFlatIP(self.embed_dim)
       self.id_map: Dict[int, str] = {}
       self.next_faiss_id = 0
       self._persist_faiss()


   def _persist_faiss(self):
       faiss.write_index(self.index, self.faiss_index_path)
       with open(self.faiss_map_path, "w", encoding="utf-8") as f:
           json.dump({str(k): v for k, v in self.id_map.items()}, f)


   def _embed(self, texts: List[str]) -> np.ndarray:
       vecs = self.embedder.encode(texts, convert_to_numpy=True, normalize_embeddings=True)
       if vecs.ndim == 1:
           vecs = vecs.reshape(1, -1)
       return vecs.astype("float32")


   def _tokens_est(self, text: str) -> int:
       text = text or ""
       return max(1, int(len(text.split()) * 1.25))


   def _importance_score(self, role: str, text: str, meta: Dict[str, Any]) -> float:
       base = 0.35
       length_bonus = min(0.45, math.log1p(len(text)) / 20.0)
       role_bonus = 0.08 if role == "user" else 0.03
       pin = 0.35 if meta.get("pinned") else 0.0
       signal = meta.get("signal", "")
       signal_bonus = 0.18 if signal in {"decision", "preference", "fact", "task"} else 0.0
       q_bonus = 0.06 if "?" in text else 0.0
       number_bonus = 0.05 if any(ch.isdigit() for ch in text) else 0.0
       return float(min(1.0, base + length_bonus + role_bonus + pin + signal_bonus + q_bonus + number_bonus))


   def upsert_kv(self, k: str, v: Any):
       conn = sqlite3.connect(self.db_path)
       cur = conn.cursor()
       cur.execute(
           "INSERT INTO kv_store (k, v_json, updated_ts) VALUES (?, ?, ?) ON CONFLICT(k) DO UPDATE SET v_json=excluded.v_json, updated_ts=excluded.updated_ts",
           (k, json.dumps(v, ensure_ascii=False), _now_ts()),
       )
       conn.commit()
       conn.close()


   def get_kv(self, k: str, default=None):
       conn = sqlite3.connect(self.db_path)
       cur = conn.cursor()
       cur.execute("SELECT v_json FROM kv_store WHERE k=?", (k,))
       row = cur.fetchone()
       conn.close()
       if not row:
           return default
       try:
           return json.loads(row[0])
       except Exception:
           return default


   def add_memory(self, role: str, text: str, meta: Optional[Dict[str, Any]] = None) -> str:
       meta = meta or {}
       text = (text or "").strip()
       mid = meta.get("mid") or f"m:{_sha(f'{_now_ts()}::{role}::{text[:80]}::{np.random.randint(0, 10**9)}')}"
       created_ts = _now_ts()
       tokens_est = self._tokens_est(text)
       importance = float(meta.get("importance")) if meta.get("importance") is not None else self._importance_score(role, text, meta)


       conn = sqlite3.connect(self.db_path)
       cur = conn.cursor()
       cur.execute(
           "INSERT OR REPLACE INTO memories (mid, role, text, created_ts, importance, tokens_est, meta_json) VALUES (?, ?, ?, ?, ?, ?, ?)",
           (mid, role, text, created_ts, importance, tokens_est, json.dumps(meta, ensure_ascii=False)),
       )
       conn.commit()
       conn.close()


       vec = self._embed([text])
       fid = self.next_faiss_id
       self.next_faiss_id += 1
       self.index.add(vec)
       self.id_map[fid] = mid
       self._persist_faiss()


       return mid



Source_link

READ ALSO

Poolside Releases Laguna S 2.1, an Open-Weight Agentic Coding Model Punching Above Its Weight Class on SWE-Bench Multilingual

Meta Open-Sources Astryx: An Agent-Ready React Design System With 150+ Accessible Components, Seven Themes, and a CLI

Related Posts

Poolside Releases Laguna S 2.1, an Open-Weight Agentic Coding Model Punching Above Its Weight Class on SWE-Bench Multilingual
Al, Analytics and Automation

Poolside Releases Laguna S 2.1, an Open-Weight Agentic Coding Model Punching Above Its Weight Class on SWE-Bench Multilingual

July 22, 2026
Meta Open-Sources Astryx: An Agent-Ready React Design System With 150+ Accessible Components, Seven Themes, and a CLI
Al, Analytics and Automation

Meta Open-Sources Astryx: An Agent-Ready React Design System With 150+ Accessible Components, Seven Themes, and a CLI

July 21, 2026
Alibaba’s Tongyi Lab Releases Qwen-Audio-3.0-TTS, a Hosted Text-to-Speech Model in Flash and Plus Tiers Across 16 Languages
Al, Analytics and Automation

Alibaba’s Tongyi Lab Releases Qwen-Audio-3.0-TTS, a Hosted Text-to-Speech Model in Flash and Plus Tiers Across 16 Languages

July 21, 2026
Best Local LLMs You Can Run on a Single 24GB GPU in 2026: Qwen, Gemma, Mistral, DeepSeek Compared
Al, Analytics and Automation

Best Local LLMs You Can Run on a Single 24GB GPU in 2026: Qwen, Gemma, Mistral, DeepSeek Compared

July 20, 2026
Someone Fine-Tuned OpenBMB’s MiniCPM5-1B on Claude Fable 5 Traces to Ship a 657MB Local Thinking Model
Al, Analytics and Automation

Someone Fine-Tuned OpenBMB’s MiniCPM5-1B on Claude Fable 5 Traces to Ship a 657MB Local Thinking Model

July 20, 2026
Perplexity AI Releases WANDR: An Open Benchmark Evaluating Research Agents That Must Search Wide And Deep
Al, Analytics and Automation

Perplexity AI Releases WANDR: An Open Benchmark Evaluating Research Agents That Must Search Wide And Deep

July 19, 2026
Next Post
Black Forest Labs' new Self-Flow technique makes training multimodal AI models 2.8x more efficient

Black Forest Labs' new Self-Flow technique makes training multimodal AI models 2.8x more efficient

POPULAR NEWS

Trump ends trade talks with Canada over a digital services tax

Trump ends trade talks with Canada over a digital services tax

June 28, 2025
15 Trending Songs on TikTok in 2025 (+ How to Use Them)

15 Trending Songs on TikTok in 2025 (+ How to Use Them)

June 18, 2025
Communication Effectiveness Skills For Business Leaders

Communication Effectiveness Skills For Business Leaders

June 10, 2025
App Development Cost in Singapore: Pricing Breakdown & Insights

App Development Cost in Singapore: Pricing Breakdown & Insights

June 22, 2025
Comparing the Top 7 Large Language Models LLMs/Systems for Coding in 2025

Comparing the Top 7 Large Language Models LLMs/Systems for Coding in 2025

November 4, 2025

EDITOR'S PICK

BMO, Walmart Canada Team Up on Shopping Services

BMO, Walmart Canada Team Up on Shopping Services

December 4, 2025
Google Maps simplifies battery predictions and trip planning for 350+ Android Auto EV models.

Google Maps simplifies battery predictions and trip planning for 350+ Android Auto EV models.

April 5, 2026
Transforming the Future of Shopping- Insider

Transforming the Future of Shopping- Insider

August 14, 2025
AI Salon Booking App Development in Dubai Enterprise Guide

AI Salon Booking App Development in Dubai Enterprise Guide

February 23, 2026

About

We bring you the best Premium WordPress Themes that perfect for news, magazine, personal blog, etc. Check our landing page for details.

Follow us

Categories

  • Account Based Marketing
  • Ad Management
  • Al, Analytics and Automation
  • Brand Management
  • Channel Marketing
  • Digital Marketing
  • Direct Marketing
  • Event Management
  • Google Marketing
  • Marketing Attribution and Consulting
  • Marketing Automation
  • Mobile Marketing
  • PR Solutions
  • Social Media Management
  • Technology And Software
  • Uncategorized

Recent Posts

  • How Beverage Culture is Weaving its way into Trade Show Programs
  • The best Google AI tools for college students on summer break
  • How Visit Philadelphia turned 3,000 voicemails into 2 million impressions
  • LinkedIn Wend Answer Today for July 21, 2026 (Puzzle #43)
  • About Us
  • Disclaimer
  • Contact Us
  • Privacy Policy
No Result
View All Result
  • Technology And Software
    • Account Based Marketing
    • Channel Marketing
    • Marketing Automation
      • Al, Analytics and Automation
      • Ad Management
  • Digital Marketing
    • Social Media Management
    • Google Marketing
  • Direct Marketing
    • Brand Management
    • Marketing Attribution and Consulting
  • Mobile Marketing
  • Event Management
  • PR Solutions