惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
阮一峰的网络日志
阮一峰的网络日志
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
P
Palo Alto Networks Blog
Know Your Adversary
Know Your Adversary
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cisco Talos Blog
Cisco Talos Blog
L
Lohrmann on Cybersecurity
AWS News Blog
AWS News Blog
J
Java Code Geeks
博客园_首页
Scott Helme
Scott Helme
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
V
Visual Studio Blog
Cloudbric
Cloudbric
Jina AI
Jina AI
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
S
SegmentFault 最新的问题
The Last Watchdog
The Last Watchdog
SecWiki News
SecWiki News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
W
WeLiveSecurity
K
Kaspersky official blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Hacker News: Ask HN
Hacker News: Ask HN
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
量子位
Google Online Security Blog
Google Online Security Blog
博客园 - Franky
Simon Willison's Weblog
Simon Willison's Weblog
博客园 - 三生石上(FineUI控件)
Recent Commits to openclaw:main
Recent Commits to openclaw:main

MarkTechPost

A Coding Implementation of End-to-End Brain Decoding from MEG Signals Using NeuralSet and Deep Learning for Predicting Linguistic Features Meta Introduces Autodata: An Agentic Framework That Turns AI Models into Autonomous Data Scientists for High-Quality Training Data Creation A Coding Guide on LLM Post Training with TRL from Supervised Fine Tuning to DPO and GRPO Reasoning Qwen AI Releases Qwen-Scope: An Open-Source Sparse AutoEncoders (SAE) Suite That Turns LLM Internal Features into Practical Development Tools A Coding Deep Dive into Agentic UI, Generative UI, State Synchronization, and Interrupt-Driven Approval Flows Moonshot AI Open-Sources FlashKDA: CUTLASS Kernels for Kimi Delta Attention with Variable-Length Batching and H20 Benchmarks Microsoft Research’s World-R1 Uses Flow-GRPO and 3D-Aware Rewards to Inject Geometric Consistency Into Wan 2.1 Without Architectural Changes A Coding Implementation on Pyright Type Checking Covering Generics, Protocols, Strict Mode, Type Narrowing, and Modern Python Typing IBM Releases Two Granite Speech 4.1 2B Models: Autoregressive ASR with Translation and Non-Autoregressive Editing for Fast Inference Top 10 KV Cache Compression Techniques for LLM Inference: Reducing Memory Overhead Across Eviction, Quantization, and Low-Rank Methods Qwen Team Releases FlashQLA: a High-Performance Linear Attention Kernel Library That Achieves Up to 3× Speedup on NVIDIA Hopper GPUs Step by Step Guide to Build a Complete PII Detection and Redaction Pipeline with OpenAI Privacy Filter Meta FAIR Releases NeuralSet: A Python Package for Neuro-AI That Supports fMRI, M/EEG, Spikes, and HuggingFace Embeddings smol-audio: A Colab-Friendly Notebook Collection for Fine-Tuning Whisper, Parakeet, Voxtral, Granite Speech, and Audio Flamingo 3 A Coding Implementation on Document Parsing Benchmarking with LlamaIndex ParseBench Using Python, Hugging Face, and Evaluation Metrics Poolside AI Introduces Laguna XS.2 and M.1: Agentic Coding Models Reaching 68.2% and 72.5% on SWE-bench Verified How to Build Traceable and Evaluated LLM Workflows Using Promptflow, Prompty, and OpenAI OpenAI Releases Privacy Filter: A 1.5B-Parameter Open-Source PII Redaction Model with 50M Active Parameters Top 10 Physical AI Models Powering Real-World Robots in 2026 How to Build a Lightweight Vision-Language-Action-Inspired Embodied Agent with Latent World Modeling and Model Predictive Control Meet Talkie-1930: A 13B Open-Weight LLM Trained on Pre-1931 English Text for Historical Reasoning and Generalization Research OpenMOSS Releases MOSS-Audio: An Open-Source Foundation Model for Speech, Sound, Music, and Time-Aware Audio Reasoning Meta AI Releases Sapiens2: A High-Resolution Human-Centric Vision Model for Pose, Segmentation, Normals, Pointmap, and Albedo The LoRA Assumption That Breaks in Production How to Build a Fully Searchable AI Knowledge Base with OpenKB, OpenRouter, and Llama How to Build Smarter Multilingual Text Wrapping with BudouX Through Parsing, HTML Rendering, Model Introspection, and Toy Training Top 7 Benchmarks That Actually Matter for Agentic Reasoning in Large Language Models RAG Without Vectors: How PageIndex Retrieves by Reasoning A Coding Tutorial on Datashader on Rendering Massive Datasets with High-Performance Python Visual Analytics xAI Launches grok-voice-think-fast-1.0: Topping τ-voice Bench at 67.3%, Outperforming Gemini, GPT Realtime, and More A Coding Implementation on kvcached for Elastic KV Cache Memory, Bursty LLM Serving, and Multi-Model GPU Sharing Google DeepMind Introduces Vision Banana: An Instruction-Tuned Image Generator That Beats SAM 3 on Segmentation and Depth Anything V3 on Metric Depth Estimation Meet GitNexus: An Open-Source MCP-Native Knowledge Graph Engine That Gives Claude Code and Cursor Full Codebase Structural Awareness A Coding Implementation on Deepgram Python SDK for Transcription, Text-to-Speech, Async Audio Processing, and Text Intelligence A Coding Implementation on Microsoft’s OpenMementos with Trace Structure Analysis, Context Compression, and Fine-Tuning Data Preparation DeepSeek AI Releases DeepSeek-V4: Compressed Sparse Attention and Heavily Compressed Attention Enable One-Million-Token Contexts Google DeepMind Introduces Decoupled DiLoCo: An Asynchronous Training Architecture Achieving 88% Goodput Under High Hardware Failure Rates Mend Releases AI Security Governance Framework: Covering Asset Inventory, Risk Tiering, AI Supply Chain Security, and Maturity Model Mend.io Releases AI Security Governance Framework Covering Asset Inventory, Risk Tiering, AI Supply Chain Security, and Maturity Model OpenAI Releases GPT-5.5, a Fully Retrained Agentic Model That Scores 82.7% on Terminal-Bench 2.0 and 84.9% on GDPval A Coding Tutorial on OpenMythos on Recurrent-Depth Transformers with Depth Extrapolation, Adaptive Computation, and Mixture-of-Experts Routing Google Cloud AI Research Introduces ReasoningBank: A Memory Framework that Distills Reasoning Strategies from Agent Successes and Failures Xiaomi Releases MiMo-V2.5-Pro and MiMo-V2.5: Matching Frontier Model Benchmarks at Significantly Lower Token Cost How to Design a Production-Grade CAMEL Multi-Agent System with Planning, Tool Use, Self-Consistency, and Critique-Driven Refinement Alibaba Qwen Team Releases Qwen3.6-27B: A Dense Open-Weight Model Outperforming 397B MoE on Agentic Coding Benchmarks A Detailed Implementation on Equinox with JAX Native Modules, Filtered Transforms, Stateful Layers, and End-to-End Training Workflows Next Leap to Harness Engineering: JiuwenClaw Pioneers ‘Coordination Engineering’ Photon Releases Spectrum: An Open-Source TypeScript Framework that Deploys AI Agents Directly to iMessage, WhatsApp, and Telegram OpenAI Open-Sources Euphony: A Browser-Based Visualization Tool for Harmony Chat Data and Codex Session Logs Hugging Face Releases ml-intern: An Open-Source AI Agent that Automates the LLM Post-Training Workflow A Coding Implementation to Build a Conditional Bayesian Hyperparameter Optimization Pipeline with Hyperopt, TPE, and Early Stopping Google Introduces Simula: A Reasoning-First Framework for Generating Controllable, Scalable Synthetic Datasets Across Specialized AI Domains A Coding Implementation on Qwen 3.6-35B-A3B Covering Multimodal Inference, Thinking Control, Tool Calling, MoE Routing, RAG, and Session Persistence Moonshot AI Releases Kimi K2.6 with Long-Horizon Coding, Agent Swarm Scaling to 300 Sub-Agents and 4,000 Coordinated Steps A Coding Implementation on Microsoft’s Phi-4-Mini for Quantized Inference Reasoning Tool Use RAG and LoRA Fine-Tuning OpenAI Scales Trusted Access for Cyber Defense With GPT-5.4-Cyber: a Fine-Tuned Model Built for Verified Security Defenders Moonshot AI and Tsinghua Researchers Propose PrfaaS: A Cross-Datacenter KVCache Architecture that Rethinks How LLMs are Served at Scale Meet OpenMythos: An Open-Source PyTorch Reconstruction of Claude Mythos Where 770M Parameters Match a 1.3B Transformer How TabPFN Leverages In-Context Learning to Achieve Superior Accuracy on Tabular Datasets Compared to Random Forest and CatBoost A Coding Implementation to Build an AI-Powered File Type Detection and Security Analysis Pipeline with Magika and OpenAI NVIDIA Releases Ising: the First Open Quantum AI Model Family for Hybrid Quantum-Classical Systems xAI Launches Standalone Grok Speech-to-Text and Text-to-Speech APIs, Targeting Enterprise Voice Developers A Coding Tutorial for Running PrismML Bonsai 1-Bit LLM on CUDA with GGUF, Benchmarking, Chat, JSON, and RAG A Coding Guide for Property-Based Testing Using Hypothesis with Stateful, Differential, and Metamorphic Test Design Anthropic Releases Claude Opus 4.7: A Major Upgrade for Agentic Coding, High-Resolution Vision, and Long-Horizon Autonomous Tasks Google AI Releases Auto-Diagnose: An Large Language Model LLM-Based System to Diagnose Integration Test Failures at Scale A End-to-End Coding Guide to Running OpenAI GPT-OSS Open-Weight Models with Advanced Inference Workflows Top 19 AI Red Teaming Tools (2026): Secure Your ML Models A Coding Guide to Build a Production-Grade Background Task Processing System Using Huey with SQLite, Scheduling, Retries, Pipelines, and Concurrency Control Qwen Team Open-Sources Qwen3.6-35B-A3B: A Sparse MoE Vision-Language Model with 3B Active Parameters and Agentic Coding Capabilities OpenAI Launches GPT-Rosalind: Its First Life Sciences AI Model Built to Accelerate Drug Discovery and Genomics Research Building Transformer-Based NQS for Frustrated Spin Systems with NetKet UCSD and Together AI Research Introduces Parcae: A Stable Architecture for Looped Language Models That Achieves the Quality of a Transformer Twice the Size How to Build a Universal Long-Term Memory Layer for AI Agents Using Mem0 and OpenAI A Coding Implementation to Build Multi-Agent AI Systems with SmolAgents Using Code Execution, Tool Calling, and Dynamic Orchestration A Technical Deep Dive into the Essential Stages of Modern Large Language Model Training, Alignment, and Deployment Google AI Launches Gemini 3.1 Flash TTS: A New Benchmark in Expressive and Controllable AI Voice Google DeepMind Releases Gemini Robotics-ER 1.6: Bringing Enhanced Embodied Reasoning and Instrument Reading to Physical AI Google Launches ‘Skills’ in Chrome: Turning Reusable AI Prompts into One-Click Browser Workflows A Coding Implementation of Crawl4AI for Web Crawling, Markdown Generation, JavaScript Execution, and LLM-Based Structured Extraction TinyFish AI Releases Full Web Infrastructure Platform for AI Agents: Search, Fetch, Browser, and Agent Under One API Key NVIDIA and the University of Maryland Researchers Released Audio Flamingo Next (AF-Next): A Super Powerful and Open Large Audio-Language Model A Hands-On Coding Tutorial for Microsoft VibeVoice Covering Speaker-Aware ASR, Real-Time TTS, and Speech-to-Speech Pipelines Meta AI and KAUST Researchers Propose Neural Computers That Fold Computation, Memory, and I/O Into One Learned Model A Coding Implementation of MolmoAct for Depth-Aware Spatial Reasoning, Visual Trajectory Tracing, and Robotic Action Prediction MiniMax Just Open Sourced MiniMax M2.7: A Self-Evolving Agent Model that Scores 56.22% on SWE-Pro and 57.0% on Terminal Bench 2 Liquid AI Releases LFM2.5-VL-450M: a 450M-Parameter Vision-Language Model with Bounding Box Prediction, Multilingual Support, and Sub-250ms Edge Inference Researchers from MIT, NVIDIA, and Zhejiang University Propose TriAttention: A KV Cache Compression Method That Matches Full Attention at 2.5× Higher Throughput How to Build a Secure Local-First Agent Runtime with OpenClaw Gateway, Skills, and Controlled Tool Execution How Knowledge Distillation Compresses Ensemble Intelligence into a Single Deployable AI Model Alibaba’s Tongyi Lab Releases VimRAG: a Multimodal RAG Framework that Uses a Memory Graph to Navigate Massive Visual Contexts A Coding Guide to Markerless 3D Human Kinematics with Pose2Sim, RTMPose, and OpenSim NVIDIA Releases AITune: An Open-Source Inference Toolkit That Automatically Finds the Fastest Inference Backend for Any PyTorch Model Five AI Compute Architectures Every Engineer Should Know: CPUs, GPUs, TPUs, NPUs, and LPUs Compared An End-to-End Coding Guide to NVIDIA KVPress for Long-Context LLM Inference, KV Cache Compression, and Memory-Efficient Generation Meta Superintelligence Lab Releases Muse Spark: A Multimodal Reasoning Model With Thought Compression and Parallel Agents Sigmoid vs ReLU Activation Functions: The Inference Cost of Losing Geometric Context A Coding Guide to Build Advanced Document Intelligence Pipelines with Google LangExtract, OpenAI Models, Structured Extraction, and Interactive Visualization Google AI Research Introduces PaperOrchestra: A Multi-Agent Framework for Automated AI Research Paper Writing A Comprehensive Implementation Guide to ModelScope for Model Search, Inference, Fine-Tuning, Evaluation, and Export
Build a Reinforcement Learning Powered Agent that Learns to Retrieve Relevant Long-Term Memories for Accurate LLM Question Answering
Asif Razzaq · 2026-04-28 · via MarkTechPost

In this tutorial, we build a Reinforcement Learning–driven agent that learns how to retrieve relevant memories from a long-term memory bank. We start by constructing a synthetic memory dataset and generating queries that require the agent to recall specific information. Using OpenAI embeddings, we convert both memories and queries into vector representations, enabling similarity signals to guide candidate retrieval. We then design a custom RL environment in which the agent observes features of candidate memories and learns a policy to select the most useful one. By training the agent with the PPO algorithm, we enable it to improve retrieval decisions beyond simple similarity search. Finally, we evaluate the system by comparing the RL-based retriever with a baseline approach and demonstrate how an LLM can use retrieved memories to generate accurate answers.

import sys
import subprocess
import pkgutil
import os
import json
import math
import random
import textwrap
import getpass
from dataclasses import dataclass
from typing import List, Dict, Any, Tuple


def _install_if_missing(packages):
   missing = []
   for package_name, import_name in packages:
       if pkgutil.find_loader(import_name) is None:
           missing.append(package_name)
   if missing:
       subprocess.check_call([sys.executable, "-m", "pip", "install", "-q"] + missing)


_install_if_missing([
   ("openai>=1.40.0", "openai"),
   ("gymnasium>=0.29.1", "gymnasium"),
   ("stable-baselines3>=2.3.2", "stable_baselines3"),
   ("numpy>=1.26.4", "numpy"),
   ("pandas>=2.2.2", "pandas"),
   ("scikit-learn>=1.5.1", "sklearn"),
   ("matplotlib>=3.9.0", "matplotlib"),
   ("tqdm>=4.66.4", "tqdm"),
])


import numpy as np
import pandas as pd
import gymnasium as gym
from gymnasium import spaces
from tqdm.auto import tqdm
import matplotlib.pyplot as plt
from sklearn.metrics.pairwise import cosine_similarity
from stable_baselines3 import PPO
from stable_baselines3.common.vec_env import DummyVecEnv
from openai import OpenAI


SEED = 42
random.seed(SEED)
np.random.seed(SEED)


try:
   from google.colab import userdata
   OPENAI_API_KEY = userdata.get("OPENAI_API_KEY")
except Exception:
   OPENAI_API_KEY = None


if not OPENAI_API_KEY:
   OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")


if not OPENAI_API_KEY:
   OPENAI_API_KEY = getpass.getpass("Enter OPENAI_API_KEY: ").strip()


client = OpenAI(api_key=OPENAI_API_KEY)


EMBED_MODEL = "text-embedding-3-small"
CHAT_MODEL = "gpt-4o-mini"


def chunked(xs, n):
   for i in range(0, len(xs), n):
       yield xs[i:i+n]


def embed_texts(texts: List[str], model: str = EMBED_MODEL, batch_size: int = 64) -> np.ndarray:
   outputs = []
   for batch in tqdm(list(chunked(texts, batch_size)), desc="Embedding"):
       resp = client.embeddings.create(model=model, input=batch)
       batch_vecs = [d.embedding for d in resp.data]
       outputs.extend(batch_vecs)
   arr = np.array(outputs, dtype=np.float32)
   norms = np.linalg.norm(arr, axis=1, keepdims=True) + 1e-12
   arr = arr / norms
   return arr


def chat_answer(question: str, retrieved_memories: List[Dict[str, Any]], model: str = CHAT_MODEL) -> str:
   memory_block = "\n".join([f"[Memory {i+1}] {m['text']}" for i, m in enumerate(retrieved_memories)])
   system = "You are a precise QA assistant. Answer the question using only the provided memories. If the memories do not contain the answer, say 'I do not know from the provided memories.'"
   user = f"Question: {question}\n\nRetrieved memories:\n{memory_block}\n\nAnswer:"
   resp = client.chat.completions.create(
       model=model,
       temperature=0,
       messages=[
           {"role": "system", "content": system},
           {"role": "user", "content": user},
       ],
   )
   return resp.choices[0].message.content.strip()


def llm_judge_exact(question: str, gold_answer: str, predicted_answer: str, model: str = CHAT_MODEL) -> float:
   system = "You are a strict evaluator. Return only JSON with a single key 'score'. Use 1.0 if the predicted answer is semantically correct, 0.0 otherwise."
   user = json.dumps({
       "question": question,
       "gold_answer": gold_answer,
       "predicted_answer": predicted_answer,
   }, ensure_ascii=False)
   resp = client.chat.completions.create(
       model=model,
       temperature=0,
       response_format={"type": "json_object"},
       messages=[
           {"role": "system", "content": system},
           {"role": "user", "content": user},
       ],
   )
   txt = resp.choices[0].message.content.strip()
   try:
       obj = json.loads(txt)
       score = float(obj["score"])
       return 1.0 if score >= 0.5 else 0.0
   except Exception:
       return 0.0

We set up the environment required for our reinforcement learning–based memory retrieval system. We install all required libraries, import the necessary modules, and securely load the OpenAI API key for embedding and language model interactions. We also define helper functions that generate embeddings, produce answers from retrieved memories, and evaluate answers using an LLM-based judging mechanism.

@dataclass
class MemoryItem:
   memory_id: int
   topic: str
   entity: str
   slot: str
   value: str
   text: str


