🤖 Build an AI Agent with smolagents · สร้าง AI Agent ด้วย smolagents · 用 smolagents 做一个 AI 智能体

A chatbot answers. An agent does the work: it writes Python, runs it, reads the result, and tries again. Free, on your own machine. · แชตบอตแค่ตอบ ส่วน agent ลงมือทำงาน มันเขียน Python รันเอง อ่านผลลัพธ์ แล้วลองใหม่ ฟรี บนเครื่องของคุณเอง · 聊天机器人只会回答。智能体会动手干活:写 Python、运行、读结果,再接着试。免费,就在你自己的电脑上。

🇬🇧 English

In Make Your Own AI Teacher you ran a model with Ollama. You asked a question and it answered. That is a chatbot.

An agent is the same model inside a loop. It gets a job. It writes a small piece of Python. The code runs, the agent reads what came back, and it decides the next step. It stops when it can give a final answer.

smolagents is Hugging Face's library for building agents. "Smol" is internet slang for small, and the library means it: the part that runs the loop is short enough to read in an afternoon.

🇹🇭 ไทย

ใน สร้างครู AI ของคุณเอง คุณรันโมเดลด้วย Ollama คุณถามคำถาม แล้วมันตอบ นั่นคือแชตบอต

agent คือโมเดลตัวเดิมที่ทำงานวนเป็นรอบ มันได้รับงาน เขียน Python สั้น ๆ โค้ดถูกรัน agent อ่านสิ่งที่ได้กลับมา แล้วตัดสินใจขั้นต่อไป มันหยุดเมื่อให้คำตอบสุดท้ายได้

smolagents คือไลบรารีของ Hugging Face สำหรับสร้าง agent คำว่า "smol" เป็นแสลงในอินเทอร์เน็ต แปลว่าเล็ก และมันเล็กจริง ส่วนที่ทำให้ loop ทำงานสั้นพอจะอ่านจบในบ่ายเดียว

🇨🇳 中文

做一个你自己的 AI 老师里,你用 Ollama 跑过一个模型。你提问,它回答。那是聊天机器人。

智能体(agent)就是把同一个模型放进一个循环里。它接到一个任务,写一小段 Python。代码运行后,它读回来的结果,再决定下一步。能给出最终答案时,它就停下。

smolagents 是 Hugging Face 做智能体的库。"smol" 是网络俚语,意思是"小",它也真的很小:驱动循环的那部分代码,一个下午就能读完。

WHY CODE Most agents ask the model for JSON, one tool call at a time. A smolagents CodeAgent asks for Python instead, so one step can loop, do maths and call three tools. Hugging Face measured about 30% fewer steps this way, and the CodeAct paper found up to 20% more tasks solved. agent ส่วนใหญ่ขอให้โมเดลตอบเป็น JSON และเรียกเครื่องมือได้ทีละครั้ง CodeAgent ของ smolagents ขอเป็น Python แทน ขั้นเดียวจึงวนลูป คิดเลข และเรียกเครื่องมือสามตัวได้ Hugging Face วัดแล้วพบว่าใช้ขั้นตอนน้อยลงราว 30% และงานวิจัย CodeAct พบว่าทำงานสำเร็จมากขึ้นได้ถึง 20% 大多数智能体让模型输出 JSON,一次只调用一个工具。smolagents 的 CodeAgent 让模型直接写 Python,所以一步里就能循环、算数、调用三个工具。Hugging Face 测出这样大约少用 30% 的步骤,CodeAct 论文发现完成的任务最多多出 20%。

🗺️ What smolagents can do · smolagents ทำอะไรได้บ้าง · smolagents 能做什么

The whole library on one screen. The last column shows where this page teaches it. The rest is in the smolagents docs. Names checked against smolagents 1.26.0. ทั้งไลบรารีในหน้าจอเดียว คอลัมน์สุดท้ายบอกว่าหน้านี้สอนเรื่องนั้นที่ไหน ที่เหลืออยู่ใน เอกสารของ smolagents ชื่อทุกตัวตรวจกับ smolagents 1.26.0 แล้ว 整个库放在一屏里。最后一列写着本页在哪里讲它。其余的在 smolagents 文档里。所有名字都对照 smolagents 1.26.0 核对过。

It can…มันทำได้…它能… Howใช้อะไร怎么用 Hereในหน้านี้本页
solve a task by writing and running Pythonแก้โจทย์ด้วยการเขียนและรัน Python通过写 Python 并运行来完成任务 CodeAgent§2
solve a task with JSON tool calls insteadแก้โจทย์ด้วยการเรียกเครื่องมือแบบ JSON แทน改用 JSON 工具调用来完成任务 ToolCallingAgent§8
run on almost any model: Ollama on your laptop, Hugging Face, OpenAI-style servers, your own GPU, Apple Silicon, Amazon Bedrock, Azureใช้กับโมเดลได้แทบทุกแบบ ทั้ง Ollama บนแล็ปท็อป Hugging Face เซิร์ฟเวอร์แบบ OpenAI GPU ของคุณเอง Apple Silicon Amazon Bedrock และ Azure几乎能用任何模型:笔记本上的 Ollama、Hugging Face、OpenAI 式的服务器、你自己的 GPU、Apple 芯片、Amazon Bedrock、Azure LiteLLMModel, InferenceClientModel, OpenAIModel, TransformersModel, VLLMModel, MLXModel, AmazonBedrockModel, AzureOpenAIModel §2 (Ollama)(Ollama)(Ollama)
use tools you write yourselfใช้เครื่องมือที่คุณเขียนเอง使用你自己写的工具 @tool, Tool§4
take on a roleรับบทบาท扮演一个角色 instructions=§5
answer from your own documentsตอบจากเอกสารของคุณเอง根据你自己的文件回答 a search tool over an indexเครื่องมือค้นหาบนดัชนี一个基于索引的搜索工具§6
show you every step it tookแสดงทุก step ที่มันทำ给你看它走的每一步 agent.memory, return_full_code(), replay()§3
keep its code away from your computerแยกโค้ดของมันออกจากเครื่องของคุณ让它的代码远离你的电脑 additional_authorized_imports=, executor_type= "docker" / "e2b" / "modal" / "blaxel"§9
search the web and read pagesค้นเว็บและอ่านหน้าเว็บ搜索网络并阅读网页 DuckDuckGoSearchTool, VisitWebpageTool§10
use more ready-made tools: Wikipedia, speech to text, asking you a questionใช้เครื่องมือสำเร็จรูปอื่น ๆ เช่น Wikipedia แปลงเสียงเป็นข้อความ และถามคำถามคุณ使用更多现成工具:维基百科、语音转文字、向你提问 WikipediaSearchTool, SpeechToTextTool, UserInputTool
borrow tools from elsewhere: the Hugging Face Hub, a Space, LangChain, any MCP serverยืมเครื่องมือจากที่อื่น เช่น Hugging Face Hub, Space, LangChain และเซิร์ฟเวอร์ MCP ใดก็ได้从别处借工具:Hugging Face Hub、Space、LangChain、任何 MCP 服务器 Tool.from_hub, Tool.from_space, Tool.from_langchain, ToolCollection.from_mcp, MCPClient
work as a team: a manager agent hands jobs to helper agentsทำงานเป็นทีม คือ agent หัวหน้าส่งงานให้ agent ผู้ช่วย组队干活:一个管理者智能体把任务交给助手智能体 managed_agents=, name=, description=
stop every few steps to make a planหยุดทุก ๆ ไม่กี่ step เพื่อวางแผน每隔几步停下来做个计划 planning_interval=
show its work live, as it happensแสดงงานแบบสด ๆ ขณะที่ทำ边做边实时显示 agent.run(task, stream=True), stream_outputs=
run your own check after each step, or before it accepts an answerรันการตรวจของคุณเองหลังทุก step หรือก่อนยอมรับคำตอบ每一步之后、或接受答案之前,运行你自己的检查 step_callbacks=, final_answer_checks=
look at pictures, with a vision modelดูรูปภาพได้ เมื่อใช้โมเดลที่มองเห็นได้配合视觉模型看图片 agent.run(task, images=[...])
chat in a web page instead of the terminalคุยผ่านหน้าเว็บแทน terminal在网页里聊天,而不是在终端里 GradioUI(agent).launch(), pip install "smolagents[gradio]"
run from the terminal without writing Pythonรันจาก terminal โดยไม่ต้องเขียน Python不写 Python,直接在终端里运行 smolagent "your task", webagent
share agents and tools, or load other people'sแชร์ agent และเครื่องมือ หรือโหลดของคนอื่นมาใช้分享智能体和工具,或者加载别人的 agent.push_to_hub(), CodeAgent.from_hub(), tool.push_to_hub()
send every run to a monitoring dashboardส่งทุกการรันไปยังแดชบอร์ดติดตามผล把每次运行发到监控面板 pip install "smolagents[telemetry]" (OpenTelemetry)

1 📦 Get ready — Ollama, a model, one library · เตรียมตัว — Ollama โมเดล และไลบรารีหนึ่งตัว · 准备 — Ollama、一个模型、一个库

🇬🇧 English

You need Ollama from the AI Teacher lesson. If you skipped that lesson, download it from ollama.com.

This page uses qwen3, the same model as the AI Teacher lesson. If you did that lesson, you already have it. Then install smolagents. The [litellm] part adds the small piece that lets smolagents talk to Ollama.

🇹🇭 ไทย

คุณต้องมี Ollama จากบทครู AI ถ้าข้ามบทนั้นไป ดาวน์โหลดได้ที่ ollama.com

หน้านี้ใช้ qwen3 โมเดลตัวเดียวกับบทครู AI ถ้าคุณทำบทนั้นแล้วก็มีอยู่แล้ว จากนั้นติดตั้ง smolagents ส่วน [litellm] คือชิ้นเล็ก ๆ ที่ทำให้ smolagents คุยกับ Ollama ได้

🇨🇳 中文

你需要 AI 老师那一课里的 Ollama。如果跳过了那一课,就到 ollama.com 下载。

本页用的是 qwen3,和 AI 老师那一课是同一个模型。做过那一课的话,你已经有它了。然后装 smolagents。[litellm] 这部分是让 smolagents 能和 Ollama 说话的小组件。

terminal

ollama pull qwen3                  # the brain, about 5 GB. Skip it if you have it.
pip install "smolagents[litellm]"
CAREFUL · WINDOWS Windows has a 260-character limit on file paths, and litellm has very deep folders. Install it inside a long, nested folder and pip can quietly skip files. If your first agent fails with module 'litellm' has no attribute 'completion', that is what happened. Make your virtual environment in a short folder, like C:\py\agents, and install again. Windows จำกัดความยาว path ไว้ที่ 260 ตัวอักษร และ litellm มีโฟลเดอร์ซ้อนกันลึกมาก ถ้าติดตั้งในโฟลเดอร์ที่ซ้อนลึก ๆ pip อาจข้ามไฟล์ไปเงียบ ๆ ถ้า agent ตัวแรกพังด้วยข้อความ module 'litellm' has no attribute 'completion' แปลว่าเกิดเรื่องนี้ ให้สร้าง virtual environment ในโฟลเดอร์ที่ path สั้น เช่น C:\py\agents แล้วติดตั้งใหม่ Windows 对文件路径有 260 个字符的限制,而 litellm 的文件夹层级很深。装在很深的文件夹里,pip 可能会悄悄漏掉文件。如果你的第一个智能体报错 module 'litellm' has no attribute 'completion',就是这个原因。把虚拟环境建在路径短的文件夹里,比如 C:\py\agents,再装一次。

2 🐣 Your first agent — six lines · agent ตัวแรกของคุณ — หกบรรทัด · 你的第一个智能体 — 六行

🇬🇧 English

Save this as agent.py and run python agent.py.

Then watch the terminal. smolagents prints every step in colour: what the model is thinking, the code it wrote, and what that code printed.

The question is easy on purpose. Language models are bad at long sums in their head, so the agent does not guess. It writes the sum in Python and lets Python do it.

🇹🇭 ไทย

บันทึกเป็นไฟล์ agent.py แล้วรัน python agent.py

จากนั้นดูที่ terminal smolagents จะแสดงทุกขั้นเป็นสี ทั้งสิ่งที่โมเดลกำลังคิด โค้ดที่มันเขียน และสิ่งที่โค้ดนั้นพิมพ์ออกมา

คำถามนี้ง่ายโดยตั้งใจ โมเดลภาษาคิดเลขยาว ๆ ในหัวได้ไม่ดี agent จึงไม่เดา มันเขียนการคำนวณเป็น Python แล้วให้ Python คิดให้

🇨🇳 中文

把它存成 agent.py,然后运行 python agent.py

然后看终端。smolagents 会用彩色打印每一步:模型在想什么、它写了什么代码、那段代码打印出了什么。

这个问题故意出得很简单。语言模型心算长算式很差,所以智能体不去猜。它把算式写成 Python,让 Python 来算。

agent.py

from smolagents import CodeAgent, LiteLLMModel

model = LiteLLMModel(
    model_id="ollama_chat/qwen3",   # the model you pulled
    api_base="http://localhost:11434",    # Ollama, on your own machine
    num_ctx=8192,                         # room for the agent's long instructions
)

