Skip to content

API Reference parrot.knowledge.ontology.concept_embedding

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

parrot.knowledge.ontology.concept_embedding

View source

Concept embedding pipeline for the ontology knowledge layer (FEAT-159).

Provides ConceptEmbeddingPipeline, an idempotent content-hash-based sync that writes Concept embeddings into a shared PgVector concepts table scoped by tenant_id metadata. Only changed or new concepts are re-embedded on each call, and removed concepts are deleted from the store.

The hash cache is persisted as JSON at::

{ontology_dir}/.concept_hashes/{tenant_id}.json

Writes to the cache file are atomic (tmpfile + rename) to prevent corruption from partial writes or process interruption.

Class ConceptEmbeddingPipeline

Idempotent, hash-based embedding sync for ontology Concept instances.

On each sync() call the pipeline:

  1. Computes a content hash (sha256) for every concept in the supplied list.
  2. Loads the on-disk hash cache for the tenant.
  3. Diffs new hashes against cached hashes to determine added / updated / removed / unchanged sets.
  4. Embeds only changed/new concepts via vector_store.add_documents().
  5. Deletes removed concepts via vector_store.delete_documents_by_filter().
  6. Atomically writes the updated hash cache to disk.

The concepts table is shared across tenants; isolation is enforced by the tenant_id metadata field stored alongside each embedding row.

Parameter Type Default Description
vector_store Any PgVectorStore instance used for embedding storage.
embedder Any Callable or embedding client; passed to the vector store's add_documents for embedding. If the vector store handles its own embedding, this can be the same object as vector_store.
ontology_dir Path | str Base directory for ontology files. The hash-cache subdirectory .concept_hashes/ is created here.
schema str 'ontology' PostgreSQL schema name for the concepts table.
table str 'concepts' PostgreSQL table name for concept embeddings.

Method ConceptEmbeddingPipeline.sync

Synchronise concept embeddings for a tenant with the vector store.

Idempotent: re-running with the same concepts produces no embedding calls and returns unchanged=len(concepts).

sync(tenant_id: str, concepts: list[Any]) -> ConceptSyncResult

Parameter Type Default Description
tenant_id str Tenant identifier used for metadata scoping and hash- cache namespacing.
concepts list[Any] List of concept objects or dicts. Each item must expose concept_id, label, synonyms, and description (either as attributes or dict keys).

Returns: ConceptSyncResult — ConceptSyncResult with counts of added / updated / removed / unchanged concepts and the elapsed wall-clock duration in ms.

Class ConceptSyncResult

Summary of a single ConceptEmbeddingPipeline.sync() run.

Parameter Type Default Description
added int
updated int
removed int
unchanged int
duration_ms int

Fields:

Field Type Default Description
added int Number of new concepts embedded for the first time.
updated int Number of existing concepts that were re-embedded (content changed).
removed int Number of concepts deleted from the vector store.
unchanged int Number of concepts whose content hash matched the cache (no embedding call made).
duration_ms int Wall-clock time for the sync operation in milliseconds.

Alias Document

Re-exported from parrot.stores.models.Document.

Attribute logger

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