โ† ่ฟ”ๅ›žๆ€ป่งˆ

๐Ÿค— 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 โ€” ๅฎ‰ๅ…จๆฒ™็ฎฑ

ไบ”ใ€๐Ÿง  ่ฎฐๅฟ†็ณป็ปŸ

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] โ”‚ โ”‚ ๆฏไธชๆ‘˜่ฆ่ฆ†็›–ไธ€ๆฎตๆญฅ้ชค่Œƒๅ›ด โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

่ฎฐๅฟ†ไธŽ่ง„ๅˆ’็š„ๅๅŒ

ๅ…ญใ€๐Ÿ”‘ ๆ ธๅฟƒ็ฑป

็ฑปๆ–‡ไปถ่Œ่ดฃ
MultiStepAgentagents.pyๆŠฝ่ฑกๅŸบ็ฑป๏ผšrun() ไธปๅพช็Žฏใ€่ง„ๅˆ’ใ€่ฎฐๅฟ†็ฎก็†
CodeAgentagents.pyไปฃ็ ๆ‰ง่กŒ Agent๏ผšThought/Code/Observation ๅพช็Žฏ
ToolCallingAgentagents.pyๅทฅๅ…ท่ฐƒ็”จ Agent๏ผšJSON Action/Observation ๅพช็Žฏ
StructuredCodeAgentagents.py็ป“ๆž„ๅŒ–ไปฃ็  Agent๏ผšJSON {thought, code} ่พ“ๅ‡บ
Tooltools.pyๅทฅๅ…ท ABC๏ผšforward()ใ€JSON Schemaใ€Hub ้›†ๆˆ
PythonLocalExecutorlocal_python_executor.pyPython ๆฒ™็ฎฑ๏ผšๅฎ‰ๅ…จๆ‰ง่กŒไปฃ็ ใ€็Šถๆ€ๆŒไน…ๅŒ–
AgentMemorymemory.py่ฎฐๅฟ†็ฎก็†๏ผšๆญฅ้ชคๅญ˜ๅ‚จใ€ๆ‘˜่ฆๅŽ‹็ผฉ
ActionStepmemory.pyๅŠจไฝœๆญฅ้ชค๏ผš่พ“ๅ…ฅใ€่พ“ๅ‡บใ€่ง‚ๅฏŸใ€token ็”จ้‡
PlanningStepmemory.py่ง„ๅˆ’ๆญฅ้ชค๏ผšFacts Survey + Plan
FinalAnswerStepmemory.pyๆœ€็ปˆ็ญ”ๆกˆๆญฅ้ชค

ไธƒใ€๐Ÿ“‚ ๅ…ณ้”ฎๆ–‡ไปถ

ๆ–‡ไปถไฝœ็”จไบฎ็‚น
prompts/code_agent.yamlCodeAgent ๆ็คบ่ฏ~150่กŒ system_prompt + ่ง„ๅˆ’ + ๅง”ๆ‰˜ + ๅ›ž้€€
prompts/toolcalling_agent.yamlToolCallingAgent ๆ็คบ่ฏJSON Action ๆ ผๅผ๏ผŒๆ›ด็ฎ€ๆด
prompts/structured_code_agent.yamlStructuredCodeAgent ๆ็คบ่ฏJSON {thought, code} ่พ“ๅ‡บๆ ผๅผ
agents.pyAgent ๅฎž็Žฐ3็ง Agent ๅ˜ไฝ“ + ่ง„ๅˆ’ + ๅง”ๆ‰˜
tools.pyTool ABCJSON Schema + Hub ้›†ๆˆ
memory.py่ฎฐๅฟ†็ณป็ปŸActionStep/PlanningStep + ๆ‘˜่ฆ
local_python_executor.pyPython ๆฒ™็ฎฑๅฎ‰ๅ…จๆ‰ง่กŒ + ็Šถๆ€ๆŒไน…ๅŒ–

ๅ…ซใ€โœจ ่ฎพ่ฎกไบฎ็‚น

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 ็š„ๅฏ็คบ๏ผš
ๆทฑๅบฆ็ ”็ฉถ็”Ÿๆˆ ยท 2026-05-17 18:22