RAG – in a 5 lines

RAG is taking over the world as an augmentation or even replacement to traditional search. It has claims like context augmented leveraging LLM. Through new techniques like LLM and vector search, it offers a new way to offer better quality responses to users. This article is meant to demonstrate how RAG works in the bare minimal example.

Llama-index is one of the the most popular libraries these days for building a rag and its has its famous 5-line starter:

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader

documents = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("Some question about the data should go here")
print(response)

Before running the code, we need few preparation steps:

OpenAI Key

    To leverage the LLM part, the easiest way is to just call openai api, once you create a key and place it under .env file, you can load into the environment variable as a best practice.

    import os 
    import dotenv 
    dotenv.load_dotenv()
    print(os.environ['OPENAI_API_KEY'])
    

    Books Data

    We need some toy data to work with, Gutenberg books is a good place to get some free books. The code below will download the first 10 books from gutenberg project by manipulating the url. The books will be stored in a local directory – data. (precreate it if it does not exist yet)

    number_of_books = 10
    for i in range(1, number_of_books+1):
        url = f'https://www.gutenberg.org/cache/epub/{i}/pg{i}.txt'
        response = requests.get(url)
        if response.status_code == 200:
            with open(f'./data/pg{i}.txt', 'w') as f:
                f.write(response.text)
        else:
            print(f'Failed to download {url}')
    

    Load the Data and Index

    docs = SimpleDirectoryReader('data').load_data()
    index = VectorStoreIndex.from_documents(docs)
    query_engine = index.as_query_engine()
    

    The three lines above is all you need to load the data, create an index and set the index to be used as query engine. The index initialization took me a while (1minute and 30 seconds) to index the 10 books. So let’s put it into action.

    Query

    The first few books that we downloaded happen to literatures at the establishment of the US, which also includes the US consistitution, so lets try to ask a question and see how it works.

    >> response = query_engine.query("what does the congress of the US consists of?")
    >> response
    
    Response(response='The Congress of the United States consists of a Senate and House of Representatives.', source_nodes=
    

    voila, we got the answer that we need!

    In the next post, we will dive into each of the building blocks and understand what is happening behind the scene.

    One thought on “RAG – in a 5 lines

    Leave a comment