This tutorial demonstrates how to transform raw text into a structured knowledge graph using Cognee. You’ll learn how to:
Set up your Cognee development environment
Load data from various sources using the dlt integration
Process content through Cognee’s pipeline to extract entities and relationships
Visualize the generated knowledge graph in an interactive format
Search your knowledge graph with natural language queries
By the end of this tutorial, you’ll have transformed unstructured text into a rich, interconnected knowledge graph that enhances LLM reasoning capabilities.
Cognee helps you move from large amounts of unstructured text to structured, interconnected data. Here’s what this transformation looks like:Before - Raw Text:After - Structured Knowledge Graph:
First, clone both the main Cognee repository and the starter examples:
Copy
Ask AI
# Clone the main Cognee repositorygit clone https://github.com/topoteretes/cognee.git# Clone the getting started examplesgit clone https://github.com/topoteretes/cognee-starter.git
These repositories contain all the necessary code and examples for this tutorial.
Create a new Python file for your data loading example:
Copy
Ask AI
touch load_data_example.py
Copy the following pipeline code into your file:
Copy
Ask AI
import asyncioimport webbrowserimport osfrom cognee.api.v1.add import addfrom cognee.api.v1.cognify import cognifyfrom cognee.api.v1.search import search, SearchTypefrom cognee.api.v1.visualize.visualize import visualize_graphasync def main(): # Sample data to process sample_text = """ Artificial Intelligence (AI) is revolutionizing healthcare through machine learning algorithms that can analyze medical images, predict patient outcomes, and assist in drug discovery. Deep learning models are particularly effective at pattern recognition in radiology, helping doctors detect early signs of cancer and other diseases. Natural Language Processing (NLP), a subset of AI, enables computers to understand and process human language. This technology powers chatbots, translation services, and sentiment analysis tools used across various industries. Computer Vision, another AI domain, allows machines to interpret visual information from the world around them. Applications include autonomous vehicles, facial recognition systems, and quality control in manufacturing. """ print("🔄 Adding data to Cognee...") await add(sample_text) print("🧠 Processing data through Cognee pipeline...") await cognify() print("🔍 Searching the knowledge graph...") results = await search( query_text="How is AI being used in healthcare?", query_type=SearchType.GRAPH_COMPLETION ) print("📊 Search Results:") for result in results: print(f"- {result}") print("📈 Generating visualization...") await visualize_graph() # Open the generated visualization home_dir = os.path.expanduser("~") html_file = os.path.join(home_dir, "graph_visualization.html") print(f"🌐 Opening visualization at: {html_file}") webbrowser.open(f"file://{html_file}") print("✅ Tutorial completed successfully!")if __name__ == '__main__': asyncio.run(main())
This script demonstrates the complete Cognee workflow: adding data, processing it, searching the knowledge graph, and generating a visualization.
This command will process your text through Cognee’s pipeline, extracting entities like “Artificial Intelligence,” “machine learning,” and “healthcare,” then building relationships between them.You should see output similar to:
Copy
Ask AI
🔄 Adding data to Cognee...🧠 Processing data through Cognee pipeline...🔍 Searching the knowledge graph...📊 Search Results:- AI is revolutionizing healthcare through machine learning algorithms...📈 Generating visualization...🌐 Opening visualization at: /Users/yourname/graph-visualization.html✅ Tutorial completed successfully!
Cognee supports loading data from 30+ sources through dlt integration. When you are adding a new set of data to your existing cognee memory after cognification or starting fresh, only thing you need to do is run cognee.add and cognee.cognify.Here are some examples for different inputs:
Have questions about loading your data or want to share your knowledge graph visualizations? Join our community to connect with other developers and get support!