{"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\/docs\/secure-code-execution-of-smolagents\/","title":{"rendered":"\u5b89\u5168\u6267\u884c Smolagents \u4ee3\u7801"},"content":{"rendered":"<p>\u5982\u679c\u60a8\u662f\u5efa\u7acb\u4ee3\u7406\u7684\u65b0\u624b\uff0c\u8bf7\u52a1\u5fc5\u9996\u5148\u9605\u8bfb\u00a0<a href=\"https:\/\/huggingface.co\/docs\/smolagents\/conceptual_guides\/intro_agents\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u4ee3\u7406\u5165\u95e8<\/a>\u00a0\u548c\u00a0<a href=\"https:\/\/huggingface.co\/docs\/smolagents\/guided_tour\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">smolagents \u5bfc\u6e38<\/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\u7801\u4ee3\u7406<\/h3>\n\n\n\n<p><a href=\"https:\/\/huggingface.co\/papers\/2402.01030\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u591a\u4e2a<\/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\u8fd9\u8868\u660e\uff0c\u8ba9 LLM \u7528\u4ee3\u7801\u7f16\u5199\u5176\u64cd\u4f5c\uff08\u5de5\u5177\u8c03\u7528\uff09\u8981\u6bd4\u5f53\u524d\u7684\u5de5\u5177\u8c03\u7528\u6807\u51c6\u683c\u5f0f\u597d\u5f97\u591a\uff0c\u56e0\u4e3a\u5f53\u524d\u7684\u5de5\u5177\u8c03\u7528\u6807\u51c6\u683c\u5f0f\u5728\u6574\u4e2a\u884c\u4e1a\u4e2d\u90fd\u662f \"\u5c06\u64cd\u4f5c\u5199\u6210\u5305\u542b\u5de5\u5177\u540d\u79f0\u548c\u4f7f\u7528\u53c2\u6570\u7684 JSON \u683c\u5f0f\"\u3002<\/p>\n\n\n\n<p>\u4e3a\u4ec0\u4e48\u4ee3\u7801\u66f4\u597d\uff1f\u56e0\u4e3a\u6211\u4eec\u7684\u4ee3\u7801\u8bed\u8a00\u662f\u4e13\u95e8\u4e3a\u8868\u8fbe\u8ba1\u7b97\u673a\u6267\u884c\u7684\u64cd\u4f5c\u800c\u8bbe\u8ba1\u7684\u3002\u5982\u679c JSON \u4ee3\u7801\u6bb5\u662f\u4e00\u79cd\u66f4\u597d\u7684\u65b9\u5f0f\uff0c\u90a3\u4e48\u8fd9\u4e2a\u8f6f\u4ef6\u5305\u5c31\u4f1a\u662f\u7528 JSON \u4ee3\u7801\u6bb5\u7f16\u5199\u7684\uff0c\u9b54\u9b3c\u4e5f\u4f1a\u5632\u7b11\u6211\u4eec\u3002<\/p>\n\n\n\n<p>\u4ee3\u7801\u53ea\u662f\u5728\u8ba1\u7b97\u673a\u4e0a\u8868\u8fbe\u64cd\u4f5c\u7684\u4e00\u79cd\u66f4\u597d\u7684\u65b9\u5f0f\u3002\u5b83\u6709\u66f4\u597d\u7684<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u53ef\u7ec4\u5408\u6027\uff1a<\/strong>\u00a0\u60a8\u80fd\u5426\u50cf\u5b9a\u4e49 python \u51fd\u6570\u4e00\u6837\uff0c\u5728 JSON \u64cd\u4f5c\u4e2d\u5d4c\u5957 JSON \u64cd\u4f5c\uff0c\u6216\u5b9a\u4e49\u4e00\u7ec4 JSON \u64cd\u4f5c\u4ee5\u4fbf\u4ee5\u540e\u91cd\u590d\u4f7f\u7528\uff1f<\/li>\n\n\n\n<li><strong>\u5bf9\u8c61\u7ba1\u7406\uff1a<\/strong>\u00a0\u5982\u4f55\u5b58\u50a8\u7c7b\u4f3c\u4e8e\u00a0<code>\u751f\u6210\u56fe\u50cf<\/code>\u00a0\u5728 JSON \u4e2d\uff1f<\/li>\n\n\n\n<li><strong>\u901a\u7528\u6027\uff1a<\/strong>\u00a0\u4ee3\u7801\u53ef\u4ee5\u8868\u8fbe\u4efb\u4f55\u8ba1\u7b97\u673a\u80fd\u505a\u7684\u4e8b\u60c5\u3002<\/li>\n\n\n\n<li><strong>LLM \u57f9\u8bad\u8bed\u6599\u5e93\u4e2d\u7684\u8868\u5f81\uff1a<\/strong>\u00a0\u4e3a\u4ec0\u4e48\u4e0d\u5229\u7528\u6cd5\u5f8b\u7855\u58eb\u57f9\u8bad\u8bed\u6599\u5e93\u4e2d\u5df2\u5305\u542b\u5927\u91cf\u9ad8\u8d28\u91cf\u884c\u52a8\u8fd9\u4e00\u5929\u8d50\u826f\u673a\u5462\uff1f<\/li>\n<\/ul>\n\n\n\n<p>\u4e0b\u56fe\u8bf4\u660e\u4e86\u8fd9\u4e00\u70b9\u3002\u00a0<a href=\"https:\/\/huggingface.co\/papers\/2402.01030\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u53ef\u6267\u884c\u4ee3\u7801\u884c\u52a8\u5f15\u53d1\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>\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u6211\u4eec\u628a\u91cd\u70b9\u653e\u5728\u63d0\u51fa\u4ee3\u7801\u4ee3\u7406\u4e0a\uff0c\u8fd9\u91cc\u6307\u7684\u662f python \u4ee3\u7406\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u8981\u82b1\u66f4\u5927\u7684\u7cbe\u529b\u6765\u6784\u5efa\u5b89\u5168\u7684 python \u89e3\u91ca\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\u5730 python \u89e3\u91ca\u5668<\/h3>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b&nbsp;<code>\u4ee3\u7801\u4ee3\u7406<\/code>&nbsp;\u5728\u4f60\u7684\u73af\u5883\u4e2d\u8fd0\u884c LLM \u751f\u6210\u7684\u4ee3\u7801\u3002\u8fd9\u79cd\u6267\u884c\u4e0d\u662f\u7531\u666e\u901a Python \u89e3\u91ca\u5668\u5b8c\u6210\u7684\uff1a\u6211\u4eec\u91cd\u65b0\u6784\u5efa\u4e86\u4e00\u4e2a\u66f4\u5b89\u5168\u7684&nbsp;<code>\u672c\u5730 PythonInterpreter<\/code>&nbsp;\u4ece\u5934\u5f00\u59cb\u3002\u8be5\u89e3\u91ca\u5668\u4e13\u4e3a\u5b89\u5168\u800c\u8bbe\u8ba1\uff0c\u5176\u529f\u80fd\u5982\u4e0b<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c06\u5bfc\u5165\u9650\u5236\u5728\u7528\u6237\u660e\u786e\u4f20\u9012\u7684\u5217\u8868\u4e2d<\/li>\n\n\n\n<li>\u9650\u5236\u64cd\u4f5c\u6b21\u6570\uff0c\u9632\u6b62\u65e0\u9650\u5faa\u73af\u548c\u8d44\u6e90\u81a8\u80c0\u3002<\/li>\n\n\n\n<li>\u4e0d\u4f1a\u6267\u884c\u4efb\u4f55\u672a\u9884\u5148\u5b9a\u4e49\u7684\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6211\u4eec\u5df2\u7ecf\u5728\u8bb8\u591a\u4f7f\u7528\u6848\u4f8b\u4e2d\u4f7f\u7528\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u4ece\u672a\u53d1\u73b0\u5bf9\u73af\u5883\u9020\u6210\u4efb\u4f55\u7834\u574f\u3002<\/p>\n\n\n\n<p>\u4e0d\u8fc7\uff0c\u8fd9\u79cd\u89e3\u51b3\u65b9\u6848\u4e5f\u5e76\u975e\u4e07\u65e0\u4e00\u5931\uff1a\u6211\u4eec\u53ef\u4ee5\u60f3\u8c61\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9488\u5bf9\u6076\u610f\u884c\u4e3a\u8fdb\u884c\u5fae\u8c03\u7684 LLM \u4ecd\u7136\u4f1a\u5bf9\u73af\u5883\u9020\u6210\u5371\u5bb3\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5141\u8bb8\u4e00\u4e2a\u65e0\u5bb3\u7684\u8f6f\u4ef6\u5305\uff0c\u5982&nbsp;<code>\u6795\u5934<\/code>&nbsp;\u5982\u679c\u60a8\u9009\u62e9 LLM \u5f15\u64ce\u6765\u5904\u7406\u56fe\u50cf\uff0c\u90a3\u4e48 LLM \u53ef\u80fd\u4f1a\u751f\u6210\u6570\u4ee5\u5343\u8ba1\u7684\u56fe\u50cf\u5b58\u6863\uff0c\u4ece\u800c\u5360\u7528\u60a8\u7684\u786c\u76d8\u7a7a\u95f4\u3002\u5982\u679c\u60a8\u81ea\u5df1\u9009\u62e9\u4e86 LLM \u5f15\u64ce\uff0c\u8fd9\u79cd\u60c5\u51b5\u5f53\u7136\u4e0d\u592a\u53ef\u80fd\u53d1\u751f\uff0c\u4f46\u8fd8\u662f\u6709\u53ef\u80fd\u53d1\u751f\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u60f3\u683c\u5916\u8c28\u614e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u63cf\u8ff0\u7684\u8fdc\u7a0b\u4ee3\u7801\u6267\u884c\u9009\u9879\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\u7801\u6267\u884c\u5668<\/h3>\n\n\n\n<p>\u4e3a\u4e86\u83b7\u5f97\u6700\u5927\u7684\u5b89\u5168\u6027\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6211\u4eec\u4e0e E2B \u7684\u96c6\u6210\uff0c\u5728\u6c99\u7bb1\u73af\u5883\u4e2d\u8fd0\u884c\u4ee3\u7801\u3002\u8fd9\u662f\u4e00\u79cd\u8fdc\u7a0b\u6267\u884c\u670d\u52a1\uff0c\u53ef\u5728\u9694\u79bb\u7684\u5bb9\u5668\u4e2d\u8fd0\u884c\u4ee3\u7801\uff0c\u4f7f\u4ee3\u7801\u65e0\u6cd5\u5f71\u54cd\u672c\u5730\u73af\u5883\u3002<\/p>\n\n\n\n<p>\u4e3a\u6b64\uff0c\u60a8\u9700\u8981\u8bbe\u7f6e\u60a8\u7684 E2B \u8d26\u6237\uff0c\u5e76\u8bbe\u7f6e\u60a8\u7684\u00a0<code>E2B_API_KEY<\/code>\u00a0\u5728\u4f60\u7684\u73af\u5883\u53d8\u91cf\u4e2d\u3002\u524d\u5f80\u00a0<a href=\"https:\/\/e2b.dev\/docs\/quickstart\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">E2B \u5feb\u901f\u5165\u95e8\u6587\u6863<\/a>\u00a0\u4e86\u89e3\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u7136\u540e\u4f7f\u7528&nbsp;<code>pip install e2b-code-interpreter python-dotenv<\/code>.<\/p>\n\n\n\n<p>\u73b0\u5728\u4f60\u51c6\u5907\u597d\u4e86\uff01<\/p>\n\n\n\n<p>\u8981\u5c06\u4ee3\u7801\u6267\u884c\u5668\u8bbe\u7f6e\u4e3a E2B\uff0c\u53ea\u9700\u901a\u8fc7\u6807\u8bb0&nbsp;<code>use_e2b_executor=True<\/code>&nbsp;\u521d\u59cb\u5316\u60a8\u7684&nbsp;<code>\u4ee3\u7801\u4ee3\u7406<\/code>.\u8bf7\u6ce8\u610f\uff0c\u60a8\u5e94\u8be5\u5728&nbsp;<code>\u9644\u52a0\u6388\u6743\u8fdb\u53e3<\/code>\u4ee5\u4fbf\u6267\u884c\u5668\u5b89\u88c5\u5b83\u4eec\u3002<\/p>\n\n\n\n<p>\u590d\u5236\u7684<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u4ece smolagents \u5bfc\u5165 CodeAgent\u3001VisitWebpageTool\u3001HfApiModel\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>\u76ee\u524d\uff0cE2B \u4ee3\u7801\u6267\u884c\u4e0e\u591a\u4ee3\u7406\u4e0d\u517c\u5bb9\uff0c\u56e0\u4e3a\u8ba9\u4e00\u4e2a\u4ee3\u7406\u8c03\u7528\u672c\u5e94\u8fdc\u7a0b\u6267\u884c\u7684\u4ee3\u7801\u5757\u4f1a\u9020\u6210\u6df7\u4e71\u3002\u4f46\u6211\u4eec\u6b63\u5728\u52aa\u529b\u6dfb\u52a0\uff01<\/p>","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u60a8\u662f\u6784\u5efa\u4ee3\u7406\u7684\u65b0\u624b\uff0c\u8bf7\u52a1\u5fc5\u5148\u9605\u8bfb\u4ee3\u7406\u5165\u95e8\u548c smolagents \u6307\u5357\u3002\u4ee3\u7801\u4ee3\u7406 \u591a\u7bc7\u7814\u7a76\u8bba\u6587\u8868\u660e\uff0c\u8ba9 LLM \u5728\u4ee3\u7801\u4e2d\u7f16\u5199\u5176\u64cd\u4f5c\uff08\u5de5\u5177\u8c03\u7528\uff09\u6bd4\u5f53\u524d\u7684\u5de5\u5177\u8c03\u7528\u6807\u51c6\u683c\u5f0f\u8981\u597d\u5f97\u591a\uff0c\u56e0\u4e3a\u5f53\u524d\u7684\u5de5\u5177\u8c03\u7528\u6807\u51c6\u683c\u5f0f\u5728\u6574\u4e2a\u884c\u4e1a\u4e2d\u90fd\u662f\u6df1\u6d45\u4e0d\u4e00\u7684 \"\u7f16\u5199\u64cd\u4f5c......<\/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":"4127","reactions":{"happy":"0","normal":"0","sad":"0"},"author_info":{"name":"smolagents","author_nicename":"wd-gstargmail-com","author_url":"https:\/\/smolagents.org\/zh\/author\/wd-gstargmail-com\/"},"doc_category_info":[{"term_name":"Examples","term_url":"https:\/\/smolagents.org\/zh\/docs-category\/examples\/"}],"doc_tag_info":[],"_links":{"self":[{"href":"https:\/\/smolagents.org\/zh\/wp-json\/wp\/v2\/docs\/595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smolagents.org\/zh\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/smolagents.org\/zh\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/smolagents.org\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smolagents.org\/zh\/wp-json\/wp\/v2\/comments?post=595"}],"version-history":[{"count":1,"href":"https:\/\/smolagents.org\/zh\/wp-json\/wp\/v2\/docs\/595\/revisions"}],"predecessor-version":[{"id":597,"href":"https:\/\/smolagents.org\/zh\/wp-json\/wp\/v2\/docs\/595\/revisions\/597"}],"wp:attachment":[{"href":"https:\/\/smolagents.org\/zh\/wp-json\/wp\/v2\/media?parent=595"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/smolagents.org\/zh\/wp-json\/wp\/v2\/doc_category?post=595"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/smolagents.org\/zh\/wp-json\/wp\/v2\/doc_tag?post=595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}