Build RAG Apps | Clarifai Docs
Use the Clarifai SDKs to build RAG Applications
In the field of text generation, Retrieval-Augmented Generation (RAG) enhances the capabilities of Large Language Models (LLMs) by combining information retrieval with natural language generation.
This approach addresses key limitations of LLMs by retrieving relevant data from external knowledge bases in real time, enriching responses with increased precision and contextual relevance.
Clarifai SDKs streamline the creation of RAG-based applications by minimizing the complexity of integrating retrieval and generation steps. This makes it easier for developers to build powerful, context-aware AI solutions with improved accuracy and reliability.
Click here to learn more about RAG.
Before using the Python SDK, Node.js SDK, or any of our gRPC clients, ensure they are properly installed on your machine. Refer to their respective installation guides for instructions on how to install and initialize them.
Prerequisites
You need a PAT (Personal Access Token) key to authenticate your connection to the Clarifai platform. You can generate the PAT key in your personal settings page by navigating to the Security section.
Clone the Clarifai Examples repository to get the data files required for the building RAG.
!git clone https://github.com/Clarifai/examples.git
%cd /content/examples/
To run on a local system use: cd examples/
Before you proceed install llama_index using pip install llama-index-core==0.10.24
Initializing RAG
The first step in building a RAG-based application is initializing the RAG object. You can do this in three ways:
- Using User ID – Automatically creates a new app.
- Using App URL – Initializes RAG with an existing app and its data.
- Using Workflow URL or ID – Uses a specific workflow that includes the RAG Prompter and LLM model for seamless integration.
You can set a specific version of LLM like this: https://clarifai.com/mistralai/completion/models/mistral-7B-Instruct/model_version/version_id.
- Python SDK
- Node.js SDK
# Import the RAG module from Clarifai for conversational AI tasks
from clarifai.rag import RAG
# Set the user ID for authentication (For creating a new App)
USER_ID = 'USER_ID'
# Alternatively, initialize RAG system using an existing app's URL
APP_URL = 'APP_URL'
# Define the URL of the Mistral-7B language model
LLM_URL = 'https://clarifai.com/mistralai/completion/models/mistral-7B-Instruct'
# Define a template string for generating prompts during inference
RAG_PROMPT_TEMPLATE = "<s>[INST] Context information is below:\n{data.hits}\nGiven the context information and not prior knowledge, answer the query.\nQuery: {data.text.raw}\nAnswer: [/INST]"
# Setup a RAG object with specified parameters such as user ID, model URL, minimum score threshold, and prompt template
# Option 1: Setup a RAG object with specified parameters using the user_id to create a new app
rag_object_user = RAG.setup(
user_id=USER_ID,
pat=PAT,
llm_url=LLM_URL,
min_score=0.5,
max_results=2,
prompt_template=RAG_PROMPT_TEMPLATE
)
# Option 2: Alternatively, setup a RAG object using an existing app's URL
rag_object_app = RAG.setup(
app_url=APP_URL,
pat=PAT,
llm_url=LLM_URL,
min_score=0.5,
max_results=2,
prompt_template=RAG_PROMPT_TEMPLATE
)
# Choose which initialization method to use based on your setup:
# For new app creation:
print(rag_object_user.prompt_workflow)
# For existing app initialization:
print(rag_object_app.prompt_workflow)
import { RAG } from "clarifai-nodejs";
// Import the RAG module from Clarifai for conversational AI tasks
// Define the URL of the Mistral-7B language model
const llmUrl =
"https://clarifai.com/mistralai/completion/models/mistral-7B-Instruct";
// Define a template string for generating prompts during inference
const ragPromptTemplate =
"<s>[INST] Context information is below:\n{data.hits}\nGiven the context information and not prior knowledge, answer the query.\nQuery: {data.text.raw}\nAnswer: [/INST]";
// Setup a RAG object with specified parameters such as user ID, model URL, minimum score threshold, and prompt template
// Option 1: Initialize using userId and create a new app
const ragObject = await RAG.setup({
authConfig: {
userId: process.env.CLARIFAI_USER_ID,
pat: process.env.CLARIFAI_PAT,
},
llmUrl,
minScore: 0.5,
maxResults: 2,
promptTemplate: ragPromptTemplate,
});
// Option 2: Alternatively, initialize using an existing app's URL
const ragObjectApp = await RAG.setup({
authConfig: {
appUrl: process.env.CLARIFAI_APP_URL,
pat: process.env.CLARIFAI_PAT,
},
llmUrl,
minScore: 0.5,
maxResults: 2,
promptTemplate: ragPromptTemplate,
});
// Choose which initialization method to use based on your setup:
// For new app creation:
console.log(ragObject.promptWorkflow);
// For existing app initialization:
console.log(ragObjectApp.promptWorkflow);
Now let's ask questions that are related to the answer we received before so that we can be sure the RAG has understood the context properly.