AWS Builder Center
Building Smarter AI Applications with Retrieval-Augmented Generation (RAG) on AWS

Building Smarter AI Applications with Retrieval-Augmented Generation (RAG) on AWS

Learn how Retrieval-Augmented Generation (RAG) combines information retrieval with generative AI to create more accurate and context-aware applications. Explore how RAG works, how Amazon Bedrock Knowledge Bases can support RAG workflows, and how developers can use this approach to build AI applications that work with their own data.

Generative AI applications can produce impressive responses, but large language models do not automatically have access to every piece of information a user may need. They can also sometimes generate responses that are incomplete, outdated, or not grounded in a specific knowledge source.
Retrieval-Augmented Generation, commonly called RAG, provides a way to address this challenge. Instead of relying only on the knowledge contained within a foundation model, a RAG application retrieves relevant information from an external knowledge source and provides that information as context to the model.
This makes RAG particularly useful for applications that need to answer questions using specific documents, organizational information, product data, or other private and changing information.

What Is RAG?

RAG is an architecture that combines two major capabilities:
  1. Retrieval – Finding relevant information from a knowledge source.
  2. Generation – Using a generative AI model to produce an answer based on the retrieved information.
A simplified RAG workflow looks like this:
User Question → Retrieve Relevant Information → Add Information as Context → Generate Response
For example, imagine a student uploads several course notes and asks:
"Explain the difference between linear regression and logistic regression."
A RAG-based application can first retrieve relevant sections from the uploaded material and then provide those sections to a generative AI model. The model can use that context to generate a clear explanation.

Why Is RAG Useful?

A foundation model may have broad general knowledge, but an application often needs to work with information that is specific to its use case.
RAG can help applications:
  • Answer questions using specific documents
  • Provide context from private or domain-specific information
  • Reduce dependence on information stored only in the model
  • Work with information that can be updated without retraining the foundation model
  • Build document question-answering applications
  • Create knowledge assistants and support applications
RAG does not guarantee that every response will be correct, so the retrieved information and generated answers still need to be evaluated.

How a RAG Application Works

A typical RAG workflow can be divided into several stages.

1. Prepare the Knowledge Source

The application first needs a source of information, such as documents, manuals, FAQs, reports, or educational material.
Large documents are generally divided into smaller sections so that relevant pieces can be retrieved efficiently.

2. Create Embeddings

Text can be converted into numerical representations called embeddings.
Embeddings represent the semantic meaning of text in a form that can be used for similarity-based retrieval.
For example, the questions:
  • "How does supervised learning work?"
  • "Explain supervised machine learning."
use different words but have similar meanings. Embeddings can help identify this semantic similarity.

3. Store and Retrieve Relevant Information

The embeddings and associated content can be stored in a system designed for similarity search.
When a user asks a question, the question can also be represented as an embedding. The system then searches for information that is semantically relevant to the question.

4. Provide Context to the AI Model

The retrieved information is added to the model's input as context.
Instead of asking the model to answer only from its general knowledge, the application can instruct it to use the retrieved information when generating the response.

5. Generate the Final Answer

The foundation model processes the user's question together with the retrieved context and generates a response.
The final result can therefore be more relevant to the application's specific knowledge source.

RAG with Amazon Bedrock

Amazon Bedrock provides capabilities for building generative AI applications using foundation models through APIs.
For RAG applications, Amazon Bedrock Knowledge Bases can help developers connect foundation models with organizational or application-specific data.
A simplified architecture can be represented as:
Data Sources → Knowledge Base → Retrieval → Foundation Model → Generated Response
This approach allows developers to build applications that can retrieve relevant information from connected data and use it as context for generative AI responses.

Example: AI Learning Assistant

Consider a student-focused AI learning assistant.
The application could contain:
  • Lecture notes
  • Course materials
  • Programming documentation
  • Frequently asked questions
  • Subject-specific reference material
A student could ask:
"Explain the formula for linear regression using my study material."
The application could retrieve the relevant section from the available learning material and provide it as context to the foundation model. The model could then generate an explanation based on that retrieved content.
Possible features could include:
  • Document-based question answering
  • Topic explanations
  • Study-material summaries
  • Quiz generation
  • Programming concept explanations
  • Reference-based answers
These are example features for a proposed application rather than claims of an already deployed system.

RAG vs. Traditional Prompting

Traditional prompting asks a foundation model to generate an answer using the information available within the model and the prompt.
With RAG, the application first retrieves relevant information from an external knowledge source and includes that information in the model's context.
Traditional PromptingRAG
Relies mainly on model knowledge and provided promptUses retrieved external information as context
Suitable for general questionsUseful for domain-specific information
External knowledge must be manually providedInformation can be retrieved automatically
May not reflect updated private informationCan work with an updated knowledge source
RAG is therefore useful when an application needs to work with information beyond a model's general knowledge.

Important Considerations

Building a RAG application requires more than simply connecting a model to documents.

Data Quality

Poor or outdated source information can lead to poor answers. The quality of the knowledge source directly affects retrieval and generation.

Retrieval Quality

If the system retrieves irrelevant information, the model may not have the right context to answer the question accurately.

Security

Applications should carefully control access to private or sensitive information and ensure that users only retrieve information they are authorized to access.

Evaluation

RAG applications should be evaluated for both retrieval quality and answer quality. Developers should test whether the system retrieves relevant information and whether the generated response is supported by that information.

Cost and Performance

Retrieval, storage, embedding generation, and model inference can all contribute to application cost and latency. The architecture should therefore be designed according to the application's requirements.

Conclusion

Retrieval-Augmented Generation provides a practical architecture for combining external knowledge with generative AI.
Instead of relying entirely on a foundation model's existing knowledge, a RAG application retrieves relevant information and provides it as context before generating a response.
With services such as Amazon Bedrock and Amazon Bedrock Knowledge Bases, developers can explore ways to build AI applications that work with domain-specific and application-specific information.
For students and developers learning generative AI, building a small document-based question-answering application is a useful way to understand how retrieval, embeddings, knowledge sources, and foundation models work together.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article