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.
Optionaldocument?: stringRaw text content for the document.
Optionalembedding?: number[]Embedding for the document.
Optionalid?: stringID for the document.
Optionalmetadata?: 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.
OptionalaugmentedGeneration?: booleanWhether to augment with retrieved context (default: true).
Optionalcallback?: (token: string) => voidToken callback for streaming.
Input messages or a single string.
OptionalnResults?: numberNumber of docs to retrieve (default: 3).
Optionalpredicate?: (value: QueryResult) => booleanFilter applied to retrieved docs.
OptionalpromptGenerator?: (messages: Message[], retrievedDocs: QueryResult[]) => stringBuilds the context-augmented prompt from messages and retrieved docs.
OptionalquestionGenerator?: (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.
OptionalmetadataGenerator?: (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.
OptionaltextSplitter?: 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.
Optionaldocument?: stringNew content for the document.
Optionalembedding?: number[]New embedding for the document. If not provided, it will be generated based on the document.
The ID of the document to update.
Optionalmetadata?: Record<string, any>New metadata for the document.
Promise that resolves once the document is updated.
Orchestrates Retrieval Augmented Generation. Coordinates a
VectorStoreand anLLMto ingest, retrieve, and generate.Example