LLMLingua-2 context compression sidecar for Hermes — FastAPI service that compresses long conversation contexts lossily before the Hermes summarizer runs. Plugin-friendly, no core Hermes edits.
Find a file
Mark f4134e72dd Scaffold LLMLingua-2 sidecar: FastAPI service with /compress and /health endpoints
- Segments input: system / tool calls / recency tail protected from compression
- LLMLingua-2 compresses middle segment only
- Env-configurable model, device, default ratio
- README with architecture diagram and API reference
- .gitignore for Python artifacts
2026-09-18 07:18:30 -07:00
app Scaffold LLMLingua-2 sidecar: FastAPI service with /compress and /health endpoints 2026-09-18 07:18:30 -07:00
.gitignore Scaffold LLMLingua-2 sidecar: FastAPI service with /compress and /health endpoints 2026-09-18 07:18:30 -07:00
README.md Scaffold LLMLingua-2 sidecar: FastAPI service with /compress and /health endpoints 2026-09-18 07:18:30 -07:00
requirements.txt Scaffold LLMLingua-2 sidecar: FastAPI service with /compress and /health endpoints 2026-09-18 07:18:30 -07:00

LLMLingua-2 Sidecar

Lossy context compression for Hermes conversations, built on LLMLingua-2.

Why

Hermes sessions can blow past the 256K compression threshold inside one cooldown window. The built-in summarizer then runs on an already-over-threshold context. This sidecar sits in front of the summarizer: it token-level compresses the middle of the conversation (everything except the recency buffer, system prompt, and tool calls), so the summarizer sees a smaller, cleaner input. Result: fewer tokens per turn, cheaper sessions, and less pressure on the compression threshold.

Architecture

Hermes session
    │
    ▼
[compression trigger] ──► POST /compress
                              │
                              ▼
                    ┌─────────────────────┐
                    │   FastAPI sidecar    │
                    │                     │
                    │  1. Segment input   │
                    │     (system / tools │
                    │     / recency /     │
                    │     middle)          │
                    │                     │
                    │  2. LLMLingua-2 on  │
                    │     middle only     │
                    │                     │
                    │  3. Return segments │
                    └─────────────────────┘
                              │
                              ▼
                    Hermes summarizer
                    (on compressed middle)

Quick Start

pip install -r requirements.txt
python -m uvicorn app.main:app --host 0.0.0.0 --port 8317

Health check:

curl http://localhost:8317/health

Compress a conversation:

curl -X POST http://localhost:8317/compress \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Very long conversation..."}
    ],
    "target_ratio": 0.3,
    "recency_tail_turns": 6
  }'

Configuration

Env Var Default Description
LLMLINGUA_MODEL microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank HuggingFace model ID
LLMLINGUA_DEVICE cpu cpu or cuda
LLMLINGUA_DEFAULT_RATIO 0.3 Default compression ratio (0.0–1.0)
PORT 8317 Listen port

API

GET /health

Returns model load status and device.

POST /compress

Request body:

{
  "messages": [
    {"role": "system", "content": "..."},
    {"role": "user", "content": "..."},
    {"role": "assistant", "content": "..."}
  ],
  "target_ratio": 0.3,
  "recency_tail_turns": 6
}

Response:

{
  "compressed_messages": [...],
  "original_tokens": 150000,
  "compressed_tokens": 45000,
  "ratio": 0.3,
  "segments": {
    "system": 1,
    "tools": 0,
    "recency": 6,
    "middle_compressed": 42
  }
}

Guarantees

  • System prompt: never compressed.
  • Tool/function calls: never compressed (JSON integrity).
  • Code blocks: never compressed (variable names survive).
  • Recency tail: last N turns excluded from compression.

Eval Harness

See eval/ for the quality gate. Run real conversation traces through the sidecar, compare downstream answers against uncompressed baselines, and only ship if quality holds.

cd eval/
python run_eval.py --sessions ../test_sessions/*.jsonl --ratio 0.3

License

MIT