Skip to content

API Reference parrot.knowledge.graphindex.factory

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.knowledge.graphindex.factory

View source

Factory for persistent, write-enabled graph memory toolkits.

Productizes the load → assemble → embed → publish loop so an agent (or a mixin) can open its durable knowledge graph in one call::

toolkit = await build_graph_memory_toolkit(
    db_dir=Path("~/.parrot/agents/my-agent/graph_memory"),
    agent_id="my-agent",
)

The returned GraphIndexToolkit has every write tool wired for write-through persistence (via :class:GraphPublisher), so knowledge the agent files survives process restarts — "the agent forgets, the graph does not".

GraphIndexToolkit lives in the sibling ai-parrot-tools distribution; it is imported lazily inside the factory (mirroring ToolInterface._capture_knowledge_toolkit) so ai-parrot core keeps no hard dependency on it.

Attribute DEFAULT_DIMENSION

Alias GraphAssembler

Re-exported from parrot.knowledge.graphindex.assemble.GraphAssembler.

Alias GraphPublisher

Re-exported from parrot.knowledge.graphindex.publish.GraphPublisher.

Class HashingGraphEmbedder

Deterministic, offline embedder compatible with GraphIndexToolkit.

Implements the subset of the GraphIndexEmbedder API the toolkit uses: model.encode, index (the shared FAISS index), an internal _node_id_map (FAISS position → node_id) and embed_nodes. Used as the zero-dependency fallback when no real embedding model is configured — lexical bag-of-tokens vectors are enough for find_node/search_hybrid over agent memory.

Parameter Type Default Description
dimension int DEFAULT_DIMENSION Embedding vector dimension.

Method HashingGraphEmbedder.embed_nodes

Embed (or re-embed) nodes, keeping FAISS positions stable.

New node ids are appended; an already-embedded node id has its vector refreshed in place. The FAISS index is rebuilt in the same object (reset + add) so shared references stay valid and positions remain aligned with node_id_list.

embed_nodes(nodes: list[UniversalNode], batch_size: int = 64) -> list[UniversalNode]

Parameter Type Default Description
nodes list[UniversalNode] Nodes to embed.
batch_size int 64 Unused (single batch); kept for API parity.

Returns: list[UniversalNode] — The same nodes with embedding_ref populated.

Method HashingGraphEmbedder.get_embedding

Return the stored vector for a node id, or None.

get_embedding(node_id: str) -> Optional[np.ndarray]

Parameter Type Default Description
node_id str

Returns: Optional[np.ndarray]

Method HashingGraphEmbedder.search_similar

FAISS L2 search over the embedded nodes.

search_similar(query: str, top_k: int = 10) -> list[tuple[str, float]]

Parameter Type Default Description
query str Natural-language query text.
top_k int 10 Maximum results.

Returns: list[tuple[str, float]] — (node_id, distance) pairs, closest first.

Alias MergedOntology

Re-exported from parrot.knowledge.ontology.schema.MergedOntology.

Alias TenantContext

Re-exported from parrot.knowledge.ontology.schema.TenantContext.

Alias UniversalNode

Re-exported from parrot.knowledge.graphindex.schema.UniversalNode.

Function build_graph_memory_toolkit

Open (or create) a persistent graph memory and return its toolkit.

Loads the durable graph plane, assembles the in-memory rustworkx graph, embeds the loaded nodes, and wires a :class:GraphPublisher so every toolkit write persists as an audited, revertible commit.

build_graph_memory_toolkit(db_dir: Optional[Path | str] = None, tenant_id: str = 'default', agent_id: str = 'agent', run_id: Optional[str] = None, embedder: Optional[Any] = None, client: Optional[Any] = None, dimension: int = DEFAULT_DIMENSION, backend: Optional[str] = None, dsn: Optional[str] = None, schema: str = 'graphindex') -> 'GraphIndexToolkit'

Parameter Type Default Description
db_dir Optional[Path | str] None Directory holding the per-tenant SQLite graph plane. Required (and only used) when backend="sqlite".
tenant_id str 'default' Tenant identifier (one <tenant_id>.db per tenant on the SQLite backend; ignored by the Postgres backend, whose shared schema is not per-tenant partitioned in v1).
agent_id str 'agent' Stable agent identity stamped onto writes.
run_id Optional[str] None Optional run/session id stamped onto writes (mutable on the returned toolkit — set per ask).
embedder Optional[Any] None Optional embedder implementing the GraphIndexEmbedder surface. Defaults to the deterministic offline :class:HashingGraphEmbedder.
client Optional[Any] None Optional AbstractClient for explain().
dimension int DEFAULT_DIMENSION Vector dimension for the default embedder.
backend Optional[str] None Registered backend name ("sqlite" when omitted and db_dir is given). Explicit "sqlite" (per-tenant SQLite file) or "postgres" (FEAT-520 — shared graphindex.* schema; unlocks the toolkit's temporal/hybrid tools, see parrot_tools.graphindex.toolkit).
dsn Optional[str] None Postgres DSN. Only used when backend="postgres"; defaults to pg_schema.GRAPHINDEX_PG_DSN when not given.
schema str 'graphindex' Postgres schema name. Only used when backend="postgres".

Returns: 'GraphIndexToolkit' — A write-enabled, persistence-backed GraphIndexToolkit.

Raises:

  • ValueError — For an unknown backend, a missing db_dir on the sqlite backend, or db_dir combined with an explicit non-sqlite backend.

Alias create_graph_persistence

Re-exported from parrot.knowledge.graphindex.backends.create_graph_persistence.

Attribute logger

Function make_stub_tenant_context

Build a minimal :class:TenantContext for local graph planes.

The ontology is a stub (same pattern as the flowtask component) — full hydration is only needed for ArangoDB/ontology deployments.

make_stub_tenant_context(tenant_id: str) -> TenantContext

Parameter Type Default Description
tenant_id str Tenant identifier for the graph plane.

Returns: TenantContext — A TenantContext suitable for SQLitePersistence.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally