Skip to content

Phase 5: Define ChunkVector LanceDB model #7

Description

@tinkermonkey

Implement the ChunkVector Pydantic model in storage/vector_store.py using LanceDB's LanceModel integration, establishing the vector table schema as a data contract.

Requirements

  • Replace the placeholder docstring in src/context_library/storage/vector_store.py with working code
  • Define module-level constants VECTOR_DIR and EMBEDDING_DIM
  • Define a ChunkVector class subclassing LanceModel with all fields from the persistence design
  • Required fields: chunk_hash (str), content (str), vector (Vector), domain (str), source_id (str), source_version (int), created_at (str)
  • VECTOR_DIR must resolve to ~/.context-library/vectors

Design Guidance

  • Full implementation for storage/vector_store.py:
"""LanceDB-backed vector index; derived and fully rebuildable from the document store."""

from lancedb.pydantic import LanceModel, Vector

VECTOR_DIR = "~/.context-library/vectors"
EMBEDDING_DIM = 384  # all-MiniLM-L6-v2; change here when swapping embedding model


class ChunkVector(LanceModel):
    """Schema for the LanceDB chunk_vectors table.

    chunk_hash is the join key to the SQLite chunks table.
    LanceDB is derived and disposable; it can be fully rebuilt from SQLite.
    """

    chunk_hash: str       # join key to SQLite chunks table
    content: str          # denormalized for reranker access without SQLite lookup
    vector: Vector(EMBEDDING_DIM)
    domain: str           # supports filtered vector search by domain
    source_id: str        # supports filtered vector search by source
    source_version: int   # supports filtered vector search by version
    created_at: str       # ISO 8601 timestamp
  • Vector(EMBEDDING_DIM) is the callable from lancedb.pydantic that defines the vector column with a fixed dimensionality at the schema level. It is not the same as a Python type annotation — it must be called with the integer dimension.
  • EMBEDDING_DIM = 384 corresponds to all-MiniLM-L6-v2, the default embedding model provided by sentence-transformers. If the embedding model is changed later, only this constant needs updating.
  • content is denormalized here (duplicated from SQLite) to allow the reranker to score results without a second database round-trip.
  • created_at uses ISO 8601 string format (e.g., "2024-01-15T10:30:00Z") for compatibility with LanceDB's string-based filtering.
  • This is the only placeholder module that contains actual executable code in the scaffold; all other placeholder modules remain docstring-only.

Acceptance Criteria

  • src/context_library/storage/vector_store.py defines VECTOR_DIR = "~/.context-library/vectors"
  • EMBEDDING_DIM = 384 is defined as a module-level constant
  • ChunkVector is importable: from context_library.storage.vector_store import ChunkVector
  • issubclass(ChunkVector, LanceModel) is True
  • ChunkVector model fields are: chunk_hash (str), content (str), vector (Vector(384)), domain (str), source_id (str), source_version (int), created_at (str)
  • No other module in src/context_library/ contains executable code beyond this file's contents

Dependencies

Phase 2, Phase 3

Parent Issue

Part of #1

Discussion

This work is detailed in discussion 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions