Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:1Panel dev MaxKB PGVector

From Leeroopedia
Knowledge Sources
Domains RAG, Vector_Database
Last Updated 2026-10-10 00:00 GMT

Overview

Concrete vector store implementation using PostgreSQL pgvector for embedding storage and similarity search, provided by MaxKB.

Description

The PGVector class extends BaseVectorStore and provides the persistence and retrieval layer for the RAG pipeline. It stores paragraph embeddings as pgvector columns alongside full-text search vectors (`SearchVector`), enabling three search modes: embedding similarity, keyword search, and blended search. The class manages the full lifecycle of vector records — batch saving, querying, updating, and deleting — via Django ORM `Embedding` model records.

Usage

Import this class when you need to:

  • Store paragraph embeddings in pgvector after document splitting
  • Query similar paragraphs by embedding vector or keyword at chat time
  • Perform hit testing (similarity validation) for knowledge base configuration

Code Reference

Source Location

  • Repository: MaxKB
  • File: apps/knowledge/vector/pg_vector.py

Signature

class PGVector(BaseVectorStore):
    def _batch_save(
        self,
        text_list: List[Dict],
        embedding: Embeddings,
        is_the_task_interrupted: callable,
    ) -> bool:
        """Batch-embed and persist paragraph vectors with full-text search vectors."""

    def query(
        self,
        query_text: str,
        query_embedding: List[float],
        knowledge_id_list: list[str],
        document_id_list: list[str],
        exclude_document_id_list: list[str],
        exclude_paragraph_list: list[str],
        is_active: bool,
        top_n: int,
        similarity: float,
        search_mode: SearchMode,
    ) -> List[Dict]:
        """Query paragraphs by embedding similarity, keywords, or blended mode."""

    def hit_test(
        self,
        query_text: str,
        knowledge_id_list: list[str],
        exclude_document_id_list: list[str],
        top_number: int,
        similarity: float,
        search_mode: SearchMode,
        embedding: Embeddings,
    ) -> List[Dict]:
        """Run a similarity hit test for knowledge base validation."""

Import

from knowledge.vector.pg_vector import PGVector
# Or via the factory:
from common.config.embedding_config import VectorStore
vector = VectorStore.get_embedding_vector()

I/O Contract

Inputs

Name Type Required Description
text_list List[Dict] Yes (for _batch_save) Paragraph dicts with keys: document_id, paragraph_id, knowledge_id, is_active, source_id, source_type, text
embedding Embeddings Yes LangChain embedding model instance for vector generation
is_the_task_interrupted callable Yes Cancellation check function returning bool
query_text str Yes (for query) User question text for keyword search
query_embedding List[float] Yes (for query) Embedded query vector
knowledge_id_list list[str] Yes Knowledge base IDs to search within
top_n int Yes Maximum number of results to return
similarity float Yes Similarity threshold (0.0–1.0)
search_mode SearchMode Yes Search mode: embedding, keywords, or blend
is_active bool Yes Filter only active embeddings

Outputs

Name Type Description
_batch_save returns bool True if bulk_create succeeded
query returns List[Dict] List of dicts with paragraph_id, similarity, comprehensive_score
hit_test returns List[Dict] Ranked similarity results for validation

Usage Examples

Batch Saving Embeddings

from common.config.embedding_config import VectorStore
from knowledge.task.embedding import get_embedding_model

# Get the vector store instance
vector = VectorStore.get_embedding_vector()

# Get the embedding model
embedding_model = get_embedding_model(model_id)

# Prepare paragraph data
text_list = [
    {
        "document_id": "doc-uuid",
        "paragraph_id": "para-uuid",
        "knowledge_id": "kb-uuid",
        "is_active": True,
        "source_id": "src-uuid",
        "source_type": "source-type",
        "text": "Paragraph content to embed",
    }
]

# Batch save with interruption check
vector._batch_save(text_list, embedding_model, is_the_task_interrupted=lambda: False)

Querying Similar Paragraphs

from knowledge.models import SearchMode

# Embed the user query
embedding_value = embedding_model.embed_query("What is RAG?")

# Query the vector store
results = vector.query(
    query_text="What is RAG?",
    query_embedding=embedding_value,
    knowledge_id_list=["kb-uuid-1", "kb-uuid-2"],
    document_id_list=None,
    exclude_document_id_list=[],
    exclude_paragraph_list=[],
    is_active=True,
    top_n=5,
    similarity=0.6,
    search_mode=SearchMode.embedding,
)
# results: [{"paragraph_id": "...", "similarity": 0.92, "comprehensive_score": 0.88}, ...]

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment