ENع
→AI agents
الدرس 22 · النماذج اللغوية الكبيرة عمليًا

Agents and tool use

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةAdvanced
عن هذا الدرس

A language model on its own only writes text. An agent is a program that runs the model in a loop and lets it call tools: the model writes a structured tool call, the program runs the tool, and the result goes back into the message list for the model to read. Watch a small Python agent answer "How hot is it in Kuwait City, in °F?" with two tool calls and three model calls, then see the ReAct pattern behind it and the limits every agent needs.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 22 · تفاعلي
01 / 10

What an agent is

A language model on its own only writes text. An agent is a program that runs a model in a loop and lets it use tools: functions that look things up or change things. The model decides; the program acts.

language model  text in  ->  text out
agent           model + tools + a loop:
                  the model picks the next action
                  the program runs it
                  the result goes back to the model
                  repeat until the model answers
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. What an agent is

    A language model on its own only writes text. An agent is a program that runs a model in a loop and lets it use tools: functions that look things up or change things. The model decides; the program acts.

    language model  text in  ->  text out
    agent           model + tools + a loop:
                      the model picks the next action
                      the program runs it
                      the result goes back to the model
                      repeat until the model answers
  2. Tools the model can call

    The program tells the model which tools exist: each name, what it does, and its inputs, written as a JSON schema (a description of the data). Ours has two: get_weather(city) and to_fahrenheit(c).

    tools = [
      {"name": "get_weather",
       "description": "Current temperature in a city, in °C",
       "input_schema": {"type": "object",
         "properties": {"city": {"type": "string"}},
         "required": ["city"]}},
      {"name": "to_fahrenheit",
       "description": "Convert a temperature from °C to °F",
       "input_schema": {"type": "object",
         "properties": {"c": {"type": "number"}},
         "required": ["c"]}},
    ]
  3. The question goes in

    The user asks: "How hot is it in Kuwait City, in °F?" The program starts a list of messages with it. This list, the context, is everything the model will see.

    messages = [
      {"role": "user",
       "content": "How hot is it in Kuwait City, in °F?"},
    ]
    
    len(messages) -> 1
  4. The model asks for a tool

    Line 2 sends the whole list to the model. It cannot look up the weather, so instead of an answer it replies with a tool call: structured text naming get_weather and its argument, city = "Kuwait City".

    reply = llm(messages)      # the model sees 1 message
    
    reply -> {"role": "assistant",
              "tool": "get_weather",
              "args": {"city": "Kuwait City"}}
    no "answer" in reply, so the loop goes on
    the list still holds 1 message: reply waits in a variable
  5. The program runs the tool

    The model never runs code. Line 5 does: the program calls get_weather("Kuwait City"), which returns 38 °C (fixed demo data). Line 6 adds the call and its result to the list. The list grows.

    result = call_tool(reply)
      get_weather(city="Kuwait City")
      -> {"city": "Kuwait City", "temp_c": 38}
    messages += [reply, result]   # both join the list now
    
    len(messages) -> 3
  6. Read it all, decide again

    The model keeps no memory between calls, so line 2 sends the whole list again, now three messages. It reads 38 °C and asks for one more tool: to_fahrenheit(c = 38).

    reply = llm(messages)      # the model sees 3 messages
    
    reply -> {"role": "assistant",
              "tool": "to_fahrenheit",
              "args": {"c": 38}}
    the API is stateless: every call resends the history
    the list still holds 3 messages until line 6 runs
  7. Second tool, second result

    The program runs to_fahrenheit(38): 38 × 9/5 + 32 = 100.4. The call and its result join the list, now five messages. Each trip round the loop adds what the agent did and what it saw.

    result = call_tool(reply)
      to_fahrenheit(c=38)
      -> 38 * 9 / 5 + 32 = 100.4
    messages += [reply, result]   # both join the list now
    
    len(messages) -> 5
  8. The model answers

    On the third call the model has what it needs, so it replies with an answer, not a tool call. Lines 3 and 4 spot it and return it: "It is 100.4 °F in Kuwait City." Three model calls, two tool runs.

    reply = llm(messages)      # the model sees 5 messages
    reply -> {"role": "assistant",
              "answer": "It is 100.4 °F in Kuwait City."}
    "answer" in reply  -> True
    return reply["answer"]
    model calls: 3   tool runs: 2   loop limit: 5
  9. Reason, act, observe

    This loop is a well-known agent pattern called ReAct: the model reasons about what to do (often in writing), acts by calling a tool, observes the result, and repeats until it can answer. Bigger agents run the same loop with more tools.

    # a real model would write thoughts like these;
    # our scripted stand-in skips them
    Thought:      I need the weather in Kuwait City.
    Action:       get_weather(city="Kuwait City")
    Observation:  38 °C
    Thought:      Now convert it to °F.
    Action:       to_fahrenheit(c=38)
    Observation:  100.4
    Answer:       It is 100.4 °F in Kuwait City.
  10. Keep the loop safe

    Tools act in the real world, so agents need limits. Line 1 stops after five turns, so the loop cannot run for ever. Give an agent only the tools it needs, and ask a person before risky actions.

    for step in range(5):    # a hard stop
    
    least privilege   only the tools the task needs
    human approval    before sending, paying, deleting
    untrusted input   a tool result can hide instructions
                      (indirect prompt injection)

اختبار يتاح بعد الخطوة الأخيرة.