Skip to main content

Introduction

For AI models to be effective in specialized tasks, they often require domain-specific knowledge. For instance, a financial advisory chatbot needs to understand market trends and products offered by a specific bank, while an AI legal assistant must be equipped with knowledge of statutes, regulations, and past case law. A common solution is Retrieval-Augmented Generation (RAG), which retrieves relevant data from a knowledge base and combines it with the user’s prompt, thereby improving and customizing the model’s output to the provided data.

RAG Explanation

RAG operates by preprocessing a large knowledge base and dynamically retrieving relevant information at runtime. Here’s a breakdown of the process:
  1. Indexing the Knowledge Base: The corpus (collection of documents) is divided into smaller, manageable chunks of text. Each chunk is converted into a vector embedding using an embedding model. These embeddings are stored in a vector database optimized for similarity searches.
  2. Query Processing and Retrieval: When a user submits a prompt that would initially go directly to an LLM we process that and extract a query, the system searches the vector database for chunks semantically similar to the query. The most relevant chunks are retrieved and injected into the prompt sent to the generative AI model.
  3. Response Generation: The AI model then uses the retrieved information along with its pre-trained knowledge to generate a response. Not only does this reduce the likelihood of hallucination since relevant context is provided directly in the prompt but it also allows us to cite to source material as well.

Download and View the Dataset

Shell
Python
This dataset consists of movie information as below:
Python

Implement Retrieval Pipeline - ā€œRā€ part of RAG

Below we implement a simple retrieval pipeline:
  1. Embed movie documents and query
  2. Obtain top k movies ranked based on cosine similarities between the query and movie vectors.
Python
This will generate embeddings of the movies which we can use later to retrieve similar movies. When a user makes a query we can embed the query using the same model and perform a vector similarity search as shown below:
Python
We get a similarity score for each of our 1000 movies - the higher the score, the more similar the movie is to the query. We can sort this similarity score to get the movies most similar to our query = super hero action movie with a timeline twist
Python
This produces the top ten most similar movie titles below:

We can encapsulate the above in a function

Python
Which can be used as follows:
Python
Which returns an array of indices for movies that best match the query.

Generation Step - ā€œGā€ part of RAG

Below we will inject/augment the information the retrieval pipeline extracts into the prompt to the Llama3 8b Model. This will help guide the generation by grounding it from facts in our knowledge base!
Python
Which produces the grounded output below:
Text
Here we can see a simple RAG pipeline where we use semantic search to perform retrieval and pass relevant information into the prompt of an LLM to condition its generation. To learn more about the Together AI API please refer to the docs here!