Retrieval Augmented Generation (RAG) Explained
- 5 min read
In today’s information-rich world, humans have long mastered the art of information retrieval and knowledge augmentation to produce high-quality output. From researching a topic to writing a report, we seamlessly blend retrieved data with our own understanding to create insightful content. As generative AI continues to evolve, we can automate and streamline this process using Retrieval Augmented Generation (RAG). RAG leverages the power of information retrieval systems and large language models to generate human-like answers to user questions.
What is Retrieval Augmented Generation?
Retrieval augmented generation (RAG) is a technique that combines information retrieval with language model generation to improve the accuracy and relevance of the generated text, and to better ground the model’s response in evidence. In RAG, a language model is augmented with an external knowledge base or a set of documents that is passed into the context window.
RAG — The Synergy of Vector Databases and LLMs
At the core of RAG lies the seamless integration of two key components:
- Vector Databases: These specialized databases store and index information in a vector representation, enabling similarity searches and retrieval of relevant data.
- Large Language Models (LLMs): When combined with retrieved information from vector databases, LLMs can augment and synthesize the data, producing human-like output that incorporates both factual knowledge and contextual understanding.
Basic chain vs RAG chain
Let’s start with a basic chain. A basic chain is composed of a prompt, a model, and an output parser. The basic chain illustrates the core idea of using LLMs in a structured way by defining the input (prompt), the processing step (model), and the output handling (output parser). It serves as a foundational concept that can be extended and combined with other components to build more complex chains and applications.
RAG chain adds context when responding to questions. Let’s break down RAG chain by a RAG prompt, a RAG retriever, and a RAG chain.
RAG chain
RAG prompt
Let’s first examine how the prompt template incorporates context and question as values to be substituted into the prompt.
RAG retriever
Prior to constructing the prompt template, RAG chain retrieves documents relevant to the search query and include them as part of the context.
RAG chain
RAG chain utilizes RunnableParallel to prepare the expected inputs for the prompt by incorporating the context from the retrieved documents as well as the original user question. This process involves employing the retriever for document search and RunnablePassthrough to pass along the user's question. The remaining chain components resemble the basic chain described earlier.
RAG vector store
A prevalent approach to storing and searching through unstructured data involves embedding it and storing the resulting embedding vectors. Then, during query execution, the unstructured query is embedded, and the embedding vectors that exhibit the highest similarity to the embedded query are retrieved. A vector store handles the tasks of storing embedded data and performing vector searches on your behalf.
Text Embeddings and Embedding Dimension
Let us explore text embeddings and embedding dimensions by examining code examples.
What are Text Embeddings?
Text embeddings are vector representations of words or passages that encode semantic meaning using machine learning algorithms that convert textual data into numerical vectors. These vectors represent the semantic meaning of words and phrases, allowing computers to understand and process natural language more efficiently
What is Embedding Dimension?
The length of numerical vectors is called the embedding dimension. A higher dimension means the embedding vector stores more information about the semantic meaning and relationships of the text.
Text embeddings convert unstructured text like documents, paragraphs, and sentences into meaningful vector representations. The input is a piece of text, and the output is a single row vector with n dimensions ( 1 x n vector). The resulting embedding vectors can be utilized for many different applications.
The following code examples explore three text embedding models offered by Amazon Bedrock.
Conclusion
In conclusion, Retrieval Augmented Generation (RAG) is a powerful technique that harnesses the synergy between vector databases and large language models (LLMs) to generate human-like, knowledge-grounded responses. By seamlessly integrating information retrieval from vector databases with the generative capabilities of LLMs, RAG enables the creation of accurate and relevant content that incorporates both factual knowledge and contextual understanding.