agent = CodeAgent(tools=[], model=model)
agent.run("How many seconds are there in one week?")
YOU GET 604800 — and, above it, the Python the model wrote to find it. 604800 และด้านบนคือโค้ด Python ที่โมเดลเขียนเพื่อหาคำตอบ 604800 —— 上面还有模型为了算出它而写的 Python。
CAREFUL Keep num_ctx=8192. The agent's instructions are long. With Ollama's smaller default, the start of them gets cut off, and the model forgets how to write its answer. อย่าลบ num_ctx=8192 คำสั่งของ agent ยาวมาก ถ้าใช้ค่าเริ่มต้นที่เล็กกว่าของ Ollama ส่วนต้นจะถูกตัดทิ้ง แล้วโมเดลจะลืมวิธีเขียนคำตอบ 保留 num_ctx=8192。智能体的指令很长。用 Ollama 较小的默认值,开头会被截掉,模型就忘了该怎么写答案。 Be patient, too. On a laptop with no graphics card, this first answer took about three minutes in our test. ต้องใจเย็นด้วย ในการทดสอบของเรา แล็ปท็อปที่ไม่มีการ์ดจอใช้เวลาราวสามนาทีกว่าจะได้คำตอบแรก 还要有耐心。在我们的测试里,一台没有显卡的笔记本,得到这第一个答案花了大约三分钟。

3 🔍 Read the Python it wrote · อ่านโค้ด Python ที่มันเขียน · 看看它写的 Python

🇬🇧 English

The answer is only half of the story. The other half is the code the model wrote to get there. Read it. It shows you how the model worked, and it is how you catch an answer that looks right but is wrong.

There are four ways to see it:

  1. Watch the terminal. While the agent runs, smolagents prints each step. The code sits under a bar that says Executing parsed code.
  2. Ask its memory. After the run, agent.memory.steps holds every step. Each step that ran code keeps it in code_action.
  3. Save it as a file. agent.memory.return_full_code() joins all the code into one script you can keep, read slowly, and change.
  4. Replay everything. agent.replay() prints the whole run again. First come the long instructions smolagents gives the model (the reason for num_ctx=8192), then each Thought: and the code under it.

🇹🇭 ไทย

คำตอบเป็นแค่ครึ่งเดียวของเรื่อง อีกครึ่งคือโค้ดที่โมเดลเขียนเพื่อหาคำตอบนั้น อ่านมันด้วย มันบอกว่าโมเดลทำงานอย่างไร และเป็นวิธีจับคำตอบที่ดูเหมือนถูกแต่ที่จริงผิด

ดูได้สี่วิธี

  1. ดูที่ terminal ระหว่างที่ agent ทำงาน smolagents จะแสดงทุก step โค้ดอยู่ใต้แถบที่เขียนว่า Executing parsed code
  2. ถามความจำของมัน หลังรันเสร็จ agent.memory.steps เก็บทุก step ไว้ step ไหนที่รันโค้ด จะเก็บโค้ดนั้นไว้ใน code_action
  3. บันทึกเป็นไฟล์ agent.memory.return_full_code() รวมโค้ดทั้งหมดเป็นสคริปต์เดียว ให้คุณเก็บไว้ อ่านช้า ๆ และแก้ไขได้
  4. เล่นซ้ำทั้งหมด agent.replay() แสดงการรันทั้งหมดอีกครั้ง เริ่มจากคำสั่งยาว ๆ ที่ smolagents ให้โมเดล (นี่คือเหตุผลที่ต้องใช้ num_ctx=8192) แล้วตามด้วย Thought: แต่ละข้อและโค้ดข้างใต้

🇨🇳 中文

答案只是故事的一半。另一半是模型为了得到答案写的代码。去读它。它告诉你模型是怎么做的,也是你抓住"看起来对、其实错"的答案的办法。

有四种看法:

  1. 看终端。智能体运行时,smolagents 会打印每一步。代码在一条写着 Executing parsed code 的横条下面。
  2. 问它的记忆。运行结束后,agent.memory.steps 保存着每一步。运行过代码的步骤,把代码存在 code_action 里。
  3. 存成文件。agent.memory.return_full_code() 把所有代码合成一个脚本,你可以留着、慢慢读、再修改。
  4. 全部重放。agent.replay() 把整次运行再打印一遍。先是 smolagents 交给模型的一大段指令(这就是需要 num_ctx=8192 的原因),然后是每一个 Thought: 和它下面的代码。

save_code.py: the whole script, top to bottom

from smolagents import CodeAgent, LiteLLMModel

model = LiteLLMModel(
    model_id="ollama_chat/qwen3",
    api_base="http://localhost:11434",
    num_ctx=8192,
)
agent = CodeAgent(tools=[], model=model)

# 1) run the agent: it writes Python and runs it (watch the terminal)
agent.run("How many seconds are there in one week?")

# 2) print the code from each step
for step in agent.memory.steps:
    if getattr(step, "code_action", None):          # only steps that ran code
        print(f"--- step {step.step_number}")
        print(step.code_action)

# 3) save all of it to a NEW file, agent_code.py, in this same folder
#    "w" = write: it makes the file, or replaces an old one
code = agent.memory.return_full_code()
with open("agent_code.py", "w", encoding="utf-8") as f:
    f.write(code)
print("Saved agent_code.py")

# 4) replay the whole run, with the model's thinking
agent.replay()
WHAT HAPPENS
  1. The agent answers. While it works, the terminal shows its code under Executing parsed code, then Final answer: 604800.
  2. Your loop prints that code again, with --- step 1 above it. Now it is your program printing it, not smolagents.
  3. A new file appears. return_full_code() joins the code into one piece of text, and open("agent_code.py", "w") writes it to a file next to save_code.py. You see Saved agent_code.py. Run it twice and the second run replaces the first file.
  4. The replay prints everything once more, starting with the long instructions smolagents gives the model.
  5. You open the file with the commands below, and read the Python the model wrote.
  1. agent ตอบคำถาม ระหว่างที่ทำงาน terminal จะแสดงโค้ดของมันใต้ Executing parsed code แล้วตามด้วย Final answer: 604800
  2. ลูปของคุณพิมพ์โค้ดนั้นอีกครั้ง โดยมี --- step 1 อยู่ข้างบน ตอนนี้โปรแกรมของคุณเป็นคนพิมพ์ ไม่ใช่ smolagents
  3. มีไฟล์ใหม่เกิดขึ้น return_full_code() รวมโค้ดเป็นข้อความชิ้นเดียว แล้ว open("agent_code.py", "w") เขียนลงไฟล์ข้าง ๆ save_code.py คุณจะเห็น Saved agent_code.py ถ้ารันสองครั้ง ครั้งที่สองจะเขียนทับไฟล์เดิม
  4. replay แสดงทุกอย่างอีกรอบ เริ่มจากคำสั่งยาว ๆ ที่ smolagents ให้โมเดล
  5. คุณเปิดไฟล์ ด้วยคำสั่งข้างล่าง แล้วอ่านโค้ด Python ที่โมเดลเขียน
  1. 智能体回答问题。它工作时,终端会在 Executing parsed code 下面显示它的代码,然后是 Final answer: 604800
  2. 你的循环把那段代码再打印一遍,上面有一行 --- step 1。这次是你的程序在打印,不是 smolagents。
  3. 出现一个新文件。return_full_code() 把代码合成一段文字,open("agent_code.py", "w") 把它写进一个和 save_code.py 放在一起的文件。你会看到 Saved agent_code.py。运行两次的话,第二次会覆盖第一次的文件。
  4. replay 把一切再打印一遍,从 smolagents 交给模型的那一大段指令开始。
  5. 你打开这个文件(命令在下面),读一读模型写的 Python。

