RAG vs. GraphRAG: A New Era of Information Retrieval and Practical Implementation

Akash A Desai
7 min readAug 22, 2024

--

What is RAG?

Retrieval-Augmented Generation (RAG) is an AI framework that combines traditional information retrieval systems with large language models (LLMs). It enhances generative AI by integrating external knowledge sources, making responses more accurate, up-to-date, and relevant.

How Does Retrieval-Augmented Generation (RAG) Work?

RAG operates through a few key steps to enhance the performance of generative AI:

  1. Retrieval and Pre-processing: RAG utilizes advanced search algorithms to access external data, like websites, knowledge bases, and databases. The retrieved information is then pre-processed — cleaned, tokenized, and filtered — to ensure it’s ready for use.
  2. Generation: The pre-processed data is integrated into the pre-trained LLM, enriching its context. This helps the LLM generate responses that are more accurate, relevant, and informative.

Why Use RAG?

RAG provides several advantages over traditional text generation methods, particularly when accuracy and relevance are essential:

  • Access to Real-Time Data: Keeps responses up-to-date by pulling in the latest information.
  • Ensured Accuracy: Utilizes verified sources to maintain factual correctness.
  • Contextual Precision: Customizes responses to align perfectly with the query context.
  • Consistency: Minimizes contradictions by grounding responses in accurate data.
  • Efficient Retrieval: Rapidly locates relevant documents through advanced vector databases.
  • Enhanced Chatbots: Elevates chatbot interactions by integrating external knowledge for more comprehensive and context-aware responses.

RAG was designed to solve those problems, but there are scenarios where its basic form falls short. For instance:

  • Difficulty in Connecting Information: Baseline RAG struggles with questions that require linking different pieces of information to form a cohesive answer.
  • Challenges with Big Picture Understanding: It has trouble comprehending and summarizing large datasets or documents holistically.

To overcome these limitations, researchers are exploring advanced techniques. Microsoft Research introduced GraphRAG, an approach that builds a knowledge graph from a given dataset. This graph, enhanced with summaries and machine learning insights, is used to improve the quality of responses during query processing. GraphRAG significantly enhances the ability to answer complex questions by effectively synthesizing and reasoning over diverse information, surpassing the performance of traditional methods.

The GraphRAG Process 🤖

GraphRAG is an advanced version of RAG that uses knowledge graphs to enhance the retrieval and generation process. It structures data hierarchically, allowing for better reasoning over complex queries and improving the accuracy and contextual understanding of responses.

How GraphRAG Solves RAG Problems

GraphRAG addresses the limitations of RAG by introducing a structured approach to information retrieval. It constructs a knowledge graph from raw text, which captures entities, relationships, and key claims. This allows GraphRAG to reason over multiple connections and provide more accurate answers to complex queries.

How GraphRAG Works

Indexing: Raw text is divided into TextUnits and analyzed to extract entities and relationships. These TextUnits serve as the foundational elements for further processing.

Graph Construction: A knowledge graph is built, organizing entities and relationships hierarchically. The graph is refined through hierarchical clustering using the Leiden technique, which groups related entities into communities. This helps in visualizing and understanding complex datasets holistically.

Query Processing: During a query, GraphRAG utilizes the knowledge graph to retrieve relevant information. It operates in two primary query modes:

  • Global Search: Leverages community summaries to answer broad, holistic questions about the dataset.
  • Local Search: Focuses on specific entities, exploring their neighbors and related concepts for detailed reasoning.

we are going to see both in practice

LLM Integration: The retrieved data is fed into a large language model (LLM), which generates responses based on the structured knowledge from the graph.

Prompt Tuning: To achieve the best results with GraphRAG, it’s recommended to fine-tune your prompts according to the guidelines provided in the Prompt Tuning Guide. This step ensures that the model is optimally tailored to your specific data and query needs.

When to Use GraphRAG

Complex Queries: Ideal for scenarios where answers require reasoning across multiple steps or connections within the data.

High Precision Needed: Essential in situations where factual accuracy is critical, such as in legal or scientific contexts.

Large-Scale Knowledge Bases: Perfect for managing and querying extensive, interconnected datasets efficiently.

Dynamic Information: Best suited for environments where data is frequently updated or constantly evolving.

Starting the Coding Section:

In this section, we will walk through the practical steps of implementing RAG



import bs4
from langchain import hub
from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import CharacterTextSplitter
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import LanceDB
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from lancedb.rerankers import LinearCombinationReranker

loader = TextLoader("/content/HTE_gst_scheme.txt")
documents = loader.load()

text_splitter = RecursiveCharacterTextSplitter(chunk_size=1100, chunk_overlap=100)
splits = text_splitter.split_documents(docs)
reranker = LinearCombinationReranker(weight=0.3)
vectorstore = LanceDB.from_documents(documents=splits, embedding=OpenAIEmbeddings(),reranker=reranker)

# Retrieve and generate using the relevant snippets of the blog.
retriever = vectorstore.as_retriever()
prompt = hub.pull("rlm/rag-prompt")

