Implementation:1Panel dev MaxKB embedding by document
| Knowledge Sources | |
|---|---|
| Domains | Async_Tasks, RAG |
| Last Updated | 2026-10-10 00:00 GMT |
Overview
Concrete Celery task for asynchronous document embedding via the embedding model, provided by MaxKB.
Description
The embedding_by_document function is a Celery task decorated with QueueOnce to prevent duplicate execution on the same document. It retrieves the embedding model instance, delegates to ListenerManagement.embedding_by_document() to iterate paragraphs, generate embeddings, and persist them via PGVector. The task processes documents across all states (PENDING, STARTED, SUCCESS, FAILURE, etc.) unless a specific state list is provided.
Usage
Import this task when you need to:
- Trigger asynchronous embedding generation after document upload
- Re-embed documents when the embedding model changes
- Embed multiple documents in a knowledge base via embedding_by_document_list
Code Reference
Source Location
- Repository: MaxKB
- File: apps/knowledge/task/embedding.py
Signature
@celery_app.task(base=QueueOnce, once={"keys": ["document_id"]}, name="celery:embedding_by_document")
def embedding_by_document(document_id, model_id, state_list=None):
"""
Vectorize a document by embedding all its paragraphs.
Args:
document_id: Document UUID to embed
model_id: Embedding model ID to use
state_list: Optional list of document states to process
(default: all states including PENDING through IGNORED)
Returns:
None
"""
Import
from knowledge.task.embedding import embedding_by_document
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| document_id | str | Yes | UUID of the document to embed |
| model_id | str | Yes | UUID of the embedding model to use |
| state_list | list | No | Document states to process (default: all states) |
Outputs
| Name | Type | Description |
|---|---|---|
| returns | None | Task completes silently; embeddings stored via ListenerManagement |
| side effect | Embedding records | Paragraph embeddings persisted in pgvector |
Usage Examples
Triggering Embedding After Upload
from knowledge.task.embedding import embedding_by_document
# Asynchronously embed a document (queued via Celery)
embedding_by_document.delay(
document_id="doc-uuid-1234",
model_id="model-uuid-5678",
)
# With specific state filter
embedding_by_document.delay(
document_id="doc-uuid-1234",
model_id="model-uuid-5678",
state_list=["PENDING", "STARTED"],
)
Embedding a Full Knowledge Base
from knowledge.task.embedding import embedding_by_knowledge
# Re-embed all documents in a knowledge base
embedding_by_knowledge.delay(
knowledge_id="kb-uuid",
model_id="model-uuid",
)