Implementing RAG Pipelines using LightRAG and GPT-4o mini
    Deep Dives

    Implementing RAG Pipelines using LightRAG and GPT-4o mini

    Simplify the development of retriever-agent-generator pipelines with LightRAG - a Python library for LLM applications.

    Sachin Tripathi
    Sachin TripathiManager - AI Research at ADaSci
    Jul 18, 2024

    LightRAG is a Python library designed to simplify and streamline the development and optimization of retriever-agent-generator pipelines for LLM applications. It offers a modular and flexible approach, similar to PyTorch, allowing users to easily build and customize their AI solutions. This article explores the implementation of LightRAG and its working in detail.

    Table of Contents

    1. Understanding LightRAG
    2. RAG Essentials in LightRAG
    3. Building a QA Pipeline using LightRAG


    Understanding LightRAG

    LightRAG shares similar design patterns as Pytorch using only two primary but powerful base classes – Component for the pipeline and DataClass for data interaction with LLMs. LightRAG resembles PyTorch in the modular and composable structure for building and optimizing LLM applications. LightRag uses a ModelClient to bridge the LLM API and LightRAG pipeline and implements orchestrator components like retriever, embedder, generator and agent which are model-agnostic giving much more flexibility in LLM application development.

    LightRAG offers core building blocks that are easy to understand, transparent to debug, and flexible for customization. It also offers excellent methods for optimizing the task pipeline using logging, observability, configurability, optimizers and trainers.

    Component is a fundamental building block for constructing RAG pipelines using LightRAG. It represents a modular structure that performs a specific task within the pipeline. The modularity is crucial for creating flexible and customizable AI solutions. The DataClass in LightRAG builds on top of Python’s native dataclasses module which generates the class schema and signature to describe the data format to LLMs, convert the data instance to json or yaml string to show the data example to LLMs and load the data instance from a json or yaml string to get the data instance back to be processed in the program.

    DataClass in LightRAG

    RAG Essentials in LightRAG

    LightRAG consists of the following elements for building and optimizing the retriever-agent-generator pipelines with ease and flexibility

    Prompt

    LightRAG enables more control over the prompt by gathering different sections into one single prompt which is sent to the LLM as a single message. The default role of this message is system. <SYS></SYS> is used to represent the system message in the prompt. Jinja2 is used as the templating engine for the prompt.

    simple_prompt = r"""<SYS> You are a helpful assistant. </SYS> User: What can you help me with?"""

    ModelClient

    ModelClient is the standard protocol and a foundation class for all model inference SDKs to communicate with LightRAG internal components. Replacing the ModelClient with a generator, embedder, or retriever can make these functional components model-agnostic.

    Bridge between model inference SDKs and internal components

    Generator

    The generator is a pipeline designed to provide a unified interface and output using three subcomponents – prompt, modelclient and output_processors.

    Generator in LightRAG

    Embedder

    LightRAG supports all embedding models from OpenAI and thenelper/gtebase from Hugging Face hub.

    Retriever

    LightRAG supports FAISSRetriever, BM25Retriever, Reranker as Retriever, LLM as Retriever and PostgresRetriever.

    Retrievers Design in LightRAG

    Building a QA Pipeline using LightRAG

    Step 1 – Install and import the required libraries:

    Save Up to 50% on Learning

    Join our membership and get unlimited access to courses plus exclusive discounts on certifications.

    %pip install lightrag openai --quiet

    from lightrag.core import Component, Generator, DataClass, ModelClient
    from lightrag.components.model_client import OpenAIClient
    from lightrag.components.output_parsers import JsonOutputParser
    from dataclasses import dataclass, field
    from typing import Dict
    from google.colab import userdata
    import os

    Step 2 – Set up the OpenAI API key:

    os.environ["OPENAI_API_KEY"] = userdata.get("OPENAI_API_KEY")

    Step 3 – Create the DataClass for easing the interaction with LLM:

    @dataclass

    class QAOutput(DataClass):
     explanation : str = field(
         metadata = {"desc": "A simple solution of the query"}

     )
     example : str = field(metadata={"desc": "An example of the solution "})

    qa_template = r"""
    you are a helpful assistant.
    {{output_format_str}}
    User: {{input_str}}
    You:"""

    Step 4 – Create the task pipeline using the Component as the building block for the pipeline:

    class QA(Component):
       def __init__(self, model_client: ModelClient, model_kwargs: Dict):
           super().__init__()

           parser = JsonOutputParser(data_class=QAOutput, return_data_class=True)

           self.generator = Generator(
               model_client=model_client,
               model_kwargs=model_kwargs,
               template=qa_template,
               prompt_kwargs={"output_format_str": parser.format_instructions()},
               output_processors=parser,
           )

       def call(self, query: str):
           return self.generator.call({"input_str": query})
       async def acall(self, query: str):
           return await self.generator.acall({"input_str": query})

    Step 5 – Call the model:

    qa = QA(
       model_client = OpenAIClient(),
       model_kwargs = {"model": "gpt-4o-mini"}
    )

    qa

    Output:

    Step 6 – Run the QA pipeline by giving a prompt:

    qa("Explain the concept of RAG in brief.")

    Output:

    Step 7 – Check the prompt after formatting based on Jinja2 template:

    qa.generator.print_prompt(
           output_format_str=qa.generator.output_processors.format_instructions(),
          input_str="Explain the concept of RAG in brief",
    )

    Output:

    Final Words

    LightRAG emerges as an important tool in LLM research and optimization. Its modular architecture and integration of retrieval, embedding, generation and agent components accelerates the development of LLM applications. It offers flexibility and performance needed to rapidly experiment and iterate in the LLM landscape.

    References

    1. LightRAG GitHub Repo
    2. LightRAG Official Documentation


    sachin.tripathi@analyticsindiamag.com

    Sachin Tripathi

    Sachin Tripathi is the Manager of AI Research at AIM, with over a decade of experience in AI and Machine Learning. An expert in generative AI and large language models (LLMs), Sachin excels in education, delivering effective training programs. His expertise also includes programming, big data analytics, and cybersecurity. Known for simplifying complex concepts, Sachin is a leading figure in AI education and professional development.