terminal

python save_code.py
cat agent_code.py          # Mac and Linux: show the new file
type agent_code.py         # Windows: show the new file

what our terminal showed after the answer

Final answer: 604800
--- step 1
# Calculate seconds in a week
seconds_per_minute = 60
minutes_per_hour = 60
hours_per_day = 24
days_per_week = 7

total_seconds = days_per_week * hours_per_day * minutes_per_hour * seconds_per_minute
final_answer(total_seconds)
Saved agent_code.py

agent_code.py from our run (qwen3 on a laptop)

# Calculate seconds in a week
seconds_per_minute = 60
minutes_per_hour = 60
hours_per_day = 24
days_per_week = 7

total_seconds = days_per_week * hours_per_day * minutes_per_hour * seconds_per_minute
final_answer(total_seconds)

The model writes new code on every run, so your file will not match ours exactly. Our first run named its steps differently, and got the same 604800. โมเดลเขียนโค้ดใหม่ทุกครั้งที่รัน ไฟล์ของคุณจึงจะไม่เหมือนของเราทุกตัวอักษร การรันครั้งแรกของเราตั้งชื่อตัวแปรต่างออกไป แต่ได้ 604800 เหมือนกัน 模型每次运行都会写新的代码,所以你的文件不会和我们的一模一样。我们第一次运行时变量名起得不一样,但同样得到 604800。

CAREFUL Run python agent_code.py and it stops with NameError: name 'final_answer' is not defined. final_answer() only exists inside the agent. Change it to print() and the file runs on its own. ถ้ารัน python agent_code.py มันจะหยุดพร้อมข้อความ NameError: name 'final_answer' is not defined เพราะ final_answer() มีอยู่แค่ใน agent เปลี่ยนเป็น print() แล้วไฟล์จะรันเองได้ 运行 python agent_code.py,它会停下并报 NameError: name 'final_answer' is not definedfinal_answer() 只存在于智能体内部。把它改成 print(),这个文件就能自己运行了。

4 🧰 Give it a tool · ให้เครื่องมือกับมัน · 给它一个工具

🇬🇧 English

A tool is a normal Python function with @tool on top. It lets the agent reach things it cannot know by itself: your files, your school's timetable, a sensor, a website.

The model never sees the inside of your function. It only reads the name, the type hints and the docstring. So the docstring is the instruction manual. Say what the tool does, and describe every argument under Args:.

🇹🇭 ไทย

เครื่องมือ (tool) คือฟังก์ชัน Python ธรรมดาที่มี @tool อยู่ข้างบน มันช่วยให้ agent เข้าถึงสิ่งที่มันรู้เองไม่ได้ เช่น ไฟล์ของคุณ ตารางเรียนของโรงเรียน เซนเซอร์ หรือเว็บไซต์

โมเดลไม่เคยเห็นข้างในฟังก์ชัน มันอ่านแค่ชื่อ type hint และ docstring ดังนั้น docstring คือคู่มือการใช้งาน บอกว่าเครื่องมือทำอะไร และอธิบายทุก argument ไว้ใต้ Args:

🇨🇳 中文

工具(tool)就是上面加了 @tool 的普通 Python 函数。它让智能体拿到自己不可能知道的东西:你的文件、学校的课表、一个传感器、一个网站。

模型从来看不到函数里面。它只读名字、类型提示和 docstring。所以 docstring 就是说明书:写清楚工具做什么,并在 Args: 下面说明每个参数。

timetable_agent.py

from smolagents import CodeAgent, LiteLLMModel, tool

TIMETABLE = {
    "Monday":    ["English", "Maths", "Python", "Art"],
    "Tuesday":   ["Science", "Python", "Thai", "Python"],
    "Wednesday": ["Maths", "English", "Music"],
    "Thursday":  ["Python", "Python", "Python", "Sport"],
    "Friday":    ["Thai", "Science", "English"],
}

@tool
def get_timetable(day: str) -> list:
    """Returns the list of lessons for one school day.

    Args:
        day: A weekday name, like "Monday".
    """
    return TIMETABLE.get(day.capitalize(), [])

model = LiteLLMModel(model_id="ollama_chat/qwen3",
                     api_base="http://localhost:11434", num_ctx=8192)

agent = CodeAgent(tools=[get_timetable], model=model)
agent.run("Which day has the most Python lessons?")
YOU GET Thursday. The model never saw the timetable. It had to ask the tool, day by day, and count. Thursday โมเดลไม่เคยเห็นตารางเรียน มันต้องถามเครื่องมือทีละวัน แล้วนับเอง Thursday(星期四)。模型从没见过课表。它只能一天一天去问工具,然后自己数。

5 🎭 Give it a role · ให้บทบาทกับมัน · 给它一个角色

🇬🇧 English

So far the agent is nobody in particular. You can give it a role: who it is, who it is talking to, and how it should answer.

In smolagents that is one setting, instructions=. What you write there is added to the long instructions smolagents already gives the model, so it counts on every step. Keep it short and concrete: who it is, how it speaks, and anything it must always do.

🇹🇭 ไทย

ตอนนี้ agent ยังไม่ได้เป็นใครเป็นพิเศษ คุณให้บทบาทกับมันได้ ว่ามันเป็นใคร กำลังคุยกับใคร และควรตอบแบบไหน

ใน smolagents ใช้ค่าเดียว คือ instructions= สิ่งที่คุณเขียนตรงนั้นจะถูกเพิ่มเข้าไปในคำสั่งยาว ๆ ที่ smolagents ให้โมเดลอยู่แล้ว จึงมีผลทุก step เขียนให้สั้นและชัด ว่ามันเป็นใคร พูดแบบไหน และต้องทำอะไรทุกครั้ง

🇨🇳 中文

到现在为止,智能体谁也不是。你可以给它一个角色:它是谁、在跟谁说话、应该怎么回答。

在 smolagents 里这只是一个设置:instructions=。你写在那里的话,会加到 smolagents 本来就给模型的那一大段指令里,所以每一步都算数。写得短而具体:它是谁、怎么说话、有什么事必须每次都做。

in agent.py

agent = CodeAgent(
    tools=[],
    model=model,
    instructions=("You are Kru Nari, a friendly English teacher at a school in Chiang Mai. "
                  "Answer in short, simple English."),
)

The next section gives Kru Nari something to read, and one more rule: look it up before you answer. หัวข้อถัดไปจะให้ครูนารีมีเอกสารให้อ่าน และกฎอีกหนึ่งข้อ คือค้นก่อนตอบ 下一节给 Kru Nari 一些可以读的东西,再加一条规则:先查,再答。

6 📚 Let it read your own documents · ให้มันอ่านเอกสารของคุณเอง · 让它读你自己的文件

🇬🇧 English

The model knows nothing about your school: the exam dates, the library rules, this week's homework. You could paste everything into the question, but that runs out of room fast.

The better way is an index: a quick way to find the right paragraph, so the agent only reads what it needs. The index below uses rank-bm25, a small library that scores each paragraph by the words it shares with the question. Search engines started with the same idea.

Put your documents in a folder called notes/: plain .txt files, one topic per file, a blank line between paragraphs.

🇹🇭 ไทย

โมเดลไม่รู้อะไรเลยเกี่ยวกับโรงเรียนของคุณ ทั้งวันสอบ กฎห้องสมุด หรือการบ้านสัปดาห์นี้ คุณจะวางทุกอย่างลงในคำถามก็ได้ แต่ที่ว่างจะหมดเร็วมาก

วิธีที่ดีกว่าคือดัชนี (index) ซึ่งช่วยหาย่อหน้าที่ใช่ได้เร็ว agent จะได้อ่านแค่สิ่งที่ต้องใช้ ดัชนีข้างล่างใช้ rank-bm25 ไลบรารีเล็ก ๆ ที่ให้คะแนนแต่ละย่อหน้าตามคำที่ตรงกับคำถาม เครื่องมือค้นหาในยุคแรกก็เริ่มจากความคิดนี้

ใส่เอกสารของคุณไว้ในโฟลเดอร์ชื่อ notes/ เป็นไฟล์ .txt ธรรมดา หนึ่งไฟล์ต่อหนึ่งเรื่อง เว้นบรรทัดว่างระหว่างย่อหน้า

🇨🇳 中文

模型对你的学校一无所知:考试日期、图书馆规定、这周的作业。你可以把所有东西都贴进问题里,但地方很快就不够用了。

更好的办法是索引(index):一种快速找到对的那一段的办法,让智能体只读它需要的内容。下面的索引用的是 rank-bm25,一个很小的库,按每一段和问题共有的词给它打分。搜索引擎最早也是从这个想法开始的。

把你的文件放进一个叫 notes/ 的文件夹:普通的 .txt 文件,一个主题一个文件,段落之间空一行。

notes/exams.txt (one of our three example files)

English speaking test

The Grade 9 English speaking test is on Friday 19 September, in Room 204, starting at 9:00.

Bring your student card and a pencil. Phones must stay in your bag.

Each student speaks for five minutes about a photo. The teacher gives the photo on the day.

terminal

pip install rank-bm25

school_agent.py

from pathlib import Path
from rank_bm25 import BM25Okapi
from smolagents import CodeAgent, LiteLLMModel, tool

# 1) the index: every paragraph of every .txt file in notes/
chunks = []
for path in sorted(Path("notes").glob("*.txt")):
    for para in path.read_text(encoding="utf-8").split("\n\n"):
        if para.strip():
            chunks.append((path.name, para.strip()))
index = BM25Okapi([text.lower().split() for _, text in chunks])

# 2) a tool that searches the index
@tool
def search_notes(query: str) -> str:
    """Searches the school's notes and returns the three most useful
    paragraphs, each with the name of the file it came from.

    Args:
        query: What to look for, in plain words.
    """
    scores = index.get_scores(query.lower().split())
    best = sorted(range(len(chunks)), key=lambda i: scores[i], reverse=True)[:3]
    return "\n\n".join(f"[{chunks[i][0]}] {chunks[i][1]}" for i in best)

# 3) the agent: the role from section 5, plus one rule
model = LiteLLMModel(model_id="ollama_chat/qwen3",
                     api_base="http://localhost:11434", num_ctx=8192)
agent = CodeAgent(
    tools=[search_notes],
    model=model,
    instructions=("You are Kru Nari, a friendly English teacher at a school in Chiang Mai. "
                  "Answer in short, simple English. Always use search_notes before you answer "
                  "a question about the school, and say which file you used."),
)
agent.run("When is the English speaking test, and what should I bring?")
YOU GET “The English speaking test is on Friday, 19 September at 9:00 AM in Room 204. Bring your student card, pencil, and keep your phone in your bag.” Every fact is right, and it is short and simple, as the role asked. But it did not say which file it used, although the instructions told it to. A role is a request, not a guarantee: check the answer the way you check the code. “The English speaking test is on Friday, 19 September at 9:00 AM in Room 204. Bring your student card, pencil, and keep your phone in your bag.” ข้อมูลถูกทุกข้อ และสั้นง่ายตามที่บทบาทขอ แต่มันไม่ได้บอกว่าใช้ไฟล์ไหน ทั้งที่คำสั่งบอกให้บอก บทบาทเป็นคำขอ ไม่ใช่คำรับประกัน ให้ตรวจคำตอบเหมือนที่ตรวจโค้ด “The English speaking test is on Friday, 19 September at 9:00 AM in Room 204. Bring your student card, pencil, and keep your phone in your bag.” 每个事实都对,而且简短易懂,符合角色的要求。但它没有说用了哪个文件,虽然指令里要求它说。角色是一个请求,不是保证:像检查代码一样检查答案。
CAREFUL This index matches words, not meaning. We asked it “What must I wear?” Its top answer was “Phones must stay in your bag”, which only shares the word must. The uniform paragraph (“Uniform: white shirt, dark blue trousers or skirt, black shoes”) never came up, because it shares no words with the question. So ask with the words your documents use. Thai notes need more work: this index splits text at spaces, and Thai does not put spaces between words. ดัชนีนี้จับคู่คำ ไม่ใช่ความหมาย เราถามว่า “What must I wear?” คำตอบอันดับแรกคือ “Phones must stay in your bag” ซึ่งมีคำตรงกันแค่คำว่า must ส่วนย่อหน้าเรื่องชุดนักเรียน (“Uniform: white shirt, dark blue trousers or skirt, black shoes”) ไม่โผล่มาเลย เพราะไม่มีคำไหนตรงกับคำถาม ดังนั้นให้ถามด้วยคำที่เอกสารของคุณใช้ และถ้าเป็นเอกสารภาษาไทยต้องทำเพิ่ม เพราะดัชนีนี้แบ่งข้อความตามช่องว่าง แต่ภาษาไทยไม่เว้นวรรคระหว่างคำ 这个索引匹配的是,不是意思。我们问它 “What must I wear?”,排第一的答案是 “Phones must stay in your bag”,两者只共有一个词 must。讲校服的那一段(“Uniform: white shirt, dark blue trousers or skirt, black shoes”)根本没出现,因为它和问题没有一个共同的词。所以提问时要用你文件里用的词。泰文笔记还需要多做一步:这个索引按空格切分文字,而泰文词与词之间不留空格。

