Skip to main content

Build an agentic RAG to talk to your files

In this tutorial, we will demonstrate how to create an Agentic Retrieval-Augmented Generation (RAG) using aiXplain's platform.
You will use:

  • aiR: aiXplain indexing and retrieval engine
  • Quadrant: aiXplain's cloud datastore
  • Docling: A tool to transform and read PDF documents.

Prepare your Data

Create a list of documents, each with a unique ID, public URLs for the document and metadata fields that can be used for filtering.

data = [
{
"id": "doc1",
"pdf_url": "https://talk-to-pdfs.s3.us-east-2.amazonaws.com/Example+(2).pdf",
"author": "Person 1",
},
{
"id": "doc2",
"pdf_url": "https://talk-to-pdfs.s3.us-east-2.amazonaws.com/Drylab+(1).pdf",
"author": "Person 2",
},
{
"id": "doc3",
"pdf_url": "https://talk-to-pdfs.s3.us-east-2.amazonaws.com/Placeholder+Documentation.pdf",
"author": "Person 3",
},
]

Load Docling Model

Load the Docling model to extract text from your documents.

info

The Docling model may face cold starts, please activate before trying to index.

from aixplain.factories import AgentFactory, ModelFactory, IndexFactory

docling = ModelFactory.get("677bee6c6eb56331f9192a91") # Docling ID

Create Index

Use IndexFactory.create() to initialize a new index. Supply a name and description, and optionally specify an embedding_model to tailor your vector space. For a full list of embedding options, see this guide.

from aixplain.modules.model.record import Record

index = IndexFactory.create(name="PDF Files", description="Index for technical PDF files")
index.id

Transform and Index your Documents

  • Extract text from each PDF via the Docling tool.

  • Wrap extracted text in Record with attributes for metadata.

  • Batch upsert records.

records = []

for doc in data:
text = docling.run(doc["pdf_url"]).data
record = Record(id=doc["id"], value=text, attributes={"author": doc["author"], "source": doc["pdf_url"]})
records.append(record)

for i in range(0, len(records), 500):
index.upsert(records[i: i + 500])

print("Total documents in index:", index.count())

Test your Index

Perform semantic searches using queries.

response = index.search("Academy of Motion Picture Arts and Sciences")
response

Search with a filter

Use IndexFilter to narrow results by metadata attributes.

filters = [
IndexFilter(field="author", value="Person 3", operator=IndexFilterOperator.EQUALS),
]

response = index.search("placeholder", filters=filters)
response

Create an Agentic RAG

Use AgentFactory.create() to define an agent with a name, description, your custom instructions, and the index as part of the tools available to the agent.

note

Agents cannot access the metadata of the index records.

agent_instructions = '''
You are an AI assistant specialized in retrieving and providing information from PDF documents.

ONLY use your tools to answer questions.
DO NOT user your internal knowladge to answer ANY question.

PROCESS:
1. Use the search_tool to query the PDF index
2. Extract relevant content
3. Present substantive information and include document URL from metadata

'''

agent = AgentFactory.create(
name="Technical PDF Agent",
description="An agent that uses PDFs for information",
instructions=agent_instructions,
tools=[AgentFactory.create_model_tool(model=index.id)]
)

Run the Agent

response = agent.run("The Elements of Style by Strunk")
print(response.data.output)
Show output

Use session_id to maintain conversation

response = agent.run("Who is the author?", session_id=response.data.session_id)
print(response.data.output)
Show output

Deploy your Agent

By default, agents are saved as drafts available for 24 hours. Once you are happy with your agent's responses, deploy your agent.

agent.deploy()