CoolFace
Apppublic

djidubey/dspm-memory-env

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
App README

๐Ÿง  DSPM: Dynamic Semantic Patch Memory Environment

A real-world OpenEnv RL environment for LLM long-context memory compression research

Author: Dhruv Dubey Affiliation: Bansal Institute of Engineering & Technology, AKTU, Lucknow, India


๐ŸŽฏ What is this?

One of the biggest unsolved problems in AI is how LLMs handle long conversations โ€” they forget early context, hallucinate, or exceed token limits.

This environment trains RL agents to compress long multi-turn conversations using 7 semantic patch memory techniques, while maximising information retention. The agent learns which information to keep, which to compress, and which to discard.


๐Ÿ”ฌ The Science โ€” 7 DSPM Compression Techniques

TechniqueDescription
T1 Semantic FingerprintingHash-based deduplication of redundant patches
T2 SlotFusionCanonical slot collapsing of similar concepts
T3 ฮ”-Diff EncodingPatch-level change encoding (only store what changed)
T4 Causal Chain PruningEliminate intermediate nodes in causal chains
T5 Utility ScoringWeighted patch ranking by importance
T6 Shadow ScoringMarginal-value filtering of low-value patches
T7 Adaptive Budget RedistributionQuery-driven token reallocation

๐ŸŽฎ Environment Design

Action Space

ActionNameDescription
0STORE_ALLKeep all semantic patches from this turn
1COMPRESSApply full DSPM compression pipeline
2SKIPDiscard this turn (risky!)
3PRIORITIZEBoost utility score of this turn's patches

Observation Space

10-dimensional Box(0.0, 1.0):

[turn_progress, memory_fullness, patch_density, constraint_ratio,
 decision_ratio, token_ratio, questions_remaining, avg_utility,
 compression_rate, episode_step]

Reward Function

  • โ€”+retention_score (0.0โ€“1.0) at episode end based on memory recall
  • โ€”+0.05 for smart compression actions
  • โ€”-0.05 for skipping turns
  • โ€”-0.10 for memory overflow

๐Ÿ“Š Tasks (3 Difficulty Levels)

๐ŸŸข Easy

  • โ€”5-turn REST API design dialogue
  • โ€”3 retention questions
  • โ€”Token budget: 400
  • โ€”Max steps: 10

๐ŸŸก Medium

  • โ€”8-turn ML pipeline design dialogue
  • โ€”4 retention questions
  • โ€”Token budget: 600
  • โ€”Max steps: 15

๐Ÿ”ด Hard

  • โ€”11-turn distributed systems dialogue
  • โ€”5 retention questions
  • โ€”Token budget: 800
  • โ€”Max steps: 20

๐Ÿš€ API Endpoints

MethodEndpointDescription
POST/resetStart new episode
POST/stepTake action, get reward
GET/stateGet current memory state
POST/gradeGrade performance (0.0โ€“1.0)
GET/tasksList all tasks
GET/docsInteractive API documentation

Example Usage

python
import requests

BASE = "https://djidubey-dspm-memory-env.hf.space"

# Reset
r = requests.post(f"{BASE}/reset", json={"difficulty": "easy"})
obs = r.json()["observation"]

# Step
r = requests.post(f"{BASE}/step", json={"action": 1, "difficulty": "easy"})
print(r.json())

# State
r = requests.get(f"{BASE}/state", params={"difficulty": "easy"})
print(r.json())

โš™๏ธ Setup & Run Locally

bash
# Install
pip install fastapi uvicorn numpy scikit-learn openai requests

# Run
uvicorn app:app --host 0.0.0.0 --port 7860

# Run inference
export API_BASE_URL="https://router.huggingface.co/v1"
export MODEL_NAME="Qwen/Qwen2.5-72B-Instruct"
export HF_TOKEN="your_token"
python inference.py

Docker

bash
docker build -t dspm-env .
docker run -p 7860:7860 dspm-env

๐Ÿ“ˆ Baseline Scores

TaskRandom AgentGreedy Agent
Easy0.330.67
Medium0.250.50
Hard0.200.40

๐Ÿงช Research Background

This environment is based on the DSPM (Dynamic Semantic Patch Memory) framework, an IEEE-grade research implementation that achieves:

  • โ€”~60% token reduction vs raw context
  • โ€”>85% Critical Retention Rate (CRR)
  • โ€”4.2/5.0 downstream answer consistency

The framework outperforms naive truncation and LLM summarization baselines on all metrics.


๐Ÿ“ Project Structure

dspm-memory-env/
โ”œโ”€โ”€ app.py           โ† FastAPI server
โ”œโ”€โ”€ environment.py   โ† DSPM environment logic
โ”œโ”€โ”€ inference.py     โ† LLM agent baseline
โ”œโ”€โ”€ openenv.yaml     โ† OpenEnv config
โ”œโ”€โ”€ Dockerfile       โ† Container setup
โ””โ”€โ”€ README.md        โ† This file