Adding a Knowledge Engine Component
A single plugin can add any number of Knowledge Engines. Execute the commandlbp comp KnowledgeEngine in the plugin directory and follow the prompts to enter the Knowledge Engine configuration.
simple_rag.yaml and simple_rag.py files in the components/knowledge_engine/ directory. The .yaml file defines the Knowledge Engine’s basic information and configuration schemas, and the .py file is the handler for this engine:
Manifest File: Knowledge Engine
creation_schema and retrieval_schema
Unlike the oldKnowledgeRetriever component which used a single spec.config, KnowledgeEngine uses two separate schemas:
- creation_schema: Parameters users fill in when creating a knowledge base. These are passed to the plugin via
creation_settingsduring ingestion and retrieval. - retrieval_schema: Parameters users can adjust when querying the knowledge base. These are passed to the plugin via
retrieval_settings.
Capability Declaration
KnowledgeEngine can declare its supported capabilities. LangBot uses these capability declarations to determine UI behavior and available operations:Other retrieval behaviors (such as reranking, hybrid search, etc.) are controlled by the plugin’s
retrieval_schema and do not need capability flags.Plugin Handler
The following code will be generated by default (components/knowledge_engine/<engine_name>.py). You need to implement the three core methods: ingest, retrieve, and delete_document. Complete code can be found in the SimpleKnowledgeEngine example in langbot-plugin-demo.
Lifecycle Hooks
KnowledgeEngine provides two lifecycle hooks that are called when knowledge bases are created and deleted:Document Ingestion
Theingest method is called when a user uploads a document to the knowledge base:
Knowledge Retrieval
Theretrieve method is called when the knowledge base is queried:
The LangBot host automatically injects a default
top_k value (default 5) into retrieval_settings before passing it to the plugin. Plugins can access it via context.retrieval_settings.get('top_k', 5).The filters field contains Chroma-style where filter conditions extracted from retrieval_settings. When the caller provides filters in the retrieval settings (e.g., filtering by time range, file type, or custom metadata fields), the host populates this field so plugins can apply them during retrieval. If no filters are provided, this field is an empty dict.Document Deletion
Thedelete_document method is called when a user deletes a document from the knowledge base:
Host RAG APIs
KnowledgeEngine components can call LangBot host-provided RAG APIs viaself.plugin, including embedding model invocation, vector database operations, and file retrieval.
Invoke Embedding Model
Vector Upsert
Vector Search
Each result returned by
vector_search is a dict containing id (vector ID), score (distance score), and metadata (metadata provided during upsert). If you need text content in retrieval results, store the text in metadata during ingestion.Vector Delete
The
filters parameter supports Chroma-style where syntax for metadata filtering. Multiple top-level keys are AND-ed. Supported operators: $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin. Example: {"file_id": {"$eq": "abc"}}.Note: Chroma, Qdrant, and SeekDB store full metadata and can filter on any field. Milvus and pgvector only store text, file_id, and chunk_uuid — filters on other fields will be silently ignored.Testing the Knowledge Engine
After creation, execute the commandlbp run in the plugin directory to start debugging. Then in LangBot:
- Go to the “Knowledge Base” page
- Click “Create Knowledge Base”
- Select the Knowledge Engine provided by your plugin and fill in the configuration based on the engine’s
creation_schema - After creation, upload documents to test ingestion capabilities (if the engine declares
DOC_INGESTIONcapability) - Bind the knowledge base to a pipeline and test retrieval capabilities
