المستوى: متقدّم · الدرس 12 من 14
أهداف الدرس
- أن تفهم حلقة الوكيل.
- أن تعرف الموصّلات الموحّدة للأدوات (MCP).
الوكيل نموذج يستطيع أن يفعل لا أن يجيب فقط: يخطّط، ويستدعي أداة (بحث، تقويم، قاعدة بيانات)، ويقرأ النتيجة، ثم يقرّر الخطوة التالية. و«بروتوكول سياق النموذج» (MCP) معيار مفتوح يربط النماذج بالأدوات ومصادر البيانات بطريقة واحدة، كما يربط منفذ USB الأجهزة.
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
- Understand the agent loop.
- 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.
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.