Retrieval Augemented Generation (RAG) with Web Search¶
Objective¶
Use the AI Refinery SDK to create and run a RAG based AI system to help users shop for electronics by utilizing:
SearchAgent
: Searches over Google based on user queries.CustomAgent
: Reads a CSV file containing inventory information.
Steps¶
1. Configuration file¶
As a first step, you simply need to create a yaml file with all the required configuration.
-
The agent named Online Shopper is a
SearchAgent
(built-in) that searches the internet to find the providers and prices of electronics. -
The Inventory Reader is a custom agent that helps you check the inventory that you have in stock. This agent expects the data of your stock to be in a csv file named electronic_inventory.csv.
orchestrator:
agent_list:
- agent_name: Online Shopper
- agent_name: Inventory Reader
utility_agents:
- agent_class: SearchAgent
agent_name: Online Shopper
agent_description: This agent helps you find on the internet the provider and prices of electronics
- agent_class: CustomAgent
agent_name: Inventory Reader
agent_description: This agent helps you check the inventory of our offices
2. Python file¶
Now, you can start the development of your assistant. The following code snippet creates the custom agent for checking your inventory, logs in to the AI Refinery service through the DistillerClient
, creates the project using the yaml
configuration above, and runs the project in interactive mode.
import csv
import os
from typing import Optional
from air import DistillerClient, login
from dotenv import load_dotenv
load_dotenv() # loads your ACCOUNT and API_KEY from a `.env` file
login(
account=str(os.getenv("ACCOUNT")),
api_key=str(os.getenv("API_KEY")),
)
async def inventory_check(query: Optional[str] = None) -> str:
"""
Reads a CSV file and returns its contents as a string.
"""
inventory_path = "electronic_inventory.csv"
content = ""
with open(inventory_path, mode="r", newline="") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
content += ", ".join(row) + "\n"
return content.strip()
distiller_client = DistillerClient()
uuid = "myuser"
project = "warehouse_management"
distiller_client.create_project(
config_path="config.yaml", project=project
) # "config.yaml" contains the yaml configuration above
distiller_client.interactive(
project=project,
uuid=uuid,
executor_dict={"Inventory Reader": inventory_check},
)