Files
wehub-resource-sync c56bef871b
Sync docs with Docusaurus / sync (push) Waiting to run
Tests / Check if changed (push) Waiting to run
Tests / format (push) Blocked by required conditions
Tests / check-imports (push) Blocked by required conditions
Tests / Unit / macos-latest (push) Blocked by required conditions
Tests / Unit / ubuntu-latest (push) Blocked by required conditions
Tests / Unit / windows-latest (push) Blocked by required conditions
Tests / mypy (push) Blocked by required conditions
Tests / Integration / ubuntu-latest (push) Blocked by required conditions
Tests / Integration / macos-latest (push) Blocked by required conditions
Tests / Integration / windows-latest (push) Blocked by required conditions
Tests / notify-slack-on-failure (push) Blocked by required conditions
Tests / Mark tests as completed (push) Blocked by required conditions
Docker image release / Build base image (push) Waiting to run
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:22:28 +08:00

125 lines
4.7 KiB
Plaintext

---
title: "CogneeMemoryStore"
id: cogneememorystore
slug: "/cogneememorystore"
description: "A persistent memory store backed by Cognee's knowledge graph API."
---
# CogneeMemoryStore
`CogneeMemoryStore` is a persistent memory store backed by Cognee's knowledge graph API. It is the shared data layer used by [`CogneeRetriever`](../pipeline-components/retrievers/cogneeretriever.mdx) and [`CogneeWriter`](../pipeline-components/writers/cogneewriter.mdx).
<div className="key-value-table">
| | |
| --- | --- |
| **Used by** | [`CogneeRetriever`](../pipeline-components/retrievers/cogneeretriever.mdx), [`CogneeWriter`](../pipeline-components/writers/cogneewriter.mdx) |
| **Optional init variables** | `search_type`, `top_k`, `dataset_name`, `session_id`, `self_improvement`, `timeout` |
| **API reference** | [Cognee](/reference/integrations-cognee#cogneememorystore) |
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/cognee |
| **Package name** | `cognee-haystack` |
</div>
## Overview
`CogneeMemoryStore` wraps Cognee's V2 memory API:
- `add_memories` → `cognee.remember`
- `search_memories` → `cognee.recall`
- `improve` → `cognee.improve`
- `delete_all_memories` → `cognee.forget`
Cognee supports two memory tiers. Set `session_id` to use the **session cache** — fast writes with no LLM extraction, session-aware recall. Leave `session_id` as `None` to write to the **permanent knowledge graph**, which uses LLM extraction during ingestion and supports richer graph-completion queries.
Cognee configuration (LLM provider, database, vector store) is read from environment variables. See the [Cognee documentation](https://docs.cognee.ai) for setup instructions.
### Parameters
- `search_type` is *optional* and defaults to `"GRAPH_COMPLETION"`. Controls which Cognee recall strategy is used. Other useful values include `"CHUNKS"` for raw retrieval and `"SUMMARIES"` for summarized graph nodes.
- `top_k` is *optional* and defaults to `5`. Sets the default maximum number of memories returned per search.
- `dataset_name` is *optional* and defaults to `"haystack_memory"`. Names the Cognee dataset backing this store.
- `session_id` is *optional* and defaults to `None`. When set, reads and writes target the session-cache tier. When `None`, the permanent knowledge graph is used.
- `self_improvement` is *optional* and defaults to `True`. When `True`, Cognee runs graph improvement inline after every write. Set to `False` when you want `improve()` to be the sole improvement trigger.
- `timeout` is *optional* and defaults to `300`. Per-call timeout in seconds for any Cognee operation.
### Installation
Install the Cognee integration:
```bash
pip install cognee-haystack
```
Set your LLM API key (used by Cognee for graph extraction and queries):
```bash
export LLM_API_KEY="your-llm-api-key"
```
Optionally, set a separate embedding API key (defaults to `LLM_API_KEY` when unset):
```bash
export EMBEDDING_API_KEY="your-embedding-api-key"
```
## Usage
### On its own
```python
from haystack.dataclasses import ChatMessage
from haystack_integrations.memory_stores.cognee import CogneeMemoryStore
store = CogneeMemoryStore(search_type="GRAPH_COMPLETION", top_k=5)
store.add_memories(
messages=[ChatMessage.from_user("Alice enjoys hiking and outdoor activities.")],
user_id="a1b2c3d4-e5f6-7890-abcd-ef1234567890",
)
memories = store.search_memories(
query="What does Alice like?",
user_id="a1b2c3d4-e5f6-7890-abcd-ef1234567890",
)
print([msg.text for msg in memories])
```
### Session tier vs permanent graph
Use `session_id` to control which memory tier is targeted. A single store can serve both tiers — the writer's `session_id` overrides the store's `session_id` per call.
```python
from haystack.dataclasses import ChatMessage
from haystack_integrations.memory_stores.cognee import CogneeMemoryStore
store = CogneeMemoryStore(dataset_name="my_agent_memory", self_improvement=False)
# Write long-lived facts to the permanent graph (no session_id).
store.add_memories(
messages=[ChatMessage.from_user("Alice is a senior data scientist at Acme Corp.")],
)
# Write transient session context to the session cache.
store.add_memories(
messages=[ChatMessage.from_user("Alice is currently debugging a vector store issue.")],
session_id="alice_session_1",
)
# Promote the session cache into the permanent graph.
store.improve(session_id="alice_session_1")
```
### Delete all memories
```python
# Delete only this store's dataset (session cache is unaffected).
store.delete_all_memories()
# To wipe everything including the session cache, call cognee directly:
import asyncio
import cognee
asyncio.run(cognee.forget(everything=True))
```