Building Corrective RAG from scratch with open-source, local LLMs

2 min read 7 months ago
Published on Apr 22, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Step-by-Step Tutorial: Building Corrective RAG with Open-Source, Local LLMs

1. Setting Up Local LLMs

  1. Download the AMA application on your laptop to run models locally.
  2. Choose a model from the model list, such as "mrol instruct," and install it using pip.
  3. Define a variable for the downloaded model, like local_llm = mrol_instruct.

2. Creating an Index

  1. Identify a document or blog post to use as an index for retrieval.
  2. Split the content into chunks and use GPT embeddings for indexing.

3. Creating a Retriever

  1. Use a local vector collection tool like Tav web to create a retriever from the embeddings.
  2. Retrieve relevant documents by calling get_relevant_documents().

4. Designing the Logical Flow

  1. Lay out the logical steps and transformations in a graph or dictionary format.
  2. Implement functions for each node and conditional edge in the graph.
  3. Use JSON mode from AMA to structure the output for reliable interpretation downstream.

5. Implementing Logical Flow

  1. Create functions for each node in the graph to modify the state based on the logic.
  2. Use conditional statements to make decisions based on the grading results.
  3. Connect the nodes and edges to form the complete logical flow.

6. Running the Logical Flow

  1. Compile your question and execute the logical flow.
  2. Monitor the output at each step to ensure the correct processing of documents and decisions.
  3. Verify the final output for accuracy and relevance.

7. Reviewing the Results

  1. Analyze the output generated by the logical flow.
  2. Check the grading of documents and the decision-making process.
  3. Validate the answers and insights provided by the system.

By following these steps, you can build a Corrective RAG system from scratch using open-source tools and local LLMs for self-reflection and reasoning tasks.