def build_memory_bank() -> List[MemoryItem]:
   entities = [
       {
           "entity": "Astra",
           "topic": "robotics",
           "facts": {
               "battery": "18 hours",
               "sensor": "LiDAR",
               "country": "Japan",
               "release_year": "2023",
               "specialty": "warehouse navigation",
           },
       },
       {
           "entity": "Orion",
           "topic": "astronomy",
           "facts": {
               "telescope": "infrared array",
               "country": "Chile",
               "discovery_year": "2019",
               "target": "exoplanet atmospheres",
               "aperture": "8 meters",
           },
       },
       {
           "entity": "Vita",
           "topic": "biomedicine",
           "facts": {
               "compound": "VX-17",
               "trial_phase": "Phase II",
               "country": "Canada",
               "target": "inflammatory markers",
               "delivery": "oral capsule",
           },
       },
       {
           "entity": "Nimbus",
           "topic": "climate",
           "facts": {
               "satellite": "polar orbiter",
               "country": "Norway",
               "launch_year": "2022",
               "instrument": "microwave radiometer",
               "mission": "sea ice monitoring",
           },
       },
       {
           "entity": "Atlas",
           "topic": "logistics",
           "facts": {
               "fleet_size": "240 trucks",
               "hub": "Muscat",
               "software": "predictive routing",
               "fuel_policy": "hybrid-first",
               "region": "GCC",
           },
       },
       {
           "entity": "Lumos",
           "topic": "materials",
           "facts": {
               "alloy": "Ti-6Al-4V",
               "process": "laser sintering",
               "density": "4.43 g/cm3",
               "country": "Germany",
               "use_case": "aerospace brackets",
           },
       },
       {
           "entity": "Cedar",
           "topic": "agriculture",
           "facts": {
               "crop": "wheat",
               "irrigation": "drip control",
               "country": "India",
               "yield_gain": "12 percent",
               "soil_sensor": "capacitive probe",
           },
       },
       {
           "entity": "Pulse",
           "topic": "healthcare",
           "facts": {
               "device": "ECG patch",
               "battery": "7 days",
               "country": "USA",
               "connectivity": "Bluetooth Low Energy",
               "use_case": "arrhythmia screening",
           },
       },
   ]


   phrasing_templates = [
       "{entity} in {topic} uses {value} for {slot}.",
       "The {slot} associated with {entity} is {value}.",
       "{entity} has {slot}: {value}.",
       "For {entity}, the recorded {slot} is {value}.",
       "Reference note: {entity} -> {slot} = {value}.",
   ]


   distractor_templates = [
       "{entity} was discussed in a briefing about cross-domain innovation.",
       "{entity} has been compared with several other projects in recent reports.",
       "A summary note mentions {entity} among notable initiatives.",
       "{entity} appears in a high-level update without technical details.",
       "Stakeholders reviewed {entity} in a strategic planning session.",
   ]


   memory_bank = []
   memory_id = 0


   for item in entities:
       entity = item["entity"]
       topic = item["topic"]
       for slot, value in item["facts"].items():
           for t in phrasing_templates:
               text = t.format(entity=entity, topic=topic, slot=slot, value=value)
               memory_bank.append(MemoryItem(
                   memory_id=memory_id,
                   topic=topic,
                   entity=entity,
                   slot=slot,
                   value=value,
                   text=text
               ))
               memory_id += 1


       for t in distractor_templates:
           text = t.format(entity=entity)
           memory_bank.append(MemoryItem(
               memory_id=memory_id,
               topic=topic,
               entity=entity,
               slot="distractor",
               value="n/a",
               text=text
           ))
           memory_id += 1


   extra_noise = [
       "General note: system maintenance occurred on Tuesday.",
       "A committee discussed budget timelines and operational readiness.",
       "The archive includes summaries of projects across multiple departments.",
       "No relevant technical value is stated in this memory.",
       "A status update mentioned partnerships and future opportunities.",
       "An unrelated note references shipping delays and staffing changes.",
       "Background memo: the team reviewed dashboards and reporting cadence.",
       "This memory contains no answer-bearing facts.",
   ]


   for text in extra_noise:
       memory_bank.append(MemoryItem(
           memory_id=memory_id,
           topic="noise",
           entity="none",
           slot="distractor",
           value="n/a",
           text=text
       ))
       memory_id += 1


   return memory_bank


memory_bank = build_memory_bank()
memory_texts = [m.text for m in memory_bank]
memory_embeddings = embed_texts(memory_texts)


def build_queries(memory_bank: List[MemoryItem]) -> List[Dict[str, Any]]:
   patterns = [
       "What is the {slot} of {entity}?",
       "Which {slot} does {entity} have?",
       "Tell me the {slot} for {entity}.",
       "Can you recall the {slot} associated with {entity}?",
       "What was recorded as the {slot} of {entity}?",
   ]
   queries = []
   qid = 0
   for m in memory_bank:
       if m.slot == "distractor":
           continue
       q = random.choice(patterns).format(slot=m.slot.replace("_", " "), entity=m.entity)
       queries.append({
           "query_id": qid,
           "query": q,
           "entity": m.entity,
           "slot": m.slot,
           "gold_value": m.value,
           "gold_memory_id": m.memory_id,
           "gold_text": m.text,
           "topic": m.topic,
       })
       qid += 1
   random.shuffle(queries)
   return queries


queries = build_queries(memory_bank)
query_texts = [q["query"] for q in queries]
query_embeddings = embed_texts(query_texts)

We construct a synthetic long-term memory bank that simulates stored knowledge across multiple domains. We generate structured memory items and convert them into textual memories that can later be embedded for semantic retrieval. We also create query datasets from these memories and embed them so the agent can compare queries with stored knowledge.

MEM_BY_ID = {m.memory_id: m for m in memory_bank}
QUERY_BY_ID = {q["query_id"]: q for q in queries}


