Skip to content

Commit eff01ab

Browse files
bilgeyucelkacperlukawski
authored andcommitted
Add the cognee integration page
1 parent 39408c9 commit eff01ab

1 file changed

Lines changed: 159 additions & 0 deletions

File tree

integrations/cognee.md

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
---
2+
layout: integration
3+
name: Cognee
4+
description: Add persistent, knowledge-graph-backed memory to your Haystack agents and pipelines with Cognee
5+
authors:
6+
- name: deepset
7+
socials:
8+
github: deepset-ai
9+
twitter: haystack_ai
10+
linkedin: https://www.linkedin.com/company/deepset-ai/
11+
- name: Cognee
12+
socials:
13+
github: topoteretes
14+
twitter: cognee_ai
15+
linkedin: https://www.linkedin.com/company/cognee-ai/
16+
pypi: https://pypi.org/project/cognee-haystack/
17+
repo: https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/cognee
18+
type: Memory Store
19+
report_issue: https://github.com/deepset-ai/haystack-core-integrations/issues
20+
logo: /logos/cognee.png
21+
version: Haystack 2.0
22+
toc: true
23+
---
24+
25+
### **Table of Contents**
26+
27+
- [Overview](#overview)
28+
- [Installation](#installation)
29+
- [Usage](#usage)
30+
- [Available Classes](#available-classes)
31+
- [Use in a Pipeline](#use-in-a-pipeline)
32+
- [License](#license)
33+
34+
## Overview
35+
36+
[Cognee](https://www.cognee.ai/) provides open-source, knowledge-graph-backed memory for AI agents and assistants. It helps Haystack applications store facts, conversation history, and contextual knowledge as a semantic graph, then retrieve the most relevant memories using graph-completion or other search strategies.
37+
38+
The `cognee-haystack` package is part of [Haystack Core Integrations](https://github.com/deepset-ai/haystack-core-integrations) and provides:
39+
40+
- `CogneeMemoryStore`: A persistent memory store backed by Cognee's knowledge graph API.
41+
- `CogneeRetriever`: A pipeline component for retrieving memories from Cognee as system `ChatMessage` objects.
42+
- `CogneeWriter`: A pipeline component for writing `ChatMessage` memories to Cognee.
43+
44+
Cognee supports two memory tiers:
45+
46+
- **Permanent knowledge graph** — rich semantic storage backed by an LLM extraction step; supports graph-completion queries and cross-session recall.
47+
- **Session cache** — fast, LLM-free writes scoped to a single session; can be promoted to the permanent graph later via `CogneeMemoryStore.improve()`.
48+
49+
More information:
50+
51+
- [Cognee website](https://www.cognee.ai)
52+
- [Cognee documentation](https://docs.cognee.ai)
53+
- [Cognee GitHub repository](https://github.com/topoteretes/cognee)
54+
55+
## Installation
56+
57+
Install the integration:
58+
59+
```bash
60+
pip install cognee-haystack
61+
```
62+
63+
Set your LLM API key (used by Cognee for knowledge graph extraction and queries):
64+
65+
```bash
66+
export LLM_API_KEY="your-llm-api-key"
67+
```
68+
69+
Optionally, set a separate embedding API key (defaults to `LLM_API_KEY` when unset):
70+
71+
```bash
72+
export EMBEDDING_API_KEY="your-embedding-api-key"
73+
```
74+
75+
Cognee reads LLM provider, database, and vector-store settings from environment variables. See the [Cognee documentation](https://docs.cognee.ai) for the full list of configuration options.
76+
77+
## Usage
78+
79+
### Available Classes
80+
81+
- [`CogneeRetriever`](https://docs.haystack.deepset.ai/docs/cogneeretriever): Retrieves memories from Cognee as system `ChatMessage` objects.
82+
- [`CogneeWriter`](https://docs.haystack.deepset.ai/docs/cogneewriter): Writes `ChatMessage` objects to Cognee.
83+
- [`CogneeMemoryStore`](https://docs.haystack.deepset.ai/reference/integrations-cognee#cogneememorystore): The underlying store that wraps Cognee's V2 memory API (`remember`, `recall`, `improve`, `forget`).
84+
85+
### Use in a Pipeline
86+
87+
Use `CogneeRetriever` and `CogneeWriter` for explicit pipeline control over when memories are stored and retrieved.
88+
89+
The example below seeds long-lived facts into the permanent knowledge graph, then runs an agent pipeline that retrieves relevant context before each response. `cognee.recall` auto-captures each turn as a session QA entry, so no `CogneeWriter` is needed inside the agent pipeline for session memory.
90+
91+
```python
92+
from haystack import Pipeline
93+
from haystack.components.agents import Agent
94+
from haystack.components.converters import OutputAdapter
95+
from haystack.components.generators.chat import OpenAIChatGenerator
96+
from haystack.dataclasses import ChatMessage
97+
98+
from haystack_integrations.components.retrievers.cognee import CogneeRetriever
99+
from haystack_integrations.components.writers.cognee import CogneeWriter
100+
from haystack_integrations.memory_stores.cognee import CogneeMemoryStore
101+
102+
# Seed long-lived facts into the permanent knowledge graph (no session_id).
103+
seed_store = CogneeMemoryStore(dataset_name="my_agent_memory", self_improvement=False)
104+
persistent_writer = CogneeWriter(memory_store=seed_store)
105+
persistent_writer.run(
106+
messages=[
107+
ChatMessage.from_user("Alice is a senior data scientist specialising in NLP."),
108+
ChatMessage.from_user("Alice's current project is a documentation search system built with Haystack."),
109+
ChatMessage.from_user("Alice prefers concise answers with Python code examples."),
110+
]
111+
)
112+
113+
# Build the agent pipeline with a session-scoped store.
114+
SESSION = "alice_session_1"
115+
chat_store = CogneeMemoryStore(dataset_name="my_agent_memory", session_id=SESSION)
116+
117+
pipeline = Pipeline()
118+
pipeline.add_component("retriever", CogneeRetriever(memory_store=chat_store))
119+
pipeline.add_component(
120+
"memory_context",
121+
OutputAdapter(
122+
template="{{ memories + user_messages }}",
123+
output_type=list[ChatMessage],
124+
unsafe=True,
125+
),
126+
)
127+
pipeline.add_component(
128+
"agent",
129+
Agent(
130+
chat_generator=OpenAIChatGenerator(model="gpt-4o-mini"),
131+
system_prompt=(
132+
"You are a helpful assistant with long-term memory. "
133+
"System messages at the start of the conversation contain relevant memories. "
134+
"Be concise; prefer short answers and Python code examples."
135+
),
136+
),
137+
)
138+
139+
pipeline.connect("retriever.messages", "memory_context.memories")
140+
pipeline.connect("memory_context.output", "agent.messages")
141+
142+
query = "What project is Alice currently working on?"
143+
result = pipeline.run(
144+
{
145+
"retriever": {"query": query},
146+
"memory_context": {"user_messages": [ChatMessage.from_user(query)]},
147+
}
148+
)
149+
print(result["agent"]["last_message"].text)
150+
151+
# After the session, promote the session cache into the permanent graph.
152+
chat_store.improve()
153+
```
154+
155+
For a full four-phase demo (persistent seeding → session seeding → agent chat → graph promotion), see the [example script](https://github.com/deepset-ai/haystack-core-integrations/blob/main/integrations/cognee/examples/demo_memory_agent.py).
156+
157+
## License
158+
159+
`cognee-haystack` is distributed under the terms of the [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) license.

0 commit comments

Comments
 (0)