{"id":595,"date":"2025-01-11T15:26:47","date_gmt":"2025-01-11T15:26:47","guid":{"rendered":"https:\/\/smolagents.org\/?post_type=docs&#038;p=595"},"modified":"2025-01-11T15:41:43","modified_gmt":"2025-01-11T15:41:43","password":"","slug":"secure-code-execution-of-smolagents","status":"publish","type":"docs","link":"https:\/\/smolagents.org\/zh_tw\/docs\/secure-code-execution-of-smolagents\/","title":{"rendered":"\u5b89\u5168\u57f7\u884c Smolagents \u7684\u7a0b\u5f0f\u78bc"},"content":{"rendered":"<p>\u5982\u679c\u60a8\u662f\u5efa\u7acb\u4ee3\u7406\u5546\u7684\u65b0\u624b\uff0c\u8acb\u52d9\u5fc5\u5148\u95b1\u8b80\u00a0<a href=\"https:\/\/huggingface.co\/docs\/smolagents\/conceptual_guides\/intro_agents\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u4ee3\u7406\u5546\u7c21\u4ecb<\/a>\u00a0\u548c\u00a0<a href=\"https:\/\/huggingface.co\/docs\/smolagents\/guided_tour\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">smolagents \u5c0e\u89bd<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/secure_code_execution#code-agents\" target=\"_blank\" rel=\"noopener\"><\/a>\u4ee3\u78bc\u4ee3\u7406\u5546<\/h3>\n\n\n\n<p><a href=\"https:\/\/huggingface.co\/papers\/2402.01030\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u591a\u7a2e<\/a>\u00a0<a href=\"https:\/\/huggingface.co\/papers\/2411.01747\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u7814\u7a76<\/a>\u00a0<a href=\"https:\/\/huggingface.co\/papers\/2401.00812\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u6587\u4ef6<\/a>\u00a0\u5df2\u8b49\u660e\u8b93 LLM \u4ee5\u7a0b\u5f0f\u78bc\u5beb\u51fa\u5b83\u7684\u52d5\u4f5c\uff08\u5de5\u5177\u547c\u53eb\uff09\uff0c\u6bd4\u76ee\u524d\u5de5\u5177\u547c\u53eb\u7684\u6a19\u6e96\u683c\u5f0f\u8981\u597d\u5f97\u591a\uff0c\u800c\u76ee\u524d\u7684\u6a19\u6e96\u683c\u5f0f\u5728\u696d\u754c\u662f\u6df1\u6dfa\u4e0d\u4e00\u7684\u300c\u5c07\u52d5\u4f5c\u5beb\u6210\u5de5\u5177\u540d\u7a31\u548c\u8981\u4f7f\u7528\u7684\u53c3\u6578\u7684 JSON\u300d\u3002<\/p>\n\n\n\n<p>\u70ba\u4ec0\u9ebc\u7a0b\u5f0f\u78bc\u6bd4\u8f03\u597d\uff1f\u55ef\uff0c\u56e0\u70ba\u6211\u5011\u7279\u5225\u7cbe\u5fc3\u8a2d\u8a08\u4e86\u6211\u5011\u7684\u7a0b\u5f0f\u78bc\u8a9e\u8a00\uff0c\u8b93\u5b83\u80fd\u5920\u5f88\u597d\u5730\u8868\u9054\u96fb\u8166\u57f7\u884c\u7684\u52d5\u4f5c\u3002\u5982\u679c JSON \u7247\u6bb5\u662f\u66f4\u597d\u7684\u65b9\u5f0f\uff0c\u9019\u500b\u5957\u4ef6\u5c31\u6703\u7528 JSON \u7247\u6bb5\u5beb\u6210\uff0c\u800c\u9b54\u9b3c\u4e5f\u6703\u5632\u7b11\u6211\u5011\u3002<\/p>\n\n\n\n<p>\u7a0b\u5f0f\u78bc\u53ea\u662f\u4e00\u7a2e\u5728\u96fb\u8166\u4e0a\u8868\u9054\u884c\u52d5\u7684\u66f4\u597d\u65b9\u5f0f\u3002\u5b83\u6709\u66f4\u597d\u7684\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u53ef\u7d44\u5408\u6027\uff1a<\/strong>\u00a0\u60a8\u662f\u5426\u53ef\u4ee5\u5c07 JSON \u52d5\u4f5c\u76f8\u4e92\u5d4c\u5957\uff0c\u6216\u5b9a\u7fa9\u4e00\u7d44 JSON \u52d5\u4f5c\u4ee5\u4fbf\u65e5\u5f8c\u91cd\u8907\u4f7f\u7528\uff0c\u5c31\u50cf\u5b9a\u7fa9 python \u51fd\u5f0f\u4e00\u6a23\uff1f<\/li>\n\n\n\n<li><strong>\u7269\u4ef6\u7ba1\u7406\uff1a<\/strong>\u00a0\u5982\u4f55\u5132\u5b58\u52d5\u4f5c\u7684\u8f38\u51fa\uff0c\u4f8b\u5982\u00a0<code>\u7522\u751f\u5f71\u50cf<\/code>\u00a0\u5728 JSON \u4e2d\uff1f<\/li>\n\n\n\n<li><strong>\u4e00\u822c\u6027\uff1a<\/strong>\u00a0\u4ee3\u78bc\u7684\u5efa\u7acb\u662f\u70ba\u4e86\u8868\u9054\u4efb\u4f55\u60a8\u53ef\u4ee5\u8b93\u96fb\u8166\u505a\u7684\u4e8b\u60c5\u3002<\/li>\n\n\n\n<li><strong>LLM \u8a13\u7df4\u8a9e\u6599\u4e2d\u7684\u8868\u9054\uff1a<\/strong>\u00a0\u70ba\u4ec0\u9ebc\u4e0d\u597d\u597d\u5229\u7528 LLM \u8a13\u7df4\u8a9e\u6599\u5eab\u4e2d\u5df2\u5305\u542b\u5927\u91cf\u512a\u8cea\u8a34\u8a1f\u7684\u4e0a\u5929\u7737\u9867\u5462\uff1f<\/li>\n<\/ul>\n\n\n\n<p>\u4e0b\u5716\u8aaa\u660e\u4e86\u9019\u4e00\u9ede\uff0c\u53d6\u81ea\u00a0<a href=\"https:\/\/huggingface.co\/papers\/2402.01030\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u53ef\u57f7\u884c\u4ee3\u78bc\u52d5\u4f5c\u5f15\u767c\u66f4\u597d\u7684 LLM \u4ee3\u7406<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"2716\" height=\"1154\" src=\"https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/code_vs_json_actions-1.png\" alt=\"\" class=\"wp-image-596\" srcset=\"https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/code_vs_json_actions-1.png 2716w, https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/code_vs_json_actions-1-300x127.png 300w, https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/code_vs_json_actions-1-1024x435.png 1024w, https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/code_vs_json_actions-1-768x326.png 768w, https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/code_vs_json_actions-1-1536x653.png 1536w, https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/code_vs_json_actions-1-2048x870.png 2048w, https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/code_vs_json_actions-1-18x8.png 18w, https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/code_vs_json_actions-1-360x153.png 360w\" sizes=\"auto, (max-width: 2716px) 100vw, 2716px\" \/><\/figure>\n\n\n\n<p>\u9019\u5c31\u662f\u70ba\u4ec0\u9ebc\u6211\u5011\u8457\u91cd\u65bc\u63d0\u51fa\u7a0b\u5f0f\u78bc\u4ee3\u7406\u7a0b\u5f0f\uff0c\u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\u5c31\u662f python \u4ee3\u7406\u7a0b\u5f0f\uff0c\u9019\u610f\u5473\u8457\u6211\u5011\u8981\u82b1\u66f4\u5927\u7684\u5fc3\u529b\u4f86\u5efa\u7acb\u5b89\u5168\u7684 python \u89e3\u8b6f\u5668\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/secure_code_execution#local-python-interpreter\" target=\"_blank\" rel=\"noopener\"><\/a>\u672c\u6a5f python \u89e3\u8b6f\u5668<\/h3>\n\n\n\n<p>\u9810\u8a2d\u503c\u70ba&nbsp;<code>CodeAgent<\/code>&nbsp;\u5728\u60a8\u7684\u74b0\u5883\u4e2d\u57f7\u884c LLM \u7522\u751f\u7684\u7a0b\u5f0f\u78bc\u3002\u9019\u500b\u57f7\u884c\u4e0d\u662f\u7531\u666e\u901a\u7684 Python \u89e3\u8b6f\u5668\u5b8c\u6210\u7684\uff1a\u6211\u5011\u91cd\u65b0\u5efa\u7acb\u4e86\u4e00\u500b\u66f4\u5b89\u5168\u7684&nbsp;<code>LocalPythonInterpreter<\/code>&nbsp;\u5f9e\u57fa\u790e\u958b\u59cb\u3002\u672c\u89e3\u8b6f\u5668\u662f\u70ba\u4e86\u5b89\u5168\u800c\u8a2d\u8a08\u7684\uff0c\u85c9\u7531\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c07\u8f38\u5165\u9650\u5236\u70ba\u4f7f\u7528\u8005\u660e\u78ba\u50b3\u9001\u7684\u6e05\u55ae<\/li>\n\n\n\n<li>\u8a2d\u5b9a\u64cd\u4f5c\u6b21\u6578\u4e0a\u9650\uff0c\u9632\u6b62\u7121\u9650\u5faa\u74b0\u548c\u8cc7\u6e90\u81a8\u8139\u3002<\/li>\n\n\n\n<li>\u4e0d\u6703\u57f7\u884c\u4efb\u4f55\u672a\u9810\u5148\u5b9a\u7fa9\u7684\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6211\u5011\u5df2\u7d93\u5728\u8a31\u591a\u6848\u4f8b\u4e2d\u4f7f\u7528\u904e\uff0c\u5f9e\u672a\u767c\u73fe\u5c0d\u74b0\u5883\u9020\u6210\u4efb\u4f55\u7834\u58de\u3002<\/p>\n\n\n\n<p>\u7136\u800c\uff0c\u6b64\u89e3\u6c7a\u65b9\u6848\u4e26\u975e\u842c\u7121\u4e00\u5931\uff1a\u53ef\u4ee5\u60f3\u50cf\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u91dd\u5c0d\u60e1\u610f\u52d5\u4f5c\u5fae\u8abf\u7684 LLM \u4ecd\u7136\u6703\u50b7\u5bb3\u60a8\u7684\u74b0\u5883\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u5141\u8a31\u4e00\u500b\u7121\u5bb3\u7684\u5957\u4ef6\u5982&nbsp;<code>\u6795\u982d<\/code>&nbsp;\u4f86\u8655\u7406\u5f71\u50cf\uff0cLLM \u53ef\u80fd\u6703\u7522\u751f\u6578\u4ee5\u5343\u8a08\u7684\u5f71\u50cf\u5132\u5b58\u6a94\uff0c\u4f7f\u60a8\u7684\u786c\u789f\u8b8a\u5f97\u81c3\u816b\u3002\u5982\u679c\u60a8\u81ea\u5df1\u9078\u64c7\u4e86 LLM \u5f15\u64ce\uff0c\u9019\u7576\u7136\u4e0d\u592a\u53ef\u80fd\uff0c\u4f46\u4e5f\u6709\u53ef\u80fd\u767c\u751f\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u5982\u679c\u60a8\u60f3\u8981\u683c\u5916\u8b39\u614e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u63cf\u8ff0\u7684\u9060\u7aef\u7a0b\u5f0f\u78bc\u57f7\u884c\u9078\u9805\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/secure_code_execution#e2b-code-executor\" target=\"_blank\" rel=\"noopener\"><\/a>E2B \u4ee3\u78bc\u57f7\u884c\u5668<\/h3>\n\n\n\n<p>\u70ba\u4e86\u7372\u5f97\u6700\u5927\u7684\u5b89\u5168\u6027\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6211\u5011\u8207 E2B \u7684\u6574\u5408\uff0c\u5728\u6c99\u7bb1\u74b0\u5883\u4e2d\u57f7\u884c\u7a0b\u5f0f\u78bc\u3002\u9019\u662f\u4e00\u7a2e\u9060\u7aef\u57f7\u884c\u670d\u52d9\uff0c\u53ef\u5728\u9694\u96e2\u7684\u5bb9\u5668\u4e2d\u57f7\u884c\u60a8\u7684\u7a0b\u5f0f\u78bc\uff0c\u4f7f\u7a0b\u5f0f\u78bc\u7121\u6cd5\u5f71\u97ff\u60a8\u7684\u672c\u6a5f\u74b0\u5883\u3002<\/p>\n\n\n\n<p>\u70ba\u6b64\uff0c\u60a8\u9700\u8981\u8a2d\u5b9a\u60a8\u7684 E2B \u5e33\u6236\uff0c\u4e26\u8a2d\u5b9a\u60a8\u7684\u00a0<code>E2B_API_KEY<\/code>\u00a0\u5728\u60a8\u7684\u74b0\u5883\u8b8a\u6578\u4e2d\u3002\u524d\u5f80\u00a0<a href=\"https:\/\/e2b.dev\/docs\/quickstart\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">E2B \u7684\u5feb\u901f\u5165\u9580\u6587\u4ef6<\/a>\u00a0\u4ee5\u7372\u5f97\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n\n\n\n<p>\u7136\u5f8c\u7528&nbsp;<code>pip install e2b-code-interpreter python-dotenv<\/code>.<\/p>\n\n\n\n<p>\u73fe\u5728\u60a8\u5df2\u6e96\u5099\u5c31\u7dd2\uff01<\/p>\n\n\n\n<p>\u82e5\u8981\u5c07\u4ee3\u78bc\u57f7\u884c\u5668\u8a2d\u5b9a\u70ba E2B\uff0c\u53ea\u9700\u50b3\u9001\u65d7\u6a19&nbsp;<code>use_e2b_executor=True<\/code>&nbsp;\u521d\u59cb\u5316\u60a8\u7684&nbsp;<code>CodeAgent<\/code>.\u8acb\u6ce8\u610f\uff0c\u60a8\u61c9\u8a72\u5728&nbsp;<code>\u984d\u5916\u7684\u6388\u6b0a\u9032\u53e3<\/code>\uff0c\u4ee5\u4fbf\u57f7\u884c\u5668\u5b89\u88dd\u5b83\u5011\u3002<\/p>\n\n\n\n<p>\u8907\u88fd<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from smolagents import CodeAgent, VisitWebpageTool, HfApiModel\nagent = CodeAgent(\n    tools = [VisitWebpageTool()]\u3001\n    model=HfApiModel()\u3001\n    additional_authorized_imports=[\"requests\", \"markdownify\"]\u3001\n    use_e2b_executor=True\n)\n\nagent.run(\"What was Abraham Lincoln's preferred pet?\")<\/pre>\n\n\n\n<p>E2B \u4ee3\u78bc\u57f7\u884c\u76ee\u524d\u8207\u591a\u91cd\u4ee3\u7406\u7a0b\u5f0f\u4e0d\u76f8\u5bb9 - \u56e0\u70ba\u8b93\u4e00\u500b\u4ee3\u7406\u7a0b\u5f0f\u547c\u53eb\u4e00\u500b\u61c9\u8a72\u9060\u7aef\u57f7\u884c\u7684\u4ee3\u78bc Blob \u6703\u9020\u6210\u6df7\u4e82\u3002\u4f46\u6211\u5011\u6b63\u5728\u52aa\u529b\u52a0\u5165\uff01<\/p>","protected":false},"excerpt":{"rendered":"<p>If you\u2019re new to building agents, make sure to first read the\u00a0intro to agents\u00a0and the\u00a0guided tour of smolagents. Code agents Multiple\u00a0research\u00a0papers\u00a0have shown that having the LLM write its actions (the tool calls) in code is much better than the current standard format for tool calling, which is across the industry different shades of \u201cwriting actions&#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_kadence_starter_templates_imported_post":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"doc_category":[10],"doc_tag":[],"class_list":["post-595","docs","type-docs","status-publish","hentry","doc_category-examples"],"year_month":"2026-04","word_count":647,"total_views":"4366","reactions":{"happy":"0","normal":"0","sad":"0"},"author_info":{"name":"smolagents","author_nicename":"wd-gstargmail-com","author_url":"https:\/\/smolagents.org\/zh_tw\/author\/wd-gstargmail-com\/"},"doc_category_info":[{"term_name":"Examples","term_url":"https:\/\/smolagents.org\/zh_tw\/docs-category\/examples\/"}],"doc_tag_info":[],"_links":{"self":[{"href":"https:\/\/smolagents.org\/zh_tw\/wp-json\/wp\/v2\/docs\/595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smolagents.org\/zh_tw\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/smolagents.org\/zh_tw\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/smolagents.org\/zh_tw\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smolagents.org\/zh_tw\/wp-json\/wp\/v2\/comments?post=595"}],"version-history":[{"count":1,"href":"https:\/\/smolagents.org\/zh_tw\/wp-json\/wp\/v2\/docs\/595\/revisions"}],"predecessor-version":[{"id":597,"href":"https:\/\/smolagents.org\/zh_tw\/wp-json\/wp\/v2\/docs\/595\/revisions\/597"}],"wp:attachment":[{"href":"https:\/\/smolagents.org\/zh_tw\/wp-json\/wp\/v2\/media?parent=595"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/smolagents.org\/zh_tw\/wp-json\/wp\/v2\/doc_category?post=595"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/smolagents.org\/zh_tw\/wp-json\/wp\/v2\/doc_tag?post=595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}