
Building a RAG API with FastAPI and Amazon Bedrock
Learn how Retrieval-Augmented Generation (RAG) can connect private knowledge with Large Language Models, and how FastAPI and Amazon Bedrock can be combined to build a context-aware AI backend.
Large Language Models are powerful, but they have an important limitation: they do not automatically know the private or constantly changing information of our applications.
Imagine building an AI assistant for a company.
The model may understand programming, finance, customer service, and thousands of other topics, but it does not automatically know:
- your internal documentation;
- company policies;
- product information;
- support tickets;
- technical manuals;
- private datasets.
One possible solution would be fine-tuning the model.
But in many cases, that is not what we actually need.
Instead, we can use Retrieval-Augmented Generation — RAG.
RAG allows us to retrieve relevant information from our own data and provide that context to the LLM before it generates the final response.
With Amazon Bedrock Knowledge Bases, AWS provides infrastructure that can help implement this workflow without requiring us to build the entire retrieval pipeline from scratch.
What happens inside a RAG application?
The basic flow looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
User
↓
FastAPI
↓
Amazon Bedrock Knowledge Base
↓
Retrieve relevant context
↓
Foundation Model
↓
Generated answer
↓
FastAPI
↓
UserInstead of sending only the user's question to the model, the application first searches for information related to that question.
For example, imagine that we have internal documentation containing:
1
Our premium plan allows customers to create up to 50 projects.Then the user asks:
1
How many projects can I create with the premium plan?The RAG pipeline retrieves the relevant piece of documentation and builds additional context for the LLM.
Conceptually, the prompt becomes something similar to:
1
2
3
4
5
Context:
Our premium plan allows customers to create up to 50 projects.
Question:
How many projects can I create with the premium plan?Now the model does not need to guess.
It has the information required to generate the answer.
Where do embeddings enter the architecture?
Searching for context is not always the same as performing a traditional SQL query.
Consider these two sentences:
1
How many projects are included in the premium plan?and
1
What is the project limit for premium customers?They use different words, but their meaning is almost identical.
This is where embeddings become useful.
An embedding converts information into a numerical vector representing semantic meaning.
Conceptually:
1
2
3
4
5
Text
↓
Embedding Model
↓
VectorDocuments can be divided into smaller chunks and transformed into vectors.
When a user sends a question, that question can also be transformed into a vector.
The system can then search for vectors that are semantically close to each other.
Amazon Bedrock Knowledge Bases supports embedding models that can be used as part of this process.
Why use FastAPI?
Amazon Bedrock handles the AI layer, but our application still needs an interface through which other systems can interact with it.
FastAPI fits very naturally into this architecture.
It can become the layer responsible for:
- receiving the user's question;
- validating the request;
- calling AWS services;
- applying authentication and business rules;
- returning the AI response.
Our architecture becomes:
1
2
3
4
5
6
7
8
9
Frontend / Client
↓
FastAPI
↓
Amazon Bedrock
↓
Knowledge Base
↓
Foundation ModelThis separation is important.
The LLM should not become the entire application.
It is simply one component inside the architecture.
Creating the API
A simplified request model could look like this:
1
2
3
4
from pydantic import BaseModel
class QuestionRequest(BaseModel):
question: strThen we can create a FastAPI endpoint:
1
2
3
4
5
6
7
8
9
from fastapi import FastAPI
app = FastAPI()
async def ask_question(request: QuestionRequest):
return {
"question": request.question
}At this point, the API already receives and validates our input.
Now we can connect it to Bedrock.
Connecting FastAPI to Amazon Bedrock
Using the AWS SDK for Python, we can create a Bedrock Agent Runtime client.
1
2
3
4
5
6
import boto3
bedrock = boto3.client(
"bedrock-agent-runtime",
region_name="us-east-1"
)For a standard Amazon Bedrock Knowledge Base, the application can retrieve relevant information and generate a response using the Knowledge Base runtime APIs.
A simplified service could look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def ask_knowledge_base(question: str):
response = bedrock.retrieve_and_generate(
input={
"text": question
},
retrieveAndGenerateConfiguration={
"type": "KNOWLEDGE_BASE",
"knowledgeBaseConfiguration": {
"knowledgeBaseId": "YOUR_KNOWLEDGE_BASE_ID",
"modelArn": "YOUR_MODEL_ARN"
}
}
)
return response["output"]["text"]Then our FastAPI endpoint becomes:
1
2
3
4
5
6
7
8
9
async def ask_question(request: QuestionRequest):
answer = ask_knowledge_base(request.question)
return {
"question": request.question,
"answer": answer
}The client does not need to know anything about embeddings, vector retrieval, or foundation models.
It simply communicates with our API.
1
2
3
{
"question": "What is our refund policy?"
}And receives something like:
1
2
3
4
{
"question": "What is our refund policy?",
"answer": "Customers can request a refund within 30 days..."
}Behind that simple endpoint, however, several AI components may have been involved.
Retrieval is as important as generation
One of the most interesting lessons when working with RAG is that the LLM is only part of the system.
Consider this situation:
1
2
3
4
5
6
7
User question
↓
Wrong document retrieved
↓
Excellent LLM
↓
Wrong answerEven a very capable model cannot generate a reliable answer if the context given to it is poor.
That means RAG applications need to evaluate more than model quality.
We also need to think about:
- document quality;
- chunking strategy;
- embeddings;
- retrieval relevance;
- ranking;
- prompt construction;
- model reasoning.
Amazon Bedrock Knowledge Bases can also support reranking retrieval results, adding another step for improving which pieces of context reach the generation stage.
RAG vs Fine-Tuning
RAG and fine-tuning solve different problems.
A simple way to think about them is:
1
2
3
4
5
RAG
→ Give the model better knowledge.
Fine-tuning
→ Change how the model behaves.If I want my model to know my company's latest documentation, RAG is usually a strong candidate.
If I want a model to consistently follow a very specific behavior, output format, or specialized pattern, fine-tuning may become relevant.
And in more advanced architectures, both approaches can even be combined.
1
2
3
Fine-tuned model
+
Private knowledge through RAGThe key is not choosing the most sophisticated technique.
The key is understanding which problem we are actually trying to solve.
A Real Use Case: Customer Support
Imagine creating an AI customer-support assistant.
Our knowledge base could contain:
1
2
3
4
5
6
Product documentation
Pricing information
FAQ
Refund policies
Technical troubleshooting guides
Internal proceduresA customer asks:
1
How can I reset my device after a failed firmware update?The system could execute:
1
2
3
4
5
6
7
8
9
10
11
12
13
Question
↓
FastAPI
↓
Retrieve documentation
↓
Find firmware recovery section
↓
Send context to the LLM
↓
Generate answer
↓
Return responseInstead of expecting the model to magically know our product documentation, we give it access to the information it needs.
That is a much more realistic architecture for production AI systems.
Final Thoughts
When I first started studying LLM applications, it was easy to focus almost entirely on the model.
But building AI applications involves much more than calling an LLM API.
A production-oriented system may contain:
1
2
3
4
5
6
7
8
9
10
11
API
Authentication
Business logic
Data sources
Retrieval
Embeddings
Vector search
Prompt construction
LLM inference
Observability
EvaluationRAG makes this especially clear.
The quality of an AI application does not depend only on how powerful the model is.
It also depends on what information reaches the model and how that information is retrieved.
Combining FastAPI with Amazon Bedrock gives us a very interesting architecture: FastAPI remains responsible for the application layer, while Bedrock provides the generative AI capabilities and knowledge retrieval infrastructure.
And this is where studying AI becomes particularly interesting for backend developers.
We are no longer only calling models.
We are designing systems around them.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article