Thanks to visit codestin.com
Credit goes to docs.cognee.ai

Run your first Cognee example to see AI memory in action.

Basic Usage

This minimal example shows how to add content, process it, and perform a search:
import cognee
import asyncio

async def main():

    # Create a clean slate for cognee -- reset data and system state
    await cognee.prune.prune_data()
    await cognee.prune.prune_system(metadata=True)
    
    # Add sample content
    text = "Cognee turns documents into AI memory."
    await cognee.add(text)
    
    # Process with LLMs to build the knowledge graph
    await cognee.cognify()
    
    # Search the knowledge graph
    results = await cognee.search(
        query_text="What does Cognee do?"
    )
    
    # Print
    for result in results:
        print(result)

if __name__ == '__main__':
    asyncio.run(main())

What just happened

The code demonstrates Cognee’s three core operations:
  • .add — Adds data to Cognee so they can be cognified. In this case, we added a single string (“Cognee turns documents into AI memory”); from Cognee’s perspective, this string is a document.
  • .cognify — This is where the cognification happens. All documents are chunked, entities are extracted, relationships are made, and summaries are generated. In this case, we can expect entities like Frodo, One Ring, and Mordor.
  • .search — Queries the knowledge graph using vector similarity and graph traversal to find relevant information and return contextual results.

About async / await in Cognee

Cognee uses asynchronous code extensively. That means many of its functions are defined with async and must be called with await. This lets Python handle waiting (e.g. for I/O or network calls) without blocking the rest of your program.
This example uses async / await, Python’s way of doing asynchronous programming. Asynchronous programming is used when functions may block because they are waiting for something (for example, a reply from an API call). By writing async def, you define a function that can pause at certain points. The await keyword marks those calls that may need to pause. To run such functions, Python provides the asyncio library. It uses a loop, called the event loop, which executes your code in order but, whenever a function is waiting, can temporarily run another one. From inside your function, though, everything still runs top-to-bottom: each line after an await only executes once the awaited call has finished.
  • A good starting point is this guide.
  • Official documentation is available here.

Next Steps

Cognee core concepts

Learn about Cognee’s core concepts, architecture, building blocks, and main operations.