def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)

llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)

rag_chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)

query = "what are the different Payment Methods in International Trade"

rag_chain.invoke(query)
The different payment methods in international trade include using Incoterms,
which are selling terms agreed upon by the buyer and seller, and can involve
tasks, costs, and risks. Payment can also be facilitated through the use
of Electronic Bank Realisation Certificates (e-BRC) issued by banks to
confirm payment for exported goods or services. Additionally, exporters
can utilize pre-shipment and post-shipment finance from commercial banks
to complete export transactions.

Implementing GraphRAG

To get started with GraphRAG, follow these steps:

  1. Install GraphRAG
    Begin by installing the GraphRAG package using the following command:
!pip install graphrag

Step 1: Prepare Your Dataset

  1. Create and Organize Your Dataset
    Start by creating your dataset and saving it in the following directory:
  • /content/rag_exim/input/info.txt
  1. Supported File Types
    Currently, GraphRAG supports only .txt and .csv file formats. If you need to add support for CSV files, you'll need to modify the settings.yml file accordingly.
  2. Download and Format Your Data
    Ensure that your dataset is properly downloaded and formatted for seamless processing by GraphRAG.

Step 2: Initialize the Dataset for indexing

This command sets up the initial structure for indexing your dataset.

!python3 -m graphrag.index --init --root /content/rag_exim

Step 3. Indexing the Dataset


!python3 -m graphrag.index --root /content/rag_exim

Step 4. Execute the query

As we said we have 2 options global & local query execution

Global query execute


!python3 -m graphrag.query --root /content/rag_exim --method global "how to start a practical export business"

Local query execute

!python3 -m graphrag.query --root /content/rag_exim --method local "how to start a practical export business"

output:

Report records: 57
# Summary of Different Payment Methods in International Trade

In international trade, various payment methods play a crucial role in facilitating transactions between buyers and sellers. Analysts have highlighted key payment methods and their implications for risk management and financial security. Here is a synthesis of their insights:

## Letter of Credit (LC)
- **Description**: Letter of Credit involves a bank guaranteeing payment to the seller upon presentation of compliant documents.
- **Benefits**: Reduces risks for both parties involved in the transaction.
- **Implications**: Ensures trust in cross-border transactions and compliance with financial obligations.
- **References**: [Data: Reports (15, 29, 41, 35, 28, +more)].

## Advance Payment
- **Description**: Buyer pays the seller before goods are shipped, reducing seller's risk but increasing buyer's risk.
- **Benefits**: Offers security to the seller.
- **Implications**: Buyer faces higher risk in case of non-delivery or disputes.
- **References**: [Data: Reports (20, 25, 17, 23, 40, +more)].

## Documentary Collections
- **Description**: Involves banks facilitating payment through the transfer of shipping documents.
- **Benefits**: Provides security for both buyer and seller.
- **Implications**: Payment made upon acceptance or against documents.
- **References**: [Data: Reports (15, 29, 35, 21, +more)].

## Open Account
- **Description**: Goods are shipped before payment is due, placing more risk on the seller.
- **Benefits**: Offers flexibility to the buyer.
- **Implications**: Seller faces higher risk but buyer gains flexibility.
- **References**: [Data: Reports (20, 25, 17, 23, 40, +more)].

## Consignment
- **Description**: Seller ships goods to the buyer who pays upon sale or after resale.
- **Benefits**: Provides flexibility in payment.
- **Implications**: Seller retains ownership until goods are sold, posing risks for both parties.
- **References**: [Data: Reports (35, +more)].

## Cash in Advance
- **Description**: Buyer pays before goods are shipped, offering high security to the seller.
- **Benefits**: Highest level of security for the seller.
- **Implications**: Least security for the buyer.
- **References**: [Data: Reports (17, 23, 40, 42, +more)].

## Implications of Incoterms
- **Description**: Incoterms define responsibilities of buyers and sellers, influencing global trade practices.
- **Benefits**: Standardizes trade practices, minimizes disputes.
- **Implications**: Crucial for negotiating contracts and ensuring transparent trading.
- **References**: [Data: Reports (+more)].

Understanding these payment methods and their implications is essential for parties engaged in international trade to manage risks, negotiate contracts effectively, and ensure smooth transactions.
____________________________________
LLM calls: 11. LLM tokens: 38680

All source codes are available in our collab you can check them here

Conclusion

You can clearly see the difference in output quality between the two methods. GraphRAG is able to extract deeper insights from graph nodes, whereas traditional RAG struggles in this area. However, this comes at a higher cost — GraphRAG requires 11 LLM calls, leading to increased token usage.

Both methods have their trade-offs, but there are ways to enhance their performance. For GraphRAG, using a local LLaMA model could reduce costs, while RAG has various optimization techniques available. Check out our vector recipe repository for more details on improving both approaches.

--

--

Akash A Desai

Data Scientist with 4+ years of exp ! open source !Vision ! Generative AI ! Vectordb ! llms