RAG AI
Retrieval-Augmented Generation (RAG) pairs a prompt with relevant content retrieved from a local vector knowledge store. Answers are grounded in your own documents and manuals, which reduces hallucinations.
Environment configuration requirement
Provide a valid OPENAI_API_KEY as environment parameter when launching this module.

The RAG AI extension bundles two classes: KnowledgeBase and ChatWithData. You first populate a knowledge store using KnowledgeBase, then a ChatWithData instance uses that store to answer questions.
Knowledge base
The KnowledgeBase class creates, manages, and queries vectorized knowledge stores. It ingests information from various sources (files, URLs, text), splits it into searchable chunks, creates vector embeddings, and stores them in a vector database (ChromaDB). This class provides static functions only and does not require an instance. The code class name is KnowledgeBase.
addKnowledge
Adds information from a source into a specific knowledge store. The function automatically detects the source type, processes the content, splits it into chunks, creates vector embeddings, and stores them.
Parameters
store
The name of the knowledge store, for example product-manuals.
string
knowledge
The content to add. Accepts a URL string, a local file path string (supported extensions: pdf, txt, csv, docx, pptx, html), a plain text string, or a JSON object.
any
name
A name identifying this knowledge source, used for later management (for example Product Manual v2).
string
options
chunkSize
The maximum size of each text chunk. Default 2000.
integer
chunkOverlap
The number of characters to overlap between chunks. Default 400.
integer
Output
Returns true once the knowledge has been added.
Examples
Adding knowledge from a PDF file
Adding knowledge from a website
Adding knowledge from a plain text string
similaritySearch
Performs a similarity search against a knowledge store to find the text chunks most relevant to a given query.
Parameters
store
The name of the knowledge store.
string
query
The text query to search for.
string
nDocs
The maximum number of relevant chunks to return.
integer
Output
Returns an array of document objects relevant to the query.
getDocuments
Lists all named documents currently stored in a knowledge store.
Parameters
store
The name of the knowledge store.
string
Output
Returns an array of objects, one per added document source.
deleteDocument
Deletes all chunks associated with a specific named document from the knowledge store.
Parameters
store
The name of the knowledge store.
string
name
The name of the document source to delete (the same name provided in addKnowledge).
string
getMetaData
Retrieves detailed metadata for all chunks stored in a knowledge store, useful for debugging.
Parameters
store
The name of the knowledge store.
string
options
showData
If true, includes the raw text content of each chunk. Default false.
boolean
Output
Returns the metadata for all chunks stored in the knowledge store.
reset
Completely deletes all information in a given knowledge store.
Parameters
store
The name of the knowledge store to reset.
string
Chat with data
The ChatWithData class creates a conversational AI (chatbot) that answers questions based on the information held in a specific KnowledgeBase store. When you ask a question, it first searches the knowledge store for relevant information using similaritySearch. It then combines your question, the chat history, and the retrieved context into a new prompt that it sends to an OpenAI model to generate a well-informed answer. This class requires an instance. The code class name is ChatWithData.
create
Creates a chat instance linked to a specific knowledge store and configured with the desired AI behavior.
Parameters
storeName
The name of the KnowledgeBase store this chat instance will use.
string
options
openAIApiKey
Your OpenAI API key.
string
temperature
The model's creativity level, a value from 0 to 1. Default 0.1.
number
modelName
The OpenAI model to use, for example gpt-4.
string
systemMessage
A general instruction telling the chatbot how to behave.
string
nDocuments
The maximum number of documents to retrieve from the knowledge store for context. Default 4.
integer
Output
Returns the name of the created instance.
Example
delete
Deletes a chat instance.
Parameters
None.
Output
Returns true upon removal.
Irreversible action
Deleting removes the instance configuration. The underlying knowledge store remains unchanged.
executePrompt
Sends a question to the chatbot and gets an answer. This is the primary function for interacting with the chat instance.
Parameters
question
The user's question or prompt.
string
Output
Returns an object with the keys answer (the response string), history (the updated conversation history), sourceDocs (an array of the source document chunks used to generate the answer), and tokenUsage (information about the number of tokens used for the request).
addKnowledge
Adds new information to the knowledge store associated with this chat instance. This is a convenience wrapper around addKnowledge of the KnowledgeBase class.
Parameters
knowledge
The content to add. Accepts the same source types as the KnowledgeBase function.
any
resetConversion
Clears the current conversation history. The chatbot forgets the previous conversation, but the underlying knowledge store remains unchanged.
Parameters
None.
reinitialize
Reinitializes the chat instance with new configuration options.
Complete example
A step-by-step workflow demonstrating how the two classes work together.
Last updated
Was this helpful?