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.
from aixplain.factories import IndexFactory
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.
print(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": API_KEY,
"Content-Type": "application/json"
}
INDEX_ID= "<index id>"
payload = {
"action": "search",
"data": text,
"dataType": "text",
"payload": {"top_k": 3},
}
if category == "None":
category = None
if category not in ["science", "healthcare", "art", "technology"]:
raise Exception(
f"Invalid case type: {category}, it must be one of the following: science, healthcare, art, technology"
)
if category is not None:
payload["filter"] = {
"field": "category",
"operator": "==",
"value": category}
response = requests.post(f"https://models.aixplain.com/api/v2/execute/{INDEX_ID}", headers=headers, json=payload)
if 200 <= response.status_code < 300:
raw_content = response.content.decode('utf-8')
response = json.loads(raw_content)
return response
else:
return {"error": f"Request failed with status code: {response.status_code}"}
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
agent = AgentFactory.create(
name="AI Search Assistant",
description="An agent for intelligent search over indexed knowledge sources.",
instructions=(
"""
Perform search queries over structured indexed data.
Accept natural language queries, filter based on metadata like category, and return the most relevant results.
"""
),
tools=[
AgentFactory.create_model_tool(model="<index_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. 🚀