Building an Agentic Retrieval-Augmented Generation (RAG) system with SmolAgents enables the development of AI agents capable of autonomous decision-making and task execution. SmolAgents, a minimalist library by Hugging Face, facilitates the creation of such agents in a concise and efficient manner. In this article, we will go step by step to build the Agentic RAG with SmolAgents.
Read on!
SmolAgents is a cutting-edge library developed by Hugging Face to simplify the creation of intelligent agents capable of performing complex tasks. Designed with a minimalist philosophy, the library encapsulates agent logic in approximately 1,000 lines of code, making it both lightweight and powerful. Its intuitive design ensures ease of use without compromising on advanced functionalities.
To create advanced agents, several elements work together:
SmolAgents integrates these elements seamlessly, saving developers from the complexity of building such systems from scratch.
It is a powerful tool for developers looking to harness the potential of agentic AI while maintaining simplicity and scalability. Whether for creating autonomous code executors or dynamic multi-step agents, SmolAgents offers the foundation for building cutting-edge applications.
Agentic RAG combines traditional Retrieval-Augmented Generation with agentic capabilities, allowing AI systems to not only retrieve and generate information but also to reason, plan, and interact with external tools dynamically. This integration enhances the system’s ability to handle complex tasks by decomposing queries, retrieving relevant information, and iteratively refining responses.
Also read: 7 Agentic RAG System Architectures to Build AI Agents
Key Benefits of Combining SmolAgents and Agentic RAG:
SmolAgents and Agentic RAG complement each other by bringing together robust retrieval-augmented generation with dynamic, agentic reasoning and interaction capabilities. This synergy empowers developers to create intelligent, autonomous systems capable of handling sophisticated tasks across various domains.
Also read: RAG vs Agentic RAG: A Comprehensive Guide
Here’s the process of building the Agentic RAG with SmolAgents: First, we load and process data from a PDF document, splitting it into manageable chunks and generating embeddings to enable semantic search. These embeddings are stored in a vector database, allowing the system to retrieve the most relevant information in response to user queries. For external queries or additional context, a search agent is employed to fetch and integrate data from external sources. This combination of document retrieval and external search capabilities ensures that the system can provide comprehensive and accurate answers to a wide range of questions.
%pip install pypdf -q
%pip install faiss-cpu -q
!pip install -U langchain-community
from langchain.document_loaders import PyPDFLoader
from langchain.vectorstores import FAISS
# The other imports are likely correct and can remain as they are:
from langchain_openai import OpenAIEmbeddings
from langchain_openai.llms import OpenAI
from langchain_openai.chat_models import ChatOpenAI
from langchain_core.documents import Document
from langchain_text_splitters import RecursiveCharacterTextSplitter
Explanation:
These imports bring in the necessary modules for:
loader = PyPDFLoader("/content/RESPONSIBLE DATA SHARING WITH DONORS.pdf")
pages = loader.load()
The PyPDFLoader loads the PDF file and extracts its pages into a list of Document objects.
for page in pages:
print(page.page_content)
Output Showing PDF Content:
splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
)
splitted_docs = splitter.split_documents(pages) # split_document accepts a list of documents
The RecursiveCharacterTextSplitter splits the document into smaller chunks:
print(len(splitted_docs))
Output:
21
print(splitted_docs[0])
This prints the number of chunks and the content of the first three chunks.
Output:
page_content='THE CENTRE FOR HUMANITARIAN DATA
1
DECEMBER 2020
1 Because there are well-established and accepted standards and
mechanisms for sharing financial information with donors, including a role
for external audits, requests for financial data are not included in this
guidance note. This guidance note deals with sensitive personal and non-
personal data.
2 Roepstorff, K., Faltas, C. and Hövelmann, S., 2020. Counterterrorism
Measures and Sanction Regimes: Shrinking Space for Humanitarian Aid
Organisations.
THE CENTRE FOR HUMANITARIAN DATA
GUIDANCE NOTE SERIES
DATA RESPONSIBILITY IN HUMANITARIAN ACTION
RESPONSIBLE DATA SHARING WITH DONORS
KEY TAKEAWAYS:
• Sharing sensitive personal and non-personal data without adequate
safeguards can exacerbate risks for crisis-affected people, humanitarian
organizations and donors.
• Donors regularly request data from the organizations they fund in order to
fulfil their obligations' metadata={'source': '/content/RESPONSIBLE DATA
SHARING WITH DONORS.pdf', 'page': 0}
Explantion:
print(splitted_docs[1])
Output:
page_content='risks for crisis-affected people, humanitarian organizations
and donors.
• Donors regularly request data from the organizations they fund in order to
fulfil their obligations and objectives. Some of these requests relate to
sensitive information and data which needs to be protected in order to
mitigate risk.
• Common objectives for data sharing with donors include: (i) situational
awareness and programme design; (ii) accountability and transparency; and
(iii) legal, regulatory, and policy requirements.
• Common constraints related to sharing data with donors include: (i) lack of
regulatory framework for responsibly managing sensitive non-personal data;
(ii) capacity gaps; and (iii) purpose limitation.
• Donors and humanitarian organizations can take the following steps to
minimize risks while maximizing benefits when sharing sensitive data: (i)
reviewing and clarifying the formal or' metadata={'source':
'/content/RESPONSIBLE DATA SHARING WITH DONORS.pdf', 'page': 0}
print(splitted_docs[2])
Output:
page_content='limitation.
• Donors and humanitarian organizations can take the following steps to
minimize risks while maximizing benefits when sharing sensitive data: (i)
reviewing and clarifying the formal or informal frameworks that govern the
collection and sharing of disaggregated data; (ii) formalizing and
standardising requests for sensitive data; (iii) investing in data
management capacities of staff and organisations; and (iv) adopting common
principles for donor data management.
INTRODUCTION
Donors have an important role in the humanitarian data ecosystem, both as
drivers of increased data collection and analysis, and as direct users of
data. This is not a new phenomenon; the need for accountability and
transparency in the use of donor funding is broadly understood and
respected. However, in recent years, donors have begun requesting data that
can be sensitive. This includes personal data about' metadata={'source':
'/content/RESPONSIBLE DATA SHARING WITH DONORS.pdf', 'page': 0}
from google.colab import userdata
openai_api_key = userdata.get('OPENAI_API_KEY')
# Set the API key for OpenAIEmbeddings
embed_model = OpenAIEmbeddings(openai_api_key=openai_api_key)
# Generate embeddings for the documents
embeddings = embed_model.embed_documents([chunk.page_content for chunk in splitted_docs])
print(f"Embeddings shape: {len(embeddings), len(embeddings[0])}")
Output:
vector_db = FAISS.from_documents(
documents = splitted_docs,
embedding = embed_model)
similar_docs = vector_db.similarity_search("what is the objective for data sharing with donors?", k=5)
print(similar_docs[0].page_content)
Output:
in capacity to fulfil donor requirements might also deter smaller and/or
local NGOs from seeking funding, undermining localization efforts.13
OBJECTIVES FOR DATA SHARING WITH DONORS
The most commonly identified objectives for donors requesting sensitive data
from partners are situational awareness and programme design; accountability
and transparency; and legal, regulatory, and policy requirements.
Situational awareness and programme design
Donors seek information and data from humanitarian organizations in order to
understand and react to changes in humanitarian contexts. This allows donors
to improve their own programme design and evaluation, prevent duplication of
assistance, identify information gaps, and ensure appropriate targeting of
assistance.
Accountability and transparency
Donors and humanitarian organizations have an obligation to account for their
activities. Data can enable donors to explain and defend funding on foreign
aid to taxpayers.
! pip -q install smolagents
! pip -q install litellm
from smolagents import Tool
class RetrieverTool(Tool):
name = "retriever"
description = "Uses semantic search to retrieve the parts of the documentation that could be most relevant to answer your query."
inputs = {
"query": {
"type": "string",
"description": "The query to perform. This should be semantically close to your target documents. Use the affirmative form rather than a question.",
}
}
output_type = "string"
def __init__(self, vector_db, **kwargs): # Add vector_db as an argument
super().__init__(**kwargs)
self.vector_db = vector_db # Store the vector database
def forward(self, query: str) -> str:
assert isinstance(query, str), "Your search query must be a string"
docs = self.vector_db.similarity_search(query, k=4) # Perform search here
return "\nRetrieved documents:\n" + "".join(
[
f"\n\n===== Document {str(i)} =====\n" + doc.page_content
for i, doc in enumerate(docs)
]
)
retriever_tool = RetrieverTool(vector_db=vector_db) # Pass vector_db during instantiation
from smolagents import LiteLLMModel, DuckDuckGoSearchTool
model = LiteLLMModel(model_id="gpt-4o", api_key = "your_api_key")
search_tool = DuckDuckGoSearchTool()
from smolagents import HfApiModel, CodeAgent
agent = CodeAgent(
tools=[retriever_tool,search_tool], model=model, max_steps=6, verbose=True
)
agent.run("Tell me about Analytics Vidhya")
Output:
Analytics Vidhya is a leading platform for professionals in Artificial
Intelligence, Data Science, and Data Engineering. It offers various
educational resources including courses, blogs, guides, and hackathons to
facilitate learning and networking. The platform is well-known for providing
mentorship, industry-relevant content, and tools for both beginners and
experienced practitioners in the AI domain.
agent_output = agent.run("what are the constraints for data sharing with donors?")
print("Final output:")
print(agent_output)
Final Output:
Constraints for data sharing with donors include:
1. Lack of regulatory framework for responsibly managing sensitive non-
personal data.
2. Capacity gaps within organizations.
3. Purpose limitation, where data should be collected only for specified,
explicit, and legitimate purposes and not processed further in a way incompatible with those purposes.
4. Need for formalization and standardization of data requests from donors.
5. Requirement for common principles and guidelines for donor data
management.
agent_output1 = agent.run("WHAT ARE THE OBJECTIVES FOR DATA SHARING WITH DONORS?")
Final Output:
The objectives for data sharing with donors include: 1. Situational awareness
and programme design - to improve understanding of contexts, enhance
programme design, prevent duplication, and ensure appropriate targeting of
assistance. 2. Accountability and transparency - to account for activities
and explain foreign aid funding to taxpayers. 3. Legal, regulatory, and
policy requirements - to ensure compliance with national and international
laws, including counter-terrorism, migration, and other legal standards.
Here we did the following:
To build Agentic Rag explore this also: A Comprehensive Guide to Building Agentic RAG Systems with LangGraph
Advantages of Using SmolAgents for Agentic RAG:
By leveraging SmolAgents, you can easily build Agentic RAG systems that are capable of complex reasoning and dynamic interaction with external data sources, enhancing the overall performance and applicability of AI solutions.
Also read: Top 4 Agentic AI Design Patterns for Architecting AI Systems
The combination of SmolAgents and Agentic RAG represents a significant advancement in building intelligent, autonomous systems. SmolAgents’ minimalist yet powerful framework, paired with the dynamic retrieval and reasoning capabilities of Agentic RAG, enables the creation of AI agents that can handle complex tasks efficiently. This synergy enhances adaptability, security, and scalability, making it ideal for applications in research, decision-making, and automation. Together, they pave the way for next-generation AI systems that are both intelligent and autonomous.
Explore the Agentic AI Pioneer Program to deepen your understanding of Agent AI and unlock its full potential. Join us on this journey to discover innovative insights and applications!