{"id":591,"date":"2025-01-11T15:25:02","date_gmt":"2025-01-11T15:25:02","guid":{"rendered":"https:\/\/smolagents.org\/?post_type=docs&#038;p=591"},"modified":"2025-01-11T15:41:26","modified_gmt":"2025-01-11T15:41:26","password":"","slug":"tools-of-smolagents-in-depth-guide","status":"publish","type":"docs","link":"https:\/\/smolagents.org\/zh_tw\/docs\/tools-of-smolagents-in-depth-guide\/","title":{"rendered":"Smolagents \u7684\u5de5\u5177\u6df1\u5165\u6307\u5357"},"content":{"rendered":"<p>\u5728\u9019\u88e1\uff0c\u6211\u5011\u8981\u770b\u770b\u9032\u968e\u5de5\u5177\u7684\u7528\u6cd5\u3002<\/p>\n\n\n\n<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<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/tools#tools\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u5de5\u5177<\/a>\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/tools#what-is-a-tool-and-how-to-build-one\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u4ec0\u9ebc\u662f\u5de5\u5177\uff0c\u4ee5\u53ca\u5982\u4f55\u5efa\u7acb\u5de5\u5177\uff1f<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/tools#share-your-tool-to-the-hub\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u5206\u4eab\u60a8\u7684\u5de5\u5177\u5230 Hub<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/tools#import-a-space-as-a-tool\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u532f\u5165\u7a7a\u9593\u4f5c\u70ba\u5de5\u5177<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/tools#use-langchain-tools\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u4f7f\u7528 LangChain \u5de5\u5177<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/tools#manage-your-agents-toolbox\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u7ba1\u7406\u60a8\u7684\u4ee3\u7406\u5546\u5de5\u5177\u7bb1<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/tools#use-a-collection-of-tools\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u4f7f\u7528\u5de5\u5177\u96c6<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/tools#what-is-a-tool-and-how-to-build-one\" target=\"_blank\" rel=\"noopener\"><\/a>\u4ec0\u9ebc\u662f\u5de5\u5177\uff0c\u4ee5\u53ca\u5982\u4f55\u5efa\u7acb\u5de5\u5177\uff1f<\/h3>\n\n\n\n<p>\u5de5\u5177\u5927\u591a\u662f LLM \u53ef\u4ee5\u5728\u4ee3\u7406\u7cfb\u7d71\u4e2d\u4f7f\u7528\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u4f46\u8981\u4f7f\u7528\u5b83\uff0cLLM \u5c07\u9700\u8981\u88ab\u8ce6\u4e88\u4e00\u500b API\uff1a\u540d\u7a31\u3001\u5de5\u5177\u8aaa\u660e\u3001\u8f38\u5165\u985e\u578b\u548c\u8aaa\u660e\u3001\u8f38\u51fa\u985e\u578b\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u5b83\u4e0d\u80fd\u53ea\u662f\u4e00\u500b\u51fd\u6578\u3002\u5b83\u61c9\u8a72\u662f\u4e00\u500b\u985e\u5225\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u6838\u5fc3\u4f86\u8aaa\uff0c\u5de5\u5177\u662f\u4e00\u500b\u5305\u8986\u51fd\u6578\u7684\u985e\u5225\uff0c\u800c\u51fd\u6578\u7684\u5143\u8cc7\u6599\u53ef\u4ee5\u5e6b\u52a9 LLM \u77ad\u89e3\u5982\u4f55\u4f7f\u7528\u5b83\u3002<\/p>\n\n\n\n<p>\u770b\u8d77\u4f86\u662f\u9019\u6a23\u7684<\/p>\n\n\n\n<p>\u8907\u88fd<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u5f9e smolagents \u532f\u5165\u5de5\u5177\n\nclass HFModelDownloadsTool(Tool)\uff1a\n    name = \"model_download_counter\"\n    description = \"\"\"\n    \u9019\u662f\u4e00\u500b\u5de5\u5177\uff0c\u7528\u4f86\u56de\u50b3 Hugging Face Hub \u4e0a\u6307\u5b9a\u4efb\u52d9\u4e2d\u4e0b\u8f09\u6700\u591a\u7684\u6a21\u578b\u3002\n    \u5b83\u6703\u56de\u50b3\u6aa2\u67e5\u9ede\u7684\u540d\u7a31\u3002\"\"\"\n    \u8f38\u5165 = {\n        \"task\"\uff1a{\n            \"type\"\uff1a\"string\"\u3001\n            \"description\"\uff1a\u300c\u4efb\u52d9\u985e\u5225 (\u4f8b\u5982\u6587\u5b57\u5206\u985e\u3001\u6df1\u5ea6\u4f30\u7b97\u7b49)\u300d\u3001\n        }\n    }\n    output_type = \"string\"\n\n    def forward(self, task: str)\uff1a\n        from huggingface_hub import list_models\n\n        model = next(iter(list_models(filter=task, sort=\"downloads\", direction=-1)))\n        return model.id\n\nmodel_downloads_tool = HFModelDownloadsTool()<\/pre>\n\n\n\n<p>\u81ea\u8a02\u5de5\u5177\u5b50\u985e\u00a0<a href=\"https:\/\/huggingface.co\/docs\/smolagents\/v1.2.2\/en\/reference\/tools#smolagents.Tool\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u5de5\u5177<\/a>\u00a0\u4f86\u7e7c\u627f\u6709\u7528\u7684\u65b9\u6cd5\u3002\u5b50\u985e\u4e5f\u5b9a\u7fa9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c6c\u6027\u00a0<code>\u59d3\u540d<\/code>\uff0c\u8207\u5de5\u5177\u672c\u8eab\u7684\u540d\u7a31\u76f8\u5c0d\u61c9\u3002\u540d\u7a31\u901a\u5e38\u6703\u8aaa\u660e\u5de5\u5177\u7684\u4f5c\u7528\u3002\u7531\u65bc\u7a0b\u5f0f\u78bc\u6703\u56de\u50b3\u4e0b\u8f09\u6700\u591a\u7684\u4efb\u52d9\u6a21\u578b\uff0c\u56e0\u6b64\u6211\u5011\u5c07\u5b83\u547d\u540d\u70ba\u00a0<code>\u6a21\u578b\u4e0b\u8f09\u8a08\u6578\u5668<\/code>.<\/li>\n\n\n\n<li>\u5c6c\u6027\u00a0<code>\u63cf\u8ff0<\/code>\u00a0\u7528\u65bc\u586b\u5145\u4ee3\u7406\u7684\u7cfb\u7d71\u63d0\u793a\u3002<\/li>\n\n\n\n<li>\u984f\u8272\u00a0<code>\u8f38\u5165<\/code>\u00a0\u5c6c\u6027\uff0c\u9019\u662f\u4e00\u500b\u5b57\u5178\uff0c\u5176\u9375\u70ba\u00a0<code>\u300c\u985e\u578b\u300d<\/code>\u00a0\u548c\u00a0<code>\u300c\u8aaa\u660e\u300d<\/code>.\u5b83\u5305\u542b\u4e86\u5e6b\u52a9 Python \u89e3\u8b6f\u5668\u5c0d\u8f38\u5165\u505a\u51fa\u660e\u667a\u9078\u64c7\u7684\u8cc7\u8a0a\u3002<\/li>\n\n\n\n<li>\u984f\u8272\u00a0<code>\u8f38\u51fa\u985e\u578b<\/code>\u00a0\u5c6c\u6027\uff0c\u6307\u5b9a\u8f38\u51fa\u985e\u578b\u3002\u5169\u8005\u7684\u985e\u578b\u90fd\u662f\u00a0<code>\u8f38\u5165<\/code>\u00a0\u548c\u00a0<code>\u8f38\u51fa\u985e\u578b<\/code>\u00a0\u61c9\u00a0<a href=\"https:\/\/docs.pydantic.dev\/latest\/concepts\/json_schema\/#generating-json-schema\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Pydantic \u683c\u5f0f<\/a>, \u4ed6\u5011\u53ef\u4ee5\u662f\u5176\u4e2d\u4e4b\u4e00\uff1a\u00a0<code>~authorized_types()<\/code>.<\/li>\n\n\n\n<li>A\u00a0<code>\u524d\u8a00<\/code>\u00a0\u65b9\u6cd5\uff0c\u5176\u4e2d\u5305\u542b\u8981\u57f7\u884c\u7684\u63a8\u7406\u7a0b\u5f0f\u78bc\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u800c\u9019\u5c31\u662f\u5b83\u5728\u4ee3\u7406\u4e2d\u4f7f\u7528\u7684\u5168\u90e8\u9700\u6c42\uff01<\/p>\n\n\n\n<p>\u9084\u6709\u53e6\u4e00\u7a2e\u5efa\u7acb\u5de5\u5177\u7684\u65b9\u6cd5\u3002\u5728\u00a0<a href=\"https:\/\/huggingface.co\/docs\/smolagents\/guided_tour\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u5c0e\u89bd<\/a>\uff0c\u6211\u5011\u4f7f\u7528\u00a0<code>@ \u5de5\u5177<\/code>\u00a0\u88dd\u98fe\u5e2b\u3002\u64a0\u00a0<a href=\"https:\/\/huggingface.co\/docs\/smolagents\/v1.2.2\/en\/reference\/tools#smolagents.tool\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u5de5\u5177()<\/a>\u00a0\u88dd\u98fe\u5668\u662f\u300c\u5b9a\u7fa9\u7c21\u55ae\u5de5\u5177\u300d\u7684\u63a8\u85a6\u65b9\u5f0f\uff0c\u4f46\u6709\u6642\u60a8\u9700\u8981\u7684\u4e0d\u53ea\u5982\u6b64\uff1a\u5728\u4e00\u500b\u985e\u4e2d\u4f7f\u7528\u6578\u500b\u65b9\u6cd5\u4f7f\u5176\u66f4\u6e05\u6670\uff0c\u6216\u4f7f\u7528\u984d\u5916\u7684\u985e\u5225\u5c6c\u6027\u3002<\/p>\n\n\n\n<p>\u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0c\u60a8\u53ef\u4ee5\u900f\u904e\u5c07\u00a0<a href=\"https:\/\/huggingface.co\/docs\/smolagents\/v1.2.2\/en\/reference\/tools#smolagents.Tool\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u5de5\u5177<\/a>\u00a0\u5982\u4e0a\u6240\u8ff0\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/tools#share-your-tool-to-the-hub\" target=\"_blank\" rel=\"noopener\"><\/a>\u5206\u4eab\u60a8\u7684\u5de5\u5177\u5230 Hub<\/h3>\n\n\n\n<p>\u60a8\u53ef\u4ee5\u900f\u904e\u547c\u53eb\u00a0<a href=\"https:\/\/huggingface.co\/docs\/smolagents\/v1.2.2\/en\/reference\/tools#smolagents.Tool.push_to_hub\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">push_too_hub()<\/a>\u00a0\u4e0a\u7684\u5de5\u5177\u3002\u78ba\u4fdd\u60a8\u5df2\u5728 Hub \u4e0a\u70ba\u5176\u5efa\u7acb\u5132\u5b58\u5eab\uff0c\u4e26\u4f7f\u7528\u5177\u6709\u8b80\u53d6\u5b58\u53d6\u6b0a\u9650\u7684\u4ee4\u724c\u3002<\/p>\n\n\n\n<p>\u8907\u88fd<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">model_downloads_tool.push_to_hub(\"{your_username}\/hf-model-downloads\", token=\"\")<\/pre>\n\n\n\n<p>\u8981\u8b93 push to Hub \u6b63\u5e38\u904b\u4f5c\uff0c\u60a8\u7684\u5de5\u5177\u9700\u8981\u9075\u5b88\u4e00\u4e9b\u898f\u5247\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6240\u6709\u65b9\u6cd5\u90fd\u662f\u81ea\u8db3\u7684\uff0c\u4f8b\u5982\uff0c\u4f7f\u7528\u7684\u8b8a\u6578\u53ef\u4ee5\u4f86\u81ea\u5176 args\u3002<\/li>\n\n\n\n<li>\u6839\u64da\u4e0a\u8ff0\u89c0\u9ede\u3001\u00a0<strong>\u6240\u6709\u7684\u532f\u5165\u90fd\u61c9\u8a72\u76f4\u63a5\u5728\u5de5\u5177\u7684\u51fd\u5f0f\u4e2d\u5b9a\u7fa9<\/strong>\u5426\u5247\u5728\u5617\u8a66\u547c\u53eb\u00a0<a href=\"https:\/\/huggingface.co\/docs\/smolagents\/v1.2.2\/en\/reference\/tools#smolagents.Tool.save\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u5132\u5b58()<\/a>\u00a0\u6216\u00a0<a href=\"https:\/\/huggingface.co\/docs\/smolagents\/v1.2.2\/en\/reference\/tools#smolagents.Tool.push_to_hub\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">push_too_hub()<\/a>\u00a0\u4f7f\u7528\u60a8\u7684\u81ea\u8a02\u5de5\u5177\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u60a8\u5c07\u00a0<code>__\u555f\u52d5__<\/code>\u00a0\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u7d66\u5b83\u9664\u4e86\u00a0<code>\u81ea\u6211<\/code>.\u9019\u662f\u56e0\u70ba\u5728\u7279\u5b9a\u5de5\u5177\u5be6\u4f8b\u7684\u521d\u59cb\u5316\u904e\u7a0b\u4e2d\u8a2d\u5b9a\u7684\u53c3\u6578\u5f88\u96e3\u8ffd\u8e64\uff0c\u56e0\u6b64\u7121\u6cd5\u6b63\u78ba\u5730\u5206\u4eab\u5230\u96c6\u7dda\u5668\u3002\u7121\u8ad6\u5982\u4f55\uff0c\u88fd\u4f5c\u7279\u5b9a\u985e\u5225\u7684\u60f3\u6cd5\u662f\uff0c\u60a8\u5df2\u7d93\u53ef\u4ee5\u70ba\u4efb\u4f55\u9700\u8981\u786c\u7a0b\u5f0f\u78bc\u7684\u6771\u897f\u8a2d\u5b9a\u985e\u5225\u5c6c\u6027 (\u53ea\u8981\u8a2d\u5b9a\u00a0<code>your_variable=(...)<\/code>\u00a0\u76f4\u5c6c\u00a0<code>class YourTool(Tool)\uff1a<\/code>\u00a0\u884c\uff09\u3002\u7576\u7136\u60a8\u4e5f\u53ef\u4ee5\u5728\u7a0b\u5f0f\u78bc\u7684\u4efb\u4f55\u5730\u65b9\u5efa\u7acb\u985e\u5225\u5c6c\u6027\uff0c\u53ea\u8981\u5c07\u6771\u897f\u6307\u5b9a\u7d66\u00a0<code>self.your_variable<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>\u4e00\u65e6\u60a8\u7684\u5de5\u5177\u88ab\u63a8\u9001\u5230 Hub\uff0c\u60a8\u5c31\u53ef\u4ee5\u5c07\u5176\u8996\u89ba\u5316\u3002\u00a0<a href=\"https:\/\/huggingface.co\/spaces\/m-ric\/hf-model-downloads\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u9019\u88e1<\/a>\u00a0\u662f\u00a0<code>\u6a21\u578b\u4e0b\u8f09\u5de5\u5177<\/code>\u00a0\u6211\u63a8\u904e\u7684\u3002\u5b83\u6709\u5f88\u597d\u7684 gradio \u4ecb\u9762\u3002<\/p>\n\n\n\n<p>\u7576\u6df1\u5165\u7814\u7a76\u5de5\u5177\u6a94\u6848\u6642\uff0c\u60a8\u53ef\u4ee5\u767c\u73fe\u6240\u6709\u5de5\u5177\u7684\u908f\u8f2f\u90fd\u5728\u00a0<a href=\"https:\/\/huggingface.co\/spaces\/m-ric\/hf-model-downloads\/blob\/main\/tool.py\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">\u5de5\u5177.py<\/a>.\u9019\u662f\u60a8\u53ef\u4ee5\u6aa2\u8996\u4ed6\u4eba\u5206\u4eab\u7684\u5de5\u5177\u7684\u5730\u65b9\u3002<\/p>\n\n\n\n<p>\u7136\u5f8c\uff0c\u60a8\u5c31\u53ef\u4ee5\u4f7f\u7528\u00a0<a href=\"https:\/\/huggingface.co\/docs\/smolagents\/v1.2.2\/en\/reference\/tools#smolagents.load_tool\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">load_tool()<\/a>\u00a0\u6216\u4f7f\u7528\u00a0<a href=\"https:\/\/huggingface.co\/docs\/smolagents\/v1.2.2\/en\/reference\/tools#smolagents.Tool.from_hub\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">from_hub()<\/a>\u00a0\u4e26\u50b3\u9001\u5230\u00a0<code>\u5de5\u5177<\/code>\u00a0\u53c3\u6578\u3002\u7531\u65bc\u57f7\u884c\u5de5\u5177\u610f\u5473\u8457\u57f7\u884c\u81ea\u8a02\u7a0b\u5f0f\u78bc\uff0c\u60a8\u9700\u8981\u78ba\u5b9a\u60a8\u4fe1\u4efb\u5957\u4ef6\u5eab\uff0c\u56e0\u6b64\u6211\u5011\u9700\u8981\u50b3\u9001\u00a0<code>trust_remote_code=True<\/code>\u00a0\u5f9e Hub \u8f09\u5165\u5de5\u5177\u3002<\/p>\n\n\n\n<p>\u8907\u88fd<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from smolagents import load_tool, CodeAgent\n\nmodel_download_tool = load_tool(\n    \"{your_username}\/hf-model-downloads\"\u3001\n    trust_remote_code=True\n)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/tools#import-a-space-as-a-tool\" target=\"_blank\" rel=\"noopener\"><\/a>\u532f\u5165\u7a7a\u9593\u4f5c\u70ba\u5de5\u5177<\/h3>\n\n\n\n<p>\u60a8\u53ef\u4ee5\u76f4\u63a5\u5f9e Hub \u532f\u5165 Space \u4f5c\u70ba\u5de5\u5177\uff0c\u4f7f\u7528\u00a0<a href=\"https:\/\/huggingface.co\/docs\/smolagents\/v1.2.2\/en\/reference\/tools#smolagents.Tool.from_space\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Tool.from_space()<\/a>\u00a0\u65b9\u6cd5\uff01<\/p>\n\n\n\n<p>\u60a8\u53ea\u9700\u63d0\u4f9b Hub \u4e0a Space \u7684 id\u3001\u5176\u540d\u7a31\uff0c\u4ee5\u53ca\u6709\u52a9\u65bc\u4ee3\u7406\u77ad\u89e3\u8a72\u5de5\u5177\u529f\u80fd\u7684\u8aaa\u660e\u3002\u5728\u5f15\u64ce\u84cb\u4e0b\uff0c\u9019\u5c07\u4f7f\u7528\u00a0<a href=\"https:\/\/pypi.org\/project\/gradio-client\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\"><code>gradio-client<\/code><\/a>\u00a0\u51fd\u5f0f\u5eab\u4f86\u547c\u53eb Space\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u8b93\u6211\u5011\u532f\u5165\u00a0<a href=\"https:\/\/huggingface.co\/black-forest-labs\/FLUX.1-dev\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">FLUX.1-dev<\/a>\u00a0Space from the Hub and use it to generate an image.<\/p>\n\n\n\n<p>\u8907\u88fd<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">image_generation_tool = Tool.from_space(\n    \"black-forest-labs\/FLUX.1-schnell\"\u3001\n    name=\"image_generator\"\u3001\n    description=\u300c\u5f9e\u63d0\u793a\u7522\u751f\u5f71\u50cf\u300d\n)\n\nimage_generation_tool(\"A sunny beach\")<\/pre>\n\n\n\n<p>\u77a7\uff0c\u9019\u5c31\u662f\u60a8\u7684\u5716\u7247\uff01\ud83c\udfd6\ufe0f<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/sunny_beach.webp\" alt=\"\" class=\"wp-image-593\" style=\"width:1176px;height:auto\" srcset=\"https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/sunny_beach.webp 1024w, https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/sunny_beach-300x300.webp 300w, https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/sunny_beach-150x150.webp 150w, https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/sunny_beach-768x768.webp 768w, https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/sunny_beach-12x12.webp 12w, https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/sunny_beach-360x360.webp 360w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u7136\u5f8c\u60a8\u5c31\u53ef\u4ee5\u50cf\u4f7f\u7528\u5176\u4ed6\u5de5\u5177\u4e00\u6a23\u4f7f\u7528\u6b64\u5de5\u5177\u3002\u4f8b\u5982\uff0c\u8b93\u6211\u5011\u6539\u5584\u63d0\u793a&nbsp;<code>\u7a7f\u592a\u7a7a\u670d\u7684\u5154\u5b50<\/code>&nbsp;\u4e26\u7522\u751f\u5176\u5f71\u50cf\u3002<\/p>\n\n\n\n<p>\u8907\u88fd<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from smolagents import CodeAgent, HfApiModel\n\nmodel = HfApiModel(\"Qwen\/Qwen2.5-Coder-32B-Instruct\")\nagent = CodeAgent(tools=[image_generation_tool], model=model)\n\nagent.run(\n    \u300c\u6539\u5584\u6b64\u63d0\u793a\uff0c\u7136\u5f8c\u7522\u751f\u5176\u5f71\u50cf\u3002\u300d, prompt='\u4e00\u96bb\u7a7f\u8457\u592a\u7a7a\u8863\u7684\u5154\u5b50\n)<\/pre>\n\n\n\n<p>\u8907\u88fd<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">=== \u63a2\u54e1\u7684\u60f3\u6cd5\uff1a\nimproved_prompt \u53ef\u4ee5\u662f \u300c\u7a7f\u8457\u9bae\u85cd\u592a\u7a7a\u670d\u7684\u5154\u5b50\uff0c\u5728\u6708\u7403\u8868\u9762\uff0c\u5728\u660e\u4eae\u7684\u6a58\u8272\u5915\u967d\u4e0b\uff0c\u80cc\u666f\u662f\u53ef\u898b\u7684\u5730\u7403\u300d\n\n\u73fe\u5728\u6211\u5df2\u7d93\u6539\u9032\u4e86\u63d0\u793a\uff0c\u6211\u53ef\u4ee5\u4f7f\u7528\u5716\u7247\u7522\u751f\u5668\u5de5\u5177\uff0c\u6839\u64da\u9019\u500b\u63d0\u793a\u7522\u751f\u5716\u7247\u3002\n&gt;&gt;&gt; Agent \u6b63\u5728\u57f7\u884c\u4e0b\u9762\u7684\u7a0b\u5f0f\u78bc\uff1a\nimage = image_generator(prompt=\"A bright blue space suit wearing rabbit, on the surface of the moon, under a bright orange sunset, with the Earth visible in the background\")\nfinal_answer(image)<\/pre>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/rabbit_spacesuit_flux.webp\" alt=\"\" class=\"wp-image-592\" style=\"width:1178px;height:auto\" srcset=\"https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/rabbit_spacesuit_flux.webp 1024w, https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/rabbit_spacesuit_flux-300x300.webp 300w, https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/rabbit_spacesuit_flux-150x150.webp 150w, https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/rabbit_spacesuit_flux-768x768.webp 768w, https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/rabbit_spacesuit_flux-12x12.webp 12w, https:\/\/smolagents.org\/wp-content\/uploads\/2025\/01\/rabbit_spacesuit_flux-360x360.webp 360w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u9019\u6709\u591a\u9177\uff1f\ud83e\udd29<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/tools#use-langchain-tools\" target=\"_blank\" rel=\"noopener\"><\/a>\u4f7f\u7528 LangChain \u5de5\u5177<\/h3>\n\n\n\n<p>\u6211\u5011\u5f88\u559c\u6b61 Langchain\uff0c\u8a8d\u70ba\u5b83\u6709\u4e00\u5957\u975e\u5e38\u5f15\u4eba\u6ce8\u76ee\u7684\u5de5\u5177\u3002\u8981\u5f9e LangChain \u532f\u5165\u5de5\u5177\uff0c\u8acb\u4f7f\u7528&nbsp;<code>from_langchain()<\/code>&nbsp;\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 LangChain \u7db2\u8def\u641c\u5c0b\u5de5\u5177\u4f86\u91cd\u73fe\u4ecb\u7d39\u7684\u641c\u5c0b\u7d50\u679c\u3002\u9019\u500b\u5de5\u5177\u9700\u8981&nbsp;<code>pip install langchain google-search-results -q<\/code>&nbsp;\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n\n\n\n<p>\u8907\u88fd<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u5f9e langchain.agents \u532f\u5165 load_tools\n\nsearch_tool = Tool.from_langchain(load_tools([\"serpapi\"])[0])\n\nagent = CodeAgent(tools=[search_tool], model=model)\n\nagent.run(\"How many more blocks (also denoted as layers) are in BERT base encoder compared to the encoder from the architecture proposed in Attention is All You Need?\")<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/tools#manage-your-agents-toolbox\" target=\"_blank\" rel=\"noopener\"><\/a>\u7ba1\u7406\u60a8\u7684\u4ee3\u7406\u5546\u5de5\u5177\u7bb1<\/h3>\n\n\n\n<p>\u60a8\u53ef\u4ee5\u900f\u904e\u5728\u5c6c\u6027\u4e2d\u65b0\u589e\u6216\u66f4\u63db\u5de5\u5177\u4f86\u7ba1\u7406\u4ee3\u7406\u7684\u5de5\u5177\u7bb1\u3002&nbsp;<code>\u4ee3\u7406\u5de5\u5177<\/code>, \u56e0\u70ba\u5b83\u662f\u6a19\u6e96\u5b57\u5178\u3002<\/p>\n\n\n\n<p>\u8b93\u6211\u5011\u52a0\u5165&nbsp;<code>\u6a21\u578b\u4e0b\u8f09\u5de5\u5177<\/code>&nbsp;\u5230\u50c5\u4f7f\u7528\u9810\u8a2d\u5de5\u5177\u7bb1\u521d\u59cb\u5316\u7684\u73fe\u6709\u4ee3\u7406\u7a0b\u5f0f\u3002<\/p>\n\n\n\n<p>\u8907\u88fd<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u5f9e smolagents \u532f\u5165 HfApiModel\n\nmodel = HfApiModel(\"Qwen\/Qwen2.5-Coder-32B-Instruct\")\n\nagent = CodeAgent(tools=[], model=model, add_base_tools=True)\nagent.tools[model_download_tool.name] = model_download_tool<\/pre>\n\n\n\n<p>\u73fe\u5728\u6211\u5011\u53ef\u4ee5\u5229\u7528\u65b0\u5de5\u5177\uff1a<\/p>\n\n\n\n<p>\u8907\u88fd<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">agent.run(\n    \"\u60a8\u80fd\u544a\u8a34\u6211\u5728\u62b1\u62b1\u81c9\u6a1e\u7d10\u7684\u300c\u6587\u5b57\u8f49\u5f71\u7247\u300d\u4efb\u52d9\u4e2d\uff0c\u4e0b\u8f09\u6b21\u6578\u6700\u591a\u4f46\u5b57\u6bcd\u76f8\u53cd\u7684\u6a21\u578b\u540d\u7a31\u55ce\uff1f\"\n)<\/pre>\n\n\n\n<p>\u5c0f\u5fc3\u4e0d\u8981\u70ba\u4ee3\u7406\u7a0b\u5f0f\u52a0\u5165\u592a\u591a\u5de5\u5177\uff1a\u9019\u6703\u8b93\u8f03\u5f31\u7684 LLM \u5f15\u64ce\u4e0d\u582a\u8ca0\u8377\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/tools#use-a-collection-of-tools\" target=\"_blank\" rel=\"noopener\"><\/a>\u4f7f\u7528\u5de5\u5177\u96c6<\/h3>\n\n\n\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 ToolCollection \u7269\u4ef6\uff0c\u52a0\u4e0a\u60a8\u8981\u4f7f\u7528\u7684\u96c6\u5408\u7684\u6a19\u984c\uff0c\u4f86\u5229\u7528\u5de5\u5177\u96c6\u5408\u3002\u7136\u5f8c\uff0c\u4ee5\u6e05\u55ae\u7684\u65b9\u5f0f\u50b3\u9001\u5b83\u5011\uff0c\u521d\u59cb\u5316\u60a8\u7684\u4ee3\u7406\u7a0b\u5f0f\uff0c\u4e26\u958b\u59cb\u4f7f\u7528\u5b83\u5011\uff01<\/p>\n\n\n\n<p>\u8907\u88fd<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u5f9e smolagents \u532f\u5165 ToolCollection\u3001CodeAgent\n\nimage_tool_collection = ToolCollection(\n    collection_slug=\"huggingface-tools\/diffusion-tools-6630bb19a942c2306a2cdb6f\",\n    token=\"\"\n)\nagent = CodeAgent(tools=[*image_tool_collection.tools], model=model, add_base_tools=True)\n\nagent.run(\"Please draw me a picture of rivers and lakes.\")<\/pre>\n\n\n\n<p>\u70ba\u4e86\u52a0\u5feb\u555f\u52d5\u901f\u5ea6\uff0c\u53ea\u6709\u5728\u4ee3\u7406\u7a0b\u5f0f\u547c\u53eb\u6642\u624d\u6703\u8f09\u5165\u5de5\u5177\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>Here, we\u2019re going to see advanced tool usage. If you\u2019re new to building agents, make sure to first read the\u00a0intro to agents\u00a0and the\u00a0guided tour of smolagents. What is a tool, and how to build one? A tool is mostly a function that an LLM can use in an agentic system. But to use it, the&#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":[8],"doc_tag":[],"class_list":["post-591","docs","type-docs","status-publish","hentry","doc_category-tutorials"],"year_month":"2026-04","word_count":1431,"total_views":"5891","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":"Tutorials","term_url":"https:\/\/smolagents.org\/zh_tw\/docs-category\/tutorials\/"}],"doc_tag_info":[],"_links":{"self":[{"href":"https:\/\/smolagents.org\/zh_tw\/wp-json\/wp\/v2\/docs\/591","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=591"}],"version-history":[{"count":1,"href":"https:\/\/smolagents.org\/zh_tw\/wp-json\/wp\/v2\/docs\/591\/revisions"}],"predecessor-version":[{"id":594,"href":"https:\/\/smolagents.org\/zh_tw\/wp-json\/wp\/v2\/docs\/591\/revisions\/594"}],"wp:attachment":[{"href":"https:\/\/smolagents.org\/zh_tw\/wp-json\/wp\/v2\/media?parent=591"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/smolagents.org\/zh_tw\/wp-json\/wp\/v2\/doc_category?post=591"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/smolagents.org\/zh_tw\/wp-json\/wp\/v2\/doc_tag?post=591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}