0Pricing
AI Agents with LangChain & Autonomous Workflows · درس

وكلاء التصحيح الذاتي والتأمل

تعلّم كيفية بناء وكلاء قادرين على التقييم النقدي لمخرجاتهم وتحسين استجاباتهم أو إجراءاتهم تكراريًا

وكلاء التصحيح الذاتي والتأمل درس مجاني في AI Agents with LangChain & Autonomous Workflows على CoddyKit. هذا هو الدرس 3 من أصل 6. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في AI Agents with LangChain & Autonomous Workflows، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة AI Agents with LangChain & Autonomous Workflows 6 دروس في المجموع.

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

Agents That Think Twice

Ever wished your AI agent could check its own work? That's exactly what self-correction and reflection agents do!

These agents are designed to not just generate an output, but also to critically evaluate it and make improvements. Think of it as an agent having an internal "editor" or "critic".

The Reflective Cycle

The core idea of reflection is an iterative process:

  • Generate: The agent produces an initial response or action.
  • Reflect: It then uses another prompt (or a different LLM) to analyze its own output against specific criteria.
  • Refine: Based on the reflection, the agent adjusts its original output, aiming for better quality or accuracy.

This cycle can repeat multiple times until a satisfactory result is achieved.

Inside a Reflection Agent

A self-correcting agent typically involves a few key parts:

  • Generator LLM: The primary Large Language Model that creates the initial output.
  • Critic/Reflector LLM: Often the same LLM, but prompted to act as a critic, evaluating the generator's output.
  • Reflection Prompt: A carefully designed prompt that guides the critic LLM on what to evaluate and how.
  • Refinement Mechanism: The logic that takes the critic's feedback and uses it to improve the next generation.

The Generator Prompt

Just like any LLM interaction, a clear initial prompt for the "generator" is crucial. This prompt tells the agent what task to perform.

For example, if we want an agent to write a short story, the generator prompt would define the story's theme, characters, and length. The goal is a solid first draft.

prompt_template = "Write a 3-sentence story about a brave knight and a dragon."

Crafting the Reflection Prompt

The reflection prompt is where the magic happens. It instructs the LLM to review the previous output and identify areas for improvement.

This prompt should include specific criteria. For our story example, it might ask: "Is the story engaging? Does it clearly mention a knight and a dragon? Is it exactly 3 sentences?"

reflection_prompt = "Critique the following story: '{story}'. Does it meet these criteria: 1. Engaging? 2. Knight and Dragon present? 3. Exactly 3 sentences? Provide specific feedback for improvement."

Simple Reflection Example

Let's trace our story example conceptually:

  1. Generate: LLM writes an initial story draft.
  2. Reflect: LLM reads the story and the reflection prompt, then gives feedback (e.g., "It's 4 sentences, not 3.").
  3. Refine: The agent receives the feedback. It then tries to rewrite the story, incorporating the feedback (e.g., shortening it to 3 sentences).

This loop continues until the story meets all criteria or a limit is reached.

Building a Refinement Loop

In practice, the refinement process often involves a loop. The agent continues to generate and reflect until a set condition is met. This condition could be:

  • A maximum number of iterations.
  • The reflection feedback indicates no further improvements are needed.
  • A specific quality metric is achieved.

This iterative process allows for continuous self-improvement, making agents more robust.

Code: A Simple Reflection Loop

Here's a simplified Python example demonstrating a basic self-correction loop. We'll use a placeholder LLM for clarity, focusing on the generate, reflect, and refine logic.

