الخطوة 4 في الوكلاء: الاستدلال بسلسلة التفكير

يجعل التلقين بأسلوب سلسلة التفكير (CoT) الذكاء الاصطناعي «يفكّر خطوة بخطوة» قبل الإجابة، وهذا يحسّن الاستدلال بشكل كبير.

ما ستتعلمه

  • ما هي سلسلة التفكير ولماذا تنجح
  • إضافة سلسلة التفكير إلى موجه النظام (system prompt)
  • استخلاص الاستدلال من الاستجابات

الخطوة 1: المشكلة

بدون سلسلة التفكير، كثيرًا ما تقفز نماذج الذكاء الاصطناعي إلى استنتاجات متسرعة:

Q: "A bat and a ball cost $1.10. The bat costs $1.00 more than the ball.
    How much does the ball cost?"
AI: "$0.10"  ← Wrong! (The correct answer is $0.05)

مع سلسلة التفكير:

Q: "A bat and a ball cost $1.10. The bat costs $1.00 more than the ball.
    How much does the ball cost?"
AI: "Let's think step by step:
    1. Let the ball cost x dollars.
    2. The bat costs x + $1.00.
    3. Together: x + (x + $1.00) = $1.10
    4. 2x + $1.00 = $1.10
    5. 2x = $0.10
    6. x = $0.05
    So the ball costs $0.05."  ← Correct!

الخطوة 2: إضافة سلسلة التفكير إلى موجه النظام

system_prompt = """You are a helpful assistant.
When solving problems, think step by step.
Before giving your final answer, show your reasoning.
Wrap your reasoning in 思考...<|end▁of▁thinking|> tags.
"""

الخطوة 3: سلسلة تفكير منظمة مع استدعاء الدوال

ثمة نهج بديل — اجعل الذكاء الاصطناعي يعيد الاستدلال والإجابة بشكل منفصل:

cot_tools = [
    {
        "type": "function",
        "function": {
            "name": "answer_with_reasoning",
            "description": "Provide a final answer with step-by-step reasoning",
            "parameters": {
                "type": "object",
                "properties": {
                    "reasoning": {
                        "type": "string",
                        "description": "Step-by-step reasoning process"
                    },
                    "answer": {
                        "type": "string",
                        "description": "The final answer"
                    }
                },
                "required": ["reasoning", "answer"]
            }
        }
    }
]

الخطوة 4: وكيل سلسلة التفكير

def run_cot_agent(user_message: str) -> str:
    messages = [
        {"role": "system", "content": "Solve problems step by step. Show your reasoning."},
        {"role": "user", "content": user_message},
    ]

    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages,
        temperature=0,  # 0 = حتمي، وهو الأفضل لمهام الاستدلال
    )

    return response.choices[0].message.content

الخطوة 5: المقارنة بين استخدام سلسلة التفكير وعدمه

def run_without_cot(user_message: str) -> str:
    messages = [
        {"role": "system", "content": "Answer concisely and directly."},
        {"role": "user", "content": user_message},
    ]
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages,
        temperature=0,
    )
    return response.choices[0].message.content


if __name__ == "__main__":
    problems = [
        "A bat and a ball cost $1.10. The bat costs $1.00 more than the ball. How much does the ball cost?",
        "If there are 3 apples and you take away 2, how many do you have?",
        "I have two coins that add up to 30 cents, and one of them is not a nickel. What are the coins?",
    ]

    for problem in problems:
        print(f"\n{'='*60}")
        print(f"Problem: {problem}")
        print("-" * 60)
        print(f"Without CoT: {run_without_cot(problem)}")
        print("-" * 60)
        print(f"With CoT: {run_cot_agent(problem)}")

متى تستخدم سلسلة التفكير

نوع المشكلة هل تساعد سلسلة التفكير؟
مسائل رياضية نعم — بدرجة كبيرة
ألغاز منطقية نعم
استدلال متعدد الخطوات نعم
كتابة إبداعية غير ضرورية
أسئلة واقعية بسيطة غير ضرورية
تلخيص غير ضرورية

نصائح احترافية

  1. اخفض درجة الحرارة (temperature) (0-0.3) في مهام الاستدلال — فأنت تريد الاتساق
  2. ارفع درجة الحرارة (0.7-1.0) في المهام الإبداعية
  3. أضف عبارة «فكّر خطوة بخطوة» إلى أي موجه لتحسّن الاستدلال بنسبة 10-30%
  4. اجمع سلسلة التفكير مع الأدوات: «فكّر خطوة بخطوة في الأدوات التي تحتاج إلى استدعائها»