Skip to content

Graph store API reference

Knowledge-graph stores persist extracted entities and relations and retrieve a neighborhood around a query. They live in llamabot.components.graphstore and are an optional extra (pip install "llamabot[graph]").

Models

from llamabot.components.graphstore import Entity, Relation, ExtractedGraph
  • Entity(name, label, properties={}) — a node. Store identity is label:name.
  • Relation(source, relation, target, properties={}) — a directed edge. source and target are entity names.
  • ExtractedGraph(entities, relations) — payload for append / extend.
  • RetrievedSubgraph(entities, relations) — retrieve result, with .text for LLM context.

AbstractGraphStore

class AbstractGraphStore:
    def append(self, graph: ExtractedGraph) -> None: ...
    def extend(self, graphs: list[ExtractedGraph]) -> None: ...
    def retrieve(
        self, query: str, n_results: int = 10, hops: int = 2
    ) -> RetrievedSubgraph: ...
    def reset(self) -> None: ...
    def query(
        self, cypher: str, params: dict | None = None
    ) -> list[dict]: ...

n_results is the number of seed entities. hops expands an undirected neighborhood around those seeds.

LadybugGraphStore

Embedded LadybugDB (MIT). Local file under ~/.llamabot/ladybug/graphs.lbdb by default. No server.

from pathlib import Path
from llamabot.components.graphstore import LadybugGraphStore

store = LadybugGraphStore(
    graph_name="org-chart",
    storage_path=Path.home() / ".llamabot" / "ladybug",
    embedding_model="minishlab/potion-base-8M",
)

Construction lazy-imports ladybug. If it is missing, you get an ImportError that points at pip install llamabot[graph].

Each store uses CREATE GRAPH {name} ANY plus a typed Entity node table and Relation rel table so extracted labels need no extra schema while vector search still has a FLOAT[n] embedding column.

NetworkXGraphStore

In-memory networkx.MultiDiGraph. Same retrieve semantics. query raises NotImplementedError.

from llamabot.components.graphstore import NetworkXGraphStore

store = NetworkXGraphStore()

Extractor

from llamabot.components.graphstore import extract_graph, KnowledgeGraphBot

graph = extract_graph(text, model_name="gpt-4o-mini")
# or
bot = KnowledgeGraphBot(model_name="gpt-4o-mini")
graph = bot(text)

Both use StructuredBot with pydantic_model=ExtractedGraph. Pass bot= to extract_graph in tests to skip the LLM.

The older llamabot.bot.kgbot.KGBot still returns JSON triplets and is separate from this store.

See also