class MockLLM:
    def __init__(self):
        pass

    def generate_initial_output(self, task_prompt):
        # Simulate a generator LLM that sometimes gets it wrong
        if "start with 'Hello'" in task_prompt:
            return "Greetings! How are you?" # Incorrect initial attempt
        return "Hello World!" # Correct initial attempt based on prompt

    def reflect_and_suggest(self, original_task, generated_output):
        # Simulate a critic LLM
        reflection_prompt = (
            f"Given the task: '{original_task}'. "
            f"The generated output was: '{generated_output}'. "
            "Critique this output: Does it start with 'Hello'? "
            "If not, suggest a correction. Be concise."
        )
        # Simplified reflection logic for the mock LLM
        if not generated_output.strip().startswith("Hello"):
            return "Doesn't start with 'Hello'. Suggestion: Start with 'Hello'."
        return "Output meets criteria."

# --- Main Logic ---
llm = MockLLM()
task = "Create a greeting sentence that starts with 'Hello'."
current_output = llm.generate_initial_output(task)

print(f"Initial Output: {current_output}")

for i in range(3): # Max 3 attempts (initial + 2 refinements)
    reflection = llm.reflect_and_suggest(task, current_output)
    print(f"Reflection {i+1}: {reflection}")

    if "Output meets criteria" in reflection:
        print("Agent self-corrected successfully!")
        break
    else:
        # Simulate applying the suggestion
        if "Start with 'Hello'" in reflection and not current_output.startswith("Hello"):
            current_output = "Hello" + current_output[current_output.find(' '):] # Simple fix
            print(f"Refined Output: {current_output}")
        else:
            print("No specific refinement strategy for this feedback. Stopping.")
            break
print("\nFinal Output:", current_output)

Benefits & Trade-offs

Reflection agents are powerful for tasks requiring high accuracy or complex reasoning, especially when a single pass might miss nuances.

  • Improved Accuracy: Reduces errors and hallucinations.
  • Robustness: Handles complex prompts better.
  • Quality: Leads to higher-quality outputs.

However, they increase computational cost and latency due to multiple LLM calls. Use them where quality justifies the overhead.

Test Your Knowledge

Consider an agent designed to summarize a document and then self-correct. What is the primary purpose of the "reflection prompt" in this scenario?

Recap: Self-Correction

We've explored how self-correction and reflection agents work. These agents enhance AI capabilities by allowing them to critically evaluate and refine their own outputs.

  • They follow a generate, reflect, refine cycle.
  • Key components include a generator, a critic, and specific reflection prompts.
  • While increasing cost and latency, they significantly boost output quality and accuracy for complex tasks.

This ability to "think twice" is a crucial step towards more robust and intelligent AI systems!

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

هل درس «وكلاء التصحيح الذاتي والتأمل» مجاني؟

نعم — نص درس «وكلاء التصحيح الذاتي والتأمل» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة AI Agents with LangChain & Autonomous Workflows، انتقل إلى CoddyKit PRO. تتضمن دورة AI Agents with LangChain & Autonomous Workflows 6 دروس في المجموع.

ماذا ستتعلم في «وكلاء التصحيح الذاتي والتأمل»؟

تعلّم كيفية بناء وكلاء قادرين على التقييم النقدي لمخرجاتهم وتحسين استجاباتهم أو إجراءاتهم تكراريًا تتمرن على AI Agents with LangChain & Autonomous Workflows مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ AI Agents with LangChain & Autonomous Workflows؟

لا تُشترط خبرة سابقة. AI Agents with LangChain & Autonomous Workflows على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 6.

كم من الوقت يستغرق درس «وكلاء التصحيح الذاتي والتأمل»؟

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

هل يمكنني كتابة وتشغيل أكواد في درس AI Agents with LangChain & Autonomous Workflows هذا؟

نعم. كل درس في AI Agents with LangChain & Autonomous Workflows يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

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

  1. وكلاء ReAct والتخطيط والتنفيذ
  2. تصميمات الوكلاء الهرمية
  3. وكلاء التصحيح الذاتي والتأمل
  4. البنى المعرفية للوكلاء
  5. أنماط التعاون بين الوكلاء المتعددين
  6. أنظمة الوكلاء الهجينة
← العودة إلى AI Agents with LangChain & Autonomous Workflows