# Cognee

  }
    title="Cognee integration"
    href="https://github.com/topoteretes/cognee-community/tree/main/packages/graph/memgraph"
    is_external={true}
  />

Cognee is a framework for AI memory and knowledge graphs. It uses an LLM to
extract concepts and relationships from text, stores them as a knowledge
graph and searches them. The Memgraph adapter, from the Cognee community
packages, stores the graph in Memgraph. You can also follow the [Jupyter
notebook example](https://github.com/memgraph/ai-demos/blob/main/integrations/cognee/cognee.ipynb).

## Installation

Start Memgraph, for example in Docker:

```bash
docker run -p 7687:7687 memgraph/memgraph-mage:latest
```

Install Cognee with the Memgraph adapter:

```bash
pip install cognee cognee-community-graph-adapter-memgraph
```

Cognee calls an LLM to extract the graph. Set your LLM API key, for example
`LLM_API_KEY` for OpenAI, as described in the [Cognee
docs](https://docs.cognee.ai/).

## Use Cognee with Memgraph

Register the adapter, point Cognee at Memgraph, then add text, build the graph
and search it:

```python
import asyncio
import cognee
from cognee_community_graph_adapter_memgraph import register


async def main():
    # Register the Memgraph adapter and use it as the graph database
    register()
    cognee.config.set_graph_database_provider("memgraph")
    cognee.config.set_graph_db_config(
        {
            "graph_database_url": "bolt://localhost:7687",
            "graph_database_username": "",
            "graph_database_password": "",
        }
    )

    text = "Natural language processing (NLP) is a subfield of computer science focused on the interaction between computers and human language."

    # Store the text
    await cognee.add(text)

    # Extract a knowledge graph with the LLM and store it in Memgraph
    await cognee.cognify()

    # Search the graph
    results = await cognee.search(
        query_type=cognee.SearchType.GRAPH_COMPLETION, query_text="What is NLP?"
    )
    for result in results:
        print(result)


asyncio.run(main())
```

## How it works

- `cognee.add()` stores the input text.
- `cognee.cognify()` extracts concepts and relationships with the LLM and
  stores them in Memgraph as a graph.
- `cognee.search()` answers the question from the graph, with
  `GRAPH_COMPLETION` combining graph context and the LLM.

For more examples, see the [adapter
README](https://github.com/topoteretes/cognee-community/tree/main/packages/graph/memgraph).
