الوكلاء والأدوات / Agents and tools

الترتيب / Order
12 من 14 / 12 of 14
آخر تحديث / Updated
8 أكتوبر 2026 / 8 October 2026

المستوى: متقدّم · الدرس 12 من 14

أهداف الدرس

  1. أن تفهم حلقة الوكيل.
  2. أن تعرف الموصّلات الموحّدة للأدوات (MCP).

الوكيل نموذج يستطيع أن يفعل لا أن يجيب فقط: يخطّط، ويستدعي أداة (بحث، تقويم، قاعدة بيانات)، ويقرأ النتيجة، ثم يقرّر الخطوة التالية. و«بروتوكول سياق النموذج» (MCP) معيار مفتوح يربط النماذج بالأدوات ومصادر البيانات بطريقة واحدة، كما يربط منفذ USB الأجهزة.

يخطّط، يفعل، يلاحظ، يقرّر
يخطّط، يفعل، يلاحظ، يقرّر
Python
tools = {
    "office_hours": lambda teacher: {"days": ["الأحد", "الثلاثاء"], "time": "10:00–12:00"},
}

def run_agent(model, question, max_steps=5):
    """model(history, tools) returns {"type": "answer", ...} or {"type": "tool", ...}."""
    history = [{"role": "user", "content": question}]
    for _ in range(max_steps):
        step = model(history, tools=list(tools))            # plan
        if step["type"] == "answer":
            return step["text"]
        result = tools[step["tool"]](**step["args"])         # act
        history.append({"role": "tool", "content": str(result)})  # observe
    return "لم أصل إلى إجابة؛ أحيلك إلى موظّف مختص."

لا تعطِ الوكيل صلاحية أكثر مما يلزم، واطلب موافقة إنسان قبل أي فعل لا يُتراجع عنه — إرسال أو حذف أو دفع.

تمرين

لماذا نضع حدّاً أقصى لعدد الخطوات؟

الإجابة

كي لا يدور الوكيل بلا نهاية، وليتوقّف ويحيل إلى إنسان حين يتعثّر.

Level: Advanced · Lesson 12 of 14

Lesson goals

  1. Understand the agent loop.
  2. Know standard tool connectors (MCP).

An agent is a model that can act, not only answer: it plans, calls a tool (search, calendar, database), reads the result, then decides the next step. The Model Context Protocol (MCP) is an open standard that connects models to tools and data sources one way, like a USB port connects devices.

Plan, act, observe, decide
Plan, act, observe, decide
Python
tools = {
    "office_hours": lambda teacher: {"days": ["الأحد", "الثلاثاء"], "time": "10:00–12:00"},
}

def run_agent(model, question, max_steps=5):
    """model(history, tools) returns {"type": "answer", ...} or {"type": "tool", ...}."""
    history = [{"role": "user", "content": question}]
    for _ in range(max_steps):
        step = model(history, tools=list(tools))            # plan
        if step["type"] == "answer":
            return step["text"]
        result = tools[step["tool"]](**step["args"])         # act
        history.append({"role": "tool", "content": str(result)})  # observe
    return "لم أصل إلى إجابة؛ أحيلك إلى موظّف مختص."

Give an agent no more permission than it needs, and require a person's approval before anything irreversible — sending, deleting, paying.

Exercise

Why cap the number of steps?

Answer

So the agent never loops forever, and hands over to a person when stuck.

التعليقات / Comments

لا تعليقات بعد. كن أول من يسأل. / No comments yet. Be the first to ask.

تعليق الطالب يظهر فوراً، وتعليق الزائر بعد موافقة المدرّس. / A student's comment shows at once; a visitor's once the teacher approves it.

في «الذكاء الاصطناعي: من الصفر إلى الاحتراف» / In “AI: from zero to hero”

  1. 1ما الذكاء الاصطناعي التوليدي؟ / What is generative AI?فيها صور / has picturesالتعليقات مفتوحة / comments open
  2. 2الرموز والتضمينات / Tokens and embeddingsفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  3. 3المحوّل والانتباه / Transformers and attentionفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  4. 4التدريب المسبق / Pre-trainingفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  5. 5الضبط بالتعليمات / Instruction tuning (SFT)فيها كود / has codeالتعليقات مفتوحة / comments open
  6. 6التعلّم من التفضيلات / Learning from preferencesفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  7. 7نماذج الاستدلال / Reasoning modelsفيها كود / has codeالتعليقات مفتوحة / comments open
  8. 8الضبط الدقيق الفعّال / Efficient fine-tuningفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  9. 9هندسة الأوامر والسياق / Prompt and context engineeringفيها كود / has codeالتعليقات مفتوحة / comments open
  10. 10الاسترجاع المعزّز (RAG) من الصفر / RAG from scratchفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  11. 11RAG المتقدّم / Advanced RAGفيها كود / has codeالتعليقات مفتوحة / comments open
  12. 12الوكلاء والأدوات / Agents and toolsفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  13. 13التقييم والأمان / Evaluation and safetyفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  14. 14مشروع ختامي: مساعد عربي / Capstone: an Arabic assistantفيها كود / has codeفيها صور / has picturesفيها نموذج / has a formالتعليقات مفتوحة / comments open

↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق↑ ↓ to move · Enter to open · Esc to close