LocalMode
React

Knowledge Base

React hook for knowledge-base session orchestration over the KnowledgeBaseEngine contract — document store, embedding-model lifecycle, and switch-driven re-ingest.

Knowledge Base Hook

See it in action

Try the knowledge blocks for working demos — the Ingest, Search, Ask, and Data experiences, each on a core-pipeline ⇄ LangChain engine toggle powered by this hook.

useKnowledgeBase

The knowledge-base session-orchestration hook over the core KnowledgeBaseEngine contract. It owns the full session so no consuming block re-implements it: the raw-document store (documents + add / remove / clear), the embedding-model load lifecycle (via useModelLoad, key kb-embedding:${id} preserved verbatim), chunking + embedding-model config, busy / error / stats, and — the load-bearing part — re-ingest of the whole raw-document store on either an engine-kind toggle or an embedding-model switch.

The active engine is produced by an injected createEngine(kind) factory, so the hook consumes only the KnowledgeBaseEngine contract and never imports a specific engine. A core-only factory keeps @localmode/langchain entirely out of the session; a toggle-capable factory dynamically imports the LangChain engine (await import('@localmode/langchain')) only when the toggle flips.

import { useKnowledgeBase } from '@localmode/react';
import { createKnowledgeBaseEngine } from '@localmode/core';
import { transformers, isModelCached } from '@localmode/transformers';

const kb = useKnowledgeBase({
  embeddingModelId: 'Xenova/bge-small-en-v1.5',
  createEmbeddingModel: (id, onProgress) => transformers.embedding(id, { onProgress }),
  isModelCached: (id) => isModelCached(id),
  // core-only factory: never imports @localmode/langchain
  createEngine: () =>
    createKnowledgeBaseEngine({
      embeddingModel: transformers.embedding('Xenova/bge-small-en-v1.5'),
      getLanguageModel: async () => transformers.languageModel('onnx-community/granite-4.0'),
    }),
});

Options

Prop

Type

Return Value

Session

Prop

Type

Embedding-model load lifecycle

Prop

Type

The engine contract

The hook is engine-agnostic: it talks only to the KnowledgeBaseEngine contract in @localmode/core. Both engines implement it:

EngineFactoryPackage
Core pipelinecreateKnowledgeBaseEngine@localmode/core
LangChaincreateLangChainKnowledgeBaseEngine@localmode/langchain

An engine-kind toggle or an embedding-model switch drives a single effect (keyed on [engineKind, embeddingModelId]) that recreates the engine and, if the raw-document store is non-empty, re-ingests the whole corpus through it with visible progress. Regular addDocuments calls ingest incrementally.

For the engine contract and the core pipeline engine, see the Core Knowledge Base guide; for the LangChain engine, see the LangChain adapter guide.

Blocks

AppDescriptionLinks
Knowledge BaseIngest / Search / Ask / Data knowledge base on a core ⇄ LangChain engine toggle via useKnowledgeBaseLive block · Source

On this page