Cognee API Reference
Overview
The Cognee API provides a comprehensive set of functions and endpoints for building, managing, and querying knowledge graphs. This reference outlines both the Python API functions and the HTTP REST API endpoints.
Core Python API Functions
Data Management
# Add content to Cognee
await cognee.add(content)
Parameter | Type | Description |
---|---|---|
content | str | Text content, file path (using file:// prefix), or URL to add to the knowledge graph |
# Process content and build the knowledge graph
await cognee.cognify()
# Reset data and system state
await cognee.prune.prune_data() # Reset just the data
await cognee.prune.prune_system(metadata=True) # Reset system state
await cognee.prune.prune_all() # Reset everything
Search and Query
# Search the knowledge graph
from cognee.modules.search.types import SearchType
results = await cognee.search(
query_text="Your search query",
query_type=SearchType.INSIGHTS, # or INFORMATION, RAG, etc.
)
Parameter | Type | Description |
---|---|---|
query_text | str | The search query text |
query_type | SearchType | Type of search to perform (see Search Types Reference for all options) |
Note: For a comprehensive reference of all available search types, see the Search Types Reference.
Graph Visualization
import cognee.api.v1 as api
# Generate an HTML visualization of your graph
visualization_path = await api.render_graph()
The visualization is saved as an HTML file in the .cognee_system
folder.
Configuration
# Configure Cognee
import cognee
# LLM configuration
cognee.config.llm_api_key = "YOUR_API_KEY"
cognee.config.llm_model = "gpt-4" # Default model
# Database configuration
cognee.config.db_provider = "sqlite" # or "postgres"
cognee.config.db_connection_string = "postgresql://user:password@localhost:5432/cognee"
HTTP REST API
The Cognee HTTP API provides RESTful endpoints for interacting with your knowledge graph from any application.
Interactive API Documentation
Below is the interactive API documentation generated from our OpenAPI specification:
Key Endpoints
- POST /api/v1/add: Add content to the knowledge graph
- POST /api/v1/cognify: Process content and build the knowledge graph
- POST /api/v1/search: Search the knowledge graph
- GET /api/v1/render_graph: Generate a visualization of the knowledge graph
- POST /api/v1/prune: Reset data and system state
Using the API in Your Applications
Python Example
import cognee
import asyncio
async def main():
# Configure
cognee.config.llm_api_key = "YOUR_API_KEY"
# Add content
await cognee.add("Your content here")
# Process
await cognee.cognify()
# Search
from cognee.modules.search.types import SearchType
results = await cognee.search("Your query", query_type=SearchType.INSIGHTS)
# Visualize
import cognee.api.v1 as api
visualization_path = await api.render_graph()
if __name__ == "__main__":
asyncio.run(main())
HTTP API Example
import requests
# Add content
requests.post(
"http://localhost:8000/api/v1/add",
json={"content": "Your content here"}
)
# Process
requests.post("http://localhost:8000/api/v1/cognify")
# Search
response = requests.post(
"http://localhost:8000/api/v1/search",
json={
"query_text": "Your query",
"query_type": "INSIGHTS",
}
)
results = response.json()
Understanding Graph Visualization
The graph visualization shows:
- Nodes: Entities extracted from your content (people, places, concepts, etc.)
- Edges: Relationships between entities
- Categories: Nodes are colored by category (e.g., person, location, organization)
The visualization helps you understand how different pieces of information are connected in your knowledge graph.
Next Steps
- Check out the SDK Reference for more detailed information
- Explore the Search Types Reference for all available search options
- Learn about Pipelines for advanced usage
- Explore Cognee Tasks for specific use cases