Skip to content

API Reference parrot.stores.multimodal_schema

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

parrot.stores.multimodal_schema

View source

Multimodal Collection Schema for PgVector.

Defines a parallel multimodal collection table that stores both text and image embeddings in the same shared vector space, enabling cross-modal retrieval (text<->image, image<->image) via PgVector.

The table is separate from the existing text-only RAG collections. Existing PgVectorStore internals are NOT modified.

Column type selection based on QuantizationMode:

  • F32 -> VECTOR(N) (standard full-precision vector)
  • F16 -> HALFVEC(N) (16-bit half-precision, pgvector >= 0.3.0)
  • I8 -> HALFVEC(N) (stored as half-precision with client-side i8 semantics)
  • B1 -> BIT(N) (binary vector, N bits)

Note: pgvector 0.4.x supports VECTOR, HALFVEC, and BIT column types. Bit (data class) is NOT the same as BIT (SQLAlchemy column type).

Alias PGVECTOR_TYPE_MAP

Re-exported from parrot.embeddings.multimodal.quantization.PGVECTOR_TYPE_MAP.

Alias QuantizationMode

Re-exported from parrot.embeddings.multimodal.quantization.QuantizationMode.

Function create_multimodal_table

Create the multimodal collection table and HNSW index in PostgreSQL.

Uses CREATE TABLE IF NOT EXISTS semantics (safe to call repeatedly).

HNSW index ops class selection:

  • F32 → vector_cosine_ops (cosine similarity)
  • F16 / I8 → halfvec_cosine_ops (cosine similarity for half-precision)
  • B1 → bit_hamming_ops (Hamming distance for binary vectors)

create_multimodal_table(engine: Any, table_name: str, schema: str, dimension: int, quantization: QuantizationMode = QuantizationMode.F32, create_hnsw_index: bool = True) -> Any

Parameter Type Default Description
engine Any An async SQLAlchemy engine (AsyncEngine).
table_name str PostgreSQL table name.
schema str PostgreSQL schema name.
dimension int Embedding dimension.
quantization QuantizationMode QuantizationMode.F32 Quantization mode (determines column type and index ops).
create_hnsw_index bool True Whether to create the HNSW index. Default: True.

Returns: Any — The ORM class for the created table.

Function define_multimodal_collection

Define (and cache) a SQLAlchemy ORM class for a multimodal collection.

The returned class has the following columns:

  • id: Serial integer primary key.
  • embedding: Vector column (type depends on quantization).
  • modality: String column, either "text" or "image".
  • source_id: Optional reference to the source document or image.
  • doc_id: Optional document identifier.
  • text_content: Optional text content for the embedded item.
  • payload: JSONB column for arbitrary metadata.

The returned class is cached by (table_name, schema, dimension, quantization) so repeated calls with the same arguments return the same ORM class.

Thread-safe: uses a module-level lock to prevent race conditions when multiple coroutines first encounter the same cache key concurrently.

define_multimodal_collection(table_name: str, schema: str, dimension: int, quantization: QuantizationMode = QuantizationMode.F32) -> Any

Parameter Type Default Description
table_name str PostgreSQL table name.
schema str PostgreSQL schema name.
dimension int Embedding dimension (number of floats for F32/F16/I8; original element count for B1).
quantization QuantizationMode QuantizationMode.F32 Quantization mode (determines column type).

Returns: Any — An unbound SQLAlchemy DeclarativeBase-derived ORM class.

Function search_multimodal

Search the multimodal collection for nearest neighbors.

Uses cosine distance (<=> operator) for F32/F16/I8, and Hamming distance (<~> operator) for B1.

search_multimodal(engine: Any, table_name: str, schema: str, dimension: int, query_embedding: np.ndarray, quantization: QuantizationMode = QuantizationMode.F32, modality_filter: Optional[str] = None, top_k: int = 10) -> list[dict]

Parameter Type Default Description
engine Any An async SQLAlchemy engine.
table_name str PostgreSQL table name.
schema str PostgreSQL schema name.
dimension int Embedding dimension.
query_embedding np.ndarray Query vector as a numpy array of shape (D,) or (1, D).
quantization QuantizationMode QuantizationMode.F32 Quantization mode of the stored embeddings.
modality_filter Optional[str] None If provided, filter results to this modality ("text" or "image"). None returns all modalities. Passed as a bound parameter to prevent SQL injection.
top_k int 10 Number of nearest neighbors to retrieve.

Returns: list[dict] — List of dicts with keys: id, modality, source_id, doc_id, text_content, payload, distance.

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