examples: add Ollama persistent-memory recipes#180
Merged
Conversation
Add two runnable examples showing how to give a stateless Ollama chat persistent, decay-weighted memory via Dakera: - ollama_memory_chat.py: embeds recall/store around ollama.chat using ChatMemorySession (recalls across the agent's full memory, stores each turn as episodic memory, weights durable preferences by importance). - ollama_memory_proxy.py: a transparent FastAPI proxy — point any Ollama client at :8080 instead of :11434 and /api/chat gains memory with no app changes. Implements the recipe requested in ollama/ollama#16987 with corrected grounding (port 3000, dakera-deploy self-host, context injected as a system message rather than a non-existent top-level field). Both are lint-clean (ruff) and excluded from the live-server examples CI job since they require a running Ollama with a pulled model.
Contributor
Author
|
🤖 [Agent: Core Engine] Post-merge verification (review requested via Paperclip 87ab1b51; PR was merged before it landed): all SDK references verified against source — |
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.
Problem
Ollama's
/api/chatand/api/generateare stateless — every call starts fresh, so Ollama apps have no cross-session memory. Users kept asking how to add persistent recall (e.g. ollama/ollama#16987). The SDK already shipsChatMemorySession, but we had no worked Ollama example to point people at.Design
Two complementary, runnable examples under
examples/:ollama_memory_chat.py— embeds recall→chat→store directly aroundollama.chatusingChatMemorySession. Recalls across the agent's full memory (not just the current session), stores each turn as episodic memory, and weights a durable user preference at high importance so it reliably resurfaces on later, semantically related turns.ollama_memory_proxy.py— a transparent FastAPI middleware. Point any existing Ollama client at:8080instead of:11434and/api/chattransparently gains memory with no application changes. This implements the recipe requested in Community recipe: Persistent memory for Ollama apps via Dakera middleware ollama/ollama#16987, with the grounding corrected: Dakera listens on port 3000 (not 3300), self-hosting uses thedakera-deploycompose (the server needs its object store), and recalled context is injected as a system message rather than a non-existent top-levelsystemfield.Both use only the documented public SDK surface (
DakeraClient/AsyncDakeraClient,ChatMemorySession/AsyncChatMemorySession,store/recall/close).Testing
ruff check .— clean (repo config).python -m py_compile— clean.chat()injects the system context, forwards to Ollama, and stores the user+assistant turns in order;_last_user_message()handles present/absent/empty user turns.examplesCI job: unlike the other examples it requires a running Ollama daemon with a pulled model, which the CI service container can't provide. Each example guards its optional third-party imports (ollama,fastapi/httpx) with an install hint.Checklist
examples/style (memory.py,hybrid_search.py)