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
str | None
The URI of the Neo4j database. Required when
graph_driver is None.str | None
The username for authenticating with the Neo4j database.
str | None
The password for authenticating with the Neo4j database.
LLMClient | None
An instance of LLMClient for natural language processing tasks. If not provided, a default OpenAIClient will be initialized.
EmbedderClient | None
An instance of EmbedderClient for embedding tasks. If not provided, a default OpenAIEmbedder will be initialized.
CrossEncoderClient | None
An instance of CrossEncoderClient for reranking tasks. If not provided, a default OpenAIRerankerClient will be initialized.
bool
default:"True"
Whether to store the raw content of episodes.
GraphDriver | None
An instance of GraphDriver for database operations. If not provided, a default Neo4jDriver will be initialized.
int | None
The maximum number of concurrent operations allowed. Overrides SEMAPHORE_LIMIT set in the environment. If not set, the Graphiti default is used.
Tracer | None
An OpenTelemetry tracer instance for distributed tracing. If not provided, tracing is disabled (no-op).
str
default:"graphiti"
Prefix to prepend to all span names.
Example
Methods
add_episode
str
required
The name of the episode.
str
required
The content of the episode.
str
required
A description of the episode’s source.
datetime
required
The reference time for the episode.
EpisodeType
default:"EpisodeType.message"
The type of the episode. Options:
EpisodeType.message, EpisodeType.json, EpisodeType.text.str | None
An id for the graph partition the episode is a part of.
str | None
Optional uuid of the episode.
bool
default:"False"
Whether to update communities with new node information.
dict[str, type[BaseModel]] | None
Dictionary mapping entity type names to their Pydantic model definitions.
list[str] | None
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[str] | None
List of episode uuids to use as the previous episodes. If not provided, the most recent episodes by created_at date will be used.
dict[str, type[BaseModel]] | None
Dictionary mapping edge type names to their Pydantic model definitions.
dict[tuple[str, str], list[str]] | None
Mapping of (source_type, target_type) tuples to allowed edge type names.
str | None
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.
str | SagaNode | None
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.
str | None
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.
EpisodicNode
The created or updated episodic node.
list[EpisodicEdge]
List of episodic edges connecting entities to the episode.
list[EntityNode]
List of entity nodes extracted from the episode.
list[EntityEdge]
List of entity edges (relationships) extracted from the episode.
list[CommunityNode]
List of community nodes (only if update_communities=True).
list[CommunityEdge]
List of community edges (only if update_communities=True).
Example
add_episode_bulk
list[RawEpisode]
required
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.
str | None
An id for the graph partition the episode is a part of.
dict[str, type[BaseModel]] | None
Dictionary mapping entity type names to Pydantic models.
list[str] | None
List of entity type names to exclude from extraction.
dict[str, type[BaseModel]] | None
Dictionary mapping edge type names to Pydantic models.
dict[tuple[str, str], list[str]] | None
Mapping of (source_type, target_type) to allowed edge types.
str | None
Custom extraction instructions string to be included in the extract entities and extract edges prompts.
str | SagaNode | None
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[EpisodicNode]
List of created episodic nodes.
list[EpisodicEdge]
List of episodic edges.
list[EntityNode]
List of extracted entity nodes.
list[EntityEdge]
List of extracted entity edges.
list[CommunityNode]
List of community nodes (empty in bulk operations).
list[CommunityEdge]
List of community edges (empty in bulk operations).
Example
search
str
required
The search query string.
str | None
Facts will be reranked based on proximity to this node.
list[str] | None
The graph partitions to return data from.
int
default:"10"
The maximum number of results to return.
SearchFilters | None
Filters to apply to the search.
GraphDriver | None
The graph driver to use. If not provided, uses the default driver.
list[EntityEdge]
List of EntityEdge objects that are relevant to the search query.
Example
search_
str
required
The search query string.
SearchConfig
default:"COMBINED_HYBRID_SEARCH_CROSS_ENCODER"
Search configuration specifying search methods and rerankers. See search_config_recipes for preset configurations.
list[str] | None
The graph partitions to return data from.
str | None
Center node for node distance reranking.
list[str] | None
Origin nodes for breadth-first search.
SearchFilters | None
Filters to apply to the search.
GraphDriver | None
The graph driver to use.
list[EntityEdge]
List of relevant entity edges.
list[float]
Reranker scores for edges.
list[EntityNode]
List of relevant entity nodes.
list[float]
Reranker scores for nodes.
list[EpisodicNode]
List of relevant episodes.
list[float]
Reranker scores for episodes.
list[CommunityNode]
List of relevant communities.
list[float]
Reranker scores for communities.
Example
build_communities
list[str] | None
Create communities only for the listed group_ids. If blank, the entire graph will be used.
GraphDriver | None
The graph driver to use.
list[CommunityNode]
List of created community nodes.
list[CommunityEdge]
List of edges connecting entities to communities.
Example
retrieve_episodes
datetime
required
The reference time to retrieve episodes before.
int
default:"EPISODE_WINDOW_LEN"
The number of episodes to retrieve.
list[str] | None
The group ids to return data from.
EpisodeType | None
Filter episodes by source type.
GraphDriver | None
The graph driver to use.
str | None
If provided, only retrieve episodes that belong to the saga with this name.
list[EpisodicNode]
List of the most recent EpisodicNode objects.
build_indices_and_constraints
bool
default:"False"
Whether to clear existing indices before creating new ones.
Example
add_triplet
EntityNode
required
The source entity node.
EntityEdge
required
The edge connecting the nodes.
EntityNode
required
The target entity node.
list[EntityNode]
List of saved nodes (source and target).
list[EntityEdge]
List of saved edges.
remove_episode
str
required
The UUID of the episode to remove.
Example
get_nodes_and_edges_by_episode
list[str]
required
List of episode UUIDs to retrieve data for.
list[EntityNode]
List of entity nodes mentioned in the episodes.
list[EntityEdge]
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()