Skip to content

Park Information

Objective

Use the AI Refinery SDK to create and run an AI system that leverages Retrieval Augmented Generation (RAG) to help users find information about different parks.

Steps

1. Embedding the data

Before initiating the RAG process, it is essential to first embed the dataset. This embedding step transforms the data into a high-dimensional vector space, allowing us to perform efficient and accurate vector searches. By embedding the data, we facilitate the retrieval of relevant information based on semantic similarity, thereby enhancing the effectiveness of the RAG process.

For this tutorial, we have already embedded our synthetic park information data and uploaded it into a vector database index. The following script shows an example of how you can embed your data:

import os

from openai import OpenAI

from air import login
from dotenv import load_dotenv


load_dotenv() # loads your ACCOUNT and API_KEY from a .env file

if __name__ == "__main__":
    # create a distiller client
    auth = login(
        account=str(os.getenv("ACCOUNT")),
        api_key=str(os.getenv("API_KEY")),
    )

    # Production endpoint
    client = OpenAI(**auth.openai())

    print("Embedding Inference")
    response = (
        client.embeddings.create(input=["hi"], model="intfloat/e5-mistral-7b-instruct")
        .data[0]
        .embedding
    )
    print(f"Embedding result dimension: {len(response)}")

After you embed your data, please upload them to your vector database index.

2. Configuration file

Next, you need to create a yaml file with all the required configuration of your project. In the yaml configuration given below, the agent named Park Ranger is a ResearchAgent (built-in) that:

  1. searches a vector database (based on your specification) to retreive the relevant information related to a user query
  2. generates a response to the user query based on the retrieved information
utility_agents:
  - agent_class: ResearchAgent
    agent_name: Park Ranger # A name that you choose for your research agent. This needs to be listed under orchestrator.
    agent_description: This agent answers all your questions about parks information, animals that resides in each park, and the statistics on the park.
    config:
      retriever_config_list: 
      # The list of configurations of the retrievers i.e., sources used by your research agent to search for the relevant information related to a user query.
        - retriever_name: "Parks internal database" # A name you choose for your retriever
          retriever_class: AzureAISearchRetriever # AzureAISearchRetriever is the type of retriever that retrieves relevent information from a vector database. 
          description: "Data Retriever for all Park information and statistics" # Optional. A description of the retrievar

          aisearch_config:
          # The details of the vector database index that the `ResearchAgent` can search over
            base_url: <your_service_url> # base url of where your vector database index is hosted>
            api_key: <your_api_key> # your azure api key for the vector database
            index: "airefinery-sdk-demo-index" # name of the vector database index
            embedding_config:
              model: "intfloat/e5-mistral-7b-instruct" # Embedding model for the retrieval, must match the config you used for embedding your data in the embedding_column
            top_k: 5 # Number of documents to retrieve
            embedding_column: "Park_NameVector,StateVector,DescriptionVector,Species_HighlightVector" # The column of the index you stored your embedded data that you want the `ResearchAgent` to search over
            content_column: # The content that we are going to retrieve
              - "Park_Name"
              - "State"
              - "Area_acres"
              - "Species_Highlight"

orchestrator:
  agent_list:
    - agent_name: "Park Ranger" # The name you chose for your ResearchAgent above.

2. Python file

Now, you can start the development of your assistant. The following code snippet creates the custom agent for performing RAG over your vector database index, 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 os

from air import login, DistillerClient
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")),
)

distiller_client = DistillerClient()
uuid = os.getenv("UUID")
project = "ParkTrivia"

distiller_client.create_project(config_path="config.yaml", project=project)

distiller_client.interactive(project=project, uuid=uuid)