Agentic Retrieval Augmented Generation (RAG) applications represent an advanced approach in AI that integrates foundation models (FMs) with external knowledge retrieval and autonomous agent capabilities. These systems dynamically access and process information, break down complex tasks, use external tools, apply reasoning, and adapt to various contexts. They go beyond simple question answering by performing multi-step processes, making decisions, and generating complex outputs.
In this post, we demonstrate an example of building an agentic RAG application using the LlamaIndex framework. LlamaIndex is a framework that connects FMs with external data sources. It helps ingest, structure, and retrieve information from databases, APIs, PDFs, and more, enabling the agent and RAG for AI applications.
This application serves as a research tool, using the Mistral Large 2 FM on Amazon Bedrock generate responses for the agent flow. The example application interacts with well-known websites, such as Arxiv, GitHub, TechCrunch, and DuckDuckGo, and can access knowledge bases containing documentation and internal knowledge.
This application can be further expanded to accommodate broader use cases requiring dynamic interaction with internal and external APIs, as well as the integration of internal knowledge bases to provide more context-aware responses to user queries.
This solution uses the LlamaIndex framework to build an agent flow with two main components: AgentRunner and AgentWorker. The AgentRunner serves as an orchestrator that manages conversation history, creates and maintains tasks, executes task steps, and provides a user-friendly interface for interactions. The AgentWorker handles the step-by-step reasoning and task execution.
For reasoning and task planning, we use Mistral Large 2 on Amazon Bedrock. You can use other text generation FMs available from Amazon Bedrock. For the full list of supported models, see Supported foundation models in Amazon Bedrock. The agent integrates with GitHub, arXiv, TechCrunch, and DuckDuckGo APIs, while also accessing internal knowledge through a RAG framework to provide context-aware answers.
In this solution, we present two options for building the RAG framework:
You can select the RAG implementation option that best suits your preference and developer skill level.
The following diagram illustrates the solution architecture.