7 🔲 A tool that makes a file — QR codes · เครื่องมือที่สร้างไฟล์ — คิวอาร์โค้ด · 会生成文件的工具 — 二维码

🇬🇧 English

In Six More Things Python Can Ask For you made a QR code with two lines of Python. Give those two lines to the agent as a tool, and it can make one whenever a job needs it: a poster for the notice board, a link to tonight's homework, a menu on a table.

This tool is not like get_timetable. It does not hand back facts, it makes a file. And the model never sees the picture. The most it gets is the sentence your tool returns, so make that sentence say exactly what happened.

🇹🇭 ไทย

ใน อีกหกอย่างที่ Python ขอได้ คุณสร้างคิวอาร์โค้ดด้วย Python สองบรรทัด เอาสองบรรทัดนั้นให้ agent เป็นเครื่องมือ แล้วมันจะสร้างให้ได้ทุกครั้งที่งานต้องใช้ เช่น โปสเตอร์ติดบอร์ด ลิงก์การบ้านคืนนี้ หรือเมนูบนโต๊ะ

เครื่องมือนี้ไม่เหมือน get_timetable มันไม่ได้ส่งข้อมูลกลับมา แต่สร้างไฟล์ และโมเดลไม่เคยเห็นรูปนั้นเลย สิ่งที่มันได้มากที่สุดคือประโยคที่เครื่องมือของคุณส่งกลับ ดังนั้นเขียนประโยคนั้นให้บอกชัดว่าเกิดอะไรขึ้น

🇨🇳 中文

Python 还能要来的六样东西里,你用两行 Python 做过二维码。把这两行作为工具交给智能体,它就能在需要时随时做一个:公告栏上的海报、今晚作业的链接、桌上的菜单。

这个工具和 get_timetable 不一样。它不返回事实,它生成一个文件。而且模型从来看不到那张图。它最多能拿到你的工具返回的那句话,所以那句话要把发生了什么写清楚。

terminal

pip install "qrcode[pil]"

qr_agent.py

import qrcode
from smolagents import CodeAgent, LiteLLMModel, tool

@tool
def make_qr_code(url: str, filename: str) -> str:
    """Makes a QR code picture for a web address and saves it as a PNG file.

    Args:
        url: The web address the QR code should open, like "https://krueng.ai/".
        filename: The name to save it under, ending in .png.
    """
    qrcode.make(url).save(filename)
    return f"Saved {filename}"

model = LiteLLMModel(model_id="ollama_chat/qwen3",
                     api_base="http://localhost:11434", num_ctx=8192)
agent = CodeAgent(tools=[make_qr_code], model=model)
agent.run("Make a QR code for https://krueng.ai/ and save it as krueng.png. "
          "Then tell me the file name.")

what the agent ran — both lines in one step (qwen3 on a laptop)

make_qr_code(url="https://krueng.ai/", filename="krueng.png")
final_answer("krueng.png")
Final answer: krueng.png
YOU GET krueng.png, a 600-byte picture. We scanned it with a barcode reader and it read back https://krueng.ai/. Now look at the two lines the model wrote: it called the tool and called final_answer() in the same step, so it never read the tool's reply. It could name the file only because it chose the name. When the answer depends on what a tool gives back, print() it and let the next step read it. krueng.png รูปขนาด 600 ไบต์ เราสแกนด้วยเครื่องอ่านบาร์โค้ด แล้วได้ https://krueng.ai/ กลับมา ทีนี้ดูสองบรรทัดที่โมเดลเขียน มันเรียกเครื่องมือแล้วเรียก final_answer() ใน step เดียวกัน แปลว่ามันไม่เคยอ่านคำตอบจากเครื่องมือเลย มันบอกชื่อไฟล์ได้เพราะมันเป็นคนตั้งชื่อเอง ถ้าคำตอบขึ้นอยู่กับสิ่งที่เครื่องมือส่งกลับ ให้ print() ออกมา แล้วปล่อยให้ step ถัดไปอ่าน krueng.png,一张 600 字节的图片。我们用条码阅读器扫它,读回来的是 https://krueng.ai/。再看模型写的那两行:它在同一步里调用了工具,又调用了 final_answer(),所以它根本没读工具的返回。它能说出文件名,只是因为名字是它自己起的。如果答案取决于工具返回什么,就先 print() 出来,让下一步去读。
CAREFUL The QR code holds whatever address the model passed in. If it types one from memory instead of the one you gave it, the picture is wrong and nothing looks wrong: a QR code cannot be read by eye. Put the address in the task, as above, and scan the picture with your own phone before you print two hundred posters. คิวอาร์โค้ดเก็บที่อยู่เว็บตามที่โมเดลส่งเข้าไป ถ้ามันพิมพ์จากความจำแทนที่อยู่ที่คุณให้ รูปก็จะผิด และจะไม่มีอะไรดูผิดเลย เพราะคิวอาร์โค้ดอ่านด้วยตาไม่ได้ ให้ใส่ที่อยู่ไว้ในโจทย์แบบข้างบน แล้วสแกนรูปด้วยมือถือของคุณเองก่อนจะพิมพ์โปสเตอร์สองร้อยแผ่น 二维码里存的是模型传进去的那个网址。如果它凭记忆打了一个,而不是你给的那个,图就是错的,而且看不出来:二维码用眼睛读不了。像上面那样把网址写进任务里,并且在印两百张海报之前,用你自己的手机扫一下这张图。

