Build an agentic RAG
This tutorial will guide you through creating, indexing, searching, and retrieving structured data. You will learn how to:
- Create an index and upsert and manage records
- Perform advanced search queries
- Deploy a fully functional agentic RAG
This is useful for applications such as knowledge retrieval, content search, and structured data management.
Step 1: Set Up an Index
Indexes provide the foundation for structured data storage and retrieval.
Define a Synthetic Dataset
Define your data to create your index.
synthetic_data = [
{"id": "doc1", "text": "Artificial intelligence is transforming industries worldwide.", "category": "technology"},
{"id": "doc2", "text": "The Mona Lisa, painted by Leonardo da Vinci, is one of the most famous artworks.", "category": "art"},
{"id": "doc3", "text": "Machine learning algorithms predict patient outcomes in hospitals.", "category": "healthcare"},
{"id": "doc4", "text": "The Earth orbits the Sun once every 365.25 days.", "category": "science"},
]
Create an Index
Create an index utility tool to add data.
index = IndexFactory.create("Knowledge Base Index", "Index for general knowledge retrieval")
Upsert Records
Upsert your records into the index for fast and efficient searches:
from aixplain.modules.model.record import Record
records = [
Record(value=item["text"], value_type="text", id=item["id"], attributes={"category": item["category"]})
for item in synthetic_data
]
index.upsert(records)
Index ID
Get the ID of your index model.
index.id
Refer to the Indexing and Search Guide for advanced indexing techniques and record management.
Step 2: Search with Model
Run the code below to search the utility model with your data. This helps you understand how the search works.
import requests
import json
def search_data(text: str, category: str):
"""
Search the index based on the given category filter.
"""
headers = {
"x-api-key": "your_api_key_here",
"Content-Type": "application/json"
}
payload = {
"action": "search",
"data": text,
"payload": {"filters": {"field": "meta.attributes.category", "operator": "==", "value": category}}
}
response = requests.post("https://models.aixplain.com/api/v1/execute/<model_id>", headers=headers, json=payload)
return response.json()
result = search_data("What is the impact of AI?", "technology")
print(result)
Step 3: Build an AI Agent
To automate queries and interactions, we create an agentic RAG.
Define the Agent
from aixplain.factories import AgentFactory
agent = AgentFactory.create(
name="AI Search Assistant",
description="Searches and retrieves relevant data from indexed knowledge sources.",
tools=[
AgentFactory.create_model_tool(model="<model_id>"),
]
)
Run the Agent
response = agent.run(data={"query": "What are the applications of quantum computing?"})
print(response.data.get("output"))
Step 4: Deploy the AI Agent
Once tested, deploy the agentic RAG!
agent.deploy()
You have successfully built an AI-powered search and indexing system. This setup can be extended for various applications, including chatbots, document retrieval, and intelligent search assistants. 🚀