on_device_rag library
A lightweight, pure-Dart retrieval-augmented-generation (RAG) engine that runs entirely on-device.
The package gives you chunking, embeddings, a vector store and grounded prompt assembly out of the box, with pluggable interfaces so you can swap in your own embedding model, vector store or language model. The default HashingEmbeddingModel has zero native dependencies, so the package works on every Flutter and Dart platform.
Classes
- EmbeddingModel
- Turns text into a fixed-length numeric vector so that the semantic or lexical closeness of two pieces of text can be measured as the distance between their vectors.
- HashingEmbeddingModel
- A zero-dependency, fully offline EmbeddingModel based on the hashing trick (feature hashing).
- InMemoryVectorStore
- An in-memory VectorStore that ranks documents by cosine similarity using a brute-force scan.
- LanguageModel
- A minimal interface for the text-generation model that produces the final answer in a RAG pipeline.
- PromptBuilder
- Builds a grounded prompt from a question and the retrieved context.
- RagAnswer
-
The outcome of
RagEngine.query: the complete answer text plus the sources that grounded it. - RagDocument
- A unit of knowledge stored in the RAG index.
- RagEngine
- Orchestrates a full retrieval-augmented-generation pipeline on the device.
- RagResult
-
The outcome of
RagEngine.ask: a streamed answer plus the sources that grounded it. - ScoredDocument
- A RagDocument paired with its similarity score from a search.
- TextChunker
- Splits long text into overlapping, retrieval-friendly chunks.
- VectorMath
- Vector math helpers used by the RAG engine.
- VectorStore
- A store of embedded RagDocuments that supports similarity search.
Typedefs
- DocumentFilter = bool Function(RagDocument document)
-
A predicate used to restrict searches or removals to some documents, for
example
(doc) => doc.metadata['source'] == 'manual.pdf'.