Step-by-Step Guide: Building a RAG Model with open-Source LLM (Llama 2) and Vector store (FAISS)

Large Language Models (LLMs) offer new avenues for enhancing document retrieval and analysis efficiency through Retrieval Augmented Generation (RAG) applications.
This article demonstrates the Python implementation of a RAG model using open source LLM model, LLAMA2, and vector store, Facebook AI Similarity Search (FAISS). After reading this part one, check out part two, which gives a step-by-step guide on how to deploy a RAG Model in Snowpark Container Service with Maia Foundation (formerly Data Productivity Cloud).
Llama2
Llama 2 encompasses a series of pre-trained and fine-tuned generative text models, varying in size from 7 billion to 70 billion parameters. These models, particularly the Llama-2-Chat variants, are fine-tuned for dialogue applications, making them exceptionally suitable for RAG applications.
Facebook AI Similarity Search (FAISS)
FAISS, developed by Facebook, is an open source library designed for fast similarity search on large datasets. It excels at managing and searching through high-dimensional vectors, making it an ideal choice for projects where speed and scalability are key.
In the context of a RAG model, FAISS serves as the backbone for efficiently retrieving the documents most relevant to a given query. FAISS's efficiency lies in its ability to cluster data using an "inverted file index" system, significantly speeding up searches even with millions of vectors.
Step 1 - Document Preprocessing
Preprocessing source documents is crucial for effective analysis. This involves several key steps. First, when the documents are loaded into the Python environment, the text is cleaned to remove extraneous formatting, special characters, and irrelevant content. This step ensures that subsequent analyses are performed on consistent and clean data.
Next, the documents are segmented into smaller, more manageable chunks. Each chunk is typically sized at 1,200 characters, with a 300-character overlap to maintain context. Additionally, the title of each document is appended to each chunk to enhance contextual clarity. This segmentation facilitates easier analysis and information retrieval. The supported document formats are ".md", ".pdf", and ".txt".
The pip dependencies for the following script are:
Example Python code:
Step 2 - Vectorisation and Indexing
Following preprocessing and chunking, the subsequent phase involves vectorization and indexing, which are essential steps in building any RAG model.
Vectorization entails transforming the preprocessed text data into numerical vectors using embedding techniques, such as those provided by Hugging Face. Each text chunk is encoded as a sequence of numbers, which enables computational comprehension and analysis of the documents. This transformation allows the model to interpret the textual content in a format that is suitable for further processing.
Next, FAISS (Facebook AI Similarity Search) is utilized for indexing the vectorized documents.
FAISS is a powerful library designed for fast similarity search on large datasets, particularly adept at managing and searching through high-dimensional vectors. By creating an index of these vector representations, FAISS enables users to efficiently retrieve relevant document chunks based on their queries. This indexed structure significantly enhances the speed and efficiency of document retrieval, allowing for swift access to pertinent information.
One notable advantage of FAISS is its capability to save the vector embeddings locally for future reference. This means that once the vector representations of the documents are computed and indexed, they can be stored locally and readily accessed whenever needed. This eliminates the need for repetitive vectorization and indexing processes, saving computational resources and streamlining the retrieval process in subsequent interactions or use cases of the RAG model.
This can be easily reloaded for future use by calling:
Step 3 - Loading the LLAMA2 Model and Querying for Information:
In this step, the LLAMA2 model is loaded into memory to facilitate response generation based on the contextual information retrieved from the indexed documents. LLAMA2 is a robust language model fine-tuned specifically for text generation tasks, making it an ideal choice for generating responses in a RAG model.
For this implementation, we utilize the "llama-2-7b-chat.Q6_K.gguf" model, which can be downloaded from Hugging Face via this link.
Querying for Information
Once the LLAMA2 model is loaded and the documents are indexed using FAISS, users can input queries. The system then utilizes FAISS to search the indexed documents and identify relevant chunks of information aligned with the query. FAISS returns the top 5 curated list of document chunks that closely match the user's query. These document chunks serve as contextual cues for response generation, providing the necessary information to formulate accurate responses.
Sample Query and Response
Leveraging the retrieved document chunks as contextual anchors, the system generates responses using the LLAMA2 model. LLAMA2 processes the contextual information and generates responses that directly address the user's query. The responses provide precise information derived directly from the contextual cues provided by the indexed documents.
RAG in Maia Foundation
Maia Foundation contains components that help you perform RAG queries using a no-code interface.
Below is a screenshot of a RAG-enabled AI Prompt component using an OpenAI LLM and embedding.
A full demonstration video is available in Matillion's library of AI Videos and Demos, under the heading "Automating Customer Support Part 1".
Conclusion
The deployment of a RAG model leveraging open-source technologies such as FAISS Vector Store and LLAMA2 represents a significant milestone in information retrieval. By harnessing the capabilities of these advanced tools, organizations can streamline their information retrieval processes, enabling users to promptly access pertinent data.
This approach facilitates seamless information retrieval, empowering users to make well-informed decisions based on contextualized responses. Consequently, organizations can enhance their operational efficiency and decision-making capabilities, leading to improved outcomes and better overall performance.
Look out for a future article that will use the FAISS index that was saved in this article to deploy a RAG model in Snowpark Container Service (SPCS).
Part two can be found here!
To try out Maia Foundation for free, using your own data, run a free trial.
Ready to get moving?
See how quickly your team can start delivering business-ready data, with Matillion.
Join Matillion
We need driven, curious people who think big and move fast to join our team.






























