For the complete documentation index, see llms.txt. This page is also available as Markdown.

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

Input
Key
Description
Type

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

Input
Description
Type

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

Input
Description
Type

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

Input
Description
Type

store

The name of the knowledge store.

string

name

The name of the document source to delete (the same name provided in addKnowledge).

string

Irreversible action

getMetaData

Retrieves detailed metadata for all chunks stored in a knowledge store, useful for debugging.

Parameters

Input
Key
Description
Type

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

Input
Description
Type

store

The name of the knowledge store to reset.

string

Irreversible action

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

Input
Key
Description
Type

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

Mark the API key as a secret

Right-click the input carrying the API key and mark it as a secret.

Output

Returns the name of the created instance.

Example

delete

Deletes a chat instance.

Parameters

None.

Output

Returns true upon removal.

Irreversible action

executePrompt

Sends a question to the chatbot and gets an answer. This is the primary function for interacting with the chat instance.

Parameters

Input
Description
Type

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

Input
Description
Type

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.

1

Populate a knowledge store

Use addKnowledge of the KnowledgeBase class to add information to a store.

2

Create a chat instance

Create a ChatWithData instance linked to the store you just populated.

3

Ask a question

Use executePrompt to ask a question related to the document you added.

The chatbot finds the relevant section in the PDF, uses it as context, and provides a specific answer.

4

Ask a follow-up question

The chatbot remembers the context of the conversation.

The chatbot understands that the follow-up refers to the maximum operating temperature and answers in the requested unit.

Last updated

Was this helpful?