8 ⚖️ Code or JSON? Swap one word · โค้ดหรือ JSON? เปลี่ยนคำเดียว · 代码还是 JSON?换一个词

🇬🇧 English

smolagents has a second kind of agent, ToolCallingAgent. It does not write Python. Each step it writes one small block of JSON that names one tool and its arguments. That is how most AI products call tools.

Change one word in the code above and ask something simple. Then compare what the two agents print.

🇹🇭 ไทย

smolagents มี agent อีกแบบชื่อ ToolCallingAgent มันไม่เขียน Python แต่ละ step มันเขียน JSON ก้อนเล็กหนึ่งก้อน บอกชื่อเครื่องมือหนึ่งตัวกับ argument ของมัน ผลิตภัณฑ์ AI ส่วนใหญ่เรียกเครื่องมือแบบนี้

เปลี่ยนคำเดียวในโค้ดข้างบน แล้วถามอะไรง่าย ๆ จากนั้นเทียบสิ่งที่ agent ทั้งสองแบบพิมพ์ออกมา

🇨🇳 中文

smolagents 还有第二种智能体:ToolCallingAgent。它不写 Python。每一步它写一小块 JSON,指明一个工具和它的参数。大多数 AI 产品就是这样调用工具的。

把上面代码里的一个词换掉,问个简单的问题。然后比较两种智能体打印出来的东西。

from smolagents import ToolCallingAgent

agent = ToolCallingAgent(tools=[get_timetable], model=model)
agent.run("How many lessons are there on Wednesday?")
CodeAgentToolCallingAgent
The model writesโมเดลเขียน模型写的是 PythonJSON
One step canหนึ่ง step ทำได้一步能做 loop, do maths, call many toolsวนลูป คิดเลข เรียกหลายเครื่องมือ循环、算数、调用多个工具 call one toolเรียกเครื่องมือหนึ่งตัว调用一个工具
Best forเหมาะกับ适合 problems with many stepsโจทย์ที่มีหลายขั้น多步骤的问题 simple, predictable jobsงานง่ายที่คาดเดาได้简单、可预测的任务
The riskความเสี่ยง风险 runs code on your computerรันโค้ดบนเครื่องของคุณ在你电脑上运行代码 only runs the tools you gave itรันได้แค่เครื่องมือที่คุณให้只运行你给它的工具

9 🛡️ It runs code on your computer — be careful · มันรันโค้ดบนเครื่องคุณ — ต้องระวัง · 它在你电脑上运行代码 — 要小心

🇬🇧 English

A CodeAgent writes code, and your computer runs it. That is the whole point, and also the danger.

smolagents does not hand the code to normal Python. Its own small interpreter runs it line by line, and it refuses any import you have not allowed. Safe modules like math, random and datetime are allowed from the start. Add more by name.

🇹🇭 ไทย

CodeAgent เขียนโค้ด แล้วเครื่องของคุณรันโค้ดนั้น นี่คือจุดเด่นของมัน และก็เป็นอันตรายของมันด้วย

smolagents ไม่ส่งโค้ดให้ Python ปกติรัน ตัวแปลภาษาเล็ก ๆ ของมันเองรันทีละบรรทัด และจะปฏิเสธ import ที่คุณไม่ได้อนุญาต โมดูลที่ปลอดภัยอย่าง math, random และ datetime ใช้ได้ตั้งแต่แรก ถ้าจะเพิ่ม ให้ใส่ชื่อเข้าไป

🇨🇳 中文

CodeAgent 写代码,你的电脑来运行。这正是它的用处,也正是它的危险。

smolagents 不把代码交给普通的 Python。它用自己的小解释器一行一行地运行,并且拒绝任何你没允许的 importmathrandomdatetime 这类安全的模块一开始就能用。想加别的,就写上名字。

agent = CodeAgent(tools=[get_timetable], model=model,
                  additional_authorized_imports=["csv"])   # allow one more module

what you see if the model tries to import os

InterpreterError: Import of os is not allowed.
CAREFUL Hugging Face says this is not a security wall. It stops accidents, not attackers. If the agent reads anything a stranger wrote, like web pages or other people's files, run its code in a sandbox. executor_type="docker" puts the code in a container. E2B, Modal and Blaxel are cloud sandboxes. Hugging Face บอกว่านี่ไม่ใช่กำแพงความปลอดภัย มันกันอุบัติเหตุได้ แต่กันผู้โจมตีไม่ได้ ถ้า agent อ่านสิ่งที่คนแปลกหน้าเขียน เช่น หน้าเว็บ หรือไฟล์ของคนอื่น ให้รันโค้ดใน sandbox executor_type="docker" จะใส่โค้ดไว้ใน container ส่วน E2B, Modal และ Blaxel เป็น sandbox บนคลาวด์ Hugging Face 说这不是安全防线。它能防意外,防不了攻击者。如果智能体会读陌生人写的东西,比如网页或别人的文件,就把代码放进沙箱里跑。executor_type="docker" 会把代码放进容器。E2B、Modal 和 Blaxel 是云端沙箱。

10 🌐 Feed it the web — the built-in search tools · ป้อนเว็บให้มัน — เครื่องมือค้นหาที่มีมาให้ · 把网络喂给它 — 自带的搜索工具

🇬🇧 English

A model only knows what was in its training data, and that stops on the day training ended. Ask it about anything newer and it cannot know. A good model says so, as ours does below. A weaker one guesses, and a guess sounds just as sure as a fact.

The fix is to feed it the web. Give the agent a search tool, and it looks things up before it answers. smolagents comes with two: DuckDuckGoSearchTool searches and returns titles, links and short descriptions, and VisitWebpageTool opens one page and turns it into plain text the model can read. Search finds the page; visiting reads it.

🇹🇭 ไทย

โมเดลรู้แค่สิ่งที่อยู่ในข้อมูลที่ใช้ฝึกมัน และข้อมูลนั้นหยุดอยู่ที่วันที่ฝึกเสร็จ ถ้าถามเรื่องที่ใหม่กว่านั้น มันไม่มีทางรู้ โมเดลที่ดีจะบอกตรง ๆ เหมือนตัวที่เราทดสอบข้างล่าง ส่วนโมเดลที่อ่อนกว่าจะเดา และคำเดาก็ฟังดูมั่นใจพอ ๆ กับข้อเท็จจริง

ทางแก้คือป้อนเว็บให้มัน ให้เครื่องมือค้นหากับ agent แล้วมันจะค้นก่อนตอบ smolagents มีมาให้สองตัว DuckDuckGoSearchTool ค้นหาแล้วส่งชื่อเรื่อง ลิงก์ และคำอธิบายสั้น ๆ กลับมา ส่วน VisitWebpageTool เปิดหน้าเว็บหนึ่งหน้า แล้วแปลงเป็นข้อความธรรมดาที่โมเดลอ่านได้ การค้นหาช่วยหาหน้า การเปิดหน้าช่วยอ่านหน้า

