Skip to content

examples: add Ollama persistent-memory recipes#180

Merged
ferhimedamine merged 1 commit into
mainfrom
examples/ollama-memory
Jul 3, 2026
Merged

examples: add Ollama persistent-memory recipes#180
ferhimedamine merged 1 commit into
mainfrom
examples/ollama-memory

Conversation

@ferhimedamine

Copy link
Copy Markdown
Contributor

Problem

Ollama's /api/chat and /api/generate are 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 ships ChatMemorySession, 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 around ollama.chat using ChatMemorySession. 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 :8080 instead of :11434 and /api/chat transparently 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 the dakera-deploy compose (the server needs its object store), and recalled context is injected as a system message rather than a non-existent top-level system field.

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.
  • Logic verified under mocks: 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.
  • Not added to the live-server examples CI 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

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.
@ferhimedamine ferhimedamine merged commit 51005cb into main Jul 3, 2026
5 checks passed
@ferhimedamine

Copy link
Copy Markdown
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 — ChatMemorySession/AsyncChatMemorySession exports and signatures (create, store(role, content, importance=0.6, tags=), recall(query, top_k=5), close(summary=)), RecalledMemory.content, and the port-3000 default (matches server default config). Examples-only, CI green, public-repo hygiene clean. ✅ No follow-up needed; example link is live on main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant