0Pricing
Vector Databases: Pinecone, Weaviate & pgvector · درس

مسارات RAG متعددة المراحل

صمّموا ونفّذوا مسارات عمل معقدة لـ RAG تتضمن مراحل متعددة للاسترجاع والتوليد من أجل استجابات دقيقة ومتعمقة.

مسارات RAG متعددة المراحل درس مجاني في Vector Databases: Pinecone, Weaviate & pgvector على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Vector Databases: Pinecone, Weaviate & pgvector، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Vector Databases: Pinecone, Weaviate & pgvector 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

What is Multi-Stage RAG?

Traditional RAG (Retrieval Augmented Generation) works well for straightforward questions. However, for complex or ambiguous queries, a single retrieval and generation step can often fall short.

Multi-stage RAG pipelines address this by breaking down the problem into several sequential steps, refining the search and generation process at each stage to produce more accurate and nuanced answers.

Why Go Multi-Stage?

A basic RAG setup might struggle with:

  • Multi-part questions: "Who founded Apple and when was their first product released?"
  • Ambiguous queries: Needing iterative clarification.
  • Deep contextual understanding: Requiring information from disparate sources or multiple 'hops' in knowledge.

Multi-stage RAG enhances the system's ability to handle these challenges by processing information more thoroughly.

The Core Idea: Iterative Refinement

The essence of a multi-stage RAG pipeline is iterative refinement. Instead of one pass, the system performs multiple passes, where:

  • Early stages generate intermediate results or refined queries.
  • Later stages use these intermediate outputs to perform more targeted retrieval or generation.

Each step builds upon the previous one, leading to a more precise and comprehensive final answer.

Step 1: Query Decomposition

For intricate user questions, the first step often involves query decomposition. This means breaking down a complex query into a set of simpler, more focused sub-questions.

  • Example: "Tell me about the founder of Python and when was it first released?"
  • Decomposed: "Who founded Python?", "When was Python first released?"

Each sub-question can then be processed individually for more effective retrieval.

Step 2: Initial Context Retrieval

Once you have decomposed the original query into sub-questions, the next step is to perform an initial retrieval for each of these sub-queries.

This involves querying your vector database (or other knowledge sources) with each sub-question to gather a broad set of potentially relevant documents or text chunks. The goal is to collect all initial pieces of the puzzle.

Step 3: Intermediate Generation & Refinement

With the initial context retrieved, an LLM (Large Language Model) can be used to process this information. This intermediate step can involve:

  • Generating intermediate answers: Providing partial answers to sub-questions.
  • Formulating follow-up questions: Using initial context to generate new, more specific queries for a second retrieval pass.
  • Summarizing initial findings: Condensing retrieved information to guide subsequent steps.

This feedback loop helps in refining the search.

Step 4: Re-ranking & Aggregation

After potentially multiple retrieval passes and intermediate generations, you'll have various pieces of context and potentially partial answers. The final stages involve:

  • Re-ranking: Using a more powerful model or a different relevance score to select the most pertinent chunks from all retrieved documents.
  • Aggregation: Combining all relevant information and intermediate answers to synthesize a single, comprehensive, and coherent final response to the original user query.

Multi-Hop Q&A Example

Consider a 'multi-hop' question: "What is the capital of the country where the Eiffel Tower is located?"

A multi-stage pipeline could:

  1. Hop 1: Retrieve information about the "Eiffel Tower" to identify its location (Paris, France).
  2. Hop 2: Use "France" as a new query to retrieve information about its capital (Paris).
  3. Final Answer: Combine to answer "Paris".

This chaining of retrieval steps is a powerful application of multi-stage RAG.

Python Workflow Illustration

This conceptual Python code illustrates the high-level orchestration of a multi-stage RAG pipeline. It focuses on the flow rather than specific external API calls.