🇨🇳 中文

模型只知道训练数据里有的东西,而训练数据停在训练结束的那一天。问它更新的事,它不可能知道。好的模型会直说,就像下面我们测试的这个。差一点的模型会猜,而猜的话听起来和事实一样肯定。

办法是把网络喂给它。给智能体一个搜索工具,它就会先查再答。smolagents 自带两个:DuckDuckGoSearchTool 负责搜索,返回标题、链接和简短描述;VisitWebpageTool 打开一个网页,把它变成模型能读的纯文本。搜索负责找到页面,访问负责读懂页面。

terminal

pip install "smolagents[toolkit]"     # the web tools: ddgs and markdownify

web_agent.py

from smolagents import CodeAgent, LiteLLMModel, DuckDuckGoSearchTool, VisitWebpageTool

model = LiteLLMModel(model_id="ollama_chat/qwen3",
                     api_base="http://localhost:11434",
                     num_ctx=16384)                      # web pages are long: more room

agent = CodeAgent(
    tools=[DuckDuckGoSearchTool(max_results=5),          # find pages
           VisitWebpageTool(max_output_length=5000)],    # read one, cut to 5,000 characters
    model=model,
    max_steps=6,
)

agent.run("What is the newest version of the smolagents Python library, "
          "and when was it released? Search the web, then say which page "
          "you got the answer from.")
YOU GET v1.26.0, released on May 29, 2026. That is right: PyPI says the same. Without the web, the same model could only say it did not know. One search was enough, because a GitHub snippet held the answer, so it never opened a page. It still lists PyPI as a source it never read. Check sources before you trust them. v1.26.0, released on May 29, 2026 ถูกต้อง PyPI ก็บอกแบบเดียวกัน ถ้าไม่มีเว็บ โมเดลตัวเดียวกันบอกได้แค่ว่าไม่รู้ ค้นครั้งเดียวก็พอ เพราะข้อความสั้น ๆ จาก GitHub มีคำตอบอยู่แล้ว มันจึงไม่ต้องเปิดหน้าเว็บเลย แต่มันยังอ้าง PyPI เป็นแหล่งข้อมูล ทั้งที่ไม่เคยเปิดอ่าน ตรวจแหล่งข้อมูลก่อนเชื่อ v1.26.0, released on May 29, 2026。答对了:PyPI 上也是这么写的。没有网络时,同一个模型只能说它不知道。搜一次就够了,因为 GitHub 的一条摘要里已经有答案,所以它根本没打开网页。但它还是把自己没读过的 PyPI 列成了来源。相信之前先核对来源。

11 👉 Now make it yours · ทำให้เป็นของคุณเอง · 把它变成你自己的

🇬🇧 English

  • Put your own class's timetable in TIMETABLE. Ask the agent which day is the heaviest.
  • Add a second tool, get_homework(day). Ask a question that needs both tools.
  • Set max_steps=2 and give it a hard job. Watch it run out of steps.
  • Remove the Args: part of a docstring and read the error.
  • Give it Google: Web Scraping with CloakBrowser, §9 builds a tool that searches, then ranks the results by a rule you choose.
  • See how others do it: CrewAI and AutoGen build the same kind of agent with job titles, or with agents that talk to each other.
  • Compare the shapes: LangChain joins a prompt, a model and a parser with a pipe, and always runs the same steps.
  • Read the loop itself: agents.py in the smolagents GitHub repo.

Hugging Face's free Agents Course, unit 2 goes further: web search, many agents working together, and agents that see pictures.

🇹🇭 ไทย

  • ใส่ตารางเรียนของห้องคุณเองลงใน TIMETABLE แล้วถาม agent ว่าวันไหนเรียนหนักที่สุด
  • เพิ่มเครื่องมือตัวที่สอง get_homework(day) แล้วถามคำถามที่ต้องใช้ทั้งสองตัว
  • ตั้ง max_steps=2 แล้วให้งานยาก ๆ ดูมันใช้ step จนหมด
  • ลบส่วน Args: ออกจาก docstring แล้วอ่านข้อความ error
  • ให้มันค้น Google ได้: Web Scraping with CloakBrowser ข้อ 9 สร้างเครื่องมือที่ค้นหา แล้วเรียงผลลัพธ์ตามกฎที่คุณเลือก
  • ดูวิธีของคนอื่น: CrewAI และ AutoGen สร้าง agent แบบเดียวกัน ด้วยตำแหน่งงาน หรือด้วย agent ที่คุยกันเอง
  • เทียบรูปร่างกัน: LangChain ต่อพรอมป์ต โมเดล และตัวแปลงผลลัพธ์ด้วยท่อ และทำขั้นตอนเดิมทุกครั้ง
  • อ่านตัว loop เอง คือไฟล์ agents.py ใน smolagents บน GitHub

Agents Course บทที่ 2 ของ Hugging Face เรียนฟรี และไปไกลกว่านี้ ทั้งการค้นเว็บ agent หลายตัวทำงานร่วมกัน และ agent ที่ดูรูปได้

🇨🇳 中文

  • 把你们班自己的课表放进 TIMETABLE。问智能体哪一天最累。
  • 加第二个工具 get_homework(day)。问一个两个工具都要用的问题。
  • max_steps=2,给它一个难的任务。看它把步数用光。
  • 删掉 docstring 里的 Args: 部分,读一读报错。
  • 让它能搜 Google:用 CloakBrowser 做网页抓取 §9 做了一个会搜索、再按你定的规则给结果排序的工具。
  • 看看别人怎么做:CrewAI 和 AutoGen 用职位、或者用互相交谈的智能体,做同一类事情。
  • 比较形状:LangChain 用管道把提示词、模型和解析器接起来,每次都走同样的步骤。
  • 读循环本身:smolagents 的 GitHub 仓库里的 agents.py

Hugging Face 免费的 Agents Course 第 2 单元走得更远:网页搜索、多个智能体合作,还有能看图的智能体。

When a word here is new, go back to Start Python. When your agent grows, Catch Your Own Bugs shows you how to test it. ถ้าเจอคำที่ยังไม่รู้ กลับไปที่ เริ่มเขียน Python เมื่อ agent ของคุณใหญ่ขึ้น จับบั๊กของตัวเอง จะสอนวิธีทดสอบมัน 遇到不认识的词,就回开始学 Python。等你的智能体变大了,抓住自己的 bug 教你怎么测试它。