Creates a new RAG instance.
Object containing the implementations.
Large Language Model used for generation.
Vector store used for retrieval.
Adds a document to the vector store.
Parameters for the operation.
Optional
document?: stringRaw text content for the document.
Optional
embedding?: number[]Embedding for the document.
Optional
id?: stringID for the document.
Optional
metadata?: Record<string, any>Metadata for the document.
Promise that resolves to the ID of the newly added document.
Deletes documents from the vector store by the provided predicate.
Promise that resolves once the documents are deleted.
Generates a response based on the input messages and retrieved documents.
If augmentedGeneration
is true, it retrieves relevant documents from the vector store
and includes them in the prompt for the LLM.
Generation parameters.
Optional
augmentedGeneration?: booleanWhether to augment with retrieved context (default: true).
Optional
callback?: (token: string) => voidToken callback for streaming.
Input messages or a single string.
Optional
nResults?: numberNumber of docs to retrieve (default: 3).
Optional
predicate?: (value: QueryResult) => booleanFilter applied to retrieved docs.
Optional
promptGenerator?: (messages: Message[], retrievedDocs: QueryResult[]) => stringBuilds the context-augmented prompt from messages and retrieved docs.
Optional
questionGenerator?: (messages: Message[]) => stringMaps the message list to a search query (default: last message content).
Promise that resolves to the generated text.
Interrupts the ongoing text generation process.
Promise that resolves when the interruption is complete.
Initializes the RAG system by loading the vector store and LLM.
A promise that resolves to the same RAG
instance.
Splits a document into chunks and adds them to the vector store.
If no textSplitter
is provided, a default
RecursiveCharacterTextSplitter({ chunkSize: 500, chunkOverlap: 100 })
is used.
Parameters for the operation.
The content of the document to split and add.
Optional
metadataGenerator?: (chunks: string[]) => Record<string, any>[]Function to generate metadata for each chunk. Must return an array which length is equal to the number of chunks.
Optional
textSplitter?: TextSplitterText splitter implementation.
Promise that resolves to the IDs of the newly added chunks.
Unloads the RAG system, releasing resources used by the vector store and LLM.
A promise that resolves when unloading is complete.
Updates a document in the vector store by its ID.
Parameters for the update.
Optional
document?: stringNew content for the document.
Optional
embedding?: number[]New embedding for the document. If not provided, it will be generated based on the document
.
The ID of the document to update.
Optional
metadata?: Record<string, any>New metadata for the document.
Promise that resolves once the document is updated.
Orchestrates Retrieval Augmented Generation. Coordinates a
VectorStore
and anLLM
to ingest, retrieve, and generate.Example