{"id":16502,"date":"2024-12-26T20:17:51","date_gmt":"2024-12-26T12:17:51","guid":{"rendered":"https:\/\/www.aisharenet.com\/?p=16502"},"modified":"2025-01-03T11:31:56","modified_gmt":"2025-01-03T03:31:56","slug":"smolagents","status":"publish","type":"post","link":"https:\/\/www.kdjingpai.com\/ja\/smolagents\/","title":{"rendered":"Smolagents\uff1a\u5feb\u901f\u5f00\u53d1AI\u667a\u80fd\u4f53\uff0c\u8f7b\u91cf\u7ea7\u6784\u5efa\u667a\u80fd\u4f53\u7684\u5f00\u6e90\u9879\u76ee"},"content":{"rendered":"<p>Smolagents\u662f\u7531HuggingFace\u5f00\u53d1\u7684\u8f7b\u91cf\u7ea7\u667a\u80fd\u4ee3\u7406\u5e93\uff0c\u4e13\u6ce8\u4e8e\u7b80\u5316AI\u4ee3\u7406\u7cfb\u7edf\u7684\u5f00\u53d1\u8fc7\u7a0b\u3002\u8be5\u9879\u76ee\u4ee5\u5176\u7b80\u6d01\u7684\u8bbe\u8ba1\u7406\u5ff5\u8457\u79f0\uff0c\u6838\u5fc3\u4ee3\u7801\u4ec5\u7ea61000\u884c\uff0c\u5374\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u96c6\u6210\u80fd\u529b\u3002\u5b83\u6700\u663e\u8457\u7684\u7279\u70b9\u662f\u652f\u6301\u4ee3\u7801\u6267\u884c\u4ee3\u7406\uff0c\u8ba9AI\u80fd\u591f\u76f4\u63a5\u901a\u8fc7\u7f16\u5199Python\u4ee3\u7801\u6765\u8c03\u7528\u5404\u79cd\u5de5\u5177\u548c\u5b8c\u6210\u4efb\u52a1\u3002Smolagents\u652f\u6301\u591a\u79cd\u4e3b\u6d41\u5927\u8bed\u8a00\u6a21\u578b\uff0c\u5305\u62ec\u53ef\u901a\u8fc7HuggingFace Hub\u8bbf\u95ee\u7684\u6a21\u578b\u3001OpenAI\u548cAnthropic\u7684\u6a21\u578b\u7b49\u3002\u7279\u522b\u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0c\u8be5\u6846\u67b6\u5728\u5b89\u5168\u6027\u65b9\u9762\u505a\u4e86\u5145\u5206\u8003\u8651\uff0c\u63d0\u4f9b\u4e86\u5b89\u5168\u7684Python\u89e3\u91ca\u5668\u548c\u6c99\u76d2\u73af\u5883\uff0c\u6709\u6548\u964d\u4f4e\u4e86\u4ee3\u7801\u6267\u884c\u53ef\u80fd\u5e26\u6765\u7684\u98ce\u9669\u3002\u4f5c\u4e3a\u4e00\u4e2a\u5f00\u6e90\u9879\u76ee\uff0cSmolagents\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u57fa\u7840\u7684\u4ee3\u7406\u5f00\u53d1\u6846\u67b6\uff0c\u8fd8\u652f\u6301\u901a\u8fc7HuggingFace Hub\u5171\u4eab\u548c\u52a0\u8f7d\u5de5\u5177\uff0c\u4f7f\u5f00\u53d1\u8005\u80fd\u591f\u66f4\u4fbf\u6377\u5730\u6784\u5efa\u548c\u90e8\u7f72\u667a\u80fd\u4ee3\u7406\u7cfb\u7edf\u3002<\/p>\n<p><a href=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2024\/12\/12ed0d40477ffb7.svg\"><img decoding=\"async\" class=\"alignnone size-full wp-image-16503\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2024\/12\/12ed0d40477ffb7.svg\" alt=\"\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2024\/12\/fefce14885f9304.svg\"><img decoding=\"async\" class=\"alignnone size-full wp-image-16505\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2024\/12\/fefce14885f9304.svg\" alt=\"\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>\u529f\u80fd\u5217\u8868<\/h2>\n<ul>\n<li>\u8f7b\u91cf\u7ea7\u4ee3\u7406\u5f00\u53d1\u6846\u67b6\uff0c\u6838\u5fc3\u903b\u8f91\u4ec5\u7ea61000\u884c\u4ee3\u7801<\/li>\n<li>\u652f\u6301\u591a\u79cd\u5927\u8bed\u8a00\u6a21\u578b\u96c6\u6210\uff08HuggingFace\u3001OpenAI\u3001Anthropic\u7b49\uff09<\/li>\n<li>\u4ee3\u7801\u6267\u884c\u4ee3\u7406\u529f\u80fd\uff0c\u652f\u6301\u76f4\u63a5\u901a\u8fc7Python\u4ee3\u7801\u8c03\u7528\u5de5\u5177<\/li>\n<li>\u63d0\u4f9b\u5b89\u5168\u7684\u4ee3\u7801\u6267\u884c\u73af\u5883\u548c\u6c99\u76d2\u673a\u5236<\/li>\n<li>\u652f\u6301\u901a\u8fc7HuggingFace Hub\u5171\u4eab\u548c\u52a0\u8f7d\u5de5\u5177<\/li>\n<li>\u7b80\u5355\u76f4\u89c2\u7684API\u8bbe\u8ba1\uff0c\u4fbf\u4e8e\u5feb\u901f\u5f00\u53d1\u548c\u90e8\u7f72<\/li>\n<li>\u5b8c\u6574\u7684\u6587\u6863\u652f\u6301\u548c\u793a\u4f8b\u4ee3\u7801<\/li>\n<li>\u652f\u6301\u81ea\u5b9a\u4e49\u5de5\u5177\u5f00\u53d1\u548c\u96c6\u6210<\/li>\n<li>\u63d0\u4f9b\u591a\u79cd\u9884\u7f6e\u5de5\u5177\uff08\u5982\u641c\u7d22\u5de5\u5177DuckDuckGoSearchTool\uff09<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>\u4f7f\u7528\u5e2e\u52a9<\/h2>\n<h3>1. \u5b89\u88c5\u6b65\u9aa4<\/h3>\n<p>\u9996\u5148\u9700\u8981\u901a\u8fc7pip\u5b89\u88c5Smolagents\u5305\uff1a<\/p>\n<pre><code>pip install smolagents\r\n<\/code><\/pre>\n<h3>2. \u57fa\u7840\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<h4>2.1 \u521b\u5efa\u7b80\u5355\u4ee3\u7406<\/h4>\n<pre><code>from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel\r\n# \u521b\u5efa\u4ee3\u7406\u5b9e\u4f8b\r\nagent = CodeAgent(\r\ntools=[DuckDuckGoSearchTool()],  # \u6dfb\u52a0\u6240\u9700\u5de5\u5177\r\nmodel=HfApiModel()  # \u6307\u5b9a\u4f7f\u7528\u7684\u6a21\u578b\r\n)\r\n# \u8fd0\u884c\u4ee3\u7406\r\nresponse = agent.run(\"\u4f60\u7684\u95ee\u9898\u6216\u4efb\u52a1\u63cf\u8ff0\")\r\n<\/code><\/pre>\n<h4>2.2 \u5b89\u5168\u6027\u914d\u7f6e<\/h4>\n<p>\u4e3a\u786e\u4fdd\u4ee3\u7801\u6267\u884c\u7684\u5b89\u5168\u6027\uff0cSmolagents\u63d0\u4f9b\u4e86\u4e24\u79cd\u5b89\u5168\u673a\u5236\uff1a<\/p>\n<ul>\n<li>\u5b89\u5168Python\u89e3\u91ca\u5668\uff1a\u901a\u8fc7\u9650\u5236\u53ef\u7528\u6a21\u5757\u548c\u51fd\u6570\u6765\u4fdd\u62a4\u7cfb\u7edf<\/li>\n<li>\u6c99\u76d2\u73af\u5883\uff1a\u63d0\u4f9b\u9694\u79bb\u7684\u6267\u884c\u73af\u5883<\/li>\n<\/ul>\n<p>\u4f7f\u7528\u5b89\u5168\u89e3\u91ca\u5668\u793a\u4f8b\uff1a<\/p>\n<pre><code>from smolagents import CodeAgent, SecureInterpreter\r\nagent = CodeAgent(\r\ntools=[your_tools],\r\ninterpreter=SecureInterpreter()\r\n)\r\n<\/code><\/pre>\n<h3>3. \u9ad8\u7ea7\u529f\u80fd<\/h3>\n<h4>3.1 \u81ea\u5b9a\u4e49\u5de5\u5177\u5f00\u53d1<\/h4>\n<p>\u5f00\u53d1\u8005\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684\u5de5\u5177\u7c7b\uff1a<\/p>\n<pre><code>from smolagents import BaseTool\r\nclass MyCustomTool(BaseTool):\r\ndef __init__(self):\r\nsuper().__init__()\r\ndef __call__(self, *args, **kwargs):\r\n# \u5b9e\u73b0\u5de5\u5177\u7684\u5177\u4f53\u529f\u80fd\r\npass\r\n<\/code><\/pre>\n<h4>3.2 \u4e0eHuggingFace Hub\u96c6\u6210<\/h4>\n<p>\u53ef\u4ee5\u8f7b\u677e\u4eceHub\u52a0\u8f7d\u548c\u5206\u4eab\u5de5\u5177\uff1a<\/p>\n<pre><code># \u4eceHub\u52a0\u8f7d\u5de5\u5177\r\nfrom smolagents import load_tool\r\ntool = load_tool(\"tool_name\", from_hub=True)\r\n# \u5206\u4eab\u5de5\u5177\u5230Hub\r\ntool.push_to_hub(\"your-username\/tool-name\")\r\n<\/code><\/pre>\n<h3>4. \u6700\u4f73\u5b9e\u8df5\u5efa\u8bae<\/h3>\n<ol>\n<li>\u59cb\u7ec8\u4f7f\u7528\u5b89\u5168\u89e3\u91ca\u5668\u6216\u6c99\u76d2\u73af\u5883\u6765\u6267\u884c\u4ee3\u7801<\/li>\n<li>\u6839\u636e\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u6a21\u578b\uff0c\u8003\u8651\u6027\u80fd\u548c\u6210\u672c<\/li>\n<li>\u5408\u7406\u7ec4\u7ec7\u5de5\u5177\u96c6\uff0c\u907f\u514d\u529f\u80fd\u91cd\u590d<\/li>\n<li>\u5b9a\u671f\u66f4\u65b0\u4f9d\u8d56\u5305\u4ee5\u83b7\u53d6\u6700\u65b0\u7279\u6027\u548c\u5b89\u5168\u4fee\u590d<\/li>\n<li>\u5145\u5206\u5229\u7528\u6587\u6863\u548c\u793a\u4f8b\u4ee3\u7801\u52a0\u901f\u5f00\u53d1<\/li>\n<\/ol>\n<h3>5. \u5e38\u89c1\u95ee\u9898\u89e3\u51b3<\/h3>\n<ul>\n<li>\u5982\u679c\u9047\u5230\u6a21\u578b\u52a0\u8f7d\u95ee\u9898\uff0c\u68c0\u67e5\u7f51\u7edc\u8fde\u63a5\u548cAPI\u5bc6\u94a5\u914d\u7f6e<\/li>\n<li>\u4ee3\u7801\u6267\u884c\u9519\u8bef\uff0c\u67e5\u770b\u662f\u5426\u53d7\u5230\u5b89\u5168\u9650\u5236\uff0c\u53ef\u80fd\u9700\u8981\u8c03\u6574\u5b89\u5168\u7b56\u7565<\/li>\n<li>\u5de5\u5177\u5bfc\u5165\u5931\u8d25\uff0c\u786e\u8ba4\u662f\u5426\u6b63\u786e\u5b89\u88c5\u4e86\u6240\u6709\u4f9d\u8d56<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>Smolagents\u4e2d\u7684\u5173\u952eprompt\u5185\u5bb9<\/h2>\n<p>\u53c2\u8003\uff1ahttps:\/\/github.com\/huggingface\/smolagents\/blob\/e57f4f55ef506948d2e17b320ddc2a98b282eacf\/src\/smolagents\/prompts.py<\/p>\n<ol>\n<li><strong>\u5de5\u5177\u8c03\u7528\u7cfb\u7edf\u63d0\u793a (TOOL_CALLING_SYSTEM_PROMPT)<\/strong><\/li>\n<\/ol>\n<pre><code>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.\r\nTo do so, you have been given access to the following tools: {{tool_names}}\r\nThe tool call you write is an action: after the tool is executed, you will get the result of the tool call as an \"observation\".\r\nThis Action\/Observation can repeat N times, you should take several steps when needed.\r\nYou can use the result of the previous action as input for the next action.\r\nThe observation will always be a string: it can represent a file, like \"image_1.jpg\".\r\nThen you can use it as input for the next action. You can do it for instance as follows:\r\nObservation: \"image_1.jpg\"\r\nAction:\r\n{\r\n\"tool_name\": \"image_transformer\",\r\n\"tool_arguments\": {\"image\": \"image_1.jpg\"}\r\n}\r\nTo provide the final answer to the task, use an action blob with \"tool_name\": \"final_answer\" tool...\r\n[\u793a\u4f8b\u90e8\u5206\u7701\u7565]\r\nHere are the rules you should always follow to solve your task:\r\n1. ALWAYS provide a tool call, else you will fail.\r\n2. Always use the right arguments for the tools. Never use variable names as the action arguments, use the value instead.\r\n3. Call a tool only when needed: do not call the search agent if you do not need information, try to solve the task yourself.\r\n4. Never re-do a tool call that you previously did with the exact same parameters.\r\nNow Begin! If you solve the task correctly, you will receive a reward of $1,000,000.\r\n<\/code><\/pre>\n<ol start=\"2\">\n<li><strong>\u4ee3\u7801\u6267\u884c\u7cfb\u7edf\u63d0\u793a (CODE_SYSTEM_PROMPT)<\/strong><\/li>\n<\/ol>\n<pre><code>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.\r\nTo do so, you have been given access to a list of tools: these tools are basically Python functions which you can call with code.\r\nTo solve the task, you must plan forward to proceed in a series of steps, in a cycle of 'Thought:', 'Code:', and 'Observation:' sequences.\r\nAt each step, in the 'Thought:' sequence, you should first explain your reasoning towards solving the task and the tools that you want to use.\r\nThen in the 'Code:' sequence, you should write the code in simple Python. The code sequence must end with '&lt;end_code&gt;' sequence.\r\n[\u793a\u4f8b\u90e8\u5206\u7701\u7565]\r\nHere are the rules you should always follow to solve your task:\r\n1. Always provide a 'Thought:' sequence, and a 'Code:\\n```py' sequence ending with '```&lt;end_code&gt;' sequence, else you will fail.\r\n2. Use only variables that you have defined!\r\n3. Always use the right arguments for the tools.\r\n4. Take care to not chain too many sequential tool calls in the same code block\r\n5. Call a tool only when needed, and never re-do a tool call that you previously did with the exact same parameters.\r\n6. Don't name any new variable with the same name as a tool\r\n7. Never create any notional variables in our code\r\n8. You can use imports in your code, but only from the following list of modules: {{authorized_imports}}\r\n9. The state persists between code executions\r\n10. Don't give up! You're in charge of solving the task, not providing directions to solve it.\r\n<\/code><\/pre>\n<ol start=\"3\">\n<li><strong>\u4e8b\u5b9e\u6536\u96c6\u63d0\u793a (SYSTEM_PROMPT_FACTS)<\/strong><\/li>\n<\/ol>\n<pre><code>Below I will present you a task.\r\nYou will now build a comprehensive preparatory survey of which facts we have at our disposal and which ones we still need.\r\nTo do so, you will have to read the task and identify things that must be discovered in order to successfully complete it.\r\nDon't make any assumptions. For each item, provide a thorough reasoning. Here is how you will structure this survey:\r\n### 1. Facts given in the task\r\nList here the specific facts given in the task that could help you (there might be nothing here).\r\n### 2. Facts to look up\r\nList here any facts that we may need to look up.\r\nAlso list where to find each of these, for instance a website, a file...\r\n### 3. Facts to derive\r\nList here anything that we want to derive from the above by logical reasoning, for instance computation or simulation.\r\n<\/code><\/pre>\n<ol start=\"4\">\n<li><strong>\u8ba1\u5212\u5236\u5b9a\u63d0\u793a (SYSTEM_PROMPT_PLAN)<\/strong><\/li>\n<\/ol>\n<pre><code>You are a world expert at making efficient plans to solve any task using a set of carefully crafted tools.\r\nNow for the given task, develop a step-by-step high-level plan taking into account the above inputs and list of facts.\r\nThis plan should involve individual tasks based on the available tools, that if executed correctly will yield the correct answer.\r\nDo not skip steps, do not add any superfluous steps. Only write the high-level plan, DO NOT DETAIL INDIVIDUAL TOOL CALLS.\r\nAfter writing the final step of the plan, write the '\\n&lt;end_plan&gt;' tag and stop there.\r\n<\/code><\/pre>\n<ol start=\"5\">\n<li><strong>\u4e8b\u5b9e\u66f4\u65b0\u63d0\u793a (SYSTEM_PROMPT_FACTS_UPDATE)<\/strong><\/li>\n<\/ol>\n<pre><code>You are a world expert at gathering known and unknown facts based on a conversation.\r\nBelow you will find a task, and a history of attempts made to solve the task. You will have to produce a list of these:\r\n### 1. Facts given in the task\r\n### 2. Facts that we have learned\r\n### 3. Facts still to look up\r\n### 4. Facts still to derive\r\n<\/code><\/pre>\n<ol start=\"6\">\n<li><strong>\u8ba1\u5212\u66f4\u65b0\u63d0\u793a (SYSTEM_PROMPT_PLAN_UPDATE)<\/strong><\/li>\n<\/ol>\n<pre><code>You are a world expert at making efficient plans to solve any task using a set of carefully crafted tools.\r\nYou have been given a task:\r\n```{task}```\r\nFind below the record of what has been tried so far to solve it. Then you will be asked to make an updated plan to solve the task.\r\nIf the previous tries so far have met some success, you can make an updated plan based on these actions.\r\nIf you are stalled, you can make a completely new plan starting from scratch.\r\n<\/code><\/pre>\n<ol start=\"7\">\n<li><strong>\u7ba1\u7406\u4ee3\u7406\u63d0\u793a (MANAGED_AGENT_PROMPT)<\/strong><\/li>\n<\/ol>\n<pre><code>You're a helpful agent named '{name}'.\r\nYou have been submitted this task by your manager.\r\n---\r\nTask:\r\n{task}\r\n---\r\nYou'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 to give them a clear understanding of the answer.\r\nYour final_answer WILL HAVE to contain these parts:\r\n### 1. Task outcome (short version):\r\n### 2. Task outcome (extremely detailed version):\r\n### 3. Additional context (if relevant):<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Smolagents\u662f\u7531HuggingFace\u5f00\u53d1\u7684\u8f7b\u91cf\u7ea7\u667a\u80fd\u4ee3\u7406\u5e93\uff0c\u4e13\u6ce8\u4e8e\u7b80\u5316AI\u4ee3\u7406\u7cfb\u7edf\u7684\u5f00\u53d1\u8fc7\u7a0b\u3002\u8be5\u9879\u76ee\u4ee5\u5176\u7b80\u6d01\u7684\u8bbe\u8ba1\u7406\u5ff5\u8457\u79f0\uff0c\u6838\u5fc3\u4ee3\u7801\u4ec5\u7ea61000\u884c\uff0c\u5374\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u96c6\u6210\u80fd\u529b\u3002\u5b83\u6700\u663e\u8457\u7684\u7279\u70b9\u662f\u652f\u6301\u4ee3\u7801\u6267\u884c\u4ee3\u7406\uff0c\u8ba9AI\u80fd\u591f\u76f4\u63a5\u901a\u8fc7\u7f16&#8230;<\/p>\n","protected":false},"author":1,"featured_media":61508,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[230,201],"class_list":["post-16502","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tool","tag-aikaiyuanxiangmu","tag-aizhinengti"],"_links":{"self":[{"href":"https:\/\/www.kdjingpai.com\/ja\/wp-json\/wp\/v2\/posts\/16502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kdjingpai.com\/ja\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kdjingpai.com\/ja\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/ja\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/ja\/wp-json\/wp\/v2\/comments?post=16502"}],"version-history":[{"count":0,"href":"https:\/\/www.kdjingpai.com\/ja\/wp-json\/wp\/v2\/posts\/16502\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/ja\/wp-json\/wp\/v2\/media\/61508"}],"wp:attachment":[{"href":"https:\/\/www.kdjingpai.com\/ja\/wp-json\/wp\/v2\/media?parent=16502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/ja\/wp-json\/wp\/v2\/categories?post=16502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/ja\/wp-json\/wp\/v2\/tags?post=16502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}