class MultiStageRAG:
    def __init__(self, retriever, llm_model):
        self.retriever = retriever
        self.llm = llm_model

    def run_pipeline(self, user_query):
        # Stage 1: Decompose query into sub-questions
        sub_queries = self.llm.decompose_query(user_query)
        print(f"Decomposed queries: {sub_queries}")

        all_retrieved_docs = []
        intermediate_answers = []

        for sq in sub_queries:
            # Stage 2: Initial Retrieval for each sub-query
            docs = self.retriever.retrieve(sq)
            all_retrieved_docs.extend(docs)
            print(f"Retrieved for '{sq}': {len(docs)} docs")

            # Stage 3: Intermediate Generation (e.g., summarizing, refining)
            intermediate_ans = self.llm.generate_answer(sq, docs)
            intermediate_answers.append(intermediate_ans)

        # Stage 4: Re-rank all retrieved context and aggregate
        final_context = self.retriever.re_rank(all_retrieved_docs)
        print(f"Final context length: {len(final_context)}")

        final_answer = self.llm.generate_final_answer(user_query, final_context)
        return final_answer

# --- Mock Implementations for Demonstration ---
class MockRetriever:
    def retrieve(self, query):
        # Simulate retrieving documents based on query
        return [f"Doc for '{query}' part A", f"Doc for '{query}' part B"]

    def re_rank(self, docs):
        # Simulate re-ranking, just returns the first few for simplicity
        return docs[:3]

class MockLLM:
    def decompose_query(self, query):
        # Simple decomposition for example
        if " and " in query:
            parts = query.split(" and ")
            return [p.strip() + "?" for p in parts]
        return [query + "?"]

    def generate_answer(self, query, docs):
        # Simulate generating an intermediate answer
        return f"Intermediate answer for '{query}' based on {len(docs)} docs."

    def generate_final_answer(self, original_query, context):
        # Simulate generating a final answer
        return f"Final answer to '{original_query}' based on context: {context}."

# --- Main Execution ---
if __name__ == "__main__":
    mock_retriever = MockRetriever()
    mock_llm = MockLLM()
    pipeline = MultiStageRAG(mock_retriever, mock_llm)

    query = "What is the capital of France and who painted the Mona Lisa?"
    result = pipeline.run_pipeline(query)
    print(f"\nResult: {result}")

Multi-Stage Benefits

Which of the following is a primary benefit of using a multi-stage RAG pipeline compared to a single-pass RAG?

Recap: Mastering Complex RAG

We've explored multi-stage RAG pipelines, understanding how they tackle complex queries through iterative steps:

  • Query Decomposition: Breaking down complex questions into simpler sub-queries.
  • Iterative Retrieval: Performing multiple passes to gather and refine context.
  • LLM Refinement: Using LLMs to generate intermediate answers or guide subsequent search steps.
  • Aggregation: Combining all insights for a comprehensive and coherent final answer.

By orchestrating these steps, you can build RAG systems capable of delivering much more precise and thorough responses to even the most challenging user prompts.

الأسئلة الشائعة

هل درس «مسارات RAG متعددة المراحل» مجاني؟

نعم — نص درس «مسارات RAG متعددة المراحل» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Vector Databases: Pinecone, Weaviate & pgvector، انتقل إلى CoddyKit PRO. تتضمن دورة Vector Databases: Pinecone, Weaviate & pgvector 4 دروس في المجموع.

ماذا ستتعلم في «مسارات RAG متعددة المراحل»؟

صمّموا ونفّذوا مسارات عمل معقدة لـ RAG تتضمن مراحل متعددة للاسترجاع والتوليد من أجل استجابات دقيقة ومتعمقة. تتمرن على Vector Databases: Pinecone, Weaviate & pgvector مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Vector Databases: Pinecone, Weaviate & pgvector؟

لا تُشترط خبرة سابقة. Vector Databases: Pinecone, Weaviate & pgvector على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «مسارات RAG متعددة المراحل»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Vector Databases: Pinecone, Weaviate & pgvector هذا؟

نعم. كل درس في Vector Databases: Pinecone, Weaviate & pgvector يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تقنيات تحويل الاستعلامات
  2. مسارات RAG متعددة المراحل
  3. تقييم أداء نظام RAG
  4. إعادة ترتيب النتائج المسترجعة
← العودة إلى Vector Databases: Pinecone, Weaviate & pgvector