Overview
TheGraphiti class is the main entry point for building and managing real-time, temporally-aware knowledge graphs. It provides methods for adding episodes, searching, building communities, and managing the graph structure.
Constructor
The URI of the Neo4j database. Required when
graph_driver is None.The username for authenticating with the Neo4j database.
The password for authenticating with the Neo4j database.
An instance of LLMClient for natural language processing tasks. If not provided, a default OpenAIClient will be initialized.
An instance of EmbedderClient for embedding tasks. If not provided, a default OpenAIEmbedder will be initialized.
An instance of CrossEncoderClient for reranking tasks. If not provided, a default OpenAIRerankerClient will be initialized.
Whether to store the raw content of episodes.
An instance of GraphDriver for database operations. If not provided, a default Neo4jDriver will be initialized.
The maximum number of concurrent operations allowed. Overrides SEMAPHORE_LIMIT set in the environment. If not set, the Graphiti default is used.
An OpenTelemetry tracer instance for distributed tracing. If not provided, tracing is disabled (no-op).
Prefix to prepend to all span names.
Example
Methods
add_episode
The name of the episode.
The content of the episode.
A description of the episode’s source.
The reference time for the episode.
The type of the episode. Options:
EpisodeType.message, EpisodeType.json, EpisodeType.text.An id for the graph partition the episode is a part of.
Optional uuid of the episode.
Whether to update communities with new node information.
Dictionary mapping entity type names to their Pydantic model definitions.
List of entity type names to exclude from the graph. Entities classified into these types will not be added to the graph. Can include ‘Entity’ to exclude the default entity type.
List of episode uuids to use as the previous episodes. If not provided, the most recent episodes by created_at date will be used.
Dictionary mapping edge type names to their Pydantic model definitions.
Mapping of (source_type, target_type) tuples to allowed edge type names.
Custom extraction instructions string to be included in the extract entities and extract edges prompts. This allows for additional instructions or context to guide the extraction process.
Either a saga name (str) or a SagaNode object to associate this episode with. If a string is provided and a saga with this name already exists in the group, the episode will be added to it. Otherwise, a new saga will be created. Sagas are connected to episodes via HAS_EPISODE edges, and consecutive episodes are linked via NEXT_EPISODE edges.
UUID of the previous episode in the saga. If provided, skips the database query to find the most recent episode. Useful for efficiently adding multiple episodes to the same saga in sequence. The returned AddEpisodeResults.episode.uuid can be passed as this parameter for the next episode.
The created or updated episodic node.
List of episodic edges connecting entities to the episode.
List of entity nodes extracted from the episode.
List of entity edges (relationships) extracted from the episode.
List of community nodes (only if update_communities=True).
List of community edges (only if update_communities=True).
Example
add_episode_bulk
A list of RawEpisode objects to be processed and added to the graph. Each RawEpisode contains: name, content, source_description, source, reference_time, and optional uuid.
An id for the graph partition the episode is a part of.
Dictionary mapping entity type names to Pydantic models.
List of entity type names to exclude from extraction.
Dictionary mapping edge type names to Pydantic models.
Mapping of (source_type, target_type) to allowed edge types.
Custom extraction instructions string to be included in the extract entities and extract edges prompts.
Either a saga name (str) or a SagaNode object to associate all episodes with. If a string is provided and a saga with this name already exists in the group, the episodes will be added to it. Otherwise, a new saga will be created.
List of created episodic nodes.
List of episodic edges.
List of extracted entity nodes.
List of extracted entity edges.
List of community nodes (empty in bulk operations).
List of community edges (empty in bulk operations).
Example
search
The search query string.
Facts will be reranked based on proximity to this node.
The graph partitions to return data from.
The maximum number of results to return.
Filters to apply to the search.
The graph driver to use. If not provided, uses the default driver.
List of EntityEdge objects that are relevant to the search query.
Example
search_
The search query string.
Search configuration specifying search methods and rerankers. See search_config_recipes for preset configurations.
The graph partitions to return data from.
Center node for node distance reranking.
Origin nodes for breadth-first search.
Filters to apply to the search.
The graph driver to use.
List of relevant entity edges.
Reranker scores for edges.
List of relevant entity nodes.
Reranker scores for nodes.
List of relevant episodes.
Reranker scores for episodes.
List of relevant communities.
Reranker scores for communities.
Example
build_communities
Create communities only for the listed group_ids. If blank, the entire graph will be used.
The graph driver to use.
List of created community nodes.
List of edges connecting entities to communities.
Example
retrieve_episodes
The reference time to retrieve episodes before.
The number of episodes to retrieve.
The group ids to return data from.
Filter episodes by source type.
The graph driver to use.
If provided, only retrieve episodes that belong to the saga with this name.
List of the most recent EpisodicNode objects.
build_indices_and_constraints
Whether to clear existing indices before creating new ones.
Example
add_triplet
The source entity node.
The edge connecting the nodes.
The target entity node.
List of saved nodes (source and target).
List of saved edges.
remove_episode
The UUID of the episode to remove.
Example
get_nodes_and_edges_by_episode
List of episode UUIDs to retrieve data for.
List of entity nodes mentioned in the episodes.
List of entity edges from the episodes.
close
Example
Properties
token_tracker
- Get token usage by prompt type:
tracker.get_usage() - Get total token usage:
tracker.get_total_usage() - Print a formatted summary:
tracker.print_summary() - Reset tracking:
tracker.reset()