def keyword_overlap(a: str, b: str) -> float:
   ta = set(a.lower().replace("?", "").replace(".", "").split())
   tb = set(b.lower().replace("?", "").replace(".", "").split())
   if not ta or not tb:
       return 0.0
   return len(ta & tb) / max(1, len(ta | tb))


def get_top_k_candidates(query_idx: int, k: int = 8) -> Dict[str, Any]:
   qv = query_embeddings[query_idx:query_idx+1]
   sims = cosine_similarity(qv, memory_embeddings)[0]
   top_idx = np.argsort(-sims)[:k]
   candidates = []
   q = queries[query_idx]
   for rank, midx in enumerate(top_idx):
       mem = memory_bank[midx]
       sim = float(sims[midx])
       overlap = keyword_overlap(q["query"], mem.text)
       entity_match = 1.0 if q["entity"].lower() in mem.text.lower() else 0.0
       slot_match = 1.0 if q["slot"].replace("_", " ").lower() in mem.text.lower() else 0.0
       is_gold = 1.0 if mem.memory_id == q["gold_memory_id"] else 0.0
       candidates.append({
           "rank": rank,
           "memory_index": midx,
           "memory_id": mem.memory_id,
           "text": mem.text,
           "sim": sim,
           "overlap": overlap,
           "entity_match": entity_match,
           "slot_match": slot_match,
           "is_gold": is_gold,
       })
   return {"query": q, "candidates": candidates}


ALL_CANDIDATES = [get_top_k_candidates(i, k=8) for i in range(len(queries))]


def build_state_features(item: Dict[str, Any]) -> np.ndarray:
   q = item["query"]
   feats = []
   for c in item["candidates"]:
       feats.extend([
           c["sim"],
           c["overlap"],
           c["entity_match"],
           c["slot_match"],
           1.0 / (1.0 + c["rank"]),
       ])
   unique_topic_bonus = 1.0 if q["topic"] in q["query"].lower() else 0.0
   query_len = min(len(q["query"].split()) / 20.0, 1.0)
   feats.extend([unique_topic_bonus, query_len])
   return np.array(feats, dtype=np.float32)


STATE_DIM = len(build_state_features(ALL_CANDIDATES[0]))
NUM_ACTIONS = len(ALL_CANDIDATES[0]["candidates"])


class MemoryRetrievalEnv(gym.Env):
   metadata = {"render_modes": ["human"]}


   def __init__(self, candidate_items: List[Dict[str, Any]], seed: int = 42):
       super().__init__()
       self.candidate_items = candidate_items
       self.rng = np.random.default_rng(seed)
       self.observation_space = spaces.Box(low=-10, high=10, shape=(STATE_DIM,), dtype=np.float32)
       self.action_space = spaces.Discrete(NUM_ACTIONS)
       self.current = None


   def reset(self, seed=None, options=None):
       if seed is not None:
           self.rng = np.random.default_rng(seed)
       idx = int(self.rng.integers(0, len(self.candidate_items)))
       self.current = self.candidate_items[idx]
       obs = build_state_features(self.current)
       info = {"query_id": self.current["query"]["query_id"]}
       return obs, info


   def step(self, action):
       chosen = self.current["candidates"][int(action)]
       q = self.current["query"]


       reward = 0.0
       reward += 2.0 * chosen["is_gold"]
       reward += 0.8 * chosen["entity_match"]
       reward += 0.6 * chosen["slot_match"]
       reward += 0.5 * chosen["sim"]
       reward += 0.3 * chosen["overlap"]
       reward -= 0.15 * chosen["rank"]


       done = True
       truncated = False
       info = {
           "query_id": q["query_id"],
           "chosen_memory_id": chosen["memory_id"],
           "gold_memory_id": q["gold_memory_id"],
           "chosen_text": chosen["text"],
           "gold_text": q["gold_text"],
           "is_correct": bool(chosen["memory_id"] == q["gold_memory_id"]),
           "gold_value": q["gold_value"],
           "query": q["query"],
       }
       next_obs = np.zeros(self.observation_space.shape, dtype=np.float32)
       return next_obs, float(reward), done, truncated, info

We prepare candidate memories for each query by computing similarity scores between query embeddings and memory embeddings. We then construct feature vectors that describe each candidate memory using similarity, keyword overlap, entity matching, and rank signals. Finally, we define a custom reinforcement learning environment in which the agent learns to select the best memory to answer each query.

split_1 = int(0.7 * len(ALL_CANDIDATES))
split_2 = int(0.85 * len(ALL_CANDIDATES))
train_items = ALL_CANDIDATES[:split_1]
val_items = ALL_CANDIDATES[split_1:split_2]
test_items = ALL_CANDIDATES[split_2:]


train_env = DummyVecEnv([lambda: MemoryRetrievalEnv(train_items, seed=SEED)])
model = PPO(
   "MlpPolicy",
   train_env,
   learning_rate=3e-4,
   n_steps=256,
   batch_size=64,
   gamma=0.99,
   gae_lambda=0.95,
   ent_coef=0.01,
   clip_range=0.2,
   verbose=0,
   seed=SEED,
)


model.learn(total_timesteps=12000)


