{"id":10020294,"date":"2024-08-29T00:00:00","date_gmt":"2024-08-29T00:00:00","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020294"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T16:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020294","title":{"rendered":"AI\u5927\u6a21\u578b\u5b9e\u6218\u7bc7\uff1aAI Agent\u8bbe\u8ba1\u6a21\u5f0f\uff0cReAct"},"content":{"rendered":"<p><img decoding=\"async\" data-action=\"zoom\" class=\"aligncenter\" src=\"https:\/\/image.woshipm.com\/2024\/07\/02\/083e0566-386b-11ef-90af-00163e142b65.png\" \/><\/p>\n<p>\u5728<a href=\"https:\/\/www.woshipm.com\/ai\/6088356.html\" target=\"_blank\" rel=\"noopener\">\u300a\u5927\u4f6c\u4eec\u90fd\u5728\u5173\u6ce8\u7684AI Agent\uff0c\u5230\u5e95\u662f\u4ec0\u4e48\uff1f\u75285W1H\u5206\u6790\u6846\u67b6\u62c6\u89e3AI Agent\uff08\u4e0b\u7bc7\uff09\u300b<\/a>\u4e2d\uff0c\u98ce\u53d4\u7b80\u5355\u4ecb\u7ecd\u4e86AI Agent\u7684\u516b\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u3002\u5bf9\u4e8e\u8fd9\u516b\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u98ce\u53d4\u6574\u7406\u4e86\u4e00\u5f20\u56fe\uff0c\u6765\u9610\u660e\u5b83\u4eec\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n<p>ReAct\u6a21\u5f0f\u6700\u65e9\u51fa\u73b0\u7684Agent\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u76ee\u524d\u4e5f\u662f\u5e94\u7528\u6700\u5e7f\u6cdb\u7684\u3002\u4eceReAct\u51fa\u53d1\uff0c\u6709\u4e24\u6761\u53d1\u5c55\u8def\u7ebf\uff1a<\/p>\n<p>\u4e00\u6761\u66f4\u504f\u91cdAgent\u7684\u89c4\u5212\u80fd\u529b\uff0c\u5305\u62ecREWOO\u3001Plan &amp; Execute\u3001LLM Compiler\u3002<\/p>\n<p>\u53e6\u4e00\u6761\u66f4\u504f\u91cd\u53cd\u601d\u80fd\u529b\uff0c\u5305\u62ecBasic Reflection\u3001Reflexion\u3001Self Discover\u3001LATS\u3002<\/p>\n<p><img decoding=\"async\" data-action=\"zoom\" class=\"aligncenter\" src=\"https:\/\/image.woshipm.com\/wp-files\/2024\/08\/vCPZojgYvGLPz5bT1zqu.png\" \/><\/p>\n<p>\u5728\u540e\u7eed\u6587\u7ae0\u4e2d\uff0c\u98ce\u53d4\u5c06\u6cbf\u7740\u4e0a\u56fe\u7684\u8109\u7edc\uff0c\u7ed3\u5408\u4ea7\u54c1\u6d41\u7a0b\u548c\u6e90\u4ee3\u7801\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u516b\u79cdAI Agent\u8bbe\u8ba1\u6a21\u5f0f\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u9009\u62e9\u7ed3\u5408\u6e90\u4ee3\u7801\u5462\uff1f\u56e0\u4e3a\u5728AI\u5927\u6a21\u578b\u65f6\u4ee3\uff0c\u5f88\u591a\u7684\u6982\u5ff5\u548c\u65b9\u6cd5\u90fd\u592a\u65b0\u4e86\u3002\u53ea\u6709\u7ed3\u5408\u6e90\u4ee3\u7801\uff0c\u4ea7\u54c1\u7ecf\u7406\u624d\u80fd\u771f\u6b63\u7406\u89e3\u80cc\u540e\u7684\u539f\u7406\u548c\u903b\u8f91\uff0c\u624d\u80fd\u77e5\u9053\u4ec0\u4e48\u80fd\u505a\uff0c\u4ec0\u4e48\u4e0d\u80fd\u505a\uff0cAI\u7684\u8fb9\u754c\u5728\u54ea\u91cc\uff0c\u4ee5\u53ca\u8be5\u5982\u4f55\u4e0e\u4eba\u7c7b\u7ecf\u9a8c\u914d\u5408\u3002<\/p>\n<div class=\"js-star yyp--fancyPost\" data-id=\"45\"><\/div>\n<p>\u4e0b\u9762\uff0c\u6211\u4eec\u5148\u4eceReAct\u6a21\u5f0f\u5f00\u59cb\u3002<\/p>\n<h2  id=\"toc-1\" class=\"jltoc--item\">\u4e00\u3001ReAct\u7684\u6982\u5ff5<\/h2>\n<p>ReAct\u7684\u6982\u5ff5\u6765\u81ea\u8bba\u6587\u300aReAct: Synergizing Reasoning and Acting in Language Models\u300b\uff0c\u8fd9\u7bc7\u8bba\u6587\u63d0\u51fa\u4e86\u4e00\u79cd\u65b0\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u7ed3\u5408\u8bed\u8a00\u6a21\u578b\u4e2d\u7684\u63a8\u7406\uff08reasoning\uff09\u548c\u884c\u52a8\uff08acting\uff09\u6765\u89e3\u51b3\u591a\u6837\u5316\u7684\u8bed\u8a00\u63a8\u7406\u548c\u51b3\u7b56\u4efb\u52a1\u3002ReAct \u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u6613\u4e8e\u4eba\u7c7b\u7406\u89e3\u3001\u8bca\u65ad\u548c\u63a7\u5236\u7684\u51b3\u7b56\u548c\u63a8\u7406\u8fc7\u7a0b\u3002<\/p>\n<p>\u5b83\u7684\u5178\u578b\u6d41\u7a0b\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u53ef\u4ee5\u7528\u4e00\u4e2a\u6709\u8da3\u7684\u5faa\u73af\u6765\u63cf\u8ff0\uff1a\u601d\u8003\uff08Thought\uff09\u2192 \u884c\u52a8\uff08Action\uff09\u2192 \u89c2\u5bdf\uff08Observation\uff09\uff0c\u7b80\u79f0TAO\u5faa\u73af\u3002<\/p>\n<ul>\n<li>\u601d\u8003\uff08Thought\uff09\uff1a\u9762\u5bf9\u4e00\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u8fdb\u884c\u6df1\u5165\u7684\u601d\u8003\u3002\u8fd9\u4e2a\u601d\u8003\u8fc7\u7a0b\u662f\u5173\u4e8e\u5982\u4f55\u5b9a\u4e49\u95ee\u9898\u3001\u786e\u5b9a\u89e3\u51b3\u95ee\u9898\u6240\u9700\u7684\u5173\u952e\u4fe1\u606f\u548c\u63a8\u7406\u6b65\u9aa4\u3002<\/li>\n<li>\u884c\u52a8\uff08Action\uff09\uff1a\u786e\u5b9a\u4e86\u601d\u8003\u7684\u65b9\u5411\u540e\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u884c\u52a8\u7684\u65f6\u523b\u3002\u6839\u636e\u6211\u4eec\u7684\u601d\u8003\uff0c\u91c7\u53d6\u76f8\u5e94\u7684\u63aa\u65bd\u6216\u6267\u884c\u7279\u5b9a\u7684\u4efb\u52a1\uff0c\u4ee5\u671f\u671b\u63a8\u52a8\u95ee\u9898\u5411\u89e3\u51b3\u7684\u65b9\u5411\u53d1\u5c55\u3002<\/li>\n<li>\u89c2\u5bdf\uff08Observation\uff09\uff1a\u884c\u52a8\u4e4b\u540e\uff0c\u6211\u4eec\u5fc5\u987b\u4ed4\u7ec6\u89c2\u5bdf\u7ed3\u679c\u3002\u8fd9\u4e00\u6b65\u662f\u68c0\u9a8c\u6211\u4eec\u7684\u884c\u52a8\u662f\u5426\u6709\u6548\uff0c\u662f\u5426\u63a5\u8fd1\u4e86\u95ee\u9898\u7684\u7b54\u6848\u3002<\/li>\n<li>\u5faa\u73af\u8fed\u4ee3<\/li>\n<\/ul>\n<p><img decoding=\"async\" data-action=\"zoom\" class=\"aligncenter\" src=\"https:\/\/image.woshipm.com\/wp-files\/2024\/08\/1HLz9PSt5DnPZyPxp37E.png\" \/><\/p>\n<p>\u5982\u679c\u89c2\u5bdf\u5230\u7684\u7ed3\u679c\u5e76\u4e0d\u5339\u914d\u6211\u4eec\u9884\u671f\u7684\u7b54\u6848\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u56de\u5230\u601d\u8003\u9636\u6bb5\uff0c\u91cd\u65b0\u5ba1\u89c6\u95ee\u9898\u548c\u884c\u52a8\u8ba1\u5212\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u5f00\u59cb\u4e86\u65b0\u4e00\u8f6e\u7684TAO\u5faa\u73af\uff0c\u76f4\u5230\u627e\u5230\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>\u548cReAct\u76f8\u5bf9\u5e94\u7684\u662fReasoning-Only\u548cAction-Only\u3002\u5728Reasoning-Only\u7684\u6a21\u5f0f\u4e0b\uff0c\u5927\u6a21\u578b\u4f1a\u57fa\u4e8e\u4efb\u52a1\u8fdb\u884c\u9010\u6b65\u601d\u8003\uff0c\u5e76\u4e14\u4e0d\u7ba1\u6709\u6ca1\u6709\u83b7\u5f97\u7ed3\u679c\uff0c\u90fd\u4f1a\u628a\u601d\u8003\u7684\u6bcf\u4e00\u6b65\u90fd\u6267\u884c\u4e00\u904d\u3002\u5728Action-Only\u7684\u6a21\u5f0f\u4e0b\uff0c\u5927\u6a21\u578b\u5c31\u4f1a\u5904\u4e8e\u5b8c\u5168\u6ca1\u6709\u89c4\u5212\u7684\u72b6\u6001\u4e0b\uff0c\u5148\u8fdb\u884c\u884c\u52a8\u518d\u8fdb\u884c\u89c2\u5bdf\uff0c\u57fa\u4e8e\u89c2\u5bdf\u518d\u8c03\u6574\u884c\u52a8\uff0c\u5bfc\u81f4\u6700\u7ec8\u7ed3\u679c\u4e0d\u53ef\u63a7\u3002<\/p>\n<p><img decoding=\"async\" data-action=\"zoom\" class=\"aligncenter\" src=\"https:\/\/image.woshipm.com\/wp-files\/2024\/08\/FXc7s1Swnbz6N5ZPVVnc.png\" \/><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6b63\u5728\u6784\u5efa\u4e00\u4e2a\u667a\u80fd\u52a9\u624b\uff0c\u7528\u4e8e\u7ba1\u7406\u6211\u4eec\u7684\u65e5\u7a0b\u5b89\u6392\u3002<\/p>\n<p>\u5728reason-only\u6a21\u5f0f\u4e2d\uff0c\u667a\u80fd\u52a9\u624b\u4e13\u6ce8\u4e8e\u5206\u6790\u548c\u63a8\u7406\uff0c\u4f46\u4e0d\u76f4\u63a5\u91c7\u53d6\u884c\u52a8\u3002<\/p>\n<ul>\n<li>\u4f60\u544a\u8bc9\u667a\u80fd\u52a9\u624b\uff1a\u201c\u6211\u660e\u5929\u6709\u4e2a\u4f1a\u8bae\u3002\u201d<\/li>\n<li>\u667a\u80fd\u52a9\u624b\u5206\u6790\u8fd9\u53e5\u8bdd\uff0c\u786e\u5b9a\u660e\u5929\u7684\u4f1a\u8bae\u65f6\u95f4\u3001\u5730\u70b9\u7b49\u7ec6\u8282\u3002<\/li>\n<li>\u5b83\u53ef\u80fd\u4f1a\u63d0\u9192\u4f60\uff1a\u201c\u660e\u5929\u4e0b\u53483\u70b9\u6709\u4e2a\u4f1a\u8bae\uff0c\u5728\u516c\u53f8\u4f1a\u8bae\u5ba4\u3002\u201d<\/li>\n<\/ul>\n<p>\u5728action-only\u6a21\u5f0f\u4e2d\uff0c\u667a\u80fd\u52a9\u624b\u4e13\u6ce8\u4e8e\u6267\u884c\u4efb\u52a1\uff0c\u4f46\u4e0d\u505a\u6df1\u5165\u7684\u63a8\u7406\u6216\u5206\u6790\u3002<\/p>\n<ul>\n<li>\u4f60\u544a\u8bc9\u667a\u80fd\u52a9\u624b\uff1a\u201c\u628a\u6211\u660e\u5929\u7684\u4f1a\u8bae\u6539\u5230\u4e0a\u534810\u70b9\u3002\u201d<\/li>\n<li>\u667a\u80fd\u52a9\u624b\u7acb\u5373\u6267\u884c\u8fd9\u4e2a\u4efb\u52a1\uff0c\u5c06\u4f1a\u8bae\u65f6\u95f4\u4fee\u6539\u4e3a\u4e0a\u534810\u70b9\u3002<\/li>\n<li>\u5b83\u53ef\u80fd\u4f1a\u7b80\u5355\u786e\u8ba4\uff1a\u201c\u60a8\u7684\u4f1a\u8bae\u5df2\u6539\u5230\u660e\u5929\u4e0a\u534810\u70b9\u3002\u201d<\/li>\n<\/ul>\n<p>\u5728ReAct\u6a21\u5f0f\u4e2d\uff0c\u667a\u80fd\u52a9\u624b\u7ed3\u5408\u63a8\u7406\u548c\u884c\u52a8\uff0c\u5f62\u6210\u4e00\u4e2a\u5faa\u73af\u7684\u611f\u77e5-\u52a8\u4f5c\u5faa\u73af\u3002\u4e0d\u4ec5\u5206\u6790\u4e86\u4f60\u7684\u9700\u6c42\uff08\u63a8\u7406\uff09\uff0c\u8fd8\u5b9e\u9645\u4fee\u6539\u4e86\u65e5\u7a0b\u5b89\u6392\uff08\u884c\u52a8\uff09\u3002<\/p>\n<ul>\n<li>\u4f60\u544a\u8bc9\u667a\u80fd\u52a9\u624b\uff1a\u201c\u6211\u660e\u5929\u6709\u4e2a\u4f1a\u8bae\uff0c\u4f46\u6211\u60f3\u63d0\u524d\u5230\u4e0a\u534810\u70b9\u3002\u201d<\/li>\n<li>\u667a\u80fd\u52a9\u624b\u9996\u5148\u5206\u6790\u8fd9\u53e5\u8bdd\uff0c\u786e\u5b9a\u4f1a\u8bae\u7684\u539f\u59cb\u65f6\u95f4\u548c\u5730\u70b9\uff08\u611f\u77e5\u9636\u6bb5\uff09\u3002<\/li>\n<li>\u7136\u540e\uff0c\u5b83\u66f4\u65b0\u4f60\u7684\u65e5\u7a0b\u5b89\u6392\uff0c\u5c06\u4f1a\u8bae\u65f6\u95f4\u6539\u4e3a\u4e0a\u534810\u70b9\uff08\u51b3\u7b56\u548c\u52a8\u4f5c\u6267\u884c\u9636\u6bb5\uff09\u3002<\/li>\n<li>\u6700\u540e\uff0c\u667a\u80fd\u52a9\u624b\u786e\u8ba4\u4fee\u6539\uff0c\u5e76\u63d0\u4f9b\u989d\u5916\u7684\u4fe1\u606f\uff1a\u201c\u60a8\u7684\u4f1a\u8bae\u5df2\u6210\u529f\u6539\u5230\u660e\u5929\u4e0a\u534810\u70b9\u3002\u63d0\u9192\u60a8\uff0c\u4f1a\u8bae\u5730\u70b9\u4e0d\u53d8\u3002<\/li>\n<\/ul>\n<h2  id=\"toc-2\" class=\"jltoc--item\">\u4e8c\u3001ReAct\u7684\u5b9e\u73b0\u8fc7\u7a0b<\/h2>\n<p>\u4e0b\u9762\uff0c\u98ce\u53d4\u901a\u8fc7\u5b9e\u9645\u7684\u6e90\u7801\uff0c\u8be6\u7ec6\u4ecb\u7ecdReAct\u6a21\u5f0f\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002\u5728\u624b\u673a\u7aef\u9605\u8bfb\u6e90\u4ee3\u7801\u7684\u4f53\u9a8c\u4e0d\u592a\u597d\uff0c\u5efa\u8bae\u5927\u5bb6\u5728PC\u7aef\u6253\u5f00\u3002<\/p>\n<p>\u5927\u5bb6\u53ef\u4ee5\u79c1\u4fe1\u98ce\u53d4\uff0c\u6216\u8005\u5728\u8bc4\u8bba\u533a\u7559\u8a00\u201cReAct\u6e90\u7801\u201d\uff0c\u83b7\u53d6ReAct\u8bbe\u8ba1\u6a21\u5f0f\u7684\u793a\u4f8b\u6e90\u4ee3\u7801\u3002<\/p>\n<h3>\u7b2c\u4e00\u6b65 \u51c6\u5907Prompt\u6a21\u677f<\/h3>\n<p>\u5728\u5b9e\u73b0ReAct\u6a21\u5f0f\u7684\u65f6\u5019\uff0c\u9996\u5148\u9700\u8981\u8bbe\u8ba1\u4e00\u4e2a\u6e05\u6670\u7684Prompt\u6a21\u677f\uff0c\u4e3b\u8981\u5305\u542b\u4ee5\u4e0b\u51e0\u4e2a\u5143\u7d20\uff1a<\/p>\n<ul>\n<li>\u601d\u8003\uff08Thought\uff09\uff1a\u8fd9\u662f\u63a8\u7406\u8fc7\u7a0b\u7684\u6587\u5b57\u5c55\u793a\uff0c\u9610\u660e\u6211\u4eec\u60f3\u8981LLM\u5e2e\u6211\u4eec\u505a\u4ec0\u4e48\uff0c\u4e3a\u4e86\u8fbe\u6210\u76ee\u6807\u7684\u524d\u7f6e\u6761\u4ef6\u662f\u4ec0\u4e48<\/li>\n<li>\u884c\u52a8\uff08Action\uff09\uff1a\u6839\u636e\u601d\u8003\u7684\u7ed3\u679c\uff0c\u751f\u6210\u4e0e\u5916\u90e8\u4ea4\u4e92\u7684\u6307\u4ee4\u6587\u5b57\uff0c\u6bd4\u5982\u9700\u8981LLM\u8fdb\u884c\u5916\u90e8\u641c\u7d22<\/li>\n<li>\u884c\u52a8\u53c2\u6570\uff08Action Input\uff09\uff1a\u7528\u4e8e\u5c55\u793aLLM\u8fdb\u884c\u4e0b\u4e00\u6b65\u884c\u52a8\u7684\u53c2\u6570\uff0c\u6bd4\u5982LLM\u8981\u8fdb\u884c\u5916\u90e8\u641c\u7d22\u7684\u8bdd\uff0c\u884c\u52a8\u53c2\u6570\u5c31\u662f\u641c\u7d22\u7684\u5173\u952e\u8bcd\u3002\u4e3b\u8981\u662f\u4e3a\u4e86\u9a8c\u8bc1LLM\u662f\u5426\u80fd\u63d0\u53d6\u51c6\u786e\u7684\u884c\u52a8\u53c2\u6570<\/li>\n<li>\u89c2\u5bdf\uff08Observation\uff09\uff1a\u548c\u5916\u90e8\u884c\u52a8\u4ea4\u4e92\u4e4b\u540e\u5f97\u5230\u7684\u7ed3\u679c\uff0c\u6bd4\u5982LLM\u8fdb\u884c\u5916\u90e8\u641c\u7d22\u7684\u8bdd\uff0c\u90a3\u4e48\u89c2\u5bdf\u5c31\u662f\u641c\u7d22\u7684\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p>Prompt\u6a21\u677f\u793a\u4f8b\uff1a<\/p>\n<pre><code>TOOL_DESC = \"\"\"{name_for_model}: Call this tool to interact with the {name_for_human} API. What is the {name_for_human} API useful for? {description_for_model} Parameters: {parameters} Format the arguments as a JSON object.\"\"\"\r\nREACT_PROMPT = \"\"\"Answer the following questions as best you can. You have access to the following tools:\r\n{tool_descs}\r\nUse the following format:\r\n\r\nQuestion: the input question you must answer\r\nThought: you should always think about what to do\r\nAction: the action to take, should be one of [{tool_names}]\r\nAction Input: the input to the action\r\nObservation: the result of the action\r\n... (this Thought\/Action\/Action Input\/Observation can be repeated zero or more times)\r\nThought: I now know the final answer\r\nFinal Answer: the final answer to the original input question\r\nBegin!\r\nQuestion: {query}\"\"\"<\/code><\/pre>\n<h3>\u7b2c\u4e8c\u6b65 \u6784\u5efaAgent<\/h3>\n<p>\u4e00\u4e2aReAct Agent\u9700\u8981\u5b9a\u4e49\u597d\u4ee5\u4e0b\u5143\u7d20<\/p>\n<ul>\n<li>llm\uff1a\u80cc\u540e\u4f7f\u7528\u7684LLM\u5927\u6a21\u578b<\/li>\n<li>tools\uff1a\u540e\u7eed\u4f1a\u7528\u5230\u7684Tools\u96c6\u5408<\/li>\n<li>stop\uff1a\u4ec0\u4e48\u60c5\u51b5\u4e0bReAct Agent\u505c\u6b62\u5faa\u73af<\/li>\n<\/ul>\n<pre><code>class LLMSingleActionAgent {\r\n  llm: AzureLLM\r\n  tools: StructuredTool[]\r\n  stop: string[]\r\n  private _prompt: string = '{input}'\r\n  constructor({ llm, tools = [], stop = [] }: LLMSingleActionAgentParams) {\r\n    this.llm = llm\r\n    this.tools = tools\r\n    if (stop.length &gt; 4)\r\n      throw new Error('up to 4 stop sequences')\r\n    this.stop = stop\r\n  }\r\n }<\/code><\/pre>\n<h3>\u7b2c\u4e09\u6b65 \u5b9a\u4e49Tools<\/h3>\n<p>Tools\u6709\u4e24\u4e2a\u6700\u91cd\u8981\u7684\u53c2\u6570\uff0cname\u548cdescription\u3002<\/p>\n<p>Name\u5c31\u662f\u51fd\u6570\u540d\uff0cdescription\u662f\u5de5\u5177\u7684\u81ea\u7136\u8bed\u8a00\u63cf\u8ff0\uff0cLLM \u6839\u636edescription\u6765\u51b3\u5b9a\u662f\u5426\u9700\u8981\u4f7f\u7528\u8be5\u5de5\u5177\u3002\u5de5\u5177\u7684\u63cf\u8ff0\u5e94\u8be5\u975e\u5e38\u660e\u786e\uff0c\u8bf4\u660e\u5de5\u5177\u7684\u529f\u80fd\u3001\u4f7f\u7528\u7684\u65f6\u673a\u4ee5\u53ca\u4e0d\u9002\u7528\u7684\u60c5\u51b5\u3002<\/p>\n<pre><code>export abstract class StructuredTool {\r\n  name: string\r\n  description: string\r\n  constructor(name: string, description: string) {\r\n    this.name = name\r\n    this.description = description\r\n  }\r\n\r\n  abstract call(arg: string, config?: Record&lt;string, any&gt;): Promise&lt;string&gt;\r\n\r\n  getSchema(): string {\r\n    return `${this.declaration} | ${this.name} | ${this.description}`\r\n  }\r\n\r\n  abstract get declaration(): string\r\n}<\/code><\/pre>\n<p>\u6211\u4eec\u5148\u7b80\u5355\u5730\u5c06\u4e24\u4e2a\u63cf\u8ff0\u4fe1\u606f\u62fc\u63a5\u4e00\u4e0b\uff0c\u4e3aAgent\u63d0\u4f9b4\u4e2a\u7b97\u6570\u5de5\u5177\uff1a<\/p>\n<pre><code>1. Addition Tool: A tool for adding two numbers\r\n2. Subtraction Tool: A tool for subtracting two numbers\r\n3. Division Tool: A tool for dividing two numbers\r\n4.MultiplicationTool: Atoolformultiplyingtwonumbers<\/code><\/pre>\n<p>\u4e00\u4e2a\u5f88\u6709\u610f\u601d\u7684\u4e8b\u60c5\u662f\uff0c\u8fd9\u51e0\u4e2a\u7b97\u6570\u5de5\u5177\u51fd\u6570\u5e76\u4e0d\u9700\u8981\u5b9e\u9645\u7684\u4ee3\u7801\uff0c\u5927\u6a21\u578b\u53ef\u4ee5\u4ec5\u9760\u81ea\u8eab\u7684\u63a8\u7406\u80fd\u529b\u5c31\u5b8c\u6210\u5b9e\u9645\u7684\u7b97\u6570\u8fd0\u7b97\u3002\u5f53\u7136\uff0c\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u5de5\u5177\u51fd\u6570\uff0c\u8fd8\u662f\u9700\u8981\u8fdb\u884c\u8be6\u7ec6\u7684\u4ee3\u7801\u6784\u5efa\u3002<\/p>\n<h3>\u7b2c\u56db\u6b65 \u5faa\u73af\u6267\u884c<\/h3>\n<p>\u6267\u884c\u5668executor\u662f\u5728Agent\u7684\u8fd0\u884c\u65f6\uff0c\u534f\u8c03\u5404\u4e2a\u7ec4\u4ef6\u5e76\u6307\u5bfc\u64cd\u4f5c\u3002\u8fd8\u8bb0\u5f97ReAct\u6a21\u5f0f\u7684\u6d41\u7a0b\u5417\uff1fThought\u3001Action\u3001Observation\u3001\u5faa\u73af\uff0cExecutor\u7684\u4f5c\u7528\u5c31\u662f\u6267\u884c\u8fd9\u4e2a\u5faa\u73af\u3002<\/p>\n<pre><code>class AgentExecutor {\r\n  agent: LLMSingleActionAgent\r\n  tools: StructuredTool[] = []\r\n  maxIterations: number = 15\r\n\r\n  constructor(agent: LLMSingleActionAgent) {\r\n    this.agent = agent\r\n  }\r\n  \r\n  addTool(tools: StructuredTool | StructuredTool[]) {\r\n    const _tools = Array.isArray(tools) ? tools : [tools]\r\n    this.tools.push(..._tools)\r\n  }\r\n}<\/code><\/pre>\n<p>executor\u4f1a\u59cb\u7ec8\u8fdb\u884c\u5982\u4e0b\u4e8b\u4ef6\u5faa\u73af\u76f4\u5230\u76ee\u6807\u88ab\u89e3\u51b3\u4e86\u6216\u8005\u601d\u8003\u8fed\u4ee3\u6b21\u6570\u8d85\u8fc7\u4e86\u6700\u5927\u6b21\u6570\uff1a<\/p>\n<ul>\n<li>\u6839\u636e\u4e4b\u524d\u5df2\u7ecf\u5b8c\u6210\u7684\u6240\u6709\u6b65\u9aa4\uff08Thought\u3001Action\u3001Observation\uff09\u548c \u76ee\u6807\uff08\u7528\u6237\u7684\u95ee\u9898\uff09\uff0c\u89c4\u5212\u51fa\u63a5\u4e0b\u6765\u7684Action\uff08\u4f7f\u7528\u4ec0\u4e48\u5de5\u5177\u4ee5\u53ca\u5de5\u5177\u7684\u8f93\u5165\uff09<\/li>\n<li>\u68c0\u6d4b\u662f\u5426\u5df2\u7ecf\u8fbe\u6210\u76ee\u6807\uff0c\u5373Action\u662f\u4e0d\u662fActionFinish\u3002\u662f\u7684\u8bdd\u5c31\u8fd4\u56de\u7ed3\u679c\uff0c\u4e0d\u662f\u7684\u8bdd\u8bf4\u660e\u8fd8\u6709\u884c\u52a8\u8981\u5b8c\u6210<\/li>\n<li>\u6839\u636eAction\uff0c\u6267\u884c\u5177\u4f53\u7684\u5de5\u5177\uff0c\u7b49\u5f85\u5de5\u5177\u8fd4\u56de\u7ed3\u679c\u3002\u5de5\u5177\u8fd4\u56de\u7684\u7ed3\u679c\u5c31\u662f\u8fd9\u4e00\u8f6e\u6b65\u9aa4\u7684Observation<\/li>\n<li>\u4fdd\u5b58\u5f53\u524d\u6b65\u9aa4\u5230\u8bb0\u5fc6\u4e0a\u4e0b\u6587\uff0c\u5982\u6b64\u53cd\u590d<\/li>\n<\/ul>\n<pre><code>async call(input: promptInputs): Promise&lt;AgentFinish&gt; {\r\n    const toolsByName = Object.fromEntries(\r\n      this.tools.map(t =&gt; [t.name, t]),\r\n    )\r\n\r\n    const steps: AgentStep[] = []\r\n    let iterations = 0\r\n\r\n    while (this.shouldContinue(iterations)) {\r\n      const output = await this.agent.plan(steps, input)\r\n      console.log(iterations, output)\r\n\r\n      \/\/ Check if the agent has finished\r\n      if ('returnValues' in output)\r\n        return output\r\n\r\n      const actions = Array.isArray(output)\r\n        ? output as AgentAction[]\r\n        : [output as AgentAction]\r\n\r\n      const newSteps = await Promise.all(\r\n        actions.map(async (action) =&gt; {\r\n          const tool = toolsByName[action.tool]\r\n\r\n          if (!tool)\r\n            throw new Error(`${action.tool} is not a valid tool, try another one.`)\r\n\r\n          const observation = await tool.call(action.toolInput)\r\n\r\n          return { action, observation: observation ?? '' }\r\n        }),\r\n      )\r\n\r\n      steps.push(...newSteps)\r\n\r\n      iterations++\r\n    }\r\n\r\n    return {\r\n      returnValues: { output: 'Agent stopped due to max iterations.' },\r\n      log: '',\r\n    }\r\n  }<\/code><\/pre>\n<h3>\u7b2c\u4e94\u6b65 \u5b9e\u9645\u8fd0\u884c<\/h3>\n<p>\u6211\u4eec\u63d0\u51fa\u4e00\u4e2a\u95ee\u9898\uff0c\u770b\u770bAgent\u600e\u4e48\u901a\u8fc7ReAct\u65b9\u5f0f\u8fdb\u884c\u89e3\u51b3\u3002 &#8220;\u4e00\u79cd\u51cf\u901f\u673a\u7684\u4ef7\u683c\u662f750\u5143\uff0c\u4e00\u5bb6\u4f01\u4e1a\u9700\u8981\u8d2d\u4e7012\u53f0\u3002\u6bcf\u53f0\u51cf\u901f\u673a\u8fd0\u884c\u4e00\u5c0f\u65f6\u7684\u7535\u8d39\u662f0.5\u5143\uff0c\u4f01\u4e1a\u6bcf\u5929\u8fd0\u884c\u8fd9\u4e9b\u51cf\u901f\u673a8\u5c0f\u65f6\u3002\u8bf7\u8ba1\u7b97\u4f01\u4e1a\u8d2d\u4e70\u53ca\u4e00\u5468\u8fd0\u884c\u8fd9\u4e9b\u51cf\u901f\u673a\u7684\u603b\u82b1\u8d39\u3002&#8221;<\/p>\n<pre><code>describe('agent', () =&gt; {\r\n  const llm = new AzureLLM({\r\n    apiKey: Config.apiKey,\r\n    model: Config.model,\r\n  })\r\n  const agent = new LLMSingleActionAgent({ llm })\r\n  agent.setPrompt(REACT_PROMPT)\r\n  agent.addStop(agent.observationPrefix)\r\n  agent.addTool([new AdditionTool(), new SubtractionTool(), new DivisionTool(), new MultiplicationTool()])\r\n\r\n  const executor = new AgentExecutor(agent)\r\n  executor.addTool([new AdditionTool(), new SubtractionTool(), new DivisionTool(), new MultiplicationTool()])\r\n  it('test', async () =&gt; {\r\n    const res = await executor.call({ input: '\u4e00\u79cd\u51cf\u901f\u673a\u7684\u4ef7\u683c\u662f750\u5143\uff0c\u4e00\u5bb6\u4f01\u4e1a\u9700\u8981\u8d2d\u4e7012\u53f0\u3002\u6bcf\u53f0\u51cf\u901f\u673a\u8fd0\u884c\u4e00\u5c0f\u65f6\u7684\u7535\u8d39\u662f0.5\u5143\uff0c\u4f01\u4e1a\u6bcf\u5929\u8fd0\u884c\u8fd9\u4e9b\u51cf\u901f\u673a8\u5c0f\u65f6\u3002\u8bf7\u8ba1\u7b97\u4f01\u4e1a\u8d2d\u4e70\u53ca\u4e00\u5468\u8fd0\u884c\u8fd9\u4e9b\u51cf\u901f\u673a\u7684\u603b\u82b1\u8d39\u3002' })\r\n    expect(res).toMatchInlineSnapshot(`\r\n      {\r\n        \"log\": \"Final Answer: The total cost of purchasing and operating the gearboxes for a week is 9336 yuan.\",\r\n        \"returnValues\": {\r\n          \"output\": \"The total cost of purchasing and operating the gearboxes for a week is 9336 yuan.\",\r\n        },\r\n      }\r\n    `)\r\n  }, { timeout: 50000 })\r\n})<\/code><\/pre>\n<p>\u6211\u4eec\u6765\u770b\u4e00\u4e0bAgent\u7684\u8f93\u51fa\uff0c\u4ee5\u53caAgent\u5728\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u662f\u5982\u4f55\u601d\u8003\u548c\u884c\u52a8\u7684\u3002\u53ef\u4ee5\u770b\u5230\uff0c\u901a\u8fc7Thought\u3001Action\u3001Observation\u7684\u5faa\u73af\uff0cAI Agent\u5f88\u597d\u5730\u4e00\u6b65\u6b65\u5b8c\u6210\u6700\u7ec8\u7b54\u6848\u7684\u8f93\u51fa\u3002<\/p>\n<pre><code>Question:\u4e00\u79cd\u51cf\u901f\u673a\u7684\u4ef7\u683c\u662f750\u5143\uff0c\u4e00\u5bb6\u4f01\u4e1a\u9700\u8981\u8d2d\u4e7012\u53f0\u3002\u6bcf\u53f0\u51cf\u901f\u673a\u8fd0\u884c\u4e00\u5c0f\u65f6\u7684\u7535\u8d39\u662f0.5\u5143\uff0c\u4f01\u4e1a\u6bcf\u5929\u8fd0\u884c\u8fd9\u4e9b\u51cf\u901f\u673a8\u5c0f\u65f6\u3002\u8bf7\u8ba1\u7b97\u4f01\u4e1a\u8d2d\u4e70\u53ca\u4e00\u5468\u8fd0\u884c\u8fd9\u4e9b\u51cf\u901f\u673a\u7684\u603b\u82b1\u8d39\r\nThought:I need to calculate the total cost of purchasing and operating the gearboxes for a week.\r\nAction: Multiplication Tool\r\nAction Input: [750, 12]\r\nObservation: 9000\r\nThought: Now I need to calculate the cost of operating the gearboxes for a day.\r\nAction: Multiplication Tool\r\nActionInput:[0.5,8,12]\r\nObservation: 48\r\nThought: Now I need to calculate the cost of operating the gearboxes for a week.\r\nAction: Multiplication Tool\r\nAction Input: [48, 7]\r\nObservation: 336\r\nThought: Now I need to calculate the total cost of purchasing and operating the gearboxes for a week.\r\nAction: Addition Tool\r\nAction Input: [9000, 336]\r\nObservation: 9336<\/code><\/pre>\n<h2  id=\"toc-3\" class=\"jltoc--item\">\u603b\u7ed3<\/h2>\n<p>\u5728AI Agent\u7684\u591a\u79cd\u5b9e\u73b0\u6a21\u5f0f\u4e2d\uff0cReAct\u6a21\u5f0f\u662f\u6700\u65e9\u51fa\u73b0\u3001\u4e5f\u662f\u76ee\u524d\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684\u6a21\u5f0f\u3002ReAct\u7684\u6838\u5fc3\u601d\u60f3\u5c31\u662f\u6a21\u62df\u4eba\u601d\u8003\u548c\u884c\u52a8\u7684\u8fc7\u7a0b\uff0c\u901a\u8fc7Thought\u3001Action\u3001Observation\u7684\u5faa\u73af\uff0c\u4e00\u6b65\u6b65\u89e3\u51b3\u76ee\u6807\u95ee\u9898\u3002<\/p>\n<p>ReAct\u6a21\u5f0f\u4e5f\u5b58\u5728\u5f88\u591a\u7684\u4e0d\u8db3\uff1a<\/p>\n<ul>\n<li>\u9996\u5148\u662fLLM\u5927\u6a21\u578b\u7684\u901a\u75c5\uff0c\u5373\u4ea7\u51fa\u5185\u5bb9\u4e0d\u7a33\u5b9a\uff0c\u4e0d\u4ec5\u4ec5\u662f\u8f93\u51fa\u5185\u5bb9\u5b58\u5728\u6ce2\u52a8\uff0c\u4e5f\u4f53\u73b0\u5728\u5bf9\u590d\u6742\u95ee\u9898\u7684\u5206\u6790\uff0c\u89e3\u51b3\u4e0a\u5b58\u5728\u4e00\u5b9a\u7684\u6ce2\u52a8<\/li>\n<li>\u7136\u540e\u662f\u6210\u672c\uff0c\u91c7\u7528ReAct\u65b9\u5f0f\uff0c\u6211\u4eec\u662f\u65e0\u6cd5\u63a7\u5236\u8f93\u5165\u5185\u5bb9\u7684\u3002\u56e0\u4e3a\u5728\u4efb\u52a1\u63d0\u4ea4\u7ed9LLM\u540e\uff0cLLM\u5bf9\u4efb\u52a1\u7684\u62c6\u89e3\u3001\u5faa\u73af\u6b21\u6570\u662f\u4e0d\u53ef\u63a7\u7684\u3002\u56e0\u6b64\u5b58\u5728\u4e00\u79cd\u53ef\u80fd\u6027\uff0c\u8fc7\u4e8e\u590d\u6742\u7684\u4efb\u52a1\u5bfc\u81f4Token\u8fc7\u91cf\u6d88\u8017\u3002<\/li>\n<li>\u6700\u540e\u662f\u54cd\u5e94\u65f6\u95f4\uff0c\u6bd4\u8d77\u5927\u90e8\u5206API\u63a5\u53e3\u6beb\u79d2\u7ea7\u7684\u54cd\u5e94\uff0cLLM\u54cd\u5e94\u65f6\u95f4\u662f\u79d2\u7ea7\u4ee5\u4e0a\u3002\u5728ReAct\u6a21\u5f0f\u4e0b\uff0c\u8fd9\u4e2a\u65f6\u95f4\u53d8\u5f97\u66f4\u52a0\u4e0d\u53ef\u63a7\u3002\u56e0\u4e3a\u65e0\u6cd5\u786e\u5b9a\u9700\u8981\u62c6\u5206\u591a\u5c11\u6b65\u9aa4\uff0c\u9700\u8981\u8bbf\u95ee\u591a\u5c11\u6b21LLM\u6a21\u578b\u3002\u56e0\u6b64\u5728\u5728\u79d2\u7ea7\u63a5\u53e3\u54cd\u5e94\u7684\u80cc\u666f\u4e0b\uff0c\u505a\u6210\u540c\u6b65\u63a5\u53e3\u663e\u7136\u662f\u4e0d\u5408\u9002\u7684\uff0c\u9700\u8981\u91c7\u7528\u5f02\u6b65\u7684\u65b9\u5f0f\u3002\u800c\u5f02\u6b65\u65b9\u5f0f\uff0c\u53c8\u4f1a\u5f71\u54cd\u7528\u6237\u4f53\u9a8c\uff0c\u5bf9\u5e94\u7528\u573a\u666f\u7684\u9009\u62e9\u53c8\u9020\u6210\u4e86\u9650\u5236\u3002<\/li>\n<\/ul>\n<p>\u4f46\u662f\u65e0\u8bba\u5982\u4f55\uff0cReAct\u6846\u67b6\u63d0\u51fa\u4e86\u4e00\u79cd\u975e\u5e38\u597d\u7684\u601d\u8def\uff0c\u8ba9\u73b0\u6709\u7684\u5e94\u7528\u5f97\u5230\u4e00\u6b21\u667a\u80fd\u5316\u7684\u8fdb\u5316\u673a\u4f1a\u3002\u73b0\u5728\u5f88\u591a\u573a\u666f\u5df2\u7ecf\u6709\u4e86\u975e\u5e38\u6210\u719f\u7684ReAct Agent\u5e94\u7528\uff0c\u6bd4\u5982\u667a\u80fd\u5ba2\u670d\u3001\u77e5\u8bc6\u52a9\u624b\u3001\u4e2a\u6027\u5316\u8425\u9500\u3001\u667a\u80fd\u9500\u552e\u52a9\u7406\u7b49\u7b49\u3002<\/p>\n<p>\u5728\u4e0b\u4e00\u7bc7\u6587\u7ae0\u4e2d\uff0c\u98ce\u53d4\u5c06\u4ecb\u7ecd\u53e6\u4e00\u79cdAI Agent\u8bbe\u8ba1\u6a21\u5f0f\uff0cREWOO\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u968f\u7740\u4eba\u5de5\u667a\u80fd\u6280\u672f\u7684\u4e0d\u65ad\u8fdb\u6b65\uff0cAI Agent\u8bbe\u8ba1\u6a21\u5f0f\u9010\u6e10\u6210\u4e3a\u7814\u7a76\u548c\u5e94\u7528\u7684\u70ed\u70b9\u3002ReAct\u6a21\u5f0f\u4f5c\u4e3aAI Agent\u8bbe\u8ba1\u6a21\u5f0f\u7684\u8d77\u70b9\uff0c\u4ee5\u5176\u6a21\u62df\u4eba\u7c7b\u601d\u8003\u548c\u884c\u52a8\u8fc7\u7a0b\u7684\u7279\u70b9\uff0c\u4e3a\u5404\u79cd\u667a\u80fd\u5e94\u7528\u63d0\u4f9b\u4e86\u4e00\u79cd\u6709\u6548\u7684\u5b9e\u73b0\u9014\u5f84\u3002<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-10020294","post","type-post","status-publish","format-standard","hentry","category-aigc"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020294"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020294\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}