This tutorial introduces how to make an "plan-and-execute" style agent, which LangGraph Describe the process of utilizing and implementing step by step. The "plan-and-execute" strategy is an approach that first establishes a long-term plan when performing complex tasks, then executes that plan step by step and re-modifies the plan as needed.
What is Plan-and-Execute?
"plan-and-execute" is an approach with the following characteristics:
Long-term planning : Develop a long-term plan to draw big pictures before performing complex tasks.
Step-by-step execution and re-planning : You can run the plan you have made step by step, review each step to see if the plan is still valid, and then modify it.
Advantages : One. Explicit long-term planning : Even a strong LLM can have a hard time dealing with long-term plans at once. By explicitly establishing a long-term plan, more stable progress is possible. 2. Efficient model use : You can optimize resource consumption by using a larger/stronger model in the planning phase, and a relatively small/weak model in the execution phase.
Main contents
Tool definition : Define tools to use
Define executing agents : Create an agent that runs real jobs
Status definition : Define the agent's state
Planning phase : Create steps to make long-term plans
Replanning phase : Create steps to re-correct the plan according to the progress of the work
Graph creation and execution : Create and run graphs linking these steps
From now on, we will follow each step and take a closer look at how to implement the "plan-and-execute" agent with LangGraph.
Preferences
Copy
Copy
Copy
Copy
Define model names to use for practice
Copy
Copy
Tool definition
Define the tools to use first. In this simple example Tavily We will use the built-in search tool provided through. However, it is also very easy to make your own tools.
# Configuration file for managing API keys as environment variables
from dotenv import load_dotenv
# Load API key information
load_dotenv()
True
# Set up LangSmith tracking. https://smith.langchain.com
# !pip install -qU langchain-teddynote
from langchain_teddynote import logging
# Enter a project name.
logging.langsmith("CH17-LangGraph-Use-Cases")
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
from langchain_core.prompts import ChatPromptTemplate
# prompt definition
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a helpful assistant. Answer in Korean.",
),
("human", "{messages}"),
]
)
# LLM definition
llm = ChatOpenAI(model=MODEL_NAME, temperature=0)
# Creating a ReAct Agent
agent_executor = create_react_agent(llm, tools, state_modifier=prompt)
# running the agent
agent_executor.invoke(
{"messages": [("user", "Explain about the Langchain Korean tutorial")]}
)
'1k','Gon','G' 28,'prompt_tokens': 155,'total_tokens': 183,'complete_tokens_details': {'accepted_prediction_tokens': 0,'audio_tokens': 0,'reasoning_tokens': 0, {'audio_tokens': 0,'cached_tokens': 0}},'model_name':'gpt-4o-2024-08-06','system_fingerprint':'fp_d28bcae782','finish_reason'audio': 0,'cache_read': 0},'output_token_details': {'audio': 0,'reasoning': 0}}), ToolMessage (content='[{"title": "langchain md at main Β· teddylee777/langchain-kr - GitHub", "url": "https://github.com/teddylee777/langchain-kr/blob/main/README.md", "content": "π LangChain Korean Tutorial π Korean tutorial based on LangChain official Document, Cookbook, and other practical examples Korean tutorial.
(...sway...)
References can be found in the list of sources at the bottom of this document.\\nπ Source\\nπ Additional Resources\\nπ Getting Started\\n Before starting the tutorial, it is recommended to have basic knowledge related to LangChain. You can get basic information through the link to the source above.\\nπ‘ Country Viewions\\n If you want to contribute to this tutorial, please send a full list at any time, or register your issue to share your opinion. All contributions are of great help to the development of this project. π\nAbout\\nLangChain is a Korean tutorial based on official Document, Cookbook, and other practical examples. This tutorial will help you learn how to use LangChain more easily and effectively.
import operator
from typing import Annotated, List, Tuple
from typing_extensions import TypedDict
# swtate definition
class PlanExecute(TypedDict):
input: Annotated[str, "User's input"]
plan: Annotated[List[str], "Current plan"]
past_steps: Annotated[List[Tuple], operator.add]
response: Annotated[str, "Final response"]
from pydantic import BaseModel, Field
from typing import List
# Plan model definition
class Plan(BaseModel):
"""Sorted steps to execute the plan"""
steps: Annotated[List[str], "Different steps to follow, should be in sorted order"]
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
# create a prompt template for planning
planner_prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"""For the given objective, come up with a simple step by step plan. \
This plan should involve individual tasks, that if executed correctly will yield the correct answer. Do not add any superfluous steps. \
The result of the final step should be the final answer. Make sure that each step has all the information needed - do not skip steps.
Answer in Korean.""",
),
("placeholder", "{messages}"),
]
)
planner = planner_prompt | ChatOpenAI(
model=MODEL_NAME, temperature=0
).with_structured_output(Plan)
Plan (steps=['Understands the definition and purpose of LangGraph','Lists the main advantages of LangGraph','Lists the main disadvantages of LangGraph','Explains the reason for using LangGraph' ])
from typing import Union
class Response(BaseModel):
"""Response to user."""
# user response
response: str
class Act(BaseModel):
"""Action to perform."""
# What to do: "Response", "Plan". Use Response if you are responding to the user, or Plan if you need to use additional tools.
action: Union[Response, Plan] = Field(
description="Action to perform. If you want to respond to user, use Response. "
"If you need to further use tools to get the answer, use Plan."
)
# Define prompts to re-plan
replanner_prompt = ChatPromptTemplate.from_template(
"""For the given objective, come up with a simple step by step plan. \
This plan should involve individual tasks, that if executed correctly will yield the correct answer. Do not add any superfluous steps. \
The result of the final step should be the final answer. Make sure that each step has all the information needed - do not skip steps.
Your objective was this:
{input}
Your original plan was this:
{plan}
You have currently done the follow steps:
{past_steps}
Update your plan accordingly. If no more steps are needed and you can return to the user, then respond with that. Otherwise, fill out the plan. Only add steps to the plan that still NEED to be done. Do not return previously done steps as part of the plan.
Answer in Korean."""
)
# Create a Replanner
replanner = replanner_prompt | ChatOpenAI(
model=MODEL_NAME, temperature=0
).with_structured_output(Act)
from langchain_core.output_parsers import StrOutputParser
# Generate and return a plan based on user input
def plan_step(state: PlanExecute):
plan = planner.invoke({"messages": [("user", state["input"])]})
# Returns a list of step in the generated plan.
return {"plan": plan.steps}
# Uses an agent executor to perform a given task and return the result.
def execute_step(state: PlanExecute):
plan = state["plan"]
# Convert the plan to a string and number each step.
plan_str = "\n".join(f"{i+1}. {step}" for i, step in enumerate(plan))
task = plan[0]
# Format the current task to be executed and pass it to the agent.
task_formatted = f"""For the following plan:
{plan_str}\n\nYou are tasked with executing [step 1. {task}]."""
# Perform tasks and receive results via the agent executor
agent_response = agent_executor.invoke({"messages": [("user", task_formatted)]})
# Returns a dictionary containing the previous step and its results.
return {
"past_steps": [(task, agent_response["messages"][-1].content)],
}
# Update the plan or return a final response based on the results of the previous step.
def replan_step(state: PlanExecute):
output = replanner.invoke(state)
# When a response is returned to the user
if isinstance(output.action, Response):
return {"response": output.action.response}
# Returns a list of steps in the plan if additional steps are required.
else:
next_plan = output.action.steps
if len(next_plan) == 0:
return {"response": "No more steps needed."}
else:
return {"plan": next_plan}
# A function that determines whether the agent's execution has ended.
def should_end(state: PlanExecute):
if "response" in state and state["response"]:
return "final_report"
else:
return "execute"
final_report_prompt = ChatPromptTemplate.from_template(
"""You are given the objective and the previously done steps. Your task is to generate a final report in markdown format.
Final report should be written in professional tone.
Your objective was this:
{input}
Your previously done steps(question and answer pairs):
{past_steps}
Generate a final report in markdown format. Write your response in Korean."""
)
final_report = (
final_report_prompt
| ChatOpenAI(model=MODEL_NAME, temperature=0)
| StrOutputParser()
)
def generate_final_report(state: PlanExecute):
past_steps = "\n\n".join(
[
f"Question: {past_step[0]}\n\nAnswer: {past_step[1]}\n\n####"
for past_step in state["past_steps"]
]
)
response = final_report.invoke({"input": state["input"], "past_steps": past_steps})
return {"response": response}
Copyfrom langgraph.graph import StateGraph, START, END
from langgraph.checkpoint.memory import MemorySaver
# Create a graph
workflow = StateGraph(PlanExecute)
# node definition
workflow.add_node("planner", plan_step)
workflow.add_node("execute", execute_step)
workflow.add_node("replan", replan_step)
workflow.add_node("final_report", generate_final_report)
# edge definition
workflow.add_edge(START, "planner")
workflow.add_edge("planner", "execute")
workflow.add_edge("execute", "replan")
workflow.add_edge("final_report", END)
# Conditional Edge: Using a function to determine whether to terminate after replanning
workflow.add_conditional_edges(
"replan",
should_end,
{"execute": "execute", "final_report": "final_report"},
)
# Compile the graph
app = workflow.compile(checkpointer=MemorySaver())
==================================================
π Node: planner π
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
One. Describe the concept of Naive RAG.
2. Describe the concept of Modular RAG.
3. Compare the difference between Naive RAG and Modular RAG.
4. Modular RAG explains the benefits of production level.
==================================================
==================================================
π Node: agent in [execute] π
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
================================== Ai Message ==================================
Naive RAG (Retrieval-Augmented Generation) is a technique that combines information retrieval and natural language generation (NLG), which is a way to generate answers based on this after retrieving relevant information from external databases or documents to generate answers to a given question. Naive RAG consists mainly of two main components:
One. **Information Finder (Retriever)**: Search for documents related to questions from external databases. At this stage, mainly BM25, TF-IDF, or the latest deep learning-based search models are used.
2. **Generator**: Generates natural language answers based on the documents retrieved. At this stage, language models based primarily on Transformer are used.
The main feature of Naive RAG is that it directly utilizes the information in the searched documents to generate answers. This allows the model to take advantage of the latest information or external knowledge not included in the training data.
==================================================
==================================================
π Node: execute π
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
('One. Describe the concept of Naive RAG.','Naive RAG (Retrieval-Augmented Generation) is a technique that combines information retrieval and natural language generation (NLG), after searching for relevant information in an external database or document to generate answers to a given question, Based on this, it is a way to generate an answer. Naive RAG consists mainly of two main components:\n\n1. **Information Finder (Retriever)**: Search for documents related to questions from external databases. At this stage, mainly BM25, TF-IDF, or the latest deep learning-based search model is used.\n\n2. **Generator**: Generates natural language answers based on the documents retrieved. At this stage, language models based primarily on Transformer are used. The main feature of \n\nNaive RAG is that it directly utilizes the information from the searched documents to generate answers. This allows the model to utilize the latest information or external knowledge not included in the training data.')
==================================================
==================================================
π Node: replan π
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
2. Describe the concept of Modular RAG.
3. Compare the difference between Naive RAG and Modular RAG.
4. Modular RAG explains the benefits of production level.
==================================================
==================================================
π Node: agent in [execute] π
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
================================== Ai Message ==================================
Modular RAG (Modular RAG) is a system that uses a modular approach to perform information retrieval and generation tasks. RAG stands for "Retrieval-Augmented Generation", aiming to achieve better performance by combining two main components: search and creation.
The concept of modular RAG is as follows:
One. **Modular Structure**: Modular RAG is designed by separating each step of search and creation into an independent module. This increases the flexibility and scalability of the system by allowing each module to be individually optimized or replaced.
2. **Search module**: This module serves to retrieve relevant information from large databases or sets of documents. The information retrieved is provided as input to the creation module.
3. **Create module**: This module generates natural language responses based on information provided by the search module. The generation module uses a language model based primarily on deep learning to understand the information entered and generate appropriate responses.
4. **Integration and Adjustment**: Modular RAG improves the quality of the final response by adjusting the interaction between the search and creation modules. This allows you to generate responses that reflect the context of the information retrieved.
This modular approach offers the advantage of being able to develop each part of the system independently, and provides flexibility that can be easily applied to various domains.
==================================================
==================================================
π Node: execute π
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
('2. Describe the concept of Modular RAG','Modular RAG (Modular RAG) is a system that uses a modular approach to perform information retrieval and generation tasks. RAG stands for "Retrieval-Augmented Generation", aiming to achieve better performance by combining two main components: search and creation.\n\n Modular RAG's concept is:\n\n1. **Modular Structure**: Modular RAG is designed by separating each step of search and creation into an independent module. This increases the flexibility and scalability of the system by allowing each module to be individually optimized or replaced.\n\n2. **Search module**: This module serves to retrieve relevant information from large databases or sets of documents. The information retrieved is provided as input to the creation module.\n\n3. **Create module**: This module generates natural language responses based on information provided by the search module. The generation module uses a language model based primarily on deep learning to understand the information entered and generate appropriate responses.\n\n4. **Integration and Adjustment**: Modular RAG improves the quality of the final response by adjusting the interaction between the search and creation modules. This allows you to generate responses that reflect the context of the information retrieved.\n\n This modular approach offers the advantage of developing each part of the system independently, and provides flexibility to easily apply to various domains.')
==================================================
==================================================
π Node: replan π
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
3. Compare the difference between Naive RAG and Modular RAG.
4. Modular RAG explains the benefits of production level.
==================================================
==================================================
π Node: agent in [execute] π
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
================================== Ai Message ==================================
Let's compare the difference between Naive RAG and Modular RAG.
One. **Structural differences**:
-**Naive RAG**: Naive RAG (hereinafter referred to as RAG) is a simple structure, a combination of a pre-learned language model and a search system to generate answers to questions. This structure is mainly composed of fixed pipelines, which can reduce flexibility.
-**Modular RAG**: Modular RAG has a more flexible structure, and you can configure the system by combining various modules. Each module can be developed and improved independently, which has high system scalability and maintainability.
2. **Expansion and flexibility**:
-**Naive RAG**: Fixed pipeline may limit the addition or change of new features.
-**Modular RAG**: The interface between the modules is clearly defined, allowing you to easily add new features or replace existing ones.
3. ** Development and maintenance**:
-**Naive RAG**: Since the entire system needs to be managed in one unit, changes in certain parts can affect the entire system.
-**Modular RAG**: Each module is managed independently, so you can minimize the impact of changes in a particular module on other modules.
4. ** Performance optimization**:
-**Naive RAG**: Performance optimization can be difficult, and optimization for specific tasks may be limited.
-**Modular RAG**: Each module can be optimized individually, improving the performance of the entire system more effectively.
Due to these differences, Modular RAGs are beneficial for meeting more complex requirements and increasing applicability in a variety of environments.
==================================================
==================================================
π Node: execute π
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
('3. Compare the difference between Naive RAG and Modular RAG.','Let's compare the difference between Naive RAG and Modular RAG.\n\n1. **Structured Differences**:\n -**Naive RAG**: Naive RAG (hereinafter referred to as RAG) is a simple structure, a combination of a pre-learned language model and a search system to generate answers to questions. This structure is mainly composed of fixed pipelines, which can reduce flexibility.\n -**Modular RAG**: Modular RAG has a more flexible structure, and you can configure the system by combining various modules. Each module can be developed and improved independently, which has high system scalability and maintainability.\n\n2. **Expansion and flexibility**:\n -**Naive RAG**: Fixed pipeline may limit the addition or change of new features.\n -**Modular RAG**: The interface between modules is clearly defined, new You can easily add features or replace existing features.\n\n3. ** Development and maintenance **:\n - **Naive RAG**: The entire system needs to be managed in one unit, so changes in certain parts can affect the entire system.\n - **Modular RAG**: Each module is managed independently, so you can minimize the impact of changes in a particular module on other modules.\n\n4. ** Performance optimization**:\n - **Naive RAG**: Performance optimization can be difficult, and optimization for specific tasks may be limited. \n -**Modular RAG**: Each module can be optimized individually, improving the performance of the entire system more effectively.\n\n Due to these differences, Modular RAG meets more complex requirements, and in a variety of environments. It is advantageous to increase the applicability.')
==================================================
==================================================
π Node: replan π
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
4. Modular RAG explains the benefits of production level.
==================================================
==================================================
π Node: agent in [execute] π
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
================================== Ai Message ==================================
Modular RAG (modular RAG, Retrieval-Augmented Generation) offers several advantages in the production environment. Here are the main benefits:
One. **Expandability**: Modular RAG can easily integrate various data sources and modules to increase system scalability. This gives you flexibility when adding new data sources or features.
2. **Ease of maintenance**: The modular structure allows each component to be managed and updated independently for easy maintenance. This increases the stability of the system and allows you to respond quickly in the event of a problem.
3. ** Performance optimization**: Each module is optimized for a specific function, which can improve the performance of the entire system. For example, the search module is optimized for search, and the creation module can be optimized for text generation.
4. **Reusability**: Modular structure allows certain functions to be reused by other projects or systems. This contributes to shortening development time and reducing costs.
5. **Flexible deployment**: Modules can be distributed individually for different environments, supporting various deployment options such as cloud or onpremises.
These benefits help Modular RAG work effectively in complex production environments.
==================================================
==================================================
π Node: execute π
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
('4. Modular RAG explains the benefits of production level.','Modular RAG (modular RAG, Retrieval-Augmented Generation) offers several advantages in the production environment. Here are the main benefits:\n\n1. **Expandability**: Modular RAG can easily integrate various data sources and modules to increase system scalability. This gives you flexibility when adding new data sources or features.\n\n2. **Ease of maintenance**: The modular structure allows each component to be managed and updated independently for easy maintenance. This increases the stability of the system and allows you to respond quickly in case of problems.\n\n3. ** Performance optimization**: Each module is optimized for a specific function, which can improve the performance of the entire system. For example, search modules are optimized for search, and creation modules can be optimized for text generation.\n\n4. **Reusability**: Modular structure allows certain functions to be reused by other projects or systems. This reduces development time and reduces costs.\n\n5. **Flexible deployment**: Modules can be distributed individually for a variety of environments, supporting various deployment options such as cloud or onpremises.\n\n These benefits help Modular RAG work effectively in complex production environments.')
==================================================
==================================================
π Node: replan π
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
response:
All steps are complete. The difference between Naive RAG and Modular RAG and the benefits Modular RAG has in its production environment has been explained. If you have any additional questions, please ask at any time.
==================================================
==================================================
π Node: final_report π
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
response:
# Modular RAG and Naive RAG's benefits in comparison and production environments
## Overview
Retrieval-Augmented Generation (RAG) is a technique that combines information retrieval with natural language generation (NLG) to generate answers to your questions. RAG is mainly implemented in two forms: Naive RAG and Modular RAG. This report explains the concepts and differences between Naive RAG and Modular RAG, and discusses the benefits Modular RAG has in its production environment.
## Naive RAG Concept
Naive RAG is a simple structured system that combines pre-learned language models with search systems to generate answers to your questions. The system consists of two main components:
One. **Information Finder (Retriever)**: Search for documents related to your question in an external database.
2. **Generator**: Generates natural language answers based on the documents retrieved.
Naive RAG has the advantage of leveraging the latest or external knowledge by directly utilizing the information in the searched document to generate an answer.
## Concept of Modular RAG
Modular RAG is a system designed by separating each step of search and creation into independent modules. The system has the following characteristics:
One. ** Modular structure**: Each module can be individually optimized or replaced for high flexibility and scalability.
2. **Search module**: Search for relevant information in large databases.
3. **Create module**: Generates natural language responses based on the information retrieved.
4. **Integration and Adjustment**: Improve the quality of the response by adjusting the interaction between modules.
## Difference between Naive RAG and Modular RAG
One. **Structural differences**:
-Naive RAG consists of fixed pipe lines, which can reduce flexibility.
-Modular RAG is a modular structure, allowing you to configure the system by combining various modules.
2. **Expansion and flexibility**:
-Naive RAG may be limited in adding or changing new features.
-Modular RAG has a clearly defined interface between modules, allowing you to easily add new features or replace existing ones.
3. ** Development and maintenance**:
-Naive RAG needs to manage the entire system in one unit, so changes in certain parts can affect the entire system.
-Modular RAG is managed independently for each module, so you can minimize the impact of changes to a particular module on other modules.
4. ** Performance optimization**:
-Naive RAG can be difficult to optimize performance, and optimization for specific tasks may be limited.
-Modular RAG can optimize each module individually, improving the performance of the entire system more effectively.
## Benefits of Modular RAG's production environment
One. **Expandability**: Easily integrate various data sources and modules to increase system scalability.
2. **Ease of maintenance**: Each component can be managed and updated independently for easy maintenance.
3. ** Performance optimization**: Each module is optimized for a specific function, which can improve the performance of the entire system.
4. **Reusability**: Reusable certain features in other projects or systems, reducing development time and reducing costs.
5. **Flexible deployment**: Modules can be distributed individually for different environments, supporting various deployment options such as cloud or onpremises.
## conclusion
Modular RAG has better structural flexibility and scalability than Naive RAG, and offers the advantage of effectively meeting various requirements in a production environment. These characteristics play an important role in building and maintaining complex systems, opening up possibilities that can be easily applied to various domains.
==================================================
# Modular RAG and Naive RAG's benefits in comparison and production environments
## Overview
Retrieval-Augmented Generation (RAG) is a technique that combines information retrieval with natural language generation (NLG) to generate answers to your questions. RAG is mainly implemented in two forms: Naive RAG and Modular RAG. This report explains the concepts and differences between Naive RAG and Modular RAG, and discusses the benefits Modular RAG has in its production environment.
## Naive RAG Concept
Naive RAG is a simple structured system that combines pre-learned language models with search systems to generate answers to your questions. The system consists of two main components:
One. **Information Finder (Retriever)**: Search for documents related to your question in an external database.
2. **Generator**: Generates natural language answers based on the documents retrieved.
Naive RAG has the advantage of leveraging the latest or external knowledge by directly utilizing the information in the searched document to generate an answer.
## Concept of Modular RAG
Modular RAG is a system designed by separating each step of search and creation into independent modules. The system has the following characteristics:
One. ** Modular structure**: Each module can be individually optimized or replaced for high flexibility and scalability.
2. **Search module**: Search for relevant information in large databases.
3. **Create module**: Generates natural language responses based on the information retrieved.
4. **Integration and Adjustment**: Improve the quality of the response by adjusting the interaction between modules.
## Difference between Naive RAG and Modular RAG
One. **Structural differences**:
-Naive RAG consists of fixed pipe lines, which can reduce flexibility.
-Modular RAG is a modular structure, allowing you to configure the system by combining various modules.
2. **Expansion and flexibility**:
-Naive RAG may be limited in adding or changing new features.
-Modular RAG has a clearly defined interface between modules, allowing you to easily add new features or replace existing ones.
3. ** Development and maintenance**:
-Naive RAG needs to manage the entire system in one unit, so changes in certain parts can affect the entire system.
-Modular RAG is managed independently for each module, so you can minimize the impact of changes to a particular module on other modules.
4. ** Performance optimization**:
-Naive RAG can be difficult to optimize performance, and optimization for specific tasks may be limited.
-Modular RAG can optimize each module individually, improving the performance of the entire system more effectively.
## Benefits of Modular RAG's production environment
One. **Expandability**: Easily integrate various data sources and modules to increase system scalability.
2. **Ease of maintenance**: Each component can be managed and updated independently for easy maintenance.
3. ** Performance optimization**: Each module is optimized for a specific function, which can improve the performance of the entire system.
4. **Reusability**: Reusable certain features in other projects or systems, reducing development time and reducing costs.
5. **Flexible deployment**: Modules can be distributed individually for different environments, supporting various deployment options such as cloud or onpremises.
## conclusion
Modular RAG has better structural flexibility and scalability than Naive RAG, and offers the advantage of effectively meeting various requirements in a production environment. These characteristics play an important role in building and maintaining complex systems, opening up possibilities that can be easily applied to various domains.
from IPython.display import Markdown
Markdown(snapshot["response"])
# Comparison of Modular RAG and Naive RAG and their advantages in a production environment
## outline
Retrieval-Augmented Generation(RAG)is a technique that combines information retrieval and natural language generation (NLG) to generate answers to questions. RAG is mainly implemented in two forms: Naive RAG and Modular RAG. This report explains the concepts and differences between Naive RAG and Modular RAG, Modular RAG Discusses the advantages it has in a production environment.
## The concept of Naive RAG
Naive RAG is a simple structured system that combines a pre-trained language model and a retrieval system to generate answers to questions. The system consists of two main components:
1. **Retriever**: Retrieves documents relevant to the question from external databases.
2. **Generator**: Generates natural language answers based on the retrieved documents.
Naive RAG has the advantage of being able to utilize up-to-date information or external knowledge by directly utilizing information from the retrieved documents to generate answers.
## The concept of Modular RAG
Modular RAG is a system designed to separate each stage of search and generation into independent modules. This system has the following characteristics:
1. **Modular architecture**: Each module can be optimized or replaced individually, providing high flexibility and scalability.
2. **Search Module**: Search for relevant information in large databases.
3. **Generator Module**: Generates natural language responses based on the retrieved information.
4. **Integrate and Coordinate**: Coordinate interactions between modules to improve response quality.
## Difference between Naive RAG and Modular RAG
1. **Structural differences**:
- Naive RAG has a fixed pipeline and may be inflexible.
- Modular RAG has a modular structure, allowing the system to be configured by combining various modules.
2. **Scalability and Flexibility**:
- Naive RAG may have limitations in adding new features or changing them.
- Modular RAG has clearly defined interfaces between modules, making it easy to add new features or replace existing ones.
3. **Development and Maintenance**:
- Naive RAG must manage the entire system as a single unit, so changes to a specific part can affect the entire system.
- Modular RAG allows each module to be managed independently, minimizing the impact of changes to a specific module on other modules.
4. **Performance Optimization**:
- Naive RAG can be difficult to optimize for performance and may have limited optimizations for certain tasks.
- Modular RAG allows each module to be optimized individually, which can more effectively improve the performance of the entire system.
## Benefits of Modular RAG in a Production Environment
1. **Scalability: Easily integrate various data sources and modules, increasing the scalability of the system.
2. **Ease of maintenance**: Each component can be managed and updated independently, making maintenance easy.
3. **Performance Optimization**: Each module is optimized for a specific function, which can improve the performance of the entire system.
4. **Reusability**: Reusing specific functionality across other projects or systems reduces development time and costs.
5. **Flexible Deployment**: Modules can be individually deployed to suit different environments, supporting a variety of deployment options, including cloud and on-premises.
## conclusion
Modular RAG offers structural flexibility and scalability compared to Naive RAG, and offers the advantage of effectively meeting various requirements in a production environment. These characteristics play an important role in building and maintaining complex systems, and open up the possibility of easy application to various domains.