{"id":2459,"date":"2026-09-08T15:57:13","date_gmt":"2026-09-08T07:57:13","guid":{"rendered":"https:\/\/blog.zjml.life\/?p=2459"},"modified":"2026-09-08T15:58:56","modified_gmt":"2026-09-08T07:58:56","slug":"%e4%bb%8e%e9%9b%b6%e5%bc%80%e5%a7%8b%e5%ad%a6%e4%b9%a0-agent-%e5%bc%80%e5%8f%91-smolagents-%e6%a1%86%e6%9e%b6%e6%b7%b1%e5%ba%a6%e5%88%86%e6%9e%90%ef%bc%88%e4%ba%8c%ef%bc%89%ef%bc%9a","status":"publish","type":"post","link":"https:\/\/blog.zjml.life\/?p=2459","title":{"rendered":"\u4ece\u96f6\u5f00\u59cb\u5b66\u4e60 Agent \u5f00\u53d1 \u2014\u2014 smolagents \u6846\u67b6\u6df1\u5ea6\u5206\u6790\uff08\u4e8c\uff09\uff1aagents.py"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u4e0a\u4e00\u7bc7\u6211\u4eec\u770b\u5b8c\u4e86 cli.py\uff0c\u77e5\u9053\u4e86 Agent \u662f\u600e\u4e48\u88ab\u7ec4\u88c5\u8d77\u6765\u7684\u3002\u8fd9\u4e00\u7bc7\u6211\u4eec\u8fdb\u5165 agents.py\uff0c\u770b\u770b Agent \u5230\u5e95\u662f\u600e\u4e48&#8221;\u8dd1&#8221;\u8d77\u6765\u7684\u2014\u2014\u8fd9\u91cc\u624d\u662f smolagents \u7684\u6838\u5fc3\u3002<\/p>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-7b1167fd\"><h2 class=\"uagb-heading-text\">\u5165\u53e3\u51fd\u6570\u2014\u2014run()<\/h2><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u65b9\u6cd5\u662f agents.py \u7684\u5165\u53e3\u65b9\u6cd5\uff0c\u4ece\u8fd9\u91cc\u53ef\u4ee5\u5f00\u59cb\u7406\u89e3\u8fd9\u4e2a agent \u505a\u4e86\u4ec0\u4e48\u3002<\/p>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-28b16685\"><h3 class=\"uagb-heading-text\">\u53c2\u6570\u9884\u5904\u7406<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\uff0c\u8fd9\u91cc\u5bf9\u53c2\u6570\u8fdb\u884c\u4e86\u4e00\u4e9b\u9884\u5904\u7406\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        max_steps = max_steps or self.max_steps\n        self.task = task\n        self.interrupt_switch = False\n        if additional_args:\n            self.state.update(additional_args)\n            self.task += f\"\"\"\nYou have been provided with these additional arguments, that you can access directly using the keys as variables:\n{str(additional_args)}.\"\"\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u91cc\u628a\u4e00\u4e9b\u53c2\u6570\u653e\u5230\u4e86 self.state \u4e2d\uff0c\u8fd9\u6837 LLM \u5c31\u80fd\u591f\u77e5\u9053\u6709\u54ea\u4e9b\u53c2\u6570\u53ef\u4ee5\u88ab\u4f7f\u7528\uff0c\u5e76\u4e14\u80fd\u591f\u77e5\u9053\u8fd9\u4e9b\u53c2\u6570\u7684\u7c7b\u578b\u548c\u6570\u636e\u3002\u4ee5\u4e0b\u6211\u6765\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas as pd\n\ndf = pd.read_csv(\"sales.csv\")\nagent.run(\"\u627e\u51fa\u9500\u552e\u989d\u6700\u9ad8\u7684\u4e09\u4e2a\u6708\", additional_args={\"df\": df})<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">LLM \u6536\u5230\u7684\u4efb\u52a1\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u627e\u51fa\u9500\u552e\u989d\u6700\u9ad8\u7684\u4e09\u4e2a\u6708\nYou have been provided with these additional arguments, that you can access directly using the keys as variables:\n{'df':      month  sales\n0    2024-01  15000\n...}.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b83\u5c31\u4f1a\u5199\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>top3 = df.nlargest(3, \"sales\")\nfinal_answer(top3)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u770b\u5230\uff0c\u53d8\u91cf\u540d\u4f1a\u539f\u6837\u51fa\u73b0\u5728\u63d0\u793a\u8bcd\u91cc\uff0cLLM \u9760\u540d\u5b57\u731c\u7528\u9014\uff0c\u6240\u4ee5\u5f00\u53d1\u8005\u63d0\u9192\u6211\u4eec\uff0c\u53d8\u91cf\u540d\u4e00\u5b9a\u8981\u6309\u7167\u89c4\u8303\u53bb\u5199\uff0c\u8fd9\u6837 LLM \u624d\u80fd\u731c\u5f97\u51fa\u8fd9\u4e2a\u53d8\u91cf\u7684\u610f\u56fe\u3002\u5e76\u4e14\u8fd9\u91cc\u4f1a\u628a\u53c2\u6570\u6240\u6709\u7684\u5185\u5bb9\u90fd\u7ed9 LLM \uff0c\u6240\u4ee5\u4e00\u4e2a\u53d8\u91cf\u8981\u662f\u5f88\u5927\uff0c\u4ed6\u63d0\u4ea4\u7ed9 LLM \u4e5f\u4f1a\u6d88\u8017\u5f88\u591a token \u3002<\/p>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-87f69146\"><h3 class=\"uagb-heading-text\">\u521d\u59cb\u5316\u8bb0\u5fc6<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u5c31\u662f\u521d\u59cb\u5316\u8bb0\u5fc6\u7684\u90e8\u5206\uff0csmolagents \u5728\u8fd9\u91cc\u628a\u521d\u59cb\u63d0\u793a\u8bcd\u52a0\u5165\u5230\u4e86\u8bb0\u5fc6\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        self.memory.system_prompt = SystemPromptStep(system_prompt=self.system_prompt)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u770b\u4e00\u4e0b <code>self.system_prompt<\/code> \u65b9\u6cd5\u7684\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    @property\n    def system_prompt(self) -> str:\n        return self.initialize_system_prompt()\n\n    @abstractmethod\n    def initialize_system_prompt(self) -> str:\n        \"\"\"To be implemented in child classes\"\"\"\n        ...\n\nclass ToolCallingAgent(MultiStepAgent):\n    def initialize_system_prompt(self) -> str:\n        system_prompt = populate_template(\n            self.prompt_templates&#91;\"system_prompt\"],\n            variables={\n                \"tools\": self.tools,\n                \"managed_agents\": self.managed_agents,\n                \"custom_instructions\": self.instructions,\n            },\n        )\n        return system_prompt\n\nclass CodeAgent(MultiStepAgent):\n    def initialize_system_prompt(self) -> str:\n        system_prompt = populate_template(\n            self.prompt_templates&#91;\"system_prompt\"],\n            variables={\n                \"tools\": self.tools,\n                \"managed_agents\": self.managed_agents,\n                \"authorized_imports\": (\n                    \"You can import from any package you want.\"\n                    if \"*\" in self.authorized_imports\n                    else str(self.authorized_imports)\n                ),\n                \"custom_instructions\": self.instructions,\n                \"code_block_opening_tag\": self.code_block_tags&#91;0],\n                \"code_block_closing_tag\": self.code_block_tags&#91;1],\n            },\n        )\n        return system_prompt<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u770b\u5230\uff0c\u5bf9\u4e8e ToolCallingAgent \u548c CodeAgent \uff0c\u5206\u522b\u5b9e\u73b0\u4e86\u4e24\u5957\u7ec4\u88c5\u7cfb\u7edf\u63d0\u793a\u8bcd\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u518d\u770b\u4e00\u4e0b <code>SystemPromptStep()<\/code> \u65b9\u6cd5\u7684\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@dataclass\nclass SystemPromptStep(MemoryStep):\n    system_prompt: str\n\n    def to_messages(self, summary_mode: bool = False) -> list&#91;ChatMessage]:\n        if summary_mode:\n            return &#91;]\n        return &#91;ChatMessage(role=MessageRole.SYSTEM, content=&#91;{\"type\": \"text\", \"text\": self.system_prompt}])]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\u8fd0\u884c\u5b8c <code>self.memory.system_prompt = SystemPromptStep(system_prompt=self.system_prompt)<\/code> \u4e4b\u540e\uff0c<code>self.memory.system_prompt<\/code> \u662f\u8fd9\u6837\uff1a <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SystemPromptStep(system_prompt=\"You are an expert assistant who can solve any task using code blobs...\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u4e00\u5806\u63d0\u793a\u8bcd\u6765\u6559 Agent \u5982\u4f55\u5de5\u4f5c\uff0c\u8fd9\u90e8\u5206\u63d0\u793a\u8bcd\u5199\u5728\u4e86 prompts \u6587\u4ef6\u5939\u4e0b\u7684 yaml \u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u89c2\u5bdf\u5230\uff0c\u8fd9\u91cc\u6ca1\u6709\u628a\u7528\u6237\u672c\u6b21\u7684\u4efb\u52a1\u52a0\u8fdb\u53bb\uff0c\u6362\u53e5\u8bdd\u8bf4\uff0c\u7cfb\u7edf\u63d0\u793a\u8bcd\u4e0d\u542b\u4efb\u52a1\uff0c\u540c\u4e00\u4e2a agent \u8dd1\u4efb\u4f55\u4efb\u52a1\uff0c\u7cfb\u7edf\u63d0\u793a\u8bcd\u4e00\u6a21\u4e00\u6837\u3002\u56e0\u4e3a\u8fd9\u91cc\u6709\u5206\u5de5\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>system_prompt <\/td><td>&#8220;\u4f60\u662f\u8c01\u3001\u4f60\u6709\u4ec0\u4e48\u5de5\u5177\u3001\u4f60\u8be5\u600e\u4e48\u5de5\u4f5c&#8221;<\/td><td>\u4e0d\u53d8<\/td><\/tr><tr><td>TaskStep <\/td><td>&#8220;\u8fd9\u6b21\u8981\u5e72\u4ec0\u4e48&#8221; <\/td><td>\u6bcf\u6b21\u90fd\u53d8<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-dd73f2e3\"><h3 class=\"uagb-heading-text\">\u6e05\u7a7a\u5bf9\u8bdd\u5386\u53f2\u548c\u7edf\u8ba1\u6570\u636e<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c reset \uff0c\u5219\u9700\u8981\u6e05\u7a7a\u5bf9\u8bdd\u5386\u53f2\u548c\u7edf\u8ba1\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        if reset:\n            self.memory.reset()\n            self.monitor.reset()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u770b\u4e00\u4e0b\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u7684\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class AgentMemory:\n    def reset(self):\n        \"\"\"Reset the agent's memory, clearing all steps and keeping the system prompt.\"\"\"\n        self.steps = &#91;]\n\nclass Monitor:\n    def reset(self):\n        self.step_durations = &#91;]\n        self.total_input_token_count = 0\n        self.total_output_token_count = 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u770b\u5230\uff0c\u6ce8\u91ca\u7279\u610f\u8bf4\u4e86\uff0c\u4e0d\u6e05\u7a7a\u7cfb\u7edf\u63d0\u793a\u8bcd\u3002\u56e0\u4e3a\u7cfb\u7edf\u63d0\u793a\u8bcd\u6839\u672c\u4e0d\u5728 <code>steps<\/code> \u91cc\u9762\uff0c\u5b83\u662f <code>AgentMemory<\/code> \u4e0a\u4e00\u4e2a\u72ec\u7acb\u7684\u5c5e\u6027\u3002\u6240\u4ee5 <code>self.steps = []<\/code> \u4e0d\u4f1a\u6e05\u9664\u7cfb\u7edf\u63d0\u793a\u8bcd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u4e00\u4e0b AgentMemory \u7684\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class AgentMemory:\n    def __init__(self, system_prompt: str):\n        self.system_prompt: SystemPromptStep = SystemPromptStep(system_prompt=system_prompt)\n        self.steps: list&#91;TaskStep | ActionStep | PlanningStep] = &#91;]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6e05\u7a7a\u7edf\u8ba1\u6570\u636e\u975e\u5e38\u597d\u7406\u89e3\uff0c\u5c06\u6bcf\u6b65\u8017\u65f6\u3001\u8f93\u5165 token \u603b\u6570\u3001\u8f93\u51fa token \u603b\u6570\u90fd\u6e05\u7a7a\u4e86\u3002<\/p>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-60d93033\"><h3 class=\"uagb-heading-text\">\u6253\u5370\u65e5\u5fd7<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u7684\u4ee3\u7801\u7eaf\u7cb9\u662f\u4e00\u6bb5\u6253\u5370\u65e5\u5fd7\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        self.logger.log_task(\n            content=self.task.strip(),\n            subtitle=f\"{type(self.model).__name__} - {(self.model.model_id if hasattr(self.model, 'model_id') else '')}\",\n            level=LogLevel.INFO,\n            title=self.name if hasattr(self, \"name\") else None,\n        )<\/code><\/pre>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-0c8a43f4\"><h3 class=\"uagb-heading-text\">\u5c06\u4efb\u52a1\u653e\u5728\u8bb0\u5fc6\u4e2d<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u521a\u624d\u8bf4\u8fc7\uff0c\u4efb\u52a1\u6ca1\u6709\u88ab\u52a0\u5165\u5230\u7cfb\u7edf\u63d0\u793a\u8bcd\u4e2d\uff0c\u56e0\u4e3a\u4efb\u52a1\u53ef\u80fd\u56e0\u4e3a\u4e0b\u6b21\u5bf9\u8bdd\u800c\u53d1\u751f\u53d8\u5316\uff0c\u56e0\u6b64\u8fd9\u91cc\u5355\u72ec\u628a\u4efb\u52a1\u653e\u5728\u8bb0\u5fc6\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        self.memory.steps.append(TaskStep(task=self.task, task_images=images))<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc TaskStep \u5c31\u88ab\u653e\u5728 memory.steps \u91cc\u4e86\uff0c\u6211\u4eec\u770b\u4e00\u4e0b TaskStep \u7c7b\u7684\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@dataclass\nclass TaskStep(MemoryStep):\n    task: str\n    task_images: list&#91;\"PIL.Image.Image\"] | None = None\n\n    def to_messages(self, summary_mode: bool = False) -> list&#91;ChatMessage]:\n        content = &#91;{\"type\": \"text\", \"text\": f\"New task:\\n{self.task}\"}]\n        if self.task_images:\n            content.extend(&#91;{\"type\": \"image\", \"image\": image} for image in self.task_images])\n\n        return &#91;ChatMessage(role=MessageRole.USER, content=content)]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u770b\u5230\uff0c\u548c SystemPromptStep \u7c7b\u4e00\u6837\uff0c\u90fd\u63d0\u4f9b\u4e86\u4e00\u4e2a to_messages \u65b9\u6cd5\u3002<\/p>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-02e2cdb4\"><h3 class=\"uagb-heading-text\">\u586b\u5145\u89e3\u91ca\u5668<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">LLM \u6709\u5199python\u4ee3\u7801\u7684\u80fd\u529b\uff0c\u4f46\u662f\u5982\u4f55\u8fd0\u884cpython\u4ee3\u7801\u5462\uff1f\u6709\u4e24\u4e2a\u601d\u8def\uff0c\u7b2c\u4e00\u79cd\u6bd4\u8f83\u7b80\u5355\uff0c\u901a\u8fc7 docker \u7b49\u865a\u62df\u5316\u65b9\u5f0f\uff0c\u5728\u6c99\u76d2\u4e2d\u521b\u5efa\u4e00\u4e2a.py\u6587\u4ef6\uff0c\u7136\u540e\u8fd0\u884c\u3002\u7b2c\u4e8c\u79cd\u6bd4\u8f83\u9ebb\u70e6\uff0c\u4e5f\u662f smolagents \u9ed8\u8ba4\u7684\u65b9\u6cd5\uff0c\u5c31\u662f\u5199\u4e00\u4e2a\u89e3\u91ca\u5668\uff0c\u53bb\u5904\u7406 LLM \u5199\u7684\u4ee3\u7801\uff0c\u6253\u4e2a\u6bd4\u65b9\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">LLM \u5199\u4e86\u4ee5\u4e0b\u4ee3\u7801\uff1a x = x + y \uff0c\u6211\u4eec\u5199\u4e2a\u89e3\u91ca\u5668\uff0c\u6839\u636e\u6570\u636e\u7ed3\u6784\u7684\u65b9\u6cd5\uff0c\u628a\u8fd9\u4e2a\u4ee3\u7801\u7684\u8fd0\u7b97\u62c6\u5206\u51fa\u6765\uff0c\u4e5f\u5c31\u662fx + y \u548c x = \u4e24\u4e2a\u8fd0\u7b97\uff0c\u90a3\u4e48\u6211\u4eec\u5199\u4e00\u4e2a\u65b9\u6cd5\u6765\u5904\u7406x + y \u8fd0\u7b97\uff0c\u518d\u5199\u4e00\u4e2a\u65b9\u6cd5\u6765\u5904\u7406x = \u8fd0\u7b97\uff0c\u5c31\u53ef\u4ee5\u505a\u5230\u8fd9\u4e2a\u8fd0\u7b97\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">smolagents \u9ed8\u8ba4\u7684\u89e3\u91ca\u5668\u5728 local_python_executor.py \u4e2d\uff0c\u4ed6\u5e76\u6ca1\u6709\u5b8c\u5168\u5b9e\u73b0\u6240\u6709\u7684 python \u8bed\u6cd5\uff0c\u4f46\u662f\u5b9e\u73b0\u4e86\u5e38\u7528\u7684\uff0c\u591f\u7528\u5c31\u884c\u4e86\u3002\u6211\u4eec\u518d\u56de\u6765\u770b\u770b\u586b\u5145\u89e3\u91ca\u5668\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        if getattr(self, \"python_executor\", None):\n            self.python_executor.send_variables(variables=self.state)\n            self.python_executor.send_tools({**self.tools, **self.managed_agents})<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">send_variables() \u65b9\u6cd5\u628a\u53d8\u91cf\u653e\u8fdb\u5b57\u5178\uff0c send_tools() \u65b9\u6cd5\u628a\u5de5\u5177\u653e\u8fdb\u5b57\u5178\u3002\u8fd9\u6837 CodeAgent \u5c31\u80fd\u8c03\u7528\u4e86\u3002<\/p>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-6dd0161c\"><h3 class=\"uagb-heading-text\">\u542f\u52a8\u4e3b\u5faa\u73af<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8bbe\u5b9a\u7684\u662f\u6d41\u5f0f\u8f93\u51fa\uff0c\u90a3\u4e48\u73b0\u5728\u5c31\u53ef\u4ee5\u76f4\u63a5\u8fdb\u5165\u4e3b\u5faa\u73af\u4e86\uff0c\u4e5f\u5c31\u662f\u4ee3\u7801\u4e2d\u7684 _run_stream() \u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        if stream:\n            # The steps are returned as they are executed through a generator to iterate on.\n            return self._run_stream(task=self.task, max_steps=max_steps, images=images)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u73b0\u5728\u5148\u6765\u770b\u770b\uff0c\u5982\u679c\u4e0d\u6267\u884c\u6d41\u5f0f\u8f93\u51fa\u4e4b\u540e\u8fd8\u9700\u8981\u505a\u4ec0\u4e48\uff0c\u5f85\u4f1a\u513f\u6211\u4eec\u518d\u6765\u770b _run_stream() \u65b9\u6cd5\uff0c\u56e0\u4e3a\u5176\u5b9e\u65e0\u8bba\u662f\u6d41\u5f0f\u8f93\u51fa\u8fd8\u662f\u975e\u6d41\u5f0f\u8f93\u51fa\uff0c\u90fd\u662f\u9700\u8981\u4f9d\u9760\u8fd9\u4e2a\u65b9\u6cd5\u6765\u7ee7\u7eed\u7684\u3002\u800c\u975e\u6d41\u5f0f\u8f93\u51fa\u53ea\u662f\u5e2e\u6211\u4eec\u5728\u540e\u53f0\u9759\u9ed8\u5730\u8fd0\u884c\u5b8c\u6240\u6709\u7684\u6b65\u9aa4\u4e4b\u540e\uff0c\u5c55\u793a\u7ed9\u6211\u4eec\u3002<\/p>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-1716e27b\"><h3 class=\"uagb-heading-text\">\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u6ca1\u4ec0\u4e48\u597d\u8bf4\u7684\uff0c\u8bb0\u5f55\u4e00\u4e0b\u5f00\u59cb\u7684\u5f53\u524d\u65f6\u95f4\uff0c\u5f85\u4f1a\u513f\u901a\u8fc7\u7ed3\u675f\u65f6\u95f4\u53ef\u4ee5\u7b97\u51fa\u4e00\u5171\u6267\u884c\u4e86\u591a\u4e45\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        run_start_time = time.time()<\/code><\/pre>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-b02586a5\"><h3 class=\"uagb-heading-text\">\u6267\u884c\u4e3b\u5faa\u73af<\/h3><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>        steps = list(self._run_stream(task=self.task, max_steps=max_steps, images=images))<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u662f\u975e\u6d41\u5f0f\u8f93\u51fa\u7684\u6838\u5fc3\u4ee3\u7801\uff0c<code>list()<\/code> \u62ff\u5230\u4e00\u4e2a\u751f\u6210\u5668\uff0c\u4f1a\u4ece\u5934\u5230\u5c3e\u8fed\u4ee3\u4e00\u904d\uff0c\u628a\u6bcf\u6b21\u8fd4\u56de\u51fa\u6765\u7684\u4e1c\u897f\u88c5\u8fdb\u5217\u8868\u3002\u7b49\u4ef7\u4e8e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>steps = &#91;]\nfor item in self._run_stream(...):     # \u6bcf\u8f6c\u4e00\u5708\uff0cagent \u8d70\u4e00\u6b65\n    steps.append(item)<\/code><\/pre>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-728b15eb\"><h3 class=\"uagb-heading-text\">\u62ff\u5230 FinalAnswerStep<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><code>steps<\/code> \u7684\u7ed3\u6784\u6211\u4eec\u8fd9\u91cc\u5148\u7ed9\u51fa\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;0] ToolCall          \u2510\n&#91;1] ActionOutput      \u251c\u2500 \u7b2c 1 \u6b65\u7684\u4e09\u4e2a\u4e8b\u4ef6\n&#91;2] ActionStep        \u2518\n&#91;3] ToolCall          \u2510\n&#91;4] ActionOutput      \u251c\u2500 \u7b2c 2 \u6b65\u7684\u4e09\u4e2a\u4e8b\u4ef6\n&#91;5] ActionStep        \u2518\n&#91;6] FinalAnswerStep   \u2190 \u6536\u5c3e\uff0c\u6700\u540e\u4e00\u4e2a<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u770b\u5230\u6700\u540e\u4e00\u4e2a step \u7684\u7c7b\u578b\u662f FinalAnswerStep \uff0c\u8fd9\u5c31\u662f\u6a21\u578b\u6700\u7ec8\u7684\u8f93\u51fa\u7ed3\u679c\u3002smolagents \u63a5\u4e0b\u6765\u5c31\u8981\u5bf9\u8fd9\u4e2a\u7ed3\u679c\u8fdb\u884c\u5904\u7406\uff0c\u6240\u4ee5\u5148\u5728\u8fd9\u91cc\u505a\u4e86\u4e00\u4e2a\u5224\u65ad\uff0c\u5982\u679c\u7c7b\u578b\u4e0d\u7b26\uff0c\u76f4\u63a5\u62a5\u9519\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        # Outputs are returned only at the end. We only look at the last step.\n        assert isinstance(steps&#91;-1], FinalAnswerStep)\n        output = steps&#91;-1].output<\/code><\/pre>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-a38d7710\"><h3 class=\"uagb-heading-text\">\u5224\u65ad return_full_result \u914d\u7f6e\u9879<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u90a3\u4e2a\u5730\u65b9\u7528\u4e86\u4e00\u4e2a\u5f88\u7b80\u6d01\u7684\u7f16\u7a0b\u65b9\u5f0f\uff0c\u4f46\u662f\u6211\u89c9\u5f97\u53ef\u8bfb\u6027\u4e0d\u662f\u5f88\u9ad8\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        return_full_result = return_full_result if return_full_result is not None else self.return_full_result<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b49\u4ef7\u4e8e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if return_full_result is not None:\n    return_full_result = return_full_result      # \u7528\u53c2\u6570\u4f20\u7684\nelse:\n    return_full_result = self.return_full_result # \u7528\u6784\u9020\u65f6\u7684\u8bbe\u7f6e<\/code><\/pre>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-01652af9\"><h3 class=\"uagb-heading-text\">\u7edf\u8ba1 token<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u4e00\u5927\u6bb5\u4ee3\u7801\u90fd\u662f\u5728\u7edf\u8ba1 token \u7684\u6d88\u8017\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        if return_full_result:\n            total_input_tokens = 0\n            total_output_tokens = 0\n            correct_token_usage = True\n            for step in self.memory.steps:\n                if isinstance(step, (ActionStep, PlanningStep)):\n                    if step.token_usage is None:\n                        correct_token_usage = False\n                        break\n                    else:\n                        total_input_tokens += step.token_usage.input_tokens\n                        total_output_tokens += step.token_usage.output_tokens\n            if correct_token_usage:\n                token_usage = TokenUsage(input_tokens=total_input_tokens, output_tokens=total_output_tokens)\n            else:\n                token_usage = None<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u770b\u5230\uff0c\u53ea\u6709 ActionStep \u548c PlanningStep\u9700\u8981\u7edf\u8ba1 token \u7684\u6d88\u8017\uff0c\u56e0\u4e3a\u53ea\u6709\u8fd9\u4e24\u79cd step \u662f\u8c03\u7528\u4e86 LLM \u7684\u3002\u50cf TaskStep \u8fd9\u79cd step \uff0c\u4ed6\u6ca1\u6709\u8c03\u7528 LLM \uff0c\u5c31\u81ea\u7136\u4e0d\u9700\u8981\u53bb\u7edf\u8ba1token\u7684\u6d88\u8017\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53e6\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u89c2\u5bdf\u5230\uff0c\u8fd9\u91cc\u6709\u4e00\u4e2a correct_token_usage  \u6807\u5fd7\u4f4d\uff0c\u8fd9\u91cc\u7684\u8bbe\u8ba1\u662f\u9632\u6b62\u67d0\u4e00\u6b21 step \u4e2d LLM \u6ca1\u6709\u8fd4\u56de token \u6d88\u8017\uff0c\u8fd9\u4ee3\u8868\u7740\u6709\u53ef\u80fd\u662f\u51fa\u73b0\u4e86\u4e00\u4e9b\u95ee\u9898\uff0c\u6240\u4ee5\u5b83\u8fd9\u91cc\u63d0\u4f9b\u4e86\u4e00\u4e2a\u6807\u5fd7\u4f4d\uff0c\u7528\u4e8e\u8bb0\u5f55\u662f\u5426\u51fa\u73b0\u4e86\u51fa\u73b0\u95ee\u9898\u7684\u60c5\u51b5\uff0c\u4ee5\u9632\u6b62\u95ee\u9898\u88ab\u8fd9\u6837\u63a9\u76d6\u4e0b\u53bb\u4e86\u3002<\/p>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-f0a85e83\"><h3 class=\"uagb-heading-text\">\u5224\u65ad\u7ec8\u6b62\u539f\u56e0<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765 smolagents \u8fd8\u5224\u65ad\u4e86\u4e00\u4e0b\u7ec8\u6b62\u539f\u56e0\uff0c\u5224\u65ad\u8fd9\u6b21\u8fd0\u884c\u662f&#8221;\u6b63\u5e38\u7b54\u5b8c&#8221;\u8fd8\u662f&#8221;\u8dd1\u6ee1\u6b65\u6570\u88ab\u8feb\u6536\u573a&#8221;\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            if self.memory.steps and isinstance(getattr(self.memory.steps&#91;-1], \"error\", None), AgentMaxStepsError):\n                state = \"max_steps_error\"\n            else:\n                state = \"success\"<\/code><\/pre>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-84c10800\"><h3 class=\"uagb-heading-text\">\u66ff\u6362 Step \u5bf9\u8c61<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">smolagents \u628a memory \u91cc\u7684 Step \u5bf9\u8c61\uff0c\u5168\u90e8\u8f6c\u6362\u6210\u666e\u901a\u7684\u5b57\u5178\uff0c\u65b9\u4fbf\u7528\u6237\u672a\u6765\u8f6c\u4e3a Json \u683c\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            step_dicts = self.memory.get_full_steps()\n\nclass AgentMemory:\n    def get_full_steps(self) -> list&#91;dict]:\n        \"\"\"Return a full representation of the agent's steps, including model input messages.\"\"\"\n        if len(self.steps) == 0:\n            return &#91;]\n        return &#91;step.dict() for step in self.steps]<\/code><\/pre>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-89db6e58\"><h3 class=\"uagb-heading-text\">\u8fd4\u56de RunResult<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u662f return_full_result \uff0c\u90a3\u4e48\u5c31\u8fd4\u56de RunResult \u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            return RunResult(\n                output=output,\n                token_usage=token_usage,\n                steps=step_dicts,\n                timing=Timing(start_time=run_start_time, end_time=time.time()),\n                state=state,\n            )<\/code><\/pre>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-9d574c60\"><h2 class=\"uagb-heading-text\">\u6838\u5fc3\u65b9\u6cd5\u2014\u2014_run_stream()<\/h2><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u521a\u521a\u6211\u4eec\u770b\u4e86 run() \u65b9\u6cd5\uff0c\u53ef\u4ee5\u53d1\u73b0\u5b83\u4e8b\u5b9e\u4e0a\u628a\u6574\u4e2a\u8fd0\u884c\u7684\u6b65\u9aa4\u90fd\u4e22\u7ed9\u4e86 _run_stream() \u65b9\u6cd5\uff0c\u90a3\u4e48\u6211\u4eec\u73b0\u5728\u5c31\u6765\u770b\u770b\u8fd9\u4e2a _run_stream() \u65b9\u6cd5\uff0c\u5230\u5e95\u662f\u5982\u4f55\u8ba9\u6574\u4e2aAgent\u5faa\u73af\u8dd1\u8d77\u6765\u7684\u3002<\/p>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-d3f1fbe6\"><h3 class=\"uagb-heading-text\">\u7b2c\u4e00\u6b21\u5faa\u73af\u524d\u7684\u521d\u59cb\u5316<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u7b2c\u4e00\u6b21\u5faa\u73af\u5f00\u59cb\u524d\uff0c\u9700\u8981\u5148\u5c06\u6b65\u6570\u8bbe\u4e3a1\uff0c\u5e76\u4e14\u5c06 <code>returned_final_answer <\/code>\u8bbe\u4e3a False\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">self.step_number = 1<br>returned_final_answer = False<\/pre>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-829391f7\"><h3 class=\"uagb-heading-text\">\u5f00\u59cb\u5faa\u73af<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u5c31\u662f\u5f00\u59cb\u5faa\u73af\u4e86\uff0c\u6bcf\u6b21\u5faa\u73af\u524d\u9700\u8981\u5148\u68c0\u67e5\u662f\u5426\u9700\u8981\u4e2d\u65ad\u3002\u5982\u679c returned_final_answer \u4e3a True \u6216\u8005\u6267\u884c\u7684\u6b65\u6570\u8d85\u8fc7\u914d\u7f6e\u7684\u6700\u5927\u6b65\u6570\u7684\u65f6\u5019\uff0c\u5c31\u4e0d\u7ee7\u7eed\u8fdb\u884c\u5faa\u73af\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        while not returned_final_answer and self.step_number &lt;= max_steps:<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7531\u4e8e\u8fd9\u4e2a\u5faa\u73af\u5f00\u59cb\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u80fd\u8fd8\u5e0c\u671b\u6709\u4e00\u4e2a\u65b9\u6cd5\u80fd\u591f\u4e2d\u65ad\u3002\u6240\u4ee5 smolagents \u5728\u8fd9\u91cc\u51c6\u5907\u4e86\u4e00\u4e2a self.interrupt_switch \uff0c\u5982\u679c\u6211\u4eec\u4e0d\u60f3\u8ba9\u5b83\u7ee7\u7eed\u5faa\u73af\u4e86\uff0c\u5c31\u53ef\u4ee5\u8c03\u7528 interrupt(self) \u65b9\u6cd5\uff0c\u8fd9\u6837\u4ed6\u5c31\u4e0d\u4f1a\u7ee7\u7eed\u5faa\u73af\u4e86\u3002\u4f46\u662f\u6211\u770b\u4e86\u4ee3\u7801\uff0c\u5e76\u6ca1\u6709\u4efb\u4f55\u4e00\u4e2a\u5730\u65b9\u8c03\u7528\u4e86\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u6240\u4ee5\u8fd9\u91cc\u4e8b\u5b9e\u4e0a\u662f\u7559\u7ed9\u4e8c\u6b21\u5f00\u53d1\u7684\u4e00\u4e2a\u951a\uff0c\u76ee\u524d\u8fd9\u4e2a\u9879\u76ee\u5e76\u6ca1\u6709\u7528\u5230\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            if self.interrupt_switch:\n                raise AgentError(\"Agent interrupted.\", self.logger)<\/code><\/pre>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-8aec385b\"><h3 class=\"uagb-heading-text\">\u89c4\u5212\u6b65\u9aa4<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u5c31\u5224\u65ad\u5f53\u524d\u5faa\u73af\u9700\u4e0d\u9700\u8981\u8fdb\u884c\u89c4\u5212 step \uff0c\u5982\u679c planning_interval=3 \uff0c\u90a3\u4e48\u5c31\u5728\u7b2c 1\u30014\u30017 \u6b65\u89c4\u5212\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u5730\u65b9\u6211\u7814\u7a76\u4e86\u5f88\u4e45\uff0c\u56e0\u4e3a _generate_planning_step() \u8fd9\u4e2a\u65b9\u6cd5\u786e\u5b9e\u662f\u6709\u70b9\u5927\uff0c\u5b83\u9488\u5bf92\u00d72=4\u79cd\u60c5\u51b5\u90fd\u505a\u4e86\u5904\u7406\uff0c\u4f46\u662f\u5176\u5b9e\u6211\u4eec\u770b\u5b83\u7684\u8fd4\u56de\u503c\u8fd8\u662f\u5f88\u6e05\u6670\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    def _generate_planning_step(\n        self, task, is_first_step: bool, step: int\n    ) -> Generator&#91;ChatMessageStreamDelta | PlanningStep]:\n        start_time = time.time()\n        if is_first_step:\n            input_messages = &#91;\n                ChatMessage(\n                    role=MessageRole.USER,\n                    content=&#91;\n                        {\n                            \"type\": \"text\",\n                            \"text\": populate_template(\n                                self.prompt_templates&#91;\"planning\"]&#91;\"initial_plan\"],\n                                variables={\"task\": task, \"tools\": self.tools, \"managed_agents\": self.managed_agents},\n                            ),\n                        }\n                    ],\n                )\n            ]\n            if self.stream_outputs and hasattr(self.model, \"generate_stream\"):\n                plan_message_content = \"\"\n                output_stream = self.model.generate_stream(input_messages, stop_sequences=&#91;\"&lt;end_plan>\"])  # type: ignore\n                input_tokens, output_tokens = 0, 0\n                with Live(\"\", console=self.logger.console, vertical_overflow=\"visible\") as live:\n                    for event in output_stream:\n                        if event.content is not None:\n                            plan_message_content += event.content\n                            live.update(Markdown(plan_message_content))\n                            if event.token_usage:\n                                input_tokens = event.token_usage.input_tokens\n                                output_tokens += event.token_usage.output_tokens\n                        yield event\n            else:\n                plan_message = self.model.generate(input_messages, stop_sequences=&#91;\"&lt;end_plan>\"])\n                plan_message_content = plan_message.content\n                input_tokens, output_tokens = 0, 0\n                if plan_message.token_usage:\n                    input_tokens = plan_message.token_usage.input_tokens\n                    output_tokens = plan_message.token_usage.output_tokens\n            plan = textwrap.dedent(\n                f\"\"\"Here are the facts I know and the plan of action that I will follow to solve the task:\\n```\\n{plan_message_content}\\n```\"\"\"\n            )\n        else:\n            # Summary mode removes the system prompt and previous planning messages output by the model.\n            # Removing previous planning messages avoids influencing too much the new plan.\n            memory_messages = self.write_memory_to_messages(summary_mode=True)\n            plan_update_pre = ChatMessage(\n                role=MessageRole.SYSTEM,\n                content=&#91;\n                    {\n                        \"type\": \"text\",\n                        \"text\": populate_template(\n                            self.prompt_templates&#91;\"planning\"]&#91;\"update_plan_pre_messages\"], variables={\"task\": task}\n                        ),\n                    }\n                ],\n            )\n            plan_update_post = ChatMessage(\n                role=MessageRole.USER,\n                content=&#91;\n                    {\n                        \"type\": \"text\",\n                        \"text\": populate_template(\n                            self.prompt_templates&#91;\"planning\"]&#91;\"update_plan_post_messages\"],\n                            variables={\n                                \"task\": task,\n                                \"tools\": self.tools,\n                                \"managed_agents\": self.managed_agents,\n                                \"remaining_steps\": (self.max_steps - step),\n                            },\n                        ),\n                    }\n                ],\n            )\n            input_messages = &#91;plan_update_pre] + memory_messages + &#91;plan_update_post]\n            if self.stream_outputs and hasattr(self.model, \"generate_stream\"):\n                plan_message_content = \"\"\n                input_tokens, output_tokens = 0, 0\n                with Live(\"\", console=self.logger.console, vertical_overflow=\"visible\") as live:\n                    for event in self.model.generate_stream(\n                        input_messages,\n                        stop_sequences=&#91;\"&lt;end_plan>\"],\n                    ):  # type: ignore\n                        if event.content is not None:\n                            plan_message_content += event.content\n                            live.update(Markdown(plan_message_content))\n                            if event.token_usage:\n                                input_tokens = event.token_usage.input_tokens\n                                output_tokens += event.token_usage.output_tokens\n                        yield event\n            else:\n                plan_message = self.model.generate(input_messages, stop_sequences=&#91;\"&lt;end_plan>\"])\n                plan_message_content = plan_message.content\n                input_tokens, output_tokens = 0, 0\n                if plan_message.token_usage:\n                    input_tokens = plan_message.token_usage.input_tokens\n                    output_tokens = plan_message.token_usage.output_tokens\n            plan = textwrap.dedent(\n                f\"\"\"I still need to solve the task I was given:\\n```\\n{self.task}\\n```\\n\\nHere are the facts I know and my new\/updated plan of action to solve the task:\\n```\\n{plan_message_content}\\n```\"\"\"\n            )\n        log_headline = \"Initial plan\" if is_first_step else \"Updated plan\"\n        self.logger.log(Rule(f\"&#91;bold]{log_headline}\", style=\"orange\"), Text(plan), level=LogLevel.INFO)\n        yield PlanningStep(\n            model_input_messages=input_messages,\n            plan=plan,\n            model_output_message=ChatMessage(role=MessageRole.ASSISTANT, content=plan_message_content),\n            token_usage=TokenUsage(input_tokens=input_tokens, output_tokens=output_tokens),\n            timing=Timing(start_time=start_time, end_time=time.time()),\n        )<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ed6\u7684\u8fd4\u56de\u503c\u5c31\u662f\uff1aGenerator[ChatMessageStreamDelta | PlanningStep] \u3002\u6240\u4ee5\u5982\u679c\u5b83\u8fd4\u56de\u7684\u662f ChatMessageStreamDelta \uff0c\u90a3\u5c31\u8bf4\u660e\u5b83\u8fd8\u5728\u6d41\u5f0f\u8f93\u51fa\u4e2d\uff0c\u6240\u4ee5\u4ee3\u7801\u91cc\u9762\u5c31\u76f4\u63a5\u7528 yield \u8fd4\u56de\u7ed9\u4e86\u4e0a\u7ea7\uff0c\u5f53\u5b83\u6700\u540e\u8fd0\u884c\u5b8c\u4e86\uff0c\u90a3\u4e48\u5b83\u8fd4\u56de\u7684\u5c31\u662f PlanningStep \uff0c\u90a3\u8fd9\u5c31\u662f\u6211\u4eec\u60f3\u5f97\u5230\u7684\u89c4\u5212\u6b65\u9aa4\u4e86\uff0c\u6240\u4ee5\uff0c\u4ed6\u8fd9\u91cc\u7684\u4ee3\u7801\u662f\u8fd9\u4e48\u5199\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            # Run a planning step if scheduled\n            if self.planning_interval is not None and (\n                self.step_number == 1 or (self.step_number - 1) % self.planning_interval == 0\n            ):\n                planning_start_time = time.time()\n                planning_step = None\n                for element in self._generate_planning_step(\n                    task, is_first_step=len(self.memory.steps) == 1, step=self.step_number\n                ):  # Don't use the attribute step_number here, because there can be steps from previous runs\n                    yield element\n                    planning_step = element\n                assert isinstance(planning_step, PlanningStep)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u76f4\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u786e\u8ba4 planning_step \u8fd9\u4e2a\u53d8\u91cf\u62ff\u5230\u7684\uff0c\u4e00\u5b9a\u662f\u6211\u4eec\u60f3\u5f97\u5230\u7684\u89c4\u5212\u6b65\u9aa4\u7c7b\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e\u518d\u8d70\u4e00\u4e0b\u7edf\u4e00\u7684\u6b65\u9aa4\u6536\u5c3e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>                planning_end_time = time.time()\n                planning_step.timing = Timing(\n                    start_time=planning_start_time,\n                    end_time=planning_end_time,\n                )\n                self._finalize_step(planning_step)\n                self.memory.steps.append(planning_step)<\/code><\/pre>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-3d076cf0\"><h3 class=\"uagb-heading-text\">\u6267\u884c\u6b65\u9aa4<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u5c31\u662f\u6267\u884c action step \u4e86\uff0c\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684 ActionStep \uff0c\u7136\u540e\u7ed9 Agent \u7684 _step_stream \u65b9\u6cd5\u53bb\u6267\u884c\uff0c\u5411\u4e0a\u8fd4\u56de\u8f93\u51fa\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u76f4\u5230\u8f93\u51fa\u7684\u7c7b\u662f ActionOutput \uff0c\u8bf4\u660e\u6a21\u578b\u5df2\u7ecf\u5b8c\u6210\u4e86\u6700\u7ec8\u7684\u8f93\u51fa\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Start action step!\n            action_step_start_time = time.time()\n            action_step = ActionStep(\n                step_number=self.step_number,\n                timing=Timing(start_time=action_step_start_time),\n                observations_images=images,\n            )\n            self.logger.log_rule(f\"Step {self.step_number}\", level=LogLevel.INFO)\n            try:\n                for output in self._step_stream(action_step):\n                    # Yield all\n                    yield output\n\n                    if isinstance(output, ActionOutput) and output.is_final_answer:\n                        final_answer = output.output\n                        self.logger.log(\n                            Text(f\"Final answer: {final_answer}\", style=f\"bold {YELLOW_HEX}\"),\n                            level=LogLevel.INFO,\n                        )\n\n                        if self.final_answer_checks:\n                            self._validate_final_answer(final_answer)\n                        returned_final_answer = True\n                        action_step.is_final_answer = True<\/code><\/pre>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-1a2ef5bc\"><h3 class=\"uagb-heading-text\">\u9519\u8bef\u5904\u7406<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">smolagents \u5728\u8fd9\u91cc\u5904\u7406\u4e86\u4e00\u4e9b\u53ef\u80fd\u7684\u9519\u8bef\u60c5\u51b5\uff0c\u6bd4\u5982\u6a21\u578b\u8c03\u7528\u9519\u8bef\u4f1a\u76f4\u63a5\u5f80\u4e0a\u629b\uff0c\u800c\u8c03\u7528\u5de5\u5177\u9519\u8bef\u8fd9\u7c7b\u578b\u7684\u9519\u8bef\u5219\u4e0d\u4f1a\u5411\u4e0a\u629b\uff0c\u800c\u4f1a\u8bb0\u5f55\u5230\u8bb0\u5fc6\u4e2d\u3002\u6700\u540e\u8fd8\u4e00\u4e2a finally \u6765\u6536\u5c3e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            except AgentGenerationError as e:\n                # Agent generation errors are not caused by a Model error but an implementation error: so we should raise them and exit.\n                raise e\n            except AgentError as e:\n                # Other AgentError types are caused by the Model, so we should log them and iterate.\n                action_step.error = e\n            finally:\n                self._finalize_step(action_step)\n                self.memory.steps.append(action_step)\n                yield action_step\n                self.step_number += 1<\/code><\/pre>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-cf0405b0\"><h3 class=\"uagb-heading-text\">\u8d85\u6b65\u6570\u5f3a\u5236\u4f5c\u7b54<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">smolagents \u5728\u8fd9\u91cc\u65b0\u589e\u4e86\u4e00\u4e2a\u68c0\u6d4b\uff0c\u5f53\u4ed6\u53d1\u73b0\u6a21\u578b\u4ecd\u7136\u6ca1\u6709\u505a\u51fa\u6700\u540e\u7684\u56de\u7b54\uff0c\u4f46\u662f\u6b65\u6570\u5df2\u7ecf\u8fbe\u5230\u6240\u89c4\u5b9a\u7684\u4e0a\u9650\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u5f3a\u5236\u8981\u6c42\u6a21\u578b\u4f5c\u7b54\uff0c\u5e76\u4e14\u751f\u6210\u4e00\u4e2a\u5e26\u9519\u8bef\u63d0\u793a\u7684 ActionStep \uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        if not returned_final_answer and self.step_number == max_steps + 1:\n            final_answer = self._handle_max_steps_reached(task)\n            yield action_step<\/code><\/pre>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-5f3a53c9\"><h3 class=\"uagb-heading-text\">\u7edf\u4e00\u5305\u88c5\u6700\u7ec8\u8f93\u51fa<\/h3><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e\u4e00\u6b65\u5c31\u662f\u7edf\u4e00\u5305\u88c5\u6700\u7ec8\u8f93\u51fa\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        final_answer_step = FinalAnswerStep(handle_agent_output_types(final_answer))\n        self._finalize_step(final_answer_step)\n        yield final_answer_step<\/code><\/pre>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-94afbebd\"><h2 class=\"uagb-heading-text\">\u7ed3\u8bed<\/h2><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u770b\u4e86\u8fd9\u4e48\u591a\uff0c\u662f\u65f6\u5019\u81ea\u5df1\u5c1d\u8bd5\u505a\u4e00\u4e2a\u5c0f Agent \u4e86\uff0c\u8fd9\u6837\u80fd\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3 Agent \u5f00\u53d1\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u4e00\u7bc7\u6211\u4eec\u770b\u5b8c\u4e86 cli.py\uff0c\u77e5\u9053\u4e86 Agent \u662f\u600e\u4e48\u88ab\u7ec4\u88c5\u8d77\u6765\u7684\u3002\u8fd9\u4e00\u7bc7\u6211\u4eec\u8fdb\u5165 agents.py\uff0c\u770b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2459,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[35],"tags":[36,37],"class_list":["post-2459","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-agent-","tag-agent","tag-smolagents"],"uagb_featured_image_src":{"full":["https:\/\/pic.zjml.life\/api\/image\/1788854294339c7ab80?quality=mid",800,600,false],"thumbnail":["https:\/\/pic.zjml.life\/api\/image\/1788854294339c7ab80?quality=mid",150,150,true],"medium":["https:\/\/pic.zjml.life\/api\/image\/1788854294339c7ab80?quality=mid",300,300,false],"medium_large":["https:\/\/pic.zjml.life\/api\/image\/1788854294339c7ab80?quality=mid",768,0,false],"large":["https:\/\/pic.zjml.life\/api\/image\/1788854294339c7ab80?quality=mid",1024,1024,false],"1536x1536":["https:\/\/pic.zjml.life\/api\/image\/1788854294339c7ab80?quality=mid",1536,1536,false],"2048x2048":["https:\/\/pic.zjml.life\/api\/image\/1788854294339c7ab80?quality=mid",2048,2048,false]},"uagb_author_info":{"display_name":"zjml","author_link":"https:\/\/blog.zjml.life\/?author=1"},"uagb_comment_info":0,"uagb_excerpt":"\u4e0a\u4e00\u7bc7\u6211\u4eec\u770b\u5b8c\u4e86 cli.py\uff0c\u77e5\u9053\u4e86 Agent \u662f\u600e\u4e48\u88ab\u7ec4\u88c5\u8d77\u6765\u7684\u3002\u8fd9\u4e00\u7bc7\u6211\u4eec\u8fdb\u5165 agents.py\uff0c\u770b&hellip;","_links":{"self":[{"href":"https:\/\/blog.zjml.life\/index.php?rest_route=\/wp\/v2\/posts\/2459","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.zjml.life\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zjml.life\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zjml.life\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zjml.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2459"}],"version-history":[{"count":10,"href":"https:\/\/blog.zjml.life\/index.php?rest_route=\/wp\/v2\/posts\/2459\/revisions"}],"predecessor-version":[{"id":2469,"href":"https:\/\/blog.zjml.life\/index.php?rest_route=\/wp\/v2\/posts\/2459\/revisions\/2469"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.zjml.life\/index.php?rest_route=\/wp\/v2\/posts\/2459"}],"wp:attachment":[{"href":"https:\/\/blog.zjml.life\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zjml.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zjml.life\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}