Skip to main content
Glama
README.md
# Zenith External-Engram

**A lightweight, drop-in External-Engram memory layer for local LLMs (Ollama, `llama.cpp`, Claude Code, and MCP clients).**

Compact local models (4B–12B) excel at reasoning and tool use, but waste context and parameters trying to memorize exact symbol names, C++ structs, CLI flags, and codebase patterns. Inspired by host-RAM N-gram / Engram conditional memory, **Zenith External-Engram** pairs:

1. **Exact Symbol & Trigram N-gram matching (`pg_trgm`)** — instant recall on rare identifiers, error codes, and C++/Python symbols where dense vectors struggle.
2. **BM25-style Full-Text Search (`tsvector` + GIN)** — fast keyword & multi-term recall.
3. **Dense Semantic Vector Search (`pgvector`)** — conceptual recall via any local OpenAI-compatible embedding server (`nomic-embed-text` via Ollama or `llama-server --embedding`).
4. **Reciprocal Rank Fusion (RRF)** — fuses all three signals into a single ranked result set in milliseconds.

---

## Three Drop-In Interfaces on One Port (`:8316`)

| Endpoint | Purpose |
| :--- | :--- |
| `POST /search` | Fast JSON API for hybrid N-gram + FTS + Vector RRF search |
| `POST /mcp` | Streamable HTTP MCP server (`engram_search`, `engram_stats`) for Claude Code & MCP clients |
| `POST /v1/chat/completions` | Drop-in OpenAI-compatible proxy for **Ollama** or **`llama.cpp`** — automatically injects high-confidence `<engram_memory>` snippets into context (override target with `X-Upstream-URL`, disable per-request with `X-Engram-Disable: 1`) |

---

## 2-Minute Quickstart

### 1. Start Postgres (`pgvector` + `pg_trgm`)
```bash
docker compose up -d
```

### 2. Install Python dependencies
```bash
pip install -r requirements.txt
```

### 3. Ingest your codebases (Python, C/C++, TypeScript/JS, Go, Java, Markdown, Bash)
```bash
export DATABASE_URL="postgresql://engram:engram@127.0.0.1:5435/engram"
# Optional: point to Ollama or llama-server embeddings (768-dim nomic-embed-text)
export EMBED_URL="http://127.0.0.1:11434/v1/embeddings"
export EMBED_MODEL="nomic-embed-text"

python ingest.py /path/to/your/repo --tag my-repo --category backend
```
*(Works with or without an embedding server — if `EMBED_URL` is unavailable, trigram N-gram + FTS search still runs out of the box.)*

### 4. Start the External-Engram Server
```bash
export UPSTREAM_URL="http://127.0.0.1:11434"   # Ollama or any llama-server
python server.py
```

### 5. Use It Anywhere

**Direct Hybrid Search:**
```bash
curl -s http://127.0.0.1:8316/search \
  -H "Content-Type: application/json" \
  -d '{"query": "engram_gate", "top_k": 5}'
```

**MCP Client (`.mcp.json`):**
```json
{
  "mcpServers": {
    "zenith_engram": {
      "type": "http",
      "url": "http://127.0.0.1:8316/mcp"
    }
  }
}
```

**Drop-In Proxy for Ollama / `llama.cpp`:**
Point any OpenAI-compatible client at `http://127.0.0.1:8316/v1`. To target a specific `llama-server` slot instead of Ollama, pass header `X-Upstream-URL: http://127.0.0.1:8080`.

---

## Tests
```bash
pytest -q
```

## License
MIT