In the following sections, we present the steps to implement the agentic RAG application. You can also find the sample code in the GitHub repository.
The solution has been tested in the AWS Region us-west-2. Complete the following steps before proceeding:
AmazonSageMaker-ExecutionRole-20250213T123456.git init
git remote add origin https://github.com/aws-samples/mistral-on-aws.git
git sparse-checkout init
git sparse-checkout set "notebooks/mistral-llamaindex-agentic-rag"
git pull origin main
cd mistral-llamaindex-agentic-rag
pip install -r requirements.txt
Initialize the FM used for orchestrating the agentic flow with Amazon Bedrock Converse API. This API provides a unified interface for interacting with various FMs available on Amazon Bedrock. This standardization simplifies the development process, allowing developers to write code one time and seamlessly switch between different models without adjusting for model-specific differences. In this example, we use the Mistral Large 2 model on Amazon Bedrock.
Next, initialize the embedding model from Amazon Bedrock, which is used for converting document chunks into embedding vectors. For this example, we use Amazon Titan Text Embeddings V2. See the following code:
# Initialise and configure the BedrockConverse LLM with the Mistral Large 2 model and set it as the default in Settings
from llama_index.llms.bedrock_converse import BedrockConverse
from llama_index.core import Settings
llm = BedrockConverse(model="mistral.mistral-large-2407-v1:0", max_tokens = 2048)
Settings.llm = BedrockConverse(model="mistral.mistral-large-2407-v1:0", max_tokens = 2048)
# Initialise and configure the embedding model with Amazon Titan Text Embeddings V2, and set it as the default in Settings
from llama_index.embeddings.bedrock import BedrockEmbedding
embed_model = BedrockEmbedding(model_name="amazon.titan-embed-text-v2:0")
Settings.embed_model = BedrockEmbedding(model_name="amazon.titan-embed-text-v2:0")
Implement two functions to interact with the GitHub and TechCrunch APIs. The APIs shown in this post don’t require credentials. To provide clear communication between the agent and the foundation model, follow Python function best practices, including:
The following code sample shows the function that integrates with the GitHub API. After the function is created, use the FunctionTool.from_defaults() method to wrap the function as a tool and integrate it seamlessly into the LlamaIndex workflow.
See the code repository for the full code samples of the function that integrates with the TechCrunch API.
# Define a function to search GitHub repositories by topic, sorting by stars or update date, and return top results
import requests
def github_search(topic: str, num_results: int = 3, sort_by: str = "stars") -> list:
"""
Retrieve a specified number of GitHub repositories based on a given topic,
ranked by the specified criteria.
This function uses the GitHub API to search for repositories related to a
specific topic or keyword. The results can be sorted by the number of stars
(popularity) or the most recent update, with the most relevant repositories
appearing first according to the chosen sorting method.
Parameters:
-----------
topic : str
The topic or keyword to search for in GitHub repositories.
The topic cannot contain blank spaces.
num_results : int, optional
The number of repository results to retrieve. Defaults to 3.
sort_by : str, optional
The criterion for sorting the results. Options include:
- 'stars': Sort by the number of stars (popularity).
- 'updated': Sort by the date of the last update (most recent first).
Defaults to 'stars'.
Returns:
--------
list
A list of dictionaries, where each dictionary contains information
about a repository. Each dictionary includes:
- 'html_url': The URL of the repository.
- 'description': A brief description of the repository.
- 'stargazers_count': The number of stars (popularity) the repository has.
"""
url = f"https://api.github.com/search/repositories?q=topic:{topic}&sort={sort_by}&order=desc"
response = requests.get(url).json()
code_repos = [
{
'html_url': item['html_url'],
'description': item['description'],
'stargazers_count': item['stargazers_count'],
}
for item in response['items'][:num_results]
]
return code_repos
github_tool = FunctionTool.from_defaults(fn=github_search)
For arXiv and DuckDuckGo integration, we use LlamaIndex’s pre-built tools instead of creating custom functions. You can explore other available pre-built tools in the LlamaIndex documentation to avoid duplicating existing solutions.
# Import and configure the ArxivToolSpec and DuckDuckGoSearchToolSpec from LlamaIndex prebuilt tools
from llama_index.tools.arxiv import ArxivToolSpec
from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
arxiv_tool = ArxivToolSpec()
search_tool = DuckDuckGoSearchToolSpec()
api_tools = arxiv_tool.to_tool_list() + search_tool.to_tool_list()
# Consolidate all tools into one list.
api_tools.extend([news_tool, github_tool])
Next, programmatically build the RAG component using LlamaIndex to load, process, and chunk documents. store the embedding vectors in Amazon OpenSearch Serverless. This approach offers greater flexibility for advanced scenarios, such as loading various file types (including .epub and .ppt) and selecting advanced chunking strategies based on file types (such as HTML, JSON, and code).
Before moving forward, you can download some PDF documents for testing from the AWS website using the following command, or you can use your own documents. The following documents are AWS guides that help in choosing the right generative AI service (such as Amazon Bedrock or Amazon Q) based on use case, customization needs, and automation potential. They also assist in selecting AWS machine learning (ML) services (such as SageMaker) for building models, using pre-trained AI, and using cloud infrastructure.
# download test documents from below links
!wget -O docs/genai_on_aws.pdf https://docs.aws.amazon.com/pdfs/decision-guides/latest/generative-ai-on-aws-how-to-choose/generative-ai-on-aws-how-to-choose.pdf?did=wp_card&trk=wp_card#guide
!wget -O docs/ml_on_aws.pdf https://docs.aws.amazon.com/pdfs/decision-guides/latest/machine-learning-on-aws-how-to-choose/machine-learning-on-aws-how-to-choose.pdf?did=wp_card&trk=wp_card#guide
Load the PDF documents using SimpleDirectoryReader() in the following code. For a full list of supported file types, see the LlamaIndex documentation.
# use Llamaindex to load documents
from llama_index.core import SimpleDirectoryReader
loader = SimpleDirectoryReader('docs/')
documents = loader.load_data()
Next, create an Amazon OpenSearch Serverless collection as the vector database. Check the utils.py file for details on the create_collection() function.
# Create Amazon OpenSearch Serverless collection
from utils import *
import sagemaker
import random
region_name = "us-west-2"
suffix = random.randrange(1, 500)
collection_name = "llamaindex-blog-"+str(suffix)
notebook_execution_role = sagemaker.get_execution_role()
endpoint = create_collection(collection_name, notebook_execution_role)
After you create the collection, create an index to store embedding vectors:
## create an index in the collection
index_name = "pdf-rag"
create_index(index_name, endpoint, emb_dim=1024)
Next, use the following code to implement a document search system using LlamaIndex integrated with Amazon OpenSearch Serverless. It first sets up AWS authentication to securely access OpenSearch Service, then configures a vector client that can handle 1024-dimensional embeddings (specifically designed for the Amazon Titan Embedding V2 model). The code processes input documents by breaking them into manageable chunks of 1,024 tokens with a 20-token overlap, converts these chunks into vector embeddings, and stores them in the OpenSearch Serverless vector index. You can select a different or more advanced chunking strategy by modifying the transformations parameter in the VectorStoreIndex.from_documents() method. For more information and examples, see the LlamaIndex documentation.
import boto3
from llama_index.vector_stores.opensearch import OpensearchVectorStore, OpensearchVectorClient
from opensearchpy import RequestsHttpConnection, AWSV4SignerAuth
from llama_index.core import VectorStoreIndex, StorageContext
from llama_index.core.node_parser import SentenceSplitter
## integrate Amazon OpenSearch Serverless collection and index to llamaindex
dim = 1024 # Amazon Titan Embedding V2 model dimension
service = 'aoss'
credentials = boto3.Session().get_credentials()
awsauth = AWSV4SignerAuth(credentials, region_name, service)
client = OpensearchVectorClient(
endpoint,
index_name,
dim,
embedding_field="vector",
text_field="chunk",
http_auth=awsauth,
use_ssl=True,
verify_certs=True,
connection_class=RequestsHttpConnection,
)
# initialise vector store and save document chunks to the vector store
vector_store = OpensearchVectorStore(client)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
documents,
storage_context=storage_context,
transformations=[SentenceSplitter(chunk_size=1024, chunk_overlap=20)]
)
You can add a reranking step in the RAG pipeline, which improves the quality of information retrieved by making sure that the most relevant documents are presented to the language model, resulting in more accurate and on-topic responses:
from llama_index.postprocessor.bedrock_rerank import AWSBedrockRerank
reranker = AWSBedrockRerank(
top_n=3,
model_id="amazon.rerank-v1:0",# another rerank model option is: cohere.rerank-v3-5:0
region_name="us-west-2",
)
query_engine = index.as_query_engine(
similarity_top_k=10,
node_postprocessors=[reranker],
)
Use the following code to test the RAG framework. You can compare results by enabling or disabling the reranker model.
response = query_engine.query(
"In which situation should I use Amazon Bedrock over Amazon SageMaker?",
)
Next, convert the vector store into a LlamaIndex QueryEngineTool, which requires a tool name and a comprehensive description. This tool is then combined with other API tools to create an agent worker that executes tasks in a step-by-step manner. The code initializes an AgentRunner to orchestrate the entire workflow, analyzing text inputs and generating responses. The system can be configured to support parallel tool execution for improved efficiency.
# create QueryEngineTool based on the OpenSearch vector store
from llama_index.core.tools import QueryEngineTool, ToolMetadata
oss_tool = QueryEngineTool(
query_engine=query_engine,
metadata=ToolMetadata(
name="oss_guide_tool",
description="""
These decision guides help users select appropriate AWS machine learning and generative AI services based on specific needs.
They cover pre-built solutions, customizable platforms, and infrastructure options for ML workflows,
while outlining how generative AI can automate processes, personalize content, augment data, reduce costs,
and enable faster experimentation in various business contexts.""",
),
)
all_tools = api_tools +[oss_tool]
agent_worker = FunctionCallingAgentWorker.from_tools(
all_tools,
llm=llm,
verbose=True, # Set verbose=True to display the full trace of steps.
system_prompt = system_prompt,
# allow_parallel_tool_calls = True # Uncomment this line to allow multiple tool invocations
)
agent = AgentRunner(agent_worker)
response = agent.chat(text_input)
You have now completed building the agentic RAG application using LlamaIndex and Amazon OpenSearch Serverless. You can test the chatbot application with your own questions. For example, ask about the latest news and features regarding Amazon Bedrock, or inquire about the latest papers and most popular GitHub repositories related to generative AI.
In this section, you use Amazon Bedrock Knowledge Bases to build the RAG framework. You can create an Amazon Bedrock knowledge base on the Amazon Bedrock console or follow the provided notebook example to create it programmatically. Create a new Amazon Simple Storage Service (Amazon S3) bucket for the knowledge base, then upload the previously downloaded files to this S3 bucket. You can select different embedding models and chunking strategies that work better for your data. After you create the knowledge base, remember to sync the data. Data synchronization might take a few minutes.
To enable your newly created knowledge base to invoke the rerank model, you need to modify its permissions. First, open the Amazon Bedrock console and locate the service role that matches the one shown in the following screenshot.

