localvectordb.core module
LocalVectorDB v1.0 Core Components
This module contains the foundational classes and data structures for the new document-first architecture.
- localvectordb.core.DocumentScoringMethod
Document scoring methods for aggregating chunk scores into a document score:
“best”: Highest chunk score (single best passage matters)
“average”: Mean of all chunk scores (overall quality)
“frequency_boost”: Boosts the best chunk score by the number of quality chunks (default, good for comprehensive docs) - frequency_bias (0.3): How much to boost based on chunk count
alias of
Literal[‘best’, ‘average’, ‘frequency_boost’]
- class localvectordb.core.MetadataFieldType(*values)
-
- TEXT = 'text'
- INTEGER = 'integer'
- REAL = 'real'
- BOOLEAN = 'boolean'
- DATE = 'date'
- JSON = 'json'
- class localvectordb.core.MetadataField(type: MetadataFieldType | str | Type, indexed: bool = False, required: bool = False, default_value: Any = None, embedding_enabled: bool = False, fts_enabled: bool = False)
Bases:
objectDefines a metadata field for documents.
- Parameters:
type (MetadataFieldType or str or Type) – The type of the metadata field.
indexed (bool, optional) – Whether the field is indexed in the database, by default False.
required (bool, optional) – Whether the field is required, by default False.
default_value (Any, optional) – Default value for the field if not provided, by default None.
embedding_enabled (bool, optional) – Whether this field should have its own embeddings for vector search. Only applicable to TEXT and JSON fields, by default False.
fts_enabled (bool, optional) – Whether this field should have full-text search enabled. Only applicable to TEXT fields, by default False.
- type: MetadataFieldType | str | Type
- class localvectordb.core.ChunkPosition(start: int, end: int, line: int, column: int, end_line: int, end_column: int)
Bases:
objectExact position tracking for a chunk in the original document.
- Parameters:
start (int) – Character start position in the original document.
end (int) – Character end position in the original document.
line (int) – Line number in the original document (1-based).
column (int) – Column number in the original document (1-based).
end_line (int) – Line number of end of chunk in original document (1-based)
end_column (int) – Column number of end of chunk in original document (1-based)
- to_dict() dict
Convert the ChunkPosition to a dictionary.
- Returns:
Dictionary representation with keys ‘start’, ‘end’, ‘line’, ‘column’.
- Return type:
Examples
>>> pos = ChunkPosition(start=0, end=10, line=1, column=1, end_line=1, end_column=10) >>> pos.to_dict() {'start': 0, 'end': 10, 'line': 1, 'column': 1, 'end_line': 1, 'end_column': 10}
- classmethod from_dict(data: dict) ChunkPosition
Create a ChunkPosition instance from a dictionary.
- Parameters:
data (dict) – Dictionary with keys ‘start’, ‘end’, ‘line’, ‘column’.
- Returns:
The constructed ChunkPosition object.
- Return type:
Examples
>>> data = {'start': 0, 'end': 10, 'line': 1, 'column': 1, 'end_line': 1, 'end_column': 10} >>> ChunkPosition.from_dict(data) ChunkPosition(start=0, end=10, line=1, column=1, end_line=1, end_column=10)
- class localvectordb.core.Chunk(content: str, position: ChunkPosition, tokens: int, index: int, faiss_id: int | None = None, content_hash: str | None = None)
Bases:
objectInternal representation of a document chunk.
Encapsulates the content, position metadata, token count, and optional FAISS index identifier for a text segment.
- Parameters:
content (str) – The text content of the chunk.
position (ChunkPosition) – The location of this chunk in the original document.
tokens (int) – Number of tokens in this chunk.
index (int) – Sequential index of the chunk within the document.
faiss_id (int, optional) – Identifier in the FAISS index, if applicable.
- position: ChunkPosition
- class localvectordb.core.SectionBoundary(index: int, heading: str | None, heading_level: int | None, start_pos: int, end_pos: int, start_line: int | None = None, end_line: int | None = None, metadata: Dict[str, Any] | None = None)
Bases:
objectBoundary information for a detected section in a document.
Used during ingestion to track where sections start and end in the original text.
- class localvectordb.core.Section(index: int, heading: str | None, heading_level: int | None, start_pos: int, end_pos: int, start_line: int | None = None, end_line: int | None = None, content_hash: str | None = None, metadata: Dict[str, Any] | None = None, faiss_id: int | None = None, chunks: List[Chunk] | None = None)
Bases:
objectA section within a document, grouping multiple chunks.
Sections are an overlay on top of existing chunking. They provide a mid-level abstraction between documents and chunks for hierarchical retrieval.
- classmethod from_boundary(boundary: SectionBoundary, content_hash: str, faiss_id: int | None = None, chunks: List[Chunk] | None = None) Section
Create a Section from a SectionBoundary.
- __init__(index: int, heading: str | None, heading_level: int | None, start_pos: int, end_pos: int, start_line: int | None = None, end_line: int | None = None, content_hash: str | None = None, metadata: Dict[str, Any] | None = None, faiss_id: int | None = None, chunks: List[Chunk] | None = None) None
- class localvectordb.core.Document(id: str, content: str, metadata: Dict[str, ~typing.Any]=<factory>, created_at: datetime | None = None, updated_at: datetime | None = None, content_hash: str | None = None, chunks: List[Chunk] | None = None, sections: List[Section] | None = None)
Bases:
objectA document in the vector database
- class localvectordb.core.QueryResult(id: str, score: float, type: ~typing.Literal['document', 'chunk', 'section', 'context', 'enriched', 'group', 'aggregation'], content: str, metadata: ~typing.Dict[str, ~typing.Any] = <factory>, document_id: str | None = None, position: ~localvectordb.core.ChunkPosition | None = None)
Bases:
objectResult from a search query
- position: ChunkPosition | None = None
- get_context(original: str, window: int = 100) str | None
Get context around chunk (only for chunk results)
- classmethod from_dict(data: dict) QueryResult | None
Create a QueryResult from a dictionary response
- class localvectordb.core.ChunkAlignment(chunk_index_1: int, chunk_index_2: int, similarity: float)
Bases:
objectAlignment between a chunk in one document and its best match in another.
- Parameters:
- class localvectordb.core.DocumentComparisonResult(doc_id_1: str, doc_id_2: str, overall_similarity: float, chunk_alignments: List[ChunkAlignment], matched_ratio_1: float, matched_ratio_2: float, unmatched_chunks_1: List[int], unmatched_chunks_2: List[int])
Bases:
objectRich comparison result between two documents.
- Parameters:
doc_id_1 (str) – ID of the first document.
doc_id_2 (str) – ID of the second document.
overall_similarity (float) – Centroid-level cosine similarity between documents.
chunk_alignments (List[ChunkAlignment]) – Best match per chunk in doc_1, sorted by similarity descending.
matched_ratio_1 (float) – Fraction of doc_1 chunks with a match >= threshold.
matched_ratio_2 (float) – Fraction of doc_2 chunks with a match >= threshold.
unmatched_chunks_1 (List[int]) – Chunk indices in doc_1 with no match >= threshold.
unmatched_chunks_2 (List[int]) – Chunk indices in doc_2 with no match >= threshold.
- chunk_alignments: List[ChunkAlignment]
- class localvectordb.core.DocumentSimilarityMatrix(matrix: ndarray, doc_ids: List[str], embeddings: ndarray)
Bases:
objectNxN similarity matrix for a set of documents.
- Parameters:
matrix (np.ndarray) – (N, N) array of pairwise similarity scores.
doc_ids (List[str]) – Ordered document IDs matching rows/columns.
embeddings (np.ndarray) – (N, D) document embeddings used to compute the matrix.
- class localvectordb.core.ChunkSimilarityMatrix(matrix: ndarray, doc_id_1: str, doc_id_2: str, chunk_indices_1: List[int], chunk_indices_2: List[int])
Bases:
objectChunk-level pairwise similarity matrix between (or within) documents.
For cross-document comparison,
doc_id_1anddoc_id_2differ. For self-comparison (chord diagrams), they are the same.- Parameters: