examples: add persistent memory cookbook with Dakera integration (31 tests)#2405
Open
ferhimedamine wants to merge 1 commit into
Open
examples: add persistent memory cookbook with Dakera integration (31 tests)#2405ferhimedamine wants to merge 1 commit into
ferhimedamine wants to merge 1 commit into
Conversation
Adds a new cookbook entry showing how to give any Instructor application persistent, decay-weighted long-term memory using Dakera (https://dakera.ai) — a self-hosted vector memory server. ## What's added ### `examples/persistent_memory_dakera/` - `dakera_memory.py` — integration helper: - `DakeraMemory`: sync httpx client for `/v1/memory/store`, `/v1/memory/search`, `/v1/memory/forget` - `AsyncDakeraMemory`: async variant using `httpx.AsyncClient` - `DakeraMemoryHook`: attaches to any instructor client via `client.on()`; auto-recalls before each call (`completion:kwargs`), auto-stores after (`completion:response`); Dakera errors are silently swallowed so they never crash an LLM call - `build_context_messages(messages, hits)`: inject recalled memories as a leading system message - `run.py` — four runnable examples: 1. Manual store + recall with `build_context_messages` 2. Zero-boilerplate hook-based approach (`hook.attach(client)`) 3. Structured extraction pipeline: `UserProfile` Pydantic model 4. Multi-turn CLI chat loop that persists across restarts - `test_dakera_memory.py` — 31 tests, all passing (fully mocked, no live server needed) ### `docs/examples/persistent_memory_dakera.md` Cookbook page with usage examples, API reference, and Quick Start. ### `mkdocs.yml` Entry added under `Cookbook:` alongside `Tracing with Langfuse`. ## Quick start ```bash docker run -p 3300:3300 -e DAKERA_API_KEY=demo ghcr.io/dakera-ai/dakera:latest pip install instructor openai httpx python examples/persistent_memory_dakera/run.py ``` Co-Authored-By: Paperclip <noreply@paperclip.ing>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2404
Summary
Adds a complete cookbook recipe for persistent, cross-session memory using Dakera — a self-hosted, decay-weighted vector memory server.
Instructor currently has no memory example. This PR adds one using Instructor's native hook system, matching the pattern already established by
examples/tracing_with_langfuse.md.Files added
How
DakeraMemoryHookworksUses Instructor's
client.on()hook system — same mechanism as the Langfuse tracing integration:completion:kwargs— searches Dakera for the top-K relevant memories for the current prompt and prepends them as a system message blockcompletion:response— stores the user prompt + model reply in Dakera after the callErrors from Dakera are swallowed (non-fatal) — the hook never crashes an LLM call.
Four runnable examples
1. Manual store + recall
2. Zero-boilerplate hook (auto-recall + auto-store)
3. Structured extraction pipeline
4. Multi-turn CLI chat loop (persists across restarts)
Tests
31 tests using
unittest.mock— no live server or LLM credentials required:Prerequisites
No new dependencies added to
pyproject.toml—httpxis already a transitive dependency of instructor.