Choose the role and add the following provided IAM permission policy as an inline policy. This additional authorization grants your knowledge base the necessary permissions to successfully invoke the rerank model on Amazon Bedrock.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "bedrock:InvokeModel",
"Resource": "arn:aws:bedrock:us-west-2::foundation-model/amazon.rerank-v1:0"
},
{
"Effect": "Allow",
"Action": "bedrock:Rerank",
"Resource": "*"
}
]
}
Use the following code to integrate the knowledge base into the LlamaIndex framework. Specific configurations can be provided in the retrieval_config parameter, where numberOfResults is the maximum number of retrieved chunks from the vector store, and overrideSearchType has two valid values: HYBRID and SEMANTIC. In the rerankConfiguration, you can optionally provide a rerank modelConfiguration and numberOfRerankedResults to sort the retrieved chunks by relevancy scores and select only the defined number of results. For the full list of available configurations for retrieval_config, refer to the Retrieve API documentation.
# Configure a knowledge base retriever using AmazonKnowledgeBasesRetriever
from llama_index.core.query_engine import RetrieverQueryEngine
from llama_index.retrievers.bedrock import AmazonKnowledgeBasesRetriever
# maximum number of relevant text chunks that will be retrieved
# If you need quick, focused answers: lower numbers (1-3)
# If you need detailed, comprehensive answers: higher numbers (5-10)
top_k = 10
# search mode options: HYBRID, SEMANTIC
# HYBRID search combines the strengths of semantic search and keyword search
# Balances semantic understanding with exact matching
# https://docs.llamaindex.ai/en/stable/examples/retrievers/bedrock_retriever/
search_mode = "HYBRID"
kb_retriever = AmazonKnowledgeBasesRetriever(
knowledge_base_id=knowledge_base_id,
retrieval_config={
"vectorSearchConfiguration": {
"numberOfResults": top_k,
"overrideSearchType": search_mode,
'rerankingConfiguration': {
'bedrockRerankingConfiguration': {
'modelConfiguration': {
'modelArn': 'arn:aws:bedrock:us-west-2::foundation-model/amazon.rerank-v1:0'
},
'numberOfRerankedResults': 3
},
'type': 'BEDROCK_RERANKING_MODEL'
}
},
}
)
kb_engine = RetrieverQueryEngine(retriever=kb_retriever)
Like the first option, you can create the knowledge base as a QueryEngineTool in LlamaIndex and combine it with other API tools. Then, you can create a FunctionCallingAgentWorker using these combined tools and initialize an AgentRunner to interact with them. By using this approach, you can chat with and take advantage of the capabilities of the integrated tools.
# Create a query tool for Bedrock Knowledge Base
kb_tool = QueryEngineTool(
query_engine=kb_engine,
metadata=ToolMetadata(
name="kb_tool",
description="""
These decision guides help users select appropriate AWS machine learning and generative AI services based on specific needs.
They cover pre-built solutions, customizable platforms, and infrastructure options for ML workflows,
while outlining how generative AI can automate processes, personalize content, augment data, reduce costs,
and enable faster experimentation in various business contexts.""",
),
)
# Update the agent to include all API tools and the Knowledge Base tool.
all_tools = api_tools +[kb_tool]
agent_worker = FunctionCallingAgentWorker.from_tools(
all_tools,
llm=llm,
verbose=True, # Set verbose=True to display the full trace of steps.
system_prompt = system_prompt,
# allow_parallel_tool_calls = True # Uncomment this line to allow multiple tool invocations
)
agent = AgentRunner(agent_worker)
response = agent.chat(text_input)
Now you have built the agentic RAG solution using LlamaIndex and Amazon Bedrock Knowledge Bases.
When you finish experimenting with this solution, use the following steps to clean up the AWS resources to avoid unnecessary costs:
This post demonstrated how to build a powerful agentic RAG application using LlamaIndex and Amazon Bedrock that goes beyond traditional question answering systems. By integrating Mistral Large 2 as the orchestrating model with external APIs (GitHub, arXiv, TechCrunch, and DuckDuckGo) and internal knowledge bases, you’ve created a versatile technology discovery and research tool.
We showed you two complementary approaches to implement the RAG framework: a programmatic implementation using LlamaIndex with Amazon OpenSearch Serverless, providing maximum flexibility for advanced use cases, and a managed solution using Amazon Bedrock Knowledge Bases that simplifies document processing and storage with minimal configuration. You can try out the solution using the following code sample.
For more relevant information, see Amazon Bedrock, Amazon Bedrock Knowledge Bases, Amazon OpenSearch Serverless, and Use a reranker model in Amazon Bedrock. Refer to Mistral AI in Amazon Bedrock to see the latest Mistral models that are available on both Amazon Bedrock and AWS Marketplace.
Ying Hou, PhD, is a Sr. Specialist Solution Architect for Gen AI at AWS, where she collaborates with model providers to onboard the latest and most intelligent AI models onto AWS platforms. With deep expertise in Gen AI, ASR, computer vision, NLP, and time-series forecasting models, she works closely with customers to design and build cutting-edge ML and GenAI applications. Outside of architecting innovative AI solutions, she enjoys spending quality time with her family, getting lost in novels, and exploring the UK’s national parks.
Preston Tuggle is a Sr. Specialist Solutions Architect with the Third-Party Model Provider team at AWS. He focuses on working with model providers across Amazon Bedrock and Amazon SageMaker, helping them accelerate their go-to-market strategies through technical scaling initiatives and customer engagement.
Manuel Rioux est fièrement propulsé par WordPress