โ ่ฟๅๆป่ง
๐ค Smolagents
HuggingFace โ ็ฎๆดๅผบๅคง็ Agent ๆกๆถ
Thought-Code-Observation
Code-as-Action
Python
๐ฅ 3็งAgentๅไฝ
๐ฅ Facts Survey่งๅ
Managed Agent
HuggingFace Hub้ๆ
๐ฆ pip install smolagents
๐ง 3็งAgent็ฑปๅ
๐ 3ๅฅYAMLๆ็คบ่ฏ
๐ง Tool ABC + JSON Schema
๐ค HF Hubๅทฅๅ
ทๅธๅบ
ไธใๆ ธๅฟๆถๆ
Smolagents ๆดไฝๆถๆ โ Code-first Agent Framework
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ MultiStepAgent (ๆฝ่ฑกๅบ็ฑป) โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ run() (ไธปๅพช็ฏ) โ โ
โ โ โ โ
โ โ 1. [ๅฏ้] planning: facts survey โ initial_plan โ โ
โ โ 2. while not done: โ โ
โ โ a. LLM call โ ็ๆ Action (Code / ToolCall / JSON) โ โ
โ โ b. execute() โ ๆง่กไปฃ็ ๆๅทฅๅ
ท โ โ
โ โ c. observation โ ๆทปๅ ๅฐ memory โ โ
โ โ d. [ๅฏ้] update_plan (ๆฏNๆญฅ) โ โ
โ โ 3. final_answer โ ่ฟๅ็ปๆ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ โ CodeAgent โ โ ToolCalling โ โ Structured โ โ
โ โ (ไปฃ็ ๆง่ก) โ โ Agent โ โ CodeAgent โ โ
โ โ Python REPL โ โ (JSONๅจไฝ) โ โ (JSONไปฃ็ ) โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ โ Tool ABC โ โ AgentMemory โ โ Planning โ โ
โ โ JSON Schema โ โ ActionStep โ โ FactsSurvey โ โ
โ โ Hub Push โ โ PlanningStepโ โ UpdatePlan โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
ไธ็ง Agent ๅไฝๅฏนๆฏ
๐ CodeAgent
- ่พๅบๆ ผๅผ: Thought + Code block
- ๆง่กๆนๅผ: Python REPL ๆง่ก
- ๅทฅๅ
ท่ฐ็จ: ๅๆ Python ๅฝๆฐ่ฐ็จ
- ๆ็คบ่ฏ: code_agent.yaml
- ไผๅฟ: ็ตๆดป๏ผๅฏ็ปๅๅคไธชๅทฅๅ
ท
- ๅฃๅฟ: ้่ฆไปฃ็ ่งฃๆ
๐ ToolCallingAgent
- ่พๅบๆ ผๅผ: JSON Action blob
- ๆง่กๆนๅผ: ๅทฅๅ
ท็ดๆฅ่ฐ็จ
- ๅทฅๅ
ท่ฐ็จ: {"name": "...", "arguments": {...}}
- ๆ็คบ่ฏ: toolcalling_agent.yaml
- ไผๅฟ: ้้
ๅ็ tool calling
- ๅฃๅฟ: ไธๅฏ็ปๅ
๐ StructuredCodeAgent
- ่พๅบๆ ผๅผ: JSON {thought, code}
- ๆง่กๆนๅผ: Python REPL ๆง่ก
- ๅทฅๅ
ท่ฐ็จ: JSON ไธญ็ code ๅญๆฎต
- ๆ็คบ่ฏ: structured_code_agent.yaml
- ไผๅฟ: ็ปๆๅ + ไปฃ็ ็ตๆดป
- ๅฃๅฟ: JSON + Code ๅ่งฃๆ
ไบใ๐ ๆ็คบ่ฏๅทฅ็จ๏ผๆ ธๅฟๆทฑๅบฆๅๆ๏ผ
Smolagents ็ๆ็คบ่ฏ่ฎพ่ฎกๆฏๆ็งไนฆ็บงๅซ็โโไธๅฅ YAML ๆ็คบ่ฏ๏ผๆฏๅฅๅซ system_prompt + planning(initial_plan + update_plan) + managed_agent + final_answer ๅๅคงๆจกๅใJinja2 ๆจกๆฟๅจๆๆณจๅ
ฅๅทฅๅ
ทๆ่ฟฐ๏ผๅฝขๆๅฎๆด็ๆ็คบ่ฏๅทฅ็จไฝ็ณปใ
YAML ๆ็คบ่ฏ็ปๆ
ๆฏไธช YAML ๆไปถ็ๅๅคงๆจกๅ:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ code_agent.yaml / toolcalling_agent.yaml โ
โ / structured_code_agent.yaml โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ 1. system_prompt (~150่ก) โ
โ โโ ่บซไปฝ + ๅพช็ฏ่ฏดๆ โ
โ โโ ็คบไพ (5-6ไธชๅฎๆด็คบไพ) โ
โ โโ ๅทฅๅ
ทๆณจๅ
ฅ (Jinja2) โ
โ โโ Managed Agent ๆณจๅ
ฅ โ
โ โโ ่งๅ (10-11ๆก) โ
โ โโ custom_instructions โ
โ โ
โ 2. planning โ
โ โโ initial_plan โ
โ โ โโ Facts Survey (3็ฑป) โ
โ โ โโ Plan (ๆญฅ้ชคๅ่กจ) โ
โ โโ update_plan_pre_messages โ
โ โโ update_plan_post_messages โ
โ โ
โ 3. managed_agent โ
โ โโ task (ๅญไปฃ็ไปปๅกๆ็คบ่ฏ) โ
โ โโ report (็ปๆๆฅๅๆ ผๅผ) โ
โ โ
โ 4. final_answer โ
โ โโ pre_messages (ๅคฑ่ดฅๅ้) โ
โ โโ post_messages โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ฅ CodeAgent System Prompt โ Thought/Code/Observation ๅพช็ฏ
You are an expert assistant who can solve any task using code blobs.
You will be given a task to solve as best you can.
To do so, you have been given access to a list of tools:
these tools are basically Python functions which you can call with code.
To solve the task, you must plan forward to proceed in a series of steps,
in a cycle of Thought, Code,
and Observation sequences.
At each step, in the 'Thought:' sequence, you should first
explain your reasoning towards solving the task and the tools that you want to use.
Then in the Code sequence you should write the code in simple Python.
The code sequence must be opened with '{{code_block_opening_tag}}',
and closed with '{{code_block_closing_tag}}'.
During each intermediate step, you can use 'print()' to save whatever important
information you will then need.
These print outputs will then appear in the 'Observation:' field.
In the end you have to return a final answer using the `final_answer` tool.
๐ก CodeAgent ็ๆ ธๅฟๅๆฐโโๅทฅๅ
ทไธๆฏ JSON ่ฐ็จ๏ผ่ๆฏ Python ไปฃ็ ไธญ็ๅฝๆฐ่ฐ็จใLLM ๅ answer = document_qa(document=document, question="...") ่้ {"name": "document_qa", "arguments": {...}}ใ่ฟ่ฎฉๅคๅทฅๅ
ท็ปๅๆไธบๅฏ่ฝใ
๐ฅ ToolCallingAgent System Prompt โ JSON Action ๆ ผๅผ
You are an expert assistant who can solve any task using tool calls.
You will be given a task to solve as best you can.
To do so, you have been given access to some tools.
The tool call you write is an action: after the tool is executed,
you will get the result of the tool call as an "observation".
This Action/Observation can repeat N times.
To provide the final answer to the task, use an action blob with
"name": "final_answer" tool. It is the only way
to complete the task, else you will be stuck on a loop.
Action:
{
"name": "final_answer",
"arguments": {"answer": "insert your final answer here"}
}
๐ก ToolCallingAgent ๆฏๆดไผ ็ป็ JSON ๅทฅๅ
ท่ฐ็จๆจกๅผโโๆฏๆฌกๅช่ฐ็จไธไธชๅทฅๅ
ท๏ผ่งๅฏ็ปๆๅๅๅณๅฎไธไธๆญฅใ็ฎๅ็ดๆฅไฝไธๅฏ็ปๅใ
๐ฅ StructuredCodeAgent โ JSON Thought+Code ่พๅบ
You are an expert assistant who can solve any task using code blobs.
...
In the end you have to return a final answer using the `final_answer` tool.
You will be generating a JSON object with the following structure:
```json
{
"thought": "...",
"code": "..."
}
```
๐ก StructuredCodeAgent ็ปๅไธค่
โโJSON ๆ ผๅผ็็ปๆๅ่พๅบ๏ผไฝ code ๅญๆฎตๆฏ Python ไปฃ็ ใๆขๆปก่ถณ็ปๆๅ่งฃๆ้ๆฑ๏ผๅไฟ็ไปฃ็ ็ตๆดปๆงใ
๐ฅ Facts Survey ่งๅๆจกๅผ๏ผๆ็ฌ็น็ๆ็คบ่ฏ่ฎพ่ฎก๏ผ
You are a world expert at analyzing a situation to derive facts,
and plan accordingly towards solving a task.
Below I will present you a task. You will need to:
1. build a survey of facts known or needed to solve the task,
2. make a plan of action to solve the task.
## 1. Facts survey
### 1.1. Facts given in the task
List here the specific facts given in the task that could help you
(there might be nothing here).
### 1.2. Facts to look up
List here any facts that we may need to look up.
Also list where to find each of these, for instance a website, a file...
### 1.3. Facts to derive
List here anything that we want to derive from the above by logical reasoning,
for instance computation or simulation.
Don't make any assumptions. For each item, provide a thorough reasoning.
Do not add anything else on top of three headings above.
## 2. Plan
Then for the given task, develop a step-by-step high-level plan
taking into account the above inputs and list of facts.
After writing the final step, write the '<end_plan>' tag.
๐ก Facts Survey ๆฏ Smolagents ๆ็ฌ็น็ๆ็คบ่ฏ่ฎพ่ฎกโโไธๆฏ็ดๆฅ่งๅ๏ผ่ๆฏๅ
ๆขณ็"ๅทฒ็ฅ/ๅพ
ๆฅ/ๅพ
ๆจๅฏผ"ไธ็ฑปไบๅฎ๏ผๅๅบไบไบๅฎ่งๅใ่ฟ้ฟๅ
ไบ LLM ๅจ่งๅๆถ้ๆผๅ
ณ้ฎไฟกๆฏๆๅๅบ้่ฏฏๅ่ฎพใ
๐ฅ Managed Agent ๅงๆๆจกๅผ
You're a helpful agent named '{{name}}'.
You have been submitted this task by your manager.
---
Task:
{{task}}
---
You're helping your manager solve a wider task: so make sure to not provide
a one-line answer, but give as much information as possible.
Your final_answer WILL HAVE to contain these parts:
### 1. Task outcome (short version):
### 2. Task outcome (extremely detailed version):
### 3. Additional context (if relevant):
Put all these in your final_answer tool, everything that you do not pass as an
argument to final_answer will be lost.
And even if your task resolution is not successful, please return as much context
as possible, so that your manager can act upon this feedback.
๐ก Managed Agent ๆ็คบ่ฏๅผบๅถ็ปๆๅ่พๅบโโ็ญ็ๆฌ+่ฏฆ็ป็ๆฌ+้ขๅคไธไธๆใ่ฟ้ฟๅ
ไบๅญไปฃ็่ฟๅ่ฟไบ็ฎ็ฅ็็ปๆใๅ
ณ้ฎ่ญฆๅ๏ผ"everything not passed as final_answer will be lost"ใ
๐ฅ Final Answer ๅ้ๆบๅถ
pre_messages: |-
An agent tried to answer a user query but it got stuck and failed to do so.
You are tasked with providing an answer instead.
Here is the agent's memory:
post_messages: |-
Based on the above, please provide an answer to the following user task:
{{task}}
๐ก ๅฝ Agent ่พพๅฐ max_steps ไปๆชๅฎๆไปปๅกๆถ๏ผไธๆฏ็ดๆฅๆฅ้๏ผ่ๆฏ็จๅฆไธไธช LLM ่ฐ็จๅบไบ Agent ็่ฎฐๅฟๆฅ็ๆๅ็ญใ่ฟๆฏไผ้
็้็บง็ญ็ฅใ
ๅทฅๅ
ทๆ่ฟฐๆณจๅ
ฅ โ Jinja2 ๆจกๆฟ
# CodeAgent / StructuredCodeAgent:
On top of performing computations in the Python code snippets that you create,
you only have access to these tools, behaving like regular python functions:
```python
{%- for tool in tools.values() %}
{{ tool.to_code_prompt() }}
{% endfor %}
```
# ToolCallingAgent:
You only have access to these tools:
{%- for tool in tools.values() %}
- {{ tool.to_tool_calling_prompt() }}
{%- endfor %}
# Managed Agents (CodeAgent):
def {{ agent.name }}(task: str, additional_args: dict[str, Any]) -> str:
"""{{ agent.description }}
Args:
task: Long detailed description of the task.
additional_args: Dictionary of extra inputs...
"""
๐ก CodeAgent ๅฐๅทฅๅ
ทๆ่ฟฐไธบ Python ๅฝๆฐ็ญพๅ๏ผToolCallingAgent ๆ่ฟฐไธบๅ่กจ้กนใๅไธๅทฅๅ
ทๅจไธๅ Agent ไธญๆไธๅๅ็ฐๆนๅผโโ่ฟๆฏ"ๆฅๅฃ้้
"็ๆ็คบ่ฏๅทฅ็จใ
ไธใโป๏ธ ไธปๅพช็ฏ
MultiStepAgent.run() ไธปๅพช็ฏ:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ run(task) โ
โ โ
โ 1. [ๅฏ้] Planning Phase โ
โ โโ ่ฐ็จ LLM ็ๆ Facts Survey โ
โ โ โโ 1.1 Facts given in the task โ
โ โ โโ 1.2 Facts to look up โ
โ โ โโ 1.3 Facts to derive โ
โ โโ ็ๆ Step-by-step Plan โ
โ โ
โ 2. Execution Loop โ
โ while not done: โ
โ โโ a. ๆๅปบๆ็คบ่ฏ (system + history + task) โ
โ โโ b. LLM call โ ็ๆ Action โ
โ โ โโ CodeAgent: Thought + Code block โ
โ โ โโ ToolCallingAgent: JSON Action blob โ
โ โ โโ StructuredCodeAgent: JSON {thought, code} โ
โ โโ c. ๆง่ก Action โ
โ โ โโ CodeAgent: PythonLocalExecutor.run() โ
โ โ โโ ToolCallingAgent: tool(**arguments) โ
โ โโ d. ็ๆ Observation โ
โ โโ e. ไฟๅญๅฐ AgentMemory (ActionStep) โ
โ โโ f. [ๅฏ้] update_plan (ๆฏ planning_interval ๆญฅ) โ
โ โโ g. ๆฃๆฅ final_answer โ ็ปๆๅพช็ฏ โ
โ โ
โ 3. [ๅ้] ๅฆๆ่พพๅฐ max_steps ๆชๅฎๆ: โ
โ โโ final_answer ๆจกๅ: ็จ Agent ่ฎฐๅฟ็ๆ็ญๆก โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
ๅ
ณ้ฎๅๆฐ:
โข max_steps: ๆๅคงๆง่กๆญฅๆฐ
โข planning_interval: ๆฏNๆญฅๆดๆฐ่งๅ (None=ไป
ๅๅง่งๅ)
โข additional_authorized_imports: ๅ
่ฎธ็ Python ๅฏผๅ
ฅ
ๅใ๐ง ๅทฅๅ
ท็ณป็ป
Tool ABC โ JSON Schema ้ฉฑๅจ็ๅทฅๅ
ทๅฎไน
Tool ๆฝ่ฑกๅบ็ฑป:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Tool (ABC) โ
โ โ
โ ๅฟ
้กปๅฎ็ฐ: โ
โ โข name: str โ
โ โข description: str โ
โ โข inputs: dict[str, dict] โ
โ โโ {"param": {"type": "...", โ
โ "description": "..."}} โ
โ โข output_type: str โ
โ โข forward(**kwargs) -> Any โ
โ โ
โ ่ชๅจ็ๆ: โ
โ โข to_code_prompt() โ Python็ญพๅ โ
โ โข to_tool_calling_prompt() โ ๅ่กจ โ
โ โข JSON Schema (inputs + output) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๅ
็ฝฎๅทฅๅ
ท: โ
โ โข DuckDuckGoSearchTool โ
โ โข VisitWebpageTool โ
โ โข WikipediaSearchTool โ
โ โข SpeechToTextTool โ
โ โข PythonInterpreterTool โ
โ โข FinalAnswerTool โ
โ โข UserInputTool โ
โ โข WebSearchTool (Bing) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Hub ้ปๆ: โ
โ โข push_to_hub() โ ไธไผ ๅฐ HF Hub โ
โ โข from_hub() โ ไป Hub ไธ่ฝฝ โ
โ โข load_tool() โ ๅจๆๅ ่ฝฝ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
ๅทฅๅ
ทๆ่ฟฐ็ไธค็งๅ็ฐๆนๅผ
ๅไธๅทฅๅ
ทๅจไธๅ Agent ไธญๆไธๅๅ็ฐ๏ผ
# CodeAgent (to_code_prompt):
def web_search(query: str) -> str:
"""Search the web for information.
Args:
query: The search query to look up.
Returns:
str: The search results.
"""
# ToolCallingAgent (to_tool_calling_prompt):
- web_search: Search the web for information.
- Takes inputs: {"query": {"type": "string", "description": "The search query..."}}
- Returns an output of type: str
๐ก ่ฟ็ง"ๅไธๅทฅๅ
ท๏ผไธค็งๆ่ฟฐ"็่ฎพ่ฎกๅพ็ฒพๅฆโโCodeAgent ้่ฆๅฝๆฐ็ญพๅไปฅๅจไปฃ็ ไธญ่ฐ็จ๏ผToolCallingAgent ้่ฆ็ปๆๅๆ่ฟฐไปฅๆๅปบ JSONใ
PythonLocalExecutor โ ๅฎๅ
จๆฒ็ฎฑ
- ็ถๆๆไน
ๅ๏ผๅ้ๅๅฏผๅ
ฅๅจๆญฅ้ชค้ดไฟๆ๏ผ
The state persists between code executions๏ผ
- ๅฏผๅ
ฅ็ฝๅๅ๏ผๅชๅ
่ฎธ
authorized_imports ๅ่กจไธญ็ๆจกๅ
- print() ๆ่ท๏ผprint ่พๅบๆไธบ Observation๏ผ่้ stdout
- final_answer ๅผบๅถ๏ผๅฟ
้กป้่ฟ
final_answer() ๅทฅๅ
ท่ฟๅ็ปๆ
ไบใ๐ง ่ฎฐๅฟ็ณป็ป
AgentMemory โ ๆญฅ้ชค็บง่ฎฐๅฟ + ๆ่ฆๅ็ผฉ
AgentMemory ็ปๆ:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ AgentMemory โ
โ โ
โ steps: list[ActionStep | โ
โ PlanningStep | โ
โ FinalAnswerStep] โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ ActionStep โ โ
โ โ โข model_input_messages โ โ
โ โ โข tool_calls โ โ
โ โ โข observations โ โ
โ โ โข action_output โ โ
โ โ โข token_usage โ โ
โ โ โข duration โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ PlanningStep โ โ
โ โ โข facts_survey โ โ
โ โ โข plan โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ FinalAnswerStep โ โ
โ โ โข final_answer โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ ๅ็ผฉๆจกๅผ: โ
โ โข summary_mode=True โ โ
โ ็จ LLM ็ๆๆญฅ้ชคๆ่ฆๆฟๆขๅฎๆดๅๅฒ โ
โ โข summary_steps: list[SummaryStep] โ
โ ๆฏไธชๆ่ฆ่ฆ็ไธๆฎตๆญฅ้ชค่ๅด โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
่ฎฐๅฟไธ่งๅ็ๅๅ
- PlanningStep ็ฌ็ซๅญๅจ๏ผFacts Survey ๅ Plan ไธๆฏๆททๅจๅๅฒๆถๆฏไธญ๏ผ่ๆฏ็ฌ็ซ็่ฎฐๅฟๆญฅ้ชค
- update_plan ๅฏไปฅ่ฎฟ้ฎๅๅฒ๏ผ่งๅๆดๆฐๆถๅฏไปฅ็ๅฐไนๅ็ๅฐ่ฏ็ปๆ๏ผๅบไบไบๅฎ่ฐๆด่ฎกๅ
- Token ่ฟฝ่ธช๏ผๆฏไธช ActionStep ่ฎฐๅฝ token ็จ้๏ผ็จไบๆๆฌ็ๆง
- ๆ่ฆๅ็ผฉ๏ผๅฝๅๅฒ่ฟ้ฟๆถ๏ผ็จ LLM ็ๆๆ่ฆๆฟๆขๆงๆญฅ้ชค
ๅ
ญใ๐ ๆ ธๅฟ็ฑป
| ็ฑป | ๆไปถ | ่่ดฃ |
MultiStepAgent | agents.py | ๆฝ่ฑกๅบ็ฑป๏ผrun() ไธปๅพช็ฏใ่งๅใ่ฎฐๅฟ็ฎก็ |
CodeAgent | agents.py | ไปฃ็ ๆง่ก Agent๏ผThought/Code/Observation ๅพช็ฏ |
ToolCallingAgent | agents.py | ๅทฅๅ
ท่ฐ็จ Agent๏ผJSON Action/Observation ๅพช็ฏ |
StructuredCodeAgent | agents.py | ็ปๆๅไปฃ็ Agent๏ผJSON {thought, code} ่พๅบ |
Tool | tools.py | ๅทฅๅ
ท ABC๏ผforward()ใJSON SchemaใHub ้ๆ |
PythonLocalExecutor | local_python_executor.py | Python ๆฒ็ฎฑ๏ผๅฎๅ
จๆง่กไปฃ็ ใ็ถๆๆไน
ๅ |
AgentMemory | memory.py | ่ฎฐๅฟ็ฎก็๏ผๆญฅ้ชคๅญๅจใๆ่ฆๅ็ผฉ |
ActionStep | memory.py | ๅจไฝๆญฅ้ชค๏ผ่พๅ
ฅใ่พๅบใ่งๅฏใtoken ็จ้ |
PlanningStep | memory.py | ่งๅๆญฅ้ชค๏ผFacts Survey + Plan |
FinalAnswerStep | memory.py | ๆ็ป็ญๆกๆญฅ้ชค |
ไธใ๐ ๅ
ณ้ฎๆไปถ
| ๆไปถ | ไฝ็จ | ไบฎ็น |
prompts/code_agent.yaml | CodeAgent ๆ็คบ่ฏ | ~150่ก system_prompt + ่งๅ + ๅงๆ + ๅ้ |
prompts/toolcalling_agent.yaml | ToolCallingAgent ๆ็คบ่ฏ | JSON Action ๆ ผๅผ๏ผๆด็ฎๆด |
prompts/structured_code_agent.yaml | StructuredCodeAgent ๆ็คบ่ฏ | JSON {thought, code} ่พๅบๆ ผๅผ |
agents.py | Agent ๅฎ็ฐ | 3็ง Agent ๅไฝ + ่งๅ + ๅงๆ |
tools.py | Tool ABC | JSON Schema + Hub ้ๆ |
memory.py | ่ฎฐๅฟ็ณป็ป | ActionStep/PlanningStep + ๆ่ฆ |
local_python_executor.py | Python ๆฒ็ฎฑ | ๅฎๅ
จๆง่ก + ็ถๆๆไน
ๅ |
ๅ
ซใโจ ่ฎพ่ฎกไบฎ็น
1. Facts Survey ่งๅๆจกๅผ
ไธๆฏ็ดๆฅ่งๅ๏ผ่ๆฏๅ
ๆขณ็ไธ็ฑปไบๅฎ๏ผๅทฒ็ฅ/ๅพ
ๆฅ/ๅพ
ๆจๅฏผ๏ผ๏ผๅๅบไบไบๅฎ่งๅใ่ฟๆฏ"็ดๆฅๅ่ฎกๅ"ๆดๅฏ้ โโLLM ๅจ่งๅๆถ็ปๅธธ้ๆผๅ
ณ้ฎไฟกๆฏๆๅๅบ้่ฏฏๅ่ฎพ๏ผFacts Survey ๅผบๅถๅฎๅ
"็็น"ใ
2. Code-as-Action ่ๅผ
CodeAgent ็ๆ ธๅฟๅๆฐโโๅทฅๅ
ท่ฐ็จไธๆฏ JSON๏ผ่ๆฏ Python ไปฃ็ ใ่ฟๆๅณ็ LLM ๅฏไปฅๅจไธไธชไปฃ็ ๅไธญ็ปๅๅคไธชๅทฅๅ
ท่ฐ็จใไฝฟ็จๅพช็ฏ/ๆกไปถใๅค็ไธญ้ด็ปๆใ่ฟๆฏ"ไปฃ็ ๅณ่กๅจ"็ๆ่ดไฝ็ฐใ
3. ๅทฅๅ
ทๆ่ฟฐ็ๅๆจกๆ้้
ๅไธๅทฅๅ
ทๅจ CodeAgent ไธญๅ็ฐไธบ Python ๅฝๆฐ็ญพๅ๏ผๅจ ToolCallingAgent ไธญๅ็ฐไธบ JSON Schema ๆ่ฟฐใ่ฟไธๆฏ็ฎๅ็ๆ ผๅผ่ฝฌๆข๏ผ่ๆฏ้ๅฏนไธๅ Agent ๆง่กๆจกๅ็ๆฅๅฃ้้
ใ
4. Final Answer ๅ้ๆบๅถ
ๅฝ Agent ่พพๅฐ max_steps ไปๆชๅฎๆ๏ผไธๆฏๆฅ้๏ผ่ๆฏ็จ Agent ็ๅฎๆด่ฎฐๅฟ่ฎฉๅฆไธไธช LLM ่ฐ็จ็ๆๆไฝณ็ญๆกใ่ฟๆฏไผ้
็้็บง็ญ็ฅโโๅณไฝฟ Agent "ๅกไฝไบ"๏ผไน่ฝ็ป็จๆทไธไบๆไปทๅผ็ไฟกๆฏใ
5. Managed Agent ็็ปๆๅ่พๅบ่ฆๆฑ
ๅญไปฃ็ๅฟ
้กป่ฟๅ"็ญ็ๆฌ+่ฏฆ็ป็ๆฌ+้ขๅคไธไธๆ"ไธๆฎตๅผ็ปๆใ่ฟ้ฟๅ
ไบๅญไปฃ็่ฟๅ่ฟไบ็ฎ็ฅ็็ปๆ๏ผไนๆนไพฟ็ฎก็่
ๆๅๅ
ณ้ฎไฟกๆฏใ
6. HuggingFace Hub ๅทฅๅ
ทๅธๅบ
Tool ABC ๅ
็ฝฎ push_to_hub() / from_hub()๏ผไปปไฝๅทฅๅ
ท้ฝๅฏไปฅไธ้ฎๅๅธๅฐ HF Hub๏ผๅ
ถไป็จๆทๅฏไปฅไธ่กไปฃ็ ๅ ่ฝฝใ่ฟๆฏ"ๅทฅๅ
ทๅณๆจกๅ"็็ๆๆ่ทฏใ
๐ฏ Smolagents ๅฏนไฝ ๆๅปบ Agent ็ๅฏ็คบ๏ผ
- Facts Survey > ็ดๆฅ่งๅโโๅ
็็นไบๅฎๅ่งๅ๏ผๆฏ็ดๆฅๅ่ฎกๅๆดๅฏ้ ใ่ฟไธชๆจกๅผๅฏไปฅๆจๅนฟๅฐไปปไฝ้่ฆ่งๅ็ Agent
- Code-as-Action ๆฏ็ๆญฃ็่ๅผๅๆฐโโPython ไปฃ็ ๆฏ JSON ๅทฅๅ
ท่ฐ็จ็ตๆดปๅพๅค๏ผไฝ้่ฆๅฎๅ
จ็ๆง่กๆฒ็ฎฑ
- ๅทฅๅ
ทๆ่ฟฐ้้
ๆฏๆ็คบ่ฏๅทฅ็จ็็ป่โโๅไธๅทฅๅ
ทๅจไธๅ Agent ไธญๅบๆไธๅๆ่ฟฐๆนๅผ
- Final Answer ๅ้ๅผๅพๅญฆไน โโไธๆฏๆๆ Agent ้ฝ่ฝๆๅๅฎๆไปปๅก๏ผไผ้
็้็บงๆฏ็กฌๆฅ้ๅฅฝ
- Managed Agent ็็ปๆๅ่พๅบ่ฆๆฑโโ"็ญ็ๆฌ+่ฏฆ็ป็ๆฌ+้ขๅคไธไธๆ"ๆฏๅญไปฃ็่พๅบ็ๆไฝณๅฎ่ทต
- YAML + Jinja2 ๆฏๆ็คบ่ฏ็ฎก็็ๆไฝณๅฎ่ทตโโๆฏ Python ๅญ็ฌฆไธฒๆผๆฅๆดๅฏ่ฏป๏ผๆฏ็กฌ็ผ็ ๆด็ตๆดป
- ไธ็ง Agent ๅไฝ่ฏๆไบโโไธๅ็ๆง่กๆจกๅ๏ผไปฃ็ /JSON/ๆททๅ๏ผ้ๅไธๅ็ๅบๆฏ๏ผๆฒกๆ้ถๅผน
ๆทฑๅบฆ็ ็ฉถ็ๆ ยท 2026-05-17 18:22