def baseline_retrieve(item: Dict[str, Any]) -> Dict[str, Any]:
   best = max(item["candidates"], key=lambda x: x["sim"])
   return best


def rl_retrieve(item: Dict[str, Any]) -> Dict[str, Any]:
   obs = build_state_features(item)
   action, _ = model.predict(obs, deterministic=True)
   return item["candidates"][int(action)]


def evaluate_retriever(items: List[Dict[str, Any]], retriever_fn) -> Dict[str, Any]:
   rows = []
   for item in items:
       chosen = retriever_fn(item)
       q = item["query"]
       rows.append({
           "query_id": q["query_id"],
           "query": q["query"],
           "gold_value": q["gold_value"],
           "gold_memory_id": q["gold_memory_id"],
           "chosen_memory_id": chosen["memory_id"],
           "correct_retrieval": int(chosen["memory_id"] == q["gold_memory_id"]),
           "chosen_text": chosen["text"],
       })
   df = pd.DataFrame(rows)
   return {
       "df": df,
       "retrieval_accuracy": df["correct_retrieval"].mean(),
   }


baseline_val = evaluate_retriever(val_items, baseline_retrieve)
rl_val = evaluate_retriever(val_items, rl_retrieve)
baseline_test = evaluate_retriever(test_items, baseline_retrieve)
rl_test = evaluate_retriever(test_items, rl_retrieve)


print("Validation Retrieval Accuracy")
print("Baseline:", round(float(baseline_val["retrieval_accuracy"]), 4))
print("RL      :", round(float(rl_val["retrieval_accuracy"]), 4))
print()
print("Test Retrieval Accuracy")
print("Baseline:", round(float(baseline_test["retrieval_accuracy"]), 4))
print("RL      :", round(float(rl_test["retrieval_accuracy"]), 4))


results_df = pd.DataFrame([
   {"split": "validation", "method": "baseline_cosine", "retrieval_accuracy": float(baseline_val["retrieval_accuracy"])},
   {"split": "validation", "method": "rl_agent", "retrieval_accuracy": float(rl_val["retrieval_accuracy"])},
   {"split": "test", "method": "baseline_cosine", "retrieval_accuracy": float(baseline_test["retrieval_accuracy"])},
   {"split": "test", "method": "rl_agent", "retrieval_accuracy": float(rl_test["retrieval_accuracy"])},
])
display(results_df)


plot_df = results_df.copy()
for split_name in ["validation", "test"]:
   sub = plot_df[plot_df["split"] == split_name]
   plt.figure(figsize=(6, 4))
   plt.bar(sub["method"], sub["retrieval_accuracy"])
   plt.title(f"Retrieval Accuracy on {split_name.title()}")
   plt.ylim(0, 1)
   plt.ylabel("Accuracy")
   plt.show()

We split the datasets and initialize the reinforcement learning model. We train a PPO agent to learn a policy for selecting the most relevant memory from a set of candidates. After training, we evaluate the agent’s retrieval performance and compare it with a baseline embedding-similarity approach.

def answer_with_retriever(item: Dict[str, Any], retriever_fn) -> Dict[str, Any]:
   q = item["query"]
   chosen = retriever_fn(item)
   retrieved_memories = [{
       "memory_id": chosen["memory_id"],
       "text": chosen["text"],
   }]
   answer = chat_answer(q["query"], retrieved_memories)
   judged = llm_judge_exact(q["query"], q["gold_value"], answer)
   return {
       "query": q["query"],
       "gold_value": q["gold_value"],
       "retrieved_text": chosen["text"],
       "predicted_answer": answer,
       "answer_score": judged,
       "retrieval_correct": int(chosen["memory_id"] == q["gold_memory_id"]),
   }


sample_test_items = random.sample(test_items, min(12, len(test_items)))
baseline_answers = [answer_with_retriever(item, baseline_retrieve) for item in tqdm(sample_test_items, desc="Baseline QA")]
rl_answers = [answer_with_retriever(item, rl_retrieve) for item in tqdm(sample_test_items, desc="RL QA")]


baseline_answer_df = pd.DataFrame(baseline_answers)
rl_answer_df = pd.DataFrame(rl_answers)


print("Sample Downstream QA Accuracy")
print("Baseline:", round(float(baseline_answer_df["answer_score"].mean()), 4))
print("RL      :", round(float(rl_answer_df["answer_score"].mean()), 4))


comparison = pd.DataFrame([
   {"method": "baseline_cosine", "qa_accuracy": float(baseline_answer_df["answer_score"].mean())},
   {"method": "rl_agent", "qa_accuracy": float(rl_answer_df["answer_score"].mean())},
])
display(comparison)


plt.figure(figsize=(6, 4))
plt.bar(comparison["method"], comparison["qa_accuracy"])
plt.title("Downstream QA Accuracy from Retrieved Memories")
plt.ylim(0, 1)
plt.ylabel("Accuracy")
plt.show()


