Implementation:Run llama Llama index SimpleGraphStore
Overview
SimpleGraphStore is an in-memory graph store implementation that stores knowledge graph triplets (subject, relation, object) in a Python dictionary. It implements the GraphStore interface and provides basic CRUD operations along with persistence to JSON files via fsspec.
Source file: llama-index-core/llama_index/core/graph_stores/simple.py (186 lines)
Class Hierarchy
GraphStore └── SimpleGraphStore
The module also defines SimpleGraphStoreData, a dataclass that serves as the internal data container.
SimpleGraphStoreData
@dataclass class SimpleGraphStoreData(DataClassJsonMixin):
An internal data container that uses DataClassJsonMixin for JSON serialization support.
Data Structure
| Field | Type | Description |
|---|---|---|
graph_dict |
Dict[str, List[List[str]]] |
Maps subject strings to lists of [relation, object] pairs
|
Each entry in graph_dict maps a subject to a list of two-element lists, where each inner list contains a relation string and an object string.
get_rel_map
def get_rel_map(
self, subjs: Optional[List[str]] = None, depth: int = 2, limit: int = 30
) -> Dict[str, List[List[str]]]:
Retrieves the relationship map for given subjects with depth-aware traversal:
- If
subjsisNone, uses all subjects in the graph. - Calls the private
_get_rel_maprecursively for each subject. - Truncates results to the specified
limitbased on iteration order.
_get_rel_map (private)
def _get_rel_map(
self, subj: str, depth: int = 2, limit: int = 30
) -> List[List[str]]:
Performs recursive depth-first traversal from a single subject:
- Base case: returns empty list when
depthreaches 0. - For each relation-object pair of the subject, appends
[subj, rel, obj]triplets. - Recursively follows each object as a new subject with
depth - 1. - Respects the
limiton the number of relations per subject.
SimpleGraphStore Class
Constructor
def __init__(
self,
data: Optional[SimpleGraphStoreData] = None,
fs: Optional[fsspec.AbstractFileSystem] = None,
**kwargs: Any,
) -> None:
Initializes the store with optional pre-existing data and filesystem. Defaults to an empty SimpleGraphStoreData and a local filesystem.
Properties
| Property | Return Type | Description |
|---|---|---|
client |
None |
Returns None; not applicable for in-memory stores
|
CRUD Methods
get
def get(self, subj: str) -> List[List[str]]:
Returns all [relation, object] pairs for a given subject. Returns an empty list if the subject does not exist.
get_rel_map
def get_rel_map(
self, subjs: Optional[List[str]] = None, depth: int = 2, limit: int = 30
) -> Dict[str, List[List[str]]]:
Delegates to SimpleGraphStoreData.get_rel_map for depth-aware relationship map retrieval.
upsert_triplet
def upsert_triplet(self, subj: str, rel: str, obj: str) -> None:
Adds a new triplet to the graph. If the subject does not exist, creates a new entry. Checks for duplicates by converting existing pairs to tuples and comparing; only appends if the (rel, obj) pair is not already present.
delete
def delete(self, subj: str, rel: str, obj: str) -> None:
Removes a specific triplet. If the subject's relation list becomes empty after deletion, removes the subject key entirely from the dictionary.
Persistence Methods
persist
def persist(
self,
persist_path: str = os.path.join(DEFAULT_PERSIST_DIR, DEFAULT_PERSIST_FNAME),
fs: Optional[fsspec.AbstractFileSystem] = None,
) -> None:
Serializes the graph data to JSON at the specified path. Creates the directory if it does not exist. Uses fsspec for filesystem abstraction.
from_persist_dir (classmethod)
@classmethod
def from_persist_dir(
cls,
persist_dir: str = DEFAULT_PERSIST_DIR,
fs: Optional[fsspec.AbstractFileSystem] = None,
) -> "SimpleGraphStore":
Loads a SimpleGraphStore from a directory by constructing the persist path from DEFAULT_PERSIST_FNAME and delegating to from_persist_path.
from_persist_path (classmethod)
@classmethod
def from_persist_path(
cls, persist_path: str, fs: Optional[fsspec.AbstractFileSystem] = None
) -> "SimpleGraphStore":
Loads from a specific file path. If the file does not exist, logs a warning and returns a new empty store. Otherwise, reads the JSON file and deserializes it via SimpleGraphStoreData.from_dict.
Serialization
from_dict / to_dict
@classmethod def from_dict(cls, save_dict: dict) -> "SimpleGraphStore": def to_dict(self) -> dict:
Dictionary-based serialization using the underlying SimpleGraphStoreData class's DataClassJsonMixin methods.
Unsupported Methods
| Method | Behavior |
|---|---|
get_schema |
Raises NotImplementedError
|
query |
Raises NotImplementedError
|
These methods are part of the GraphStore interface but are not applicable to the simple in-memory implementation.
Dependencies
fsspec-- filesystem abstraction for persistencedataclasses_json.DataClassJsonMixin-- JSON serialization for the data containerllama_index.core.graph_stores.types-- providesGraphStore,DEFAULT_PERSIST_DIR,DEFAULT_PERSIST_FNAME