Ensure Python 3.9+ is installed before initializing Cognee.
Copy
Ask AI
import cogneeimport osimport asyncio# Set your OpenAI API keyos.environ["LLM_API_KEY"] = "your-openai-api-key"# Cognee is ready to use!print("Cognee initialized successfully!")
import cogneeimport asyncioasync def main(): # Add some text data text = """ Artificial Intelligence is transforming healthcare through machine learning algorithms that analyze medical images and predict patient outcomes. Deep learning models excel at pattern recognition in radiology. """ result = await cognee.add(text) print("Data added:", result)asyncio.run(main())
Transform your raw data into a structured knowledge graph:
Copy
Ask AI
import cogneeimport asyncioasync def main(): # Process all added data result = await cognee.cognify() print("Knowledge graph created:", result)asyncio.run(main())
import cogneeimport asyncioasync def main(): # Add new information new_info = "AI also helps in drug discovery and personalized medicine." result = await cognee.add(new_info) print("New data added:", result) # Incremental processing (only new data) cognify_result = await cognee.cognify() print("Processing result:", cognify_result) print("Memory updated successfully!")asyncio.run(main())
Here’s a full working example that demonstrates the entire workflow:
Copy
Ask AI
import cogneeimport asyncioimport osfrom cognee import SearchTypeasync def main(): # 1. Setup (add your API key) os.environ["LLM_API_KEY"] = "your-openai-api-key" # 2. Add knowledge knowledge_base = """ Cognee is an AI memory layer that transforms unstructured data into knowledge graphs. It uses large language models to extract entities and relationships, creating searchable semantic networks. Key features include: - Automatic entity extraction - Relationship mapping - Multi-modal data support - Natural language querying - Incremental learning """ print("📚 Adding knowledge...") await cognee.add(knowledge_base) # 3. Process into knowledge graph print("🧠 Building knowledge graph...") await cognee.cognify() # 4. Query the knowledge print("🔍 Searching knowledge...") # Natural language question results = await cognee.search( query_text="What are the key features of Cognee?", query_type=SearchType.GRAPH_COMPLETION ) print("\n✨ Answer:") print(results[0]) # Get related concepts insights = await cognee.search( query_text="Cognee features", query_type=SearchType.INSIGHTS ) print("\n🔗 Related Concepts:") for insight in insights[:3]: print(f"- {insight}")if __name__ == "__main__": asyncio.run(main())
Ready to build something amazing? Check out our examples for more advanced tutorials, or join our Discord community to get help and share your projects!