def inspect_examples(items: List[Dict[str, Any]], n: int = 5):
   chosen_items = random.sample(items, min(n, len(items)))
   rows = []
   for item in chosen_items:
       q = item["query"]
       base = baseline_retrieve(item)
       rlm = rl_retrieve(item)
       rows.append({
           "query": q["query"],
           "gold_value": q["gold_value"],
           "baseline_text": base["text"],
           "baseline_correct": int(base["memory_id"] == q["gold_memory_id"]),
           "rl_text": rlm["text"],
           "rl_correct": int(rlm["memory_id"] == q["gold_memory_id"]),
       })
   return pd.DataFrame(rows)


examples_df = inspect_examples(test_items, n=8)
pd.set_option("display.max_colwidth", 200)
display(examples_df)

We evaluate how well the retrieved memories support downstream question answering. We generate answers using the retrieved memory context and assess the answers with an LLM-based judge to determine correctness. We also inspect example queries to visually compare how the baseline retriever and the RL agent choose different memories.

def interactive_demo(question: str, top_k: int = 8):
   qv = embed_texts()
   sims = cosine_similarity(qv, memory_embeddings)[0]
   top_idx = np.argsort(-sims)[:top_k]


   candidates = []
   for rank, midx in enumerate(top_idx):
       mem = memory_bank[midx]
       candidates.append({
           "rank": rank,
           "memory_index": int(midx),
           "memory_id": int(mem.memory_id),
           "text": mem.text,
           "sim": float(sims[midx]),
           "overlap": keyword_overlap(question, mem.text),
           "entity_match": 0.0,
           "slot_match": 0.0,
           "is_gold": 0.0,
       })


   pseudo_item = {
       "query": {
           "query_id": -1,
           "query": question,
           "entity": "unknown",
           "slot": "unknown",
           "gold_value": "unknown",
           "gold_memory_id": -1,
           "gold_text": "",
           "topic": "unknown",
       },
       "candidates": candidates,
   }


   obs = build_state_features(pseudo_item)
   action, _ = model.predict(obs, deterministic=True)
   selected = pseudo_item["candidates"][int(action)]
   answer = chat_answer(question, [{"memory_id": selected["memory_id"], "text": selected["text"]}])


   print("=" * 100)
   print("QUESTION")
   print(question)
   print("=" * 100)
   print("TOP CANDIDATES")
   for c in candidates:
       print(f"[Rank {c['rank']}] sim={c['sim']:.4f} | {c['text']}")
   print("=" * 100)
   print("RL-SELECTED MEMORY")
   print(selected["text"])
   print("=" * 100)
   print("ANSWER")
   print(answer)
   print("=" * 100)


interactive_demo("What is the battery of Pulse?")
interactive_demo("Which hub does Atlas have?")
interactive_demo("Tell me the country for Cedar.")


artifact_dir = "/content/rl_agent_memory_retrieval_artifacts"
os.makedirs(artifact_dir, exist_ok=True)


results_df.to_csv(f"{artifact_dir}/retrieval_results.csv", index=False)
baseline_val["df"].to_csv(f"{artifact_dir}/baseline_val.csv", index=False)
rl_val["df"].to_csv(f"{artifact_dir}/rl_val.csv", index=False)
baseline_test["df"].to_csv(f"{artifact_dir}/baseline_test.csv", index=False)
rl_test["df"].to_csv(f"{artifact_dir}/rl_test.csv", index=False)
baseline_answer_df.to_csv(f"{artifact_dir}/baseline_qa_sample.csv", index=False)
rl_answer_df.to_csv(f"{artifact_dir}/rl_qa_sample.csv", index=False)
examples_df.to_csv(f"{artifact_dir}/example_comparisons.csv", index=False)


np.save(f"{artifact_dir}/memory_embeddings.npy", memory_embeddings)
np.save(f"{artifact_dir}/query_embeddings.npy", query_embeddings)
model.save(f"{artifact_dir}/ppo_memory_retriever")


with open(f"{artifact_dir}/memory_bank.json", "w") as f:
   json.dump([m.__dict__ for m in memory_bank], f, indent=2)


with open(f"{artifact_dir}/queries.json", "w") as f:
   json.dump(queries, f, indent=2)


print(f"Saved artifacts to: {artifact_dir}")
print("Tutorial complete.")

We build an interactive demonstration that lets us test the trained retrieval agent on new questions. We show the candidate memories, highlight the memory selected by the RL agent, and generate an answer using the selected context. Also, we save all artifacts, including embeddings, results, datasets, and the trained RL model, so that the system can be reused or further analyzed.

In conclusion, we demonstrated how reinforcement learning can enhance memory retrieval in agentic AI systems. We trained an RL agent to select relevant memories from a set of candidates using signals such as semantic similarity, keyword overlap, and entity matching. We then evaluated the retriever and observed how the learned policy compares with traditional embedding-based retrieval methods. By integrating the retriever with an LLM, we also showed how better memory selection improves downstream question-answering performance. Through experiments, visualizations, and interactive demos, we explored how RL can optimize long-term memory access in intelligent agents.


Check out the FULL CODES here. Also, feel free to follow us on Twitter and don’t forget to join our 130k+ ML SubReddit and Subscribe to our Newsletter. Wait! are you on telegram? now you can join us on telegram as well.

Need to partner with us for promoting your GitHub Repo OR Hugging Face Page OR Product Release OR Webinar etc.? Connect with us