Skip to content
Back to log

What is RAG and how does it work?

Chainabit's Chao · · 7 min read

In short

RAG combines information retrieval with text generation by a language model. We explain how the flow works, from preparing content to retrieval and answer generation.

What is RAG?

Retrieval-Augmented Generation, or RAG for short, is an approach that combines information retrieval with the text generation capabilities of large language models. Google Cloud describes RAG as a structure that brings together information access tools, such as search systems and databases, with generative language models.

In the flow AWS describes, the user query first goes to an information retrieval component. This component fetches information related to the query. Then both the query and the retrieved information are passed to the language model. While building its answer, the model uses the retrieved information along with what it learned during training.

According to AWS, RAG extends the scope of a language model to an organization's internal knowledge or content from a specific domain without retraining the model. Google Cloud likewise notes that this approach can be added to language model applications and agents that need to access private or domain-specific data.

In the flow AWS describes, information outside the model's original training data can come from APIs, databases, or content repositories. This information can take the form of files, database records, or long-form text. To make it searchable, the content is processed and converted into numerical representations.

Splitting a large text into smaller sections is one of the preparation steps Microsoft Learn describes. This process is called chunking. Chunking breaks long content into pieces that can be matched individually. This way, a search can find the sections relevant to the question instead of the whole text.

Microsoft Learn explains that the vectorization step performed while building an index produces the embeddings used in vector queries. In the method AWS describes, embedding models convert content into numerical representations. These representations are stored in a vector database and later matched against the user query.

Content does not have to consist of plain text alone. According to Google Cloud, multimodal embeddings can also be used for images, audio, and video. These representations can be retrieved together with text embeddings or embeddings in different languages. Microsoft Learn also lists OCR, image analysis, and describing an image in text among the content preparation capabilities in Foundry IQ.

How is information found when a question arrives?

In the vector search flow AWS describes, the user query is converted into a vector representation. This representation is matched against the information in the vector database. The relationship is determined by mathematical calculations on the vector representations. The relevant content chunks are then used in answer generation.

AWS gives the example of a chatbot that answers staff questions. When an employee asks about their annual leave entitlement, the system can retrieve the annual leave policy together with the employee's past leave record. In the example, this content is selected because it relates to the employee's query. The language model receives the information along with the question.

Google Cloud explains that vector databases store content as embeddings and that retrieval can be performed based on semantic similarity. The vector similarity search Microsoft Learn describes can likewise find matches between concepts rather than searching for the same words.

Different search types can be used in the same flow. According to Microsoft Learn, hybrid search combines keyword search and vector search. In a hybrid query, the text and its vector counterpart produce parallel queries for keyword and similarity searches. The relevant matches from both search types are merged into a single result set.

Google Cloud notes that in some search engines, results are scored by a re-ranker. According to Microsoft Learn, semantic ranking also rescores results based on their meaning rather than only on keywords. Google Cloud further explains that some search engines transform the query and correct spelling errors before searching.

How does retrieved information enter the answer?

According to AWS, after retrieval, the relevant information found is added as context to the user's input. This augmented input, that is, the prompt, is sent to the language model. The model generates its answer using both the user's question and the retrieved content.

Google Cloud describes this stage as grounded generation: the retrieved and preprocessed information is added to the context the model uses. The preprocessing steps Google Cloud lists include tokenization, stemming, and stop word removal. These steps may occur in the flow before the retrieved information is passed to the model.

Turn this insight into action.

Use the mobile app to continue with guided workflows.

The model in the RAG research by Lewis and colleagues has two components: a retriever and a generator. The retriever fetches text passages based on the input. The generator produces output using the context, including the original input and the retrieved passage. In the research, the generator also uses the previous tokens for each token it produces.

The researchers describe two model configurations. RAG-Sequence relies on the same retrieved passage while generating an entire output sequence. RAG-Token can use different passages for different output tokens. According to the research, this second configuration allows content from multiple passages to be selected while the answer is generated.

How are content and results kept up to date?

AWS describes updating the external information used for retrieval as a separate step. When the content changes, its embedding representation can be updated as well. According to AWS, this can be done through automated processes or periodic batch jobs.

According to AWS, RAG output can include citations or references to the content used. When the user wants more detail, they can look at the content cited. AWS also notes that developers can change the information the language model refers to and fix the problem if the wrong content is referenced for certain questions.

Microsoft Learn describes the agentic retrieval flow in Foundry IQ, in which the language model can break a complex question into multiple focused subqueries. The subqueries run in parallel. Conversation history can be used for context in query planning. The flow can return a structured response that includes grounding data and citations, as well as information about the execution.

In the classic RAG flow Microsoft Learn describes, the application sends a single query to Foundry IQ. The application separately handles passing the results to the language model. The language model builds the answer using the result set from the query. In this flow, no language model takes part in query planning.

Retrieval flow in Chao

According to Chainabit's Knowledge Management description, Chao makes added context content searchable. The content is first analyzed and summarized. Long content is split into chunks; each chunk is converted into a semantic vector representation for similarity search. When a question is asked, Chao finds the relevant chunks.

The way content is processed varies by context type. According to Chainabit, notes are stored as they are. The text of an attached file is extracted, summarized, and split into chunks. A web page added as a URL is crawled, summarized, and likewise split into chunks.

When a message arrives, Chao first analyzes whether information retrieval is needed. When it is, it searches the stored context and memory content using semantic similarity. It selects the relevant chunks and uses them as context while generating the answer. Chainabit notes that the retrieval step is skipped for simple greetings, creative requests, and preference changes.

According to Chainabit, when memory is on, the user's messages are embedded and stored for later reference. Chao's substantive answers are also stored for conversation continuity. Memory is limited to the account and is not shared. Storage is managed with the ai_memory_enabled preference.

Limits of RAG

According to Google Cloud, if the retrieved information is irrelevant, the generated text can be off-topic or wrong even if it is based on that information. AWS lists among the problems language models can run into presenting false information when they do not know the answer, and confusion caused by the same term being used with different meanings in different content.

Microsoft Learn notes that the token input language models accept is limited. A retrieval system faces the challenge of returning relevant and concise results within this limit. Users may also ask ambiguous questions or questions that depend on the conversation context. Traditional keyword search can fail when the words in the query do not match the terms in the content.

Access permissions are also part of the flow. According to Microsoft Learn, when private content is used, users and agents must retrieve only the content they are authorized to access. In the Foundry IQ description, filter-based security at query time and, for some content, inheriting existing permission information are among the mechanisms listed for this requirement.

Ready to start?

Download the app and continue from your phone.

End of Entry