{"id":9437,"date":"2025-07-22T17:56:24","date_gmt":"2025-07-22T09:56:24","guid":{"rendered":"\/?p=9437"},"modified":"2025-07-22T17:56:24","modified_gmt":"2025-07-22T09:56:24","slug":"%e4%bb%8e%e9%9b%b6%e5%bc%80%e5%a7%8b%e6%9e%84%e5%bb%ba%e4%bd%a0%e7%9a%84%e7%ac%ac%e4%b8%80%e4%b8%aa-react-agent","status":"publish","type":"post","link":"\/?p=9437","title":{"rendered":"\u4ece\u96f6\u5f00\u59cb\u6784\u5efa\u4f60\u7684\u7b2c\u4e00\u4e2a ReAct Agent"},"content":{"rendered":"<h1><strong>\u7b80\u4ecb<\/strong><\/h1>\n<p>\u5176\u5b9e\u5bf9\u4e8e <strong>AI Agent<\/strong> \u7684\u4ecb\u7ecd\u5df2\u7ecf\u975e\u5e38\u975e\u5e38\u591a\u4e86\uff0c\u7b80\u5355\u6765\u8bf4\uff0cAI Agent \u662f\u4e00\u79cd\u5177\u5907\u201c\u611f\u77e5-\u601d\u8003-\u884c\u52a8\u201d\u80fd\u529b\u7684\u667a\u80fd\u4f53\uff0c\u5b83\u80fd\u63a5\u6536\u4efb\u52a1\uff0c\u81ea\u52a8\u63a8\u7406\u5e76\u8c03\u7528\u5916\u90e8\u5de5\u5177\u5b8c\u6210\u590d\u6742\u6d41\u7a0b\u3002\u800c\u5728\u4f17\u591a Agent \u67b6\u6784\u4e2d\uff0c<strong>ReAct \u6846\u67b6\uff08Reasoning + Acting\uff09\u662f\u4e00\u79cd\u975e\u5e38\u7ecf\u5178\u7684\u601d\u7ef4\u65b9\u5f0f\u2014\u2014\u5b83\u8ba9\u5927\u8bed\u8a00\u6a21\u578b\u4e00\u8fb9\u63a8\u7406<\/strong>\uff08\u7528 Thought \u8868\u8fbe\u601d\u8003\u8fc7\u7a0b\uff09\uff0c\u4e00\u8fb9<strong>\u884c\u52a8<\/strong>\uff08\u7528 Action \u6267\u884c\u64cd\u4f5c\uff09\uff0c\u5e76\u6839\u636e\u8fd4\u56de\u7ed3\u679c\uff08Observation\uff09\u7ee7\u7eed\u51b3\u7b56\uff0c\u76f4\u5230\u7ed9\u51fa\u6700\u7ec8\u7684\u7b54\u6848\u3002<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20250722175347076.png\" alt=\"image-20250722175347076\" \/><\/p>\n<p>\u672c\u8282\u8bfe\u6211\u4eec\u5c06\u57fa\u4e8e DeepLearning.AI \u4e0a\u975e\u5e38\u70ed\u95e8\u7684\u8bfe\u7a0b\u2014\u2014<strong>\u300aAI Agents in LangGraph\u300b<\/strong>\uff0c\u5e26\u5927\u5bb6\u624b\u628a\u624b\u5b66\u4e60\u7b2c\u4e00\u8282\u8bfe\u7684\u5185\u5bb9\uff0c\u4f7f\u7528\u6700\u57fa\u7840\u7684python\u4ee3\u7801\uff0c\u6765\u770b\u770b ReAct \u6846\u67b6\u5728\u5b9e\u9645\u4efb\u52a1\u4e2d\u662f\u5982\u4f55\u8fd0\u884c\u7684\uff0c\u5982\u4f55\u7ed3\u5408\u5de5\u5177\u94fe\u5b8c\u6210\u201c\u63a8\u7406 + \u6267\u884c\u201d\u7684\u95ed\u73af\u8fc7\u7a0b\u3002<\/p>\n<p>\u90a3\u6211\u4eec\u8bdd\u4e0d\u591a\u8bf4\uff0c\u9a6c\u4e0a\u5f00\u59cb\u5427\uff01<\/p>\n<h1><strong>\u73af\u5883\u914d\u7f6e<\/strong><\/h1>\n<p>\u5f53\u7136\u5728\u6b63\u5f0f\u8fdb\u5165\u4ee3\u7801\u5b9e\u6218\u4e4b\u524d\uff0c\u6211\u4eec\u8fd8\u662f\u8981\u914d\u7f6e\u76f8\u5173\u7684\u73af\u5883\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u6765\u521b\u5efa\u76f8\u5173\u7684conda\u73af\u5883\u5e76\u4e0b\u8f7d<code>openai<\/code>\u5e93\u3002<\/p>\n<pre><code>conda create -n langgraph python=3.12 -y\nconda activate langgraph\npip install openai<\/code><\/pre>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u914d\u7f6e\u5927\u6a21\u578b\u7684\u8c03\u7528\u63a5\u53e3\u3002\u672c\u6b21\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4f7f\u7528\u7684\u662f <strong>\u963f\u91cc\u4e91\u767e\u70bc\u5e73\u53f0\u63d0\u4f9b\u7684\u5927\u8bed\u8a00\u6a21\u578b API<\/strong>\u3002\u5173\u4e8e\u5982\u4f55\u83b7\u53d6 <code>api_key<\/code>\uff0c\u6211\u5728\u524d\u9762\u7684\u6587\u7ae0\u4e2d\u5df2\u7ecf\u8be6\u7ec6\u4ecb\u7ecd\u8fc7\uff0c\u8fd9\u91cc\u5c31\u4e0d\u518d\u8d58\u8ff0\uff0c\u5177\u4f53\u53ef\u53c2\u8003\u8fd9\u7bc7\u6587\u7ae0\uff1a<a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=Mzg2NzU4MDgzMA==&amp;mid=2247531309&amp;idx=1&amp;sn=157f7379788bdf0edc2fc90e401be20a&amp;scene=21#wechat_redirect\">\u4e07\u5b57\u957f\u6587\uff01\u624b\u628a\u624b\u5e26\u4f60\u4e0a\u624b\u57fa\u4e8eLangChain\u53caQwen\u5927\u6a21\u578b\u7684\u5f00\u53d1\u4e0e\u5e94\u7528<\/a><\/p>\n<p>\u62ff\u5230 <code>api_key<\/code> \u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u8fdb\u884c\u6d4b\u8bd5\uff0c\u4ee5\u9a8c\u8bc1\u5927\u6a21\u578b\u662f\u5426\u80fd\u591f\u6b63\u5e38\u8c03\u7528\u3002\u5982\u679c\u8fd0\u884c\u540e\u6210\u529f\u8fd4\u56de\u5185\u5bb9\uff0c\u8bf4\u660e\u63a5\u53e3\u914d\u7f6e\u65e0\u8bef\uff0c\u53ef\u4ee5\u7ee7\u7eed\u540e\u7eed\u5f00\u53d1\u3002<\/p>\n<pre><code class=\"language-python\">from openai import OpenAI\naliyun_api_key = &#039;\u4f60\u7684api_key&#039;\nclient = OpenAI(\n    api_key=aliyun_api_key,\n    base_url=&quot;https:\/\/dashscope.aliyuncs.com\/compatible-mode\/v1&quot;\n)\n\nresponse = client.chat.completions.create(\n    model=&quot;qwen-max&quot;,\n    messages=[\n        {&#039;role&#039;: &#039;user&#039;, &#039;content&#039;: &quot;\u4f60\u662f\u8c01\uff1f&quot;}\n    ]\n)\n\n# \u6253\u5370\u5b8c\u6574\u56de\u7b54\u5185\u5bb9\nprint(response.choices[0].message.content)<\/code><\/pre>\n<h1><strong>Agent \u642d\u5efa<\/strong><\/h1>\n<h2><strong>\u642d\u5efa\u601d\u8def<\/strong><\/h2>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u5e38\u89c1\u7684ReAct\u6846\u67b6\u7684Agent\uff0c\u6211\u4eec\u9700\u8981\u4ee5\u4e0b\u51e0\u4e2a\u96f6\u90e8\u4ef6\uff1a<\/p>\n<ul>\n<li><strong>LLM \u6a21\u578b\uff08\u5927\u8111\uff09<\/strong>\uff1aOpenAI\u3001\u901a\u4e49\u5343\u95ee\u3001Claude \u7b49\u5927\u8bed\u8a00\u6a21\u578b\u3002\u5176\u4f5c\u7528\u662f\u8d1f\u8d23\u7406\u89e3\u7528\u6237\u8f93\u5165\u3001\u7ec4\u7ec7\u63a8\u7406\u903b\u8f91\u3001\u5224\u65ad\u8c03\u7528\u54ea\u4e2a\u5de5\u5177\u3001\u6700\u540e\u751f\u6210\u56de\u7b54\u3002\u5b83\u662f\u6574\u4e2a Agent \u63a8\u7406\u4e0e\u51b3\u7b56\u7684\u57fa\u7840\u3002<\/li>\n<li><strong>Memory\uff08\u8bb0\u5fc6\uff09<\/strong>\uff1a\u4fdd\u5b58\u5bf9\u8bdd\u7684\u4e0a\u4e0b\u6587\u3002\u5176\u4f5c\u7528\u662f\u8bb0\u5f55\u7528\u6237\u7684\u5386\u53f2\u5bf9\u8bdd\u3001\u8eab\u4efd\u4fe1\u606f\u3001\u4e0a\u4e0b\u6587\u7b49\uff0c\u8ba9 Agent \u5728\u591a\u8f6e\u5bf9\u8bdd\u4e2d\u4fdd\u6301\u201c\u8bb0\u5fc6\u201d\u3002<\/li>\n<li><strong>\u5de5\u5177\uff08\u884c\u52a8\u80fd\u529b\uff09<\/strong>\uff1a\u8ba1\u7b97\u5668\u3001\u641c\u7d22\u5668\u3001\u6570\u636e\u5e93\u67e5\u8be2\u5668\u7b49\u3002\u5176\u4f5c\u7528\u662f\u5e2e\u52a9 Agent \u6267\u884c\u5b83\u4e0d\u80fd\u76f4\u63a5\u56de\u7b54\u7684\u4efb\u52a1\uff0c\u4f8b\u5982\u67e5\u5929\u6c14\u3001\u7b97\u6570\u5b66\u9898\u3001\u63d0\u53d6\u8868\u683c\u6570\u636e\u7b49\u3002<\/li>\n<li><strong>Prompt \u6a21\u677f\uff08\u601d\u7ef4\u65b9\u5f0f\uff09<\/strong>\uff1a\u5982 ReAct \u63d0\u793a\u8bcd\u3002\u5176\u4f5c\u7528\u662f\u660e\u786e\u544a\u8bc9\u6a21\u578b\u201c\u4f60\u8981\u5982\u4f55\u601d\u8003\u3001\u5982\u4f55\u8c03\u7528\u5de5\u5177\u3001\u5982\u4f55\u8868\u8fbe\u6bcf\u4e00\u6b65\u7684\u8fc7\u7a0b\u201d\u3002<\/li>\n<\/ul>\n<p>\u603b\u7684\u6765\u8bf4\uff0c\u5927\u6a21\u578b\u662f\u5927\u8111\uff0cPrompt\u662f\u601d\u7ef4\u65b9\u5f0f\uff0c\u5de5\u5177\u662f\u884c\u52a8\u80fd\u529b\uff0cMemory\u662f\u8bb0\u5fc6\u7cfb\u7edf\u3002\u56db\u8005\u5408\u529b\uff0c\u624d\u80fd\u6784\u5efa\u51fa\u4e00\u4e2a\u771f\u6b63\u5b9e\u7528\u7684 ReAct Agent\u3002\u4e0b\u9762\u6211\u4eec\u5c31\u6765\u4e00\u6b65\u6b65\u7684\u5c06\u8fd9\u56db\u90e8\u5206\u5185\u5bb9\u8fdb\u884c\u5b8c\u5584\uff0c\u5b8c\u6210\u6700\u521d\u7684\u524d\u671f\u51c6\u5907\u5de5\u4f5c\u3002<\/p>\n<h2><strong>\u6784\u5efa\u5927\u8111\u53ca\u5185\u90e8\u8bb0\u5fc6<\/strong><\/h2>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u6784\u5efa\u4e00\u4e2a\u652f\u6301 LLM \u5bf9\u8bdd\u7684\u7c7b\uff0c\u7528\u4e8e\u4e0e\u5927\u8bed\u8a00\u6a21\u578b\u8fdb\u884c\u4ea4\u4e92\uff0c\u540c\u65f6\u8bb0\u5f55\u5b8c\u6574\u7684\u5bf9\u8bdd\u4fe1\u606f\uff0c\u4e3a\u540e\u7eed\u5faa\u73af\u8c03\u7528\u505a\u597d\u51c6\u5907\u3002<\/p>\n<pre><code class=\"language-python\">import re\nfrom openai import OpenAI\naliyun_api_key = &#039;\u4f60\u7684api_key&#039;\nclient = OpenAI(\n    api_key=aliyun_api_key,\n    base_url=&quot;https:\/\/dashscope.aliyuncs.com\/compatible-mode\/v1&quot;\n)\nclass Agent:\n    def __init__(self, system=&quot;&quot;):\n        self.system = system\n        self.messages = []\n        if self.system:\n            self.messages.append({&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: system})\n\n    def __call__(self, message):\n        self.messages.append({&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: message})\n        result = self.execute()\n        self.messages.append({&quot;role&quot;: &quot;assistant&quot;, &quot;content&quot;: result})\n        return result\n\n    def execute(self):\n        response = client.chat.completions.create(\n            model=&quot;qwen-max&quot;,\n            messages=self.messages\n        )\n        return response.choices[0].message.content<\/code><\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u4e00\u6b65\u6b65\u6765\u770b\u770b\u8fd9\u4e2a <code>Agent<\/code> \u7c7b\u5177\u4f53\u5b9e\u73b0\u4e86\u54ea\u4e9b\u529f\u80fd\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a Agent \u5bf9\u8c61\uff1a<\/p>\n<pre><code>abot = Agent(&#039;\u4f60\u662f\u4e00\u4e2a\u4e50\u4e8e\u52a9\u4eba\u7684\u673a\u5668\u4eba&#039;)<\/code><\/pre>\n<p>\u5728\u521b\u5efa\u8fd9\u4e2a\u5bf9\u8c61\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5176\u5b9e\u6211\u4eec\u662f\u5c06 <code>&#039;\u4f60\u662f\u4e00\u4e2a\u4e50\u4e8e\u52a9\u4eba\u7684\u673a\u5668\u4eba&#039;<\/code> \u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u4e86\u7c7b\u7684\u6784\u9020\u51fd\u6570 <code>__init__()<\/code>\uff0c\u5373\uff1a<\/p>\n<pre><code>class Agent:\n    def __init__(self, system=&#039;\u4f60\u662f\u4e00\u4e2a\u4e50\u4e8e\u52a9\u4eba\u7684\u673a\u5668\u4eba&#039;):<\/code><\/pre>\n<p>\u5728\u6784\u9020\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7 <code>self.system<\/code> \u548c <code>self.messages<\/code> \u5b9a\u4e49\u4e86\u4e24\u4e2a\u7c7b\u7684\u5c5e\u6027\uff1a<\/p>\n<ul>\n<li><code>self.system<\/code>\uff1a\u4fdd\u5b58\u7cfb\u7edf\u63d0\u793a\u8bcd\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u8bbe\u5b9a\u7684\u5927\u6a21\u578b\u8eab\u4efd\u3002<\/li>\n<li><code>self.messages<\/code>\uff1a\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u6574\u6bb5\u5bf9\u8bdd\u7684\u5386\u53f2\u4fe1\u606f\uff08\u5305\u542b system\u3001user\u3001assistant \u4e09\u7c7b\u89d2\u8272\u7684\u5185\u5bb9\uff09\u3002<\/li>\n<\/ul>\n<pre><code>        self.system = &#039;\u4f60\u662f\u4e00\u4e2a\u4e50\u4e8e\u52a9\u4eba\u7684\u673a\u5668\u4eba&#039;\n        self.messages = []<\/code><\/pre>\n<p>\u518d\u7136\u540e\u5c31\u662f\u628a\u7cfb\u7edf\u63d0\u793a\u8bcd\u52a0\u5165\u5230<code>self.messages<\/code> \u7684\u5217\u8868\u5f53\u4e2d\uff0c\u5373\u4fbf\u662f\u7a7a\u7684\u5b57\u7b26\u4e32\u4e5f\u662f\u9700\u8981\u52a0\u5165\u8fdb\u53bb\u7684\u3002<\/p>\n<pre><code>        if self.system:\n            self.messages.append({&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &#039;\u4f60\u662f\u4e00\u4e2a\u4e50\u4e8e\u52a9\u4eba\u7684\u673a\u5668\u4eba&#039;})<\/code><\/pre>\n<p>\u52a0\u5165\u8fdb\u53bb\u540e\uff0c<code>self.mesages<\/code>\u5c31\u4f1a\u53d8\u6210\u4e0b\u9762\u8fd9\u6837\u628a\u4e00\u6761\u4fe1\u606f\u4fdd\u5b58\u8d77\u6765\u3002<\/p>\n<pre><code>self.messages = [{&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &#039;\u4f60\u662f\u4e00\u4e2a\u4e50\u4e8e\u52a9\u4eba\u7684\u673a\u5668\u4eba&#039;}]<\/code><\/pre>\n<p>\u4ee5\u4e0a\u5c31\u5b8c\u6210\u4e86Agen\u8fd9\u4e2a\u7c7b\u521b\u5efa\u7684\u524d\u671f\u51c6\u5907\u5de5\u4f5c\u4e86\u3002\u5f53\u6211\u4eec\u53bb\u8c03\u7528\u8fd9\u4e2a\u7c7b\uff0c\u4e5f\u5c31\u662f\u548c\u6211\u4eec\u521a\u521a\u521b\u5efa\u597d\u7684<code>abot<\/code> \u53bb\u5bf9\u8bdd\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u6765\u5b8c\u6210\uff1a<\/p>\n<pre><code>result = abot(&quot;\u4f60\u662f\u8c01\uff1f&quot;)\nprint(result)<\/code><\/pre>\n<p>\u5f53\u6211\u4eec\u4f7f\u7528 <code>abot()<\/code> \u6765\u4f20\u5165\u4fe1\u606f\u65f6\uff0c\u5176\u5b9e\u662f\u8c03\u7528\u4e86 <code>Agent<\/code> \u7c7b\u4e2d\u5b9a\u4e49\u7684 <code>__call__()<\/code> \u65b9\u6cd5\uff0c\u5e76\u5c06 <code>&#039;\u4f60\u662f\u8c01\uff1f&#039;<\/code> \u4f5c\u4e3a <code>message<\/code> \u4f20\u5165\uff1a<\/p>\n<pre><code>    def __call__(self, message=&#039;\u4f60\u662f\u8c01&#039;):<\/code><\/pre>\n<p>\u7136\u540e\u4e0b\u4e00\u6b65\u5c31\u662f\u6267\u884c\u8fd9\u4e2a\u51fd\u6570\u91cc\u7684\u5185\u5bb9\uff0c\u7b2c\u4e00\u6b65\u5c31\u662f\u628a\u8fd9\u4e2a\u4fe1\u606f\u4f20\u5165\u5230\u8fd9\u4e2a\u5b57\u5178\u91cc\uff0c\u5e76\u4e14\u4e5f\u662f\u901a\u8fc7<code>.append<\/code>\u7684\u65b9\u5f0f\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002<\/p>\n<pre><code>        self.messages.append({&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &#039;\u4f60\u662f\u8c01&#039;})<\/code><\/pre>\n<p>\u7ecf\u8fc7\u8fd9\u6837\u4e4b\u540e\uff0c\u6211\u4eec\u7684\u5217\u8868\u5c31\u4f1a\u66f4\u65b0\u6210\u4e0b\u9762\u8fd9\u6837\u3002<\/p>\n<pre><code>self.messages = [{&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &#039;\u4f60\u662f\u4e00\u4e2a\u4e50\u4e8e\u52a9\u4eba\u7684\u673a\u5668\u4eba&#039;}, {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &#039;\u4f60\u662f\u8c01&#039;}]<\/code><\/pre>\n<p>\u518d\u7136\u540e\u6211\u4eec\u5c31\u662f\u53bb\u6267\u884c\u4e00\u4e2a\u51fd\u6570<code>self.execute()<\/code> \uff0c\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u5728\u53d8\u91cf<code>result<\/code>\u8fd9\u91cc\u3002<\/p>\n<pre><code>        result = self.execute()<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u5176\u5b9e\u5c31\u662fAgent\u8fd9\u4e2a\u7c7b\u91cc\u7684\u7b2c\u4e09\u4e2a\u51fd\u6570<code>def execute(self):<\/code> \uff0c\u6211\u4eec\u5c31\u662f\u7528\u7684\u6700\u666e\u901a\u7684openai\u8c03\u7528\u65b9\u6cd5\u6765\u83b7\u53d6\u56de\u590d\uff0c\u4f46\u662f\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6211\u4eec\u4f20\u5165\u7ed9\u5927\u6a21\u578b\u7684\u4fe1\u606f\u5176\u5b9e\u6b63\u662f\u6211\u4eec\u4e00\u76f4\u4fdd\u5b58\u5728<code>self.messages<\/code>\u7684\u4fe1\u606f\uff0c\u6240\u4ee5\u5927\u6a21\u578b\u4f1a\u57fa\u4e8e\u6211\u4eec\u7684\u7cfb\u7edf\u63d0\u793a\u8bcd\u548c\u7528\u6237\u63d0\u793a\u8bcd\u7ed9\u51fa\u5bf9\u5e94\u7684\u56de\u590d\uff0c\u6bd4\u5982\u8fd9\u91cc\u7684\u56de\u590d\u5c31\u5e94\u8be5\u662f\uff1a<code>\u6211\u662f\u6765\u81ea\u963f\u91cc\u4e91\u7684\u5927\u89c4\u6a21\u8bed\u8a00\u6a21\u578b\uff0c\u6211\u53eb\u901a\u4e49\u5343\u95ee\u3002<\/code>\u3002<\/p>\n<pre><code>def execute(self):\n    response = client.chat.completions.create(\n        model=&quot;qwen-max&quot;,\n        messages=self.messages\n    )\n    return response.choices[0].message.content<\/code><\/pre>\n<p>\u6700\u540e\u4fdd\u5b58\u5927\u6a21\u578b\u56de\u590d\u7684\u4fe1\u606f\u4e5f\u4f1a\u4ee5\u683c\u5f0f\u5316\u7684\u5f62\u5f0f\u6dfb\u52a0\u5230<code>self.messages<\/code> \u4e2d\uff0c\u4ee5\u4f5c\u4e3a\u804a\u5929\u8bb0\u5f55\u7b49\u5f85\u4e0b\u4e00\u6b21\u7684\u8c03\u7528\u3002\u5b8c\u6210\u4e00\u6b21\u804a\u5929\u540e\uff0c\u5728<code>self.messages<\/code> \u4fdd\u5b58\u7684\u804a\u5929\u8bb0\u5f55\u5e94\u8be5\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>self.messages = [{&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &#039;\u4f60\u662f\u4e00\u4e2a\u4e50\u4e8e\u52a9\u4eba\u7684\u673a\u5668\u4eba&#039;}, {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &#039;\u4f60\u662f\u8c01&#039;},{&quot;role&quot;: &quot;assistant&quot;, &quot;content&quot;: &#039;\u6211\u662f\u6765\u81ea\u963f\u91cc\u4e91\u7684\u5927\u89c4\u6a21\u8bed\u8a00\u6a21\u578b\uff0c\u6211\u53eb\u901a\u4e49\u5343\u95ee\u3002&#039;}]<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u6837\u7684\u65b9\u5f0f\uff0c\u6211\u4eec\u4e0d\u4ec5\u5b8c\u6210\u4e86\u4e00\u6b21\u5b8c\u6574\u7684\u95ee\u7b54\u8fc7\u7a0b\uff0c\u8fd8\u4fdd\u7559\u4e86\u6574\u4e2a\u5bf9\u8bdd\u7684\u4e0a\u4e0b\u6587\uff0c\u4e3a\u540e\u7eed\u591a\u8f6e\u4ea4\u4e92\u63d0\u4f9b\u4e86\u57fa\u7840\u3002<\/p>\n<h2><strong>\u5de5\u5177\u51c6\u5907<\/strong><\/h2>\n<p>Agent \u7684\u5de5\u5177\u662f\u6307\u5927\u8bed\u8a00\u6a21\u578b\u5728\u5b8c\u6210\u4efb\u52a1\u65f6\u53ef\u4ee5\u8c03\u7528\u7684\u5916\u90e8\u51fd\u6570\u6216\u670d\u52a1\uff0c\u6bd4\u5982\u641c\u7d22\u5f15\u64ce\u3001\u8ba1\u7b97\u5668\u3001\u6570\u636e\u5e93\u67e5\u8be2\u3001\u6587\u4ef6\u8bfb\u53d6\u7b49\uff0c\u5b83\u8ba9\u6a21\u578b\u4e0d\u4ec5\u80fd\u201c\u601d\u8003\u201d\uff0c\u8fd8\u80fd\u201c\u884c\u52a8\u201d\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u6a21\u578b\u53ef\u4ee5\u5728\u65e0\u6cd5\u76f4\u63a5\u56de\u7b54\u7684\u95ee\u9898\u9762\u524d\u4e3b\u52a8\u83b7\u53d6\u4fe1\u606f\u3001\u6267\u884c\u4ee3\u7801\u6216\u8c03\u7528API\uff0c\u4ece\u800c\u5b8c\u6210\u66f4\u590d\u6742\u3001\u66f4\u771f\u5b9e\u7684\u667a\u80fd\u4efb\u52a1\u3002\u8fd9\u79cd\u201c\u601d\u8003+\u884c\u52a8\u201d\u7684\u6a21\u5f0f\uff0c\u662f\u6784\u5efa\u667a\u80fd Agent \u7cfb\u7edf\u7684\u6838\u5fc3\u80fd\u529b\u4e4b\u4e00\u3002<\/p>\n<p>\u6bd4\u5982\u8bf4\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u4e24\u4e2a\u5c0f\u7684\u5de5\u5177\u6765\u5b8c\u6210\u540e\u7eed\u7684Agent\u4efb\u52a1\uff0c\u7b2c\u4e00\u4e2a\u662f\u8ba1\u7b97\u5668\u5de5\u5177<code>calculate(what)<\/code> \uff0c\u8fd9\u662f\u4e00\u4e2a\u7528\u4e8e\u6267\u884c\u6570\u5b66\u8868\u8fbe\u5f0f\u7684\u5de5\u5177\u51fd\u6570\uff0c\u80fd\u591f\u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u8868\u8fbe\u5f0f\uff0c\u5e76\u4f7f\u7528 Python \u7684 <code>eval()<\/code> \u51fd\u6570\u8fdb\u884c\u8ba1\u7b97\u3002<\/p>\n<pre><code>def calculate(what):\n    return eval(what)\n\nprint(calculate(&quot;3 + 7 * 2&quot;))   # \u8fd4\u56de 17\nprint(calculate(&quot;10 \/ 4&quot;))      # \u8fd4\u56de 2.5<\/code><\/pre>\n<p>\u53e6\u5916\u4e00\u4e2a\u662f\u72d7\u72d7\u4f53\u91cd\u67e5\u8be2\u5de5\u5177<code>average_dog_weight(name)<\/code> \uff0c\u8be5\u51fd\u6570\u6839\u636e\u4f20\u5165\u7684\u72d7\u7684\u54c1\u79cd\u540d\u79f0\uff0c\u8fd4\u56de\u8be5\u54c1\u79cd\u7684\u5e73\u5747\u4f53\u91cd\u4fe1\u606f\u3002\u5982\u679c\u4f20\u5165\u7684\u54c1\u79cd\u4e0d\u5728\u9884\u8bbe\u5217\u8868\u4e2d\uff0c\u5219\u8fd4\u56de\u9ed8\u8ba4\u7684\u5e73\u5747\u4f53\u91cd\u4fe1\u606f\u3002<\/p>\n<pre><code>def average_dog_weight(name):\n    if name in&quot;Scottish Terrier&quot;: \n        return(&quot;Scottish Terriers average 20 lbs&quot;)\n    elif name in&quot;Border Collie&quot;:\n        return(&quot;a Border Collies average weight is 37 lbs&quot;)\n    elif name in&quot;Toy Poodle&quot;:\n        return(&quot;a toy poodles average weight is 7 lbs&quot;)\n    else:\n        return(&quot;An average dog weights 50 lbs&quot;)\n\naverage_dog_weight(&quot;Scottish Terrier&quot;)  \n# \u8fd4\u56de &quot;Scottish Terriers average 20 lbs&quot;\naverage_dog_weight(&quot;Labrador&quot;)          \n# \u8fd4\u56de &quot;An average dog weights 50 lbs&quot;<\/code><\/pre>\n<p>\u6700\u540e\u6211\u4eec\u53ef\u4ee5\u628a\u8fd9\u4e24\u4e2a\u5de5\u5177\u6ce8\u518c\u5230\u4e00\u8d77\uff0c\u5e76\u4e14\u8fd8\u9700\u8981\u5199\u5165\u5230\u63d0\u793a\u8bcd\u6a21\u7248\u5f53\u4e2d\uff0c\u90a3\u8fd9\u6837\u5c31\u53ef\u4ee5\u8ba9\u5927\u6a21\u578b\u77e5\u9053\u6709\u54ea\u4e9b\u5de5\u5177\u53ef\u7528\uff0c\u5e76\u4e14\u8c03\u7528\u7684\u65f6\u5019\u4f7f\u7528\u4ec0\u4e48\u6837\u7684\u683c\u5f0f\u3002<\/p>\n<pre><code>known_actions = {\n    &quot;calculate&quot;: calculate,\n    &quot;average_dog_weight&quot;: average_dog_weight\n}<\/code><\/pre>\n<h2><strong>\u63d0\u793a\u8bcd\u6a21\u7248<\/strong><\/h2>\n<p>\u5728\u5b8c\u6210\u5de5\u5177\u51c6\u5907\u5e76\u660e\u786e\u4efb\u52a1\u4e86\u4ee5\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f00\u59cb\u6765\u6784\u5efa\u63d0\u793a\u8bcd\u6a21\u7248\u4e86\u3002\u8fd9\u91cc\u6240\u4f7f\u7528\u7684\u63d0\u793a\u8bcd\u6a21\u7248\u80af\u5b9a\u4e0d\u80fd\u662f\u4e0a\u9762\u90a3\u79cd\u6709\u7528\u7684\u52a9\u624b\u90a3\u4e48\u7b80\u5355\uff0c\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u5176\u80fd\u591f\u5b9e\u73b0\u601d\u8003\u3001\u884c\u52a8\u3001\u6682\u505c\u3001\u89c2\u5bdf\u8fd9\u6837\u4e00\u8f6e\u8f6e\u7684\u6b65\u9aa4\u5b8c\u6210\uff0c\u56e0\u6b64\u8fd9\u4e2a\u63d0\u793a\u8bcd\u4e5f\u662f\u9700\u8981\u590d\u6742\u4e00\u4e9b\u3002\u4ee5\u4e0b\u662f\u8fd9\u4e2a\u4efb\u52a1\u6240\u4f7f\u7528\u7684\u63d0\u793a\u8bcd\u6a21\u7248\uff1a<\/p>\n<pre><code>prompt = &quot;&quot;&quot;\nYou run in a loop of Thought, Action, PAUSE, Observation.\nAt the end of the loop you output an Answer\nUse Thought to describe your thoughts about the question you have been asked.\nUse Action to run one of the actions available to you - then return PAUSE.\nObservation will be the result of running those actions.\n\nYour available actions are:\n\ncalculate:\ne.g. calculate: 4 * 7 \/ 3\nRuns a calculation and returns the number - uses Python so be sure to use floating point syntax if necessary\n\naverage_dog_weight:\ne.g. average_dog_weight: Collie\nreturns average weight of a dog when given the breed\n\nExample session:\n\nQuestion: How much does a Bulldog weigh?\nThought: I should look the dogs weight using average_dog_weight\nAction: average_dog_weight: Bulldog\nPAUSE\n\nYou will be called again with this:\n\nObservation: A Bulldog weights 51 lbs\n\nYou then output:\n\nAnswer: A bulldog weights 51 lbs\n&quot;&quot;&quot;.strip()<\/code><\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u6df1\u5165\u7684\u6765\u770b\u770b\u6bcf\u4e00\u90e8\u5206\u5176\u5177\u4f53\u662f\u600e\u4e48\u5b9e\u73b0\u7684\uff1a<\/p>\n<p>\u7b2c\u4e00\u90e8\u5206\u662f\u5b9a\u4e49\u5927\u8bed\u8a00\u6a21\u578b\u7684\u884c\u4e3a\u6d41\u7a0b\uff0c\u5373\u6a21\u578b\u9700\u8981\u6309\u7167 <code>Thought \u2192 Action \u2192 PAUSE \u2192 Observation \u2192 Answer<\/code> \u7684\u987a\u5e8f\u6765\u601d\u8003\u548c\u56de\u7b54\u95ee\u9898\u3002\u8fd9\u79cd\u7ed3\u6784\u8ba9\u6a21\u578b\u50cf\u4eba\u4e00\u6837\uff0c\u5148\u601d\u8003\uff0c\u518d\u884c\u52a8\uff0c\u89c2\u5bdf\u7ed3\u679c\uff0c\u6700\u540e\u5f97\u51fa\u7ed3\u8bba\uff0c\u662f ReAct \u667a\u80fd\u4f53\u6a21\u5f0f\u7684\u6838\u5fc3\u903b\u8f91\u3002<\/p>\n<pre><code>You run in a loop of Thought, Action, PAUSE, Observation.\nAt the end of the loop you output an Answer<\/code><\/pre>\n<p>\u7b2c\u4e8c\u90e8\u5206\u662f\u89e3\u91ca\u5404\u4e2a\u5173\u952e\u7ed3\u6784\uff0c<code>Thought<\/code> \u8868\u793a\u6a21\u578b\u7684\u601d\u8003\u8fc7\u7a0b\uff0c<code>Action<\/code> \u7528\u4e8e\u8c03\u7528\u5de5\u5177\uff0c<code>PAUSE<\/code> \u4ee3\u8868\u6682\u65f6\u505c\u6b62\u4ee5\u7b49\u5f85\u5916\u90e8\u6267\u884c\uff0c<code>Observation<\/code> \u662f\u52a8\u4f5c\u6267\u884c\u540e\u7684\u53cd\u9988\u4fe1\u606f\u3002\u8fd9\u4e00\u6bb5\u4e3a\u6a21\u578b\u63d0\u4f9b\u4e86\u6e05\u6670\u7684\u683c\u5f0f\u6307\u5357\uff0c\u786e\u4fdd\u5b83\u77e5\u9053\u6bcf\u4e00\u6b65\u8be5\u600e\u4e48\u505a\u3002<\/p>\n<pre><code>Use Thought to describe your thoughts about the question you have been asked.\nUse Action to run one of the actions available to you - then return PAUSE.\nObservation will be the result of running those actions.<\/code><\/pre>\n<p>\u7b2c\u4e09\u90e8\u5206\u5217\u51fa\u4e86\u5f53\u524d\u6a21\u578b\u53ef\u8c03\u7528\u7684\u4e24\u4e2a\u5de5\u5177\uff0c\u4e00\u4e2a\u662f <code>calculate<\/code> \u7528\u4e8e\u6267\u884c\u6570\u5b66\u8ba1\u7b97\uff0c\u4e00\u4e2a\u662f <code>average_dog_weight<\/code> \u7528\u4e8e\u67e5\u8be2\u72d7\u7684\u5e73\u5747\u4f53\u91cd\u3002\u6bcf\u4e2a\u5de5\u5177\u90fd\u6709\u8c03\u7528\u793a\u4f8b\u548c\u7528\u9014\u63cf\u8ff0\uff0c\u660e\u786e\u544a\u8bc9\u6a21\u578b\u80fd\u5e72\u4ec0\u4e48\u3001\u600e\u4e48\u5199\uff0c\u5e2e\u52a9\u5b83\u5728\u9700\u8981\u65f6\u505a\u51fa\u6b63\u786e\u9009\u62e9\u3002<\/p>\n<pre><code>Your available actions are:\n\ncalculate:\ne.g. calculate: 4 * 7 \/ 3\nRuns a calculation and returns the number - uses Python so be sure to use floating point syntax if necessary\n\naverage_dog_weight:\ne.g. average_dog_weight: Collie\nreturns average weight of a dog when given the breed<\/code><\/pre>\n<p>\u7b2c\u56db\u90e8\u5206\u662f\u793a\u4f8b\u6f14\u793a\uff0c\u901a\u8fc7\u4e00\u4e2a\u5b8c\u6574\u7684\u4f8b\u5b50\uff08\u201cBulldog \u7684\u4f53\u91cd\u201d\uff09\u6f14\u793a\u4e86\u6574\u4e2a\u6d41\u7a0b\u5982\u4f55\u8fd0\u4f5c\uff0c\u4ece\u7528\u6237\u63d0\u95ee\uff0c\u5230\u6a21\u578b\u601d\u8003\u3001\u9009\u62e9\u5de5\u5177\u3001\u63a5\u6536\u89c2\u5bdf\u7ed3\u679c\uff0c\u518d\u5230\u6700\u7ec8\u751f\u6210\u7b54\u6848\u3002\u8fd9\u79cd few-shot \u793a\u4f8b\u8d77\u5230\u4e86\u201c\u6559\u6a21\u578b\u600e\u4e48\u505a\u201d\u7684\u4f5c\u7528\uff0c\u5e2e\u52a9\u5b83\u5feb\u901f\u5b66\u4e60\u5e76\u6a21\u4eff\u3002<\/p>\n<pre><code>Example session:\n\nQuestion: How much does a Bulldog weigh?\nThought: I should look the dogs weight using average_dog_weight\nAction: average_dog_weight: Bulldog\nPAUSE\n\nYou will be called again with this:\n\nObservation: A Bulldog weights 51 lbs\n\nYou then output:\n\nAnswer: A bulldog weights 51 lbs<\/code><\/pre>\n<p>\u6240\u4ee5\u8fd9\u4e2a\u63d0\u793a\u8bcd\u662f\u4e00\u4e2a\u5178\u578b\u7684 ReAct \u6a21\u5f0f Prompt\uff0c\u7528\u4e8e\u5f15\u5bfc\u5927\u8bed\u8a00\u6a21\u578b\u6309 \u201cThought \u2192 Action \u2192 Observation \u2192 Answer\u201d \u7684\u6d41\u7a0b\u8fdb\u884c\u591a\u6b65\u63a8\u7406\u3002\u5b83\u5b9a\u4e49\u4e86\u4e24\u4e2a\u53ef\u8c03\u7528\u5de5\u5177\uff08\u8ba1\u7b97\u5668\u4e0e\u72d7\u4f53\u91cd\u67e5\u8be2\uff09\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u6559\u4f1a\u6a21\u578b\u5982\u4f55\u601d\u8003\u3001\u9009\u62e9\u5de5\u5177\u3001\u6682\u505c\u7b49\u5f85\u6267\u884c\u7ed3\u679c\uff0c\u6700\u7ec8\u7ed9\u51fa\u56de\u7b54\u3002\u6574\u4e2a\u63d0\u793a\u7ed3\u6784\u6e05\u6670\uff0c\u903b\u8f91\u5408\u7406\uff0c\u662f\u6784\u5efa\u5de5\u5177\u589e\u5f3a\u578b\u667a\u80fd\u4f53\u7684\u7ecf\u5178\u8bbe\u8ba1\u6a21\u677f\u3002\u57fa\u4e8e\u8fd9\u4e2a\u63d0\u793a\u8bcd\u6a21\u7248\uff0c\u5927\u6a21\u578b\u5c31\u80fd\u591f\u7167\u7740\u793a\u4f8b\u6765\u4e00\u6b65\u6b65\u5b8c\u6210\u6211\u4eec\u60f3\u8981\u5176\u5b8c\u6210\u7684\u4efb\u52a1\u3002<\/p>\n<h1><strong>Agent \u7ec4\u88c5<\/strong><\/h1>\n<p>\u5f53\u6211\u4eec\u51c6\u5907\u597d\u4e86\u56db\u90e8\u5206Agent\u7684\u7ec4\u4ef6\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u5176\u7ec4\u88c5\u8d77\u6765\uff0c\u5b9e\u73b0\u771f\u6b63\u7684Agent\u6d41\u7a0b\u4e86\u3002\u5177\u4f53\u7684\u4ee3\u7801\u5b9e\u73b0\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>action_re = re.compile(&#039;^Action: (\\w+): (.*)$&#039;)\ndef query(question, max_turns=5):\n    i = 0\n    bot = Agent(prompt)\n    next_prompt = question\n    while i &lt; max_turns:\n        i += 1\n        result = bot(next_prompt)\n        print(result)\n        actions = [\n            action_re.match(a) \n            for a in result.split(&#039;\\n&#039;) \n            if action_re.match(a)\n        ]\n        if actions:\n            # There is an action to run\n            action, action_input = actions[0].groups()\n            if action notin known_actions:\n                raise Exception(&quot;Unknown action: {}: {}&quot;.format(action, action_input))\n            print(&quot; -- running {} {}&quot;.format(action, action_input))\n            observation = known_actions[action](action_input)\n            print(&quot;Observation:&quot;, observation)\n            next_prompt = &quot;Observation: {}&quot;.format(observation)\n        else:\n            return result<\/code><\/pre>\n<h2><strong>\u7b2c\u4e00\u90e8\u5206\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9a\u4e49\uff08\u8bc6\u522b Action \u884c\uff09<\/strong><\/h2>\n<p>\u8fd9\u4e00\u884c\u662f\u7528 Python \u7684 <code>re<\/code> \u6a21\u5757\u9884\u7f16\u8bd1\u4e86\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u7528\u4e8e\u5339\u914d\u6a21\u578b\u8f93\u51fa\u4e2d\u7684\u52a8\u4f5c\u8c03\u7528\u6307\u4ee4\u3002<\/p>\n<pre><code>action_re = re.compile(&#039;^Action: (\\w+): (.*)$&#039;)<\/code><\/pre>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u542b\u4e49\u662f\uff1a<\/p>\n<ul>\n<li><code>^Action:<\/code> \u8868\u793a\u5fc5\u987b\u4ee5 <code>Action:<\/code> \u5f00\u5934<\/li>\n<li><code>(\\w+)<\/code> \u5339\u914d\u4e00\u4e2a\u7531\u5b57\u6bcd\u3001\u6570\u5b57\u6216\u4e0b\u5212\u7ebf\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\uff0c\u7528\u6765\u6355\u83b7\u5de5\u5177\u540d<\/li>\n<li><code>:<\/code> \u662f\u7b2c\u4e8c\u4e2a\u5192\u53f7<\/li>\n<li><code>(.*)<\/code> \u5339\u914d\u8fd9\u4e00\u884c\u5269\u4e0b\u7684\u6240\u6709\u5185\u5bb9\uff0c\u4f5c\u4e3a\u5de5\u5177\u7684\u8f93\u5165\u53c2\u6570<\/li>\n<li><code>$<\/code> \u8868\u793a\u8fd9\u4e00\u884c\u7684\u672b\u5c3e<\/li>\n<\/ul>\n<p>\u6bd4\u5982\u8bf4\u4ee5\u4e0b\u7684\u5185\u5bb9\uff1a<\/p>\n<pre><code>Action: calculate: 5 + 3\nAction: average_dog_weight: Border Collie<\/code><\/pre>\n<p>\u5c31\u53ef\u4ee5\u63d0\u53d6\u51fa\uff1a<\/p>\n<ul>\n<li>\u5de5\u5177\u540d\uff1a\u5982 <code>calculate<\/code><\/li>\n<li>\u53c2\u6570\u5185\u5bb9\uff1a\u5982 <code>5 + 3<\/code><\/li>\n<\/ul>\n<p>\u8fd9\u5728\u6574\u4e2a ReAct \u6846\u67b6\u4e2d\u8d77\u5230\u4e86\u201c\u89e3\u6790\u6a21\u578b\u6307\u4ee4\u201d\u7684\u4f5c\u7528\uff0c\u662f\u8fde\u63a5 LLM \u4e0e\u5de5\u5177\u51fd\u6570\u7684\u6865\u6881\u3002<\/p>\n<h2><strong>\u7b2c\u4e8c\u90e8\u5206\uff1a\u51fd\u6570\u5b9a\u4e49\u548c\u521d\u59cb\u5316 Agent<\/strong><\/h2>\n<p>\u8fd9\u662f <code>query<\/code> \u51fd\u6570\u7684\u5b9a\u4e49\u90e8\u5206\uff0c\u5b83\u662f\u6574\u4e2a\u201cReAct\u5f0f\u63a8\u7406\u5bf9\u8bdd\u201d\u7684\u4e3b\u51fd\u6570\u3002<\/p>\n<pre><code>def query(question, max_turns=5):\n    i = 0\n    bot = Agent(prompt)\n    next_prompt = question<\/code><\/pre>\n<p>\u51fd\u6570\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li><code>question<\/code> \u662f\u7528\u6237\u8f93\u5165\u7684\u95ee\u9898\uff0c\u6bd4\u5982\uff1a\u201c\u6211\u6709\u4e24\u53ea\u72d7\uff0c\u4e00\u53ea\u662f Border Collie\uff0c\u4e00\u53ea\u662f Scottish Terrier\uff0c\u5b83\u4eec\u7684\u603b\u91cd\u662f\u591a\u5c11\uff1f\u201d<\/li>\n<li><code>max_turns<\/code> \u662f\u6700\u591a\u5141\u8bb8\u6a21\u578b\u63a8\u7406\u7684\u8f6e\u6570\uff0c\u7528\u4e8e\u907f\u514d\u6b7b\u5faa\u73af<\/li>\n<\/ul>\n<p>\u5185\u90e8\u53d8\u91cf\u521d\u59cb\u5316\uff1a<\/p>\n<ul>\n<li><code>i = 0<\/code>\uff1a\u8f6e\u6b21\u8ba1\u6570\u5668\uff0c\u4ece\u7b2c 0 \u8f6e\u5f00\u59cb<\/li>\n<li><code>bot = Agent(prompt)<\/code>\uff1a\u5b9e\u4f8b\u5316\u4e00\u4e2a Agent \u5bf9\u8c61\uff0c\u5e76\u52a0\u8f7d\u524d\u9762\u5199\u597d\u7684\u7cfb\u7edf\u63d0\u793a\u8bcd prompt\uff0c\u4f7f\u5f97\u6a21\u578b\u5177\u5907 ReAct \u98ce\u683c\u884c\u4e3a<\/li>\n<li><code>next_prompt = question<\/code>\uff1a\u628a\u7528\u6237\u95ee\u9898\u4f5c\u4e3a\u7b2c\u4e00\u8f6e\u5bf9\u8bdd\u7684\u8f93\u5165<\/li>\n<\/ul>\n<p>\u8fd9\u4e00\u90e8\u5206\u7684\u4f5c\u7528\u662f\u51c6\u5907\u597d\u6a21\u578b\u4e0a\u4e0b\u6587\u3001\u8f6e\u6b21\u8ba1\u6570\u5668\u548c\u521d\u59cb\u8f93\u5165\u5185\u5bb9\uff0c\u4e3a\u540e\u7eed\u7684\u5bf9\u8bdd\u5faa\u73af\u505a\u51c6\u5907\u3002<\/p>\n<h2><strong>\u7b2c\u4e09\u90e8\u5206\uff1a\u8fdb\u5165\u63a8\u7406\u5faa\u73af\uff0c\u6bcf\u8f6e\u8fd0\u884c Agent<\/strong><\/h2>\n<p>\u8fd9\u662f <code>query()<\/code> \u51fd\u6570\u7684\u4e3b\u5faa\u73af\u4f53\uff0c\u6bcf\u6b21\u5faa\u73af\u4ee3\u8868\u4e00\u6b21\u63a8\u7406\u8f6e\u6b21\u3002<\/p>\n<pre><code>while i &lt; max_turns:\n    i += 1\n    result = bot(next_prompt)\n    print(result)<\/code><\/pre>\n<p>\u89e3\u91ca\uff1a<\/p>\n<ul>\n<li>\u5faa\u73af\u6761\u4ef6\u662f\u8f6e\u6570\u4e0d\u80fd\u8d85\u8fc7 <code>max_turns<\/code> \uff0c\u8fd9\u4e5f\u5c31\u662f\u524d\u9762\u6240\u8bf4\u76845\u8f6e\u3002<\/li>\n<li>\u6bcf\u8f6e\u8f93\u5165 <code>next_prompt<\/code>\uff0c\u4e00\u5f00\u59cb\u4e5f\u662f\u6211\u4eec\u63d0\u95ee\u7684\u95ee\u9898\uff0c\u540e\u9762\u968f\u7740\u6bcf\u4e00\u8f6e\u8fd0\u884c\uff0c\u5185\u5bb9\u4e0d\u65ad\u589e\u52a0<\/li>\n<li><code>result<\/code> \u662f\u6a21\u578b\u8f93\u51fa\u7684\u5b8c\u6574\u6587\u672c\uff0c\u53ef\u80fd\u5305\u542b\uff1a\n<ul>\n<li><code>Thought:<\/code> \u6a21\u578b\u7684\u63a8\u7406\u8bf4\u660e<\/li>\n<li><code>Action:<\/code> \u5de5\u5177\u8c03\u7528\u8bf7\u6c42<\/li>\n<li><code>PAUSE<\/code> \u6682\u505c\u7b26\u53f7\uff08\u4eba\u4e3a\u63a7\u5236\uff09<\/li>\n<li>\u6216\u6700\u7ec8\u7684 <code>Answer: xxx<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u901a\u8fc7 <code>print(result)<\/code> \u6253\u5370\u7ed3\u679c\uff0c\u8ba9\u6211\u4eec\u770b\u5230\u6bcf\u4e00\u6b65\u6a21\u578b\u505a\u4e86\u4ec0\u4e48\uff0c\u662f\u975e\u5e38\u5e38\u89c1\u7684\u5f00\u53d1\u4e60\u60ef\u3002\u6b63\u5f0f\u4e0a\u7ebf\u7684\u65f6\u5019\u53ef\u4ee5\u53bb\u6389\u4ee5\u63d0\u5347\u901f\u5ea6\u3002<\/p>\n<h2><strong>\u7b2c\u56db\u90e8\u5206\uff1a\u8bc6\u522b\u8f93\u51fa\u4e2d\u662f\u5426\u5305\u542b Action \u8c03\u7528\u6307\u4ee4<\/strong><\/h2>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u7684\u76ee\u6807\u662f\u4ece\u6a21\u578b\u8f93\u51fa\u4e2d\u63d0\u53d6\u51fa <code>Action: xxx: yyy<\/code> \u7684\u6307\u4ee4\u3002<\/p>\n<pre><code>actions = [\n    action_re.match(a) \n    for a in result.split(&#039;\\n&#039;) \n    if action_re.match(a)\n]<\/code><\/pre>\n<p>\u89e3\u91ca\uff1a<\/p>\n<ul>\n<li><code>result.split(&#039;\\n&#039;)<\/code> \u5c06\u591a\u884c\u6a21\u578b\u8f93\u51fa\u6309\u884c\u62c6\u5206\u4e3a\u4e00\u4e2a\u5217\u8868<\/li>\n<li>\u5bf9\u6bcf\u4e00\u884c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f <code>action_re.match(a)<\/code> \u8fdb\u884c\u5339\u914d<\/li>\n<li>\u5982\u679c\u67d0\u4e00\u884c\u7b26\u5408\u683c\u5f0f <code>Action: \u5de5\u5177\u540d: \u53c2\u6570<\/code>\uff0c\u5c31\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u5bf9\u8c61\uff08<code>Match<\/code> \u5bf9\u8c61\uff09<\/li>\n<li>\u628a\u8fd9\u4e9b\u5339\u914d\u5bf9\u8c61\u7ec4\u6210\u4e00\u4e2a\u5217\u8868 <code>actions<\/code><\/li>\n<\/ul>\n<p>\u8fd9\u4e2a\u90e8\u5206\u7684\u4f5c\u7528\u662f\uff1a<\/p>\n<ul>\n<li>\u5206\u6790\u6a21\u578b\u7684\u8f93\u51fa\uff0c\u770b\u5b83\u6709\u6ca1\u6709\u53d1\u51fa\u201c\u6211\u60f3\u8c03\u7528\u54ea\u4e2a\u5de5\u5177\u201d\u7684\u6307\u4ee4<\/li>\n<li>\u5982\u679c\u6709\uff0c\u5c31\u63d0\u53d6\u51fa\u6765\u51c6\u5907\u6267\u884c<\/li>\n<li>\u5982\u679c\u6ca1\u6709\uff0c\u5219\u8bf4\u660e\u6a21\u578b\u5df2\u7ecf\u51c6\u5907\u597d\u8f93\u51fa\u6700\u7ec8\u7ed3\u679c\uff0c\u4e0d\u9700\u8981\u8c03\u7528\u5de5\u5177<\/li>\n<\/ul>\n<h2><strong>\u7b2c\u4e94\u90e8\u5206\uff1a\u5224\u65ad\u662f\u5426\u6267\u884c\u52a8\u4f5c\uff0c\u5e76\u63d0\u53d6\u8c03\u7528\u7ec6\u8282<\/strong><\/h2>\n<p>\u8fd9\u4e00\u6bb5\u8868\u793a\u5982\u679c <code>actions<\/code> \u5217\u8868\u975e\u7a7a\uff0c\u8bf4\u660e\u627e\u5230\u4e86\u6a21\u578b\u60f3\u6267\u884c\u7684 Action\u3002\u8fd9\u4e2a\u65f6\u5019\u5c31\u53d6\u51fa\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\uff0c\u5e76\u7528 <code>.groups()<\/code> \u65b9\u6cd5\u83b7\u5f97\u4e24\u4e2a\u5185\u5bb9\uff1a<\/p>\n<ul>\n<li><code>action<\/code>\uff1a\u5de5\u5177\u540d\u79f0\uff08\u5982 <code>calculate<\/code>\uff09<\/li>\n<li><code>action_input<\/code>\uff1a\u53c2\u6570\u5185\u5bb9\uff08\u5982 <code>5 + 3<\/code>\uff09<\/li>\n<\/ul>\n<pre><code>if actions:\n    action, action_input = actions[0].groups()<\/code><\/pre>\n<p>\u6ce8\u610f\u8fd9\u91cc\u53ea\u53d6\u7b2c\u4e00\u4e2a <code>Action<\/code>\uff0c\u610f\u5473\u7740\u5982\u679c\u6a21\u578b\u4e00\u6b21\u8f93\u51fa\u591a\u4e2a\u52a8\u4f5c\uff0c\u53ea\u4f1a\u6267\u884c\u7b2c\u4e00\u4e2a\u3002\u4f60\u53ef\u4ee5\u6269\u5c55\u4e3a\u591a\u52a8\u4f5c\u652f\u6301\uff0c\u4f46\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u662f\u5355\u8f6e\u5355\u52a8\u4f5c\u6267\u884c\u3002<\/p>\n<h2><strong>\u7b2c\u516d\u90e8\u5206\uff1a\u68c0\u67e5\u5de5\u5177\u662f\u5426\u5408\u6cd5\u5e76\u6267\u884c<\/strong><\/h2>\n<p>\u8fd9\u6bb5\u662f\u4e00\u4e2a\u4fdd\u62a4\u673a\u5236\uff0c\u786e\u4fdd\u6a21\u578b\u8c03\u7528\u7684\u5de5\u5177\u5fc5\u987b\u662f\u5728\u4f60\u5b9a\u4e49\u7684\u5de5\u5177\u5b57\u5178 <code>known_actions<\/code> \u4e2d\u6ce8\u518c\u8fc7\u7684\u3002<\/p>\n<pre><code>    if action not in known_actions:\n        raise Exception(&quot;Unknown action: {}: {}&quot;.format(action, action_input))<\/code><\/pre>\n<p>\u5982\u679c\u6a21\u578b\u5199\u9519\u4e86\u5de5\u5177\u540d\u79f0\uff0c\u6216\u8005\u8c03\u7528\u4e86\u4f60\u672a\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u8fd9\u6bb5\u4ee3\u7801\u5c31\u4f1a\u629b\u51fa\u5f02\u5e38\uff0c\u7ec8\u6b62\u7a0b\u5e8f\uff0c\u9632\u6b62\u6a21\u578b\u201c\u4e71\u8c03\u7528\u201d\u3002<\/p>\n<h2><strong>\u7b2c\u4e03\u90e8\u5206\uff1a\u5b9e\u9645\u6267\u884c\u5de5\u5177\u51fd\u6570\uff0c\u5e76\u83b7\u53d6 observation<\/strong><\/h2>\n<p>\u5f53\u786e\u4fdd\u51fd\u6570\u5b58\u5728\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5b9e\u9645\u7684\u53bb\u6267\u884c\u8fd9\u4e2a\u51fd\u6570\u5e76\u83b7\u53d6\u7ed3\u679c\u4e86\u3002<\/p>\n<pre><code>    print(&quot; -- running {} {}&quot;.format(action, action_input))\n    observation = known_actions[action](action_input)\n    print(&quot;Observation:&quot;, observation)<\/code><\/pre>\n<ul>\n<li><code>known_actions[action]<\/code> \u8868\u793a\u4ece\u5de5\u5177\u5b57\u5178\u4e2d\u53d6\u51fa\u5bf9\u5e94\u7684\u51fd\u6570\uff08\u5982 <code>calculate<\/code>\uff09<\/li>\n<li><code>(...)<\/code> \u8868\u793a\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u628a <code>action_input<\/code> \u4f5c\u4e3a\u53c2\u6570\u4f20\u8fdb\u53bb<\/li>\n<li>\u6267\u884c\u540e\u5f97\u5230\u7684\u8fd4\u56de\u503c\u5c31\u662f observation\uff08\u89c2\u5bdf\u7ed3\u679c\uff09<\/li>\n<\/ul>\n<p>\u6bd4\u5982\u8bf4\u5047\u5982\u6211\u4eec\u4f20\u5165\u7684\u662f\u4e0b\u9762\u7684\u5185\u5bb9\uff1a<\/p>\n<pre><code>action = &quot;calculate&quot;\naction_input = &quot;3 + 5&quot;\nobservation = calculate(&quot;3 + 5&quot;)  # \u2192 8<\/code><\/pre>\n<p>\u8fd9\u4e2a observation \u4f1a\u4f5c\u4e3a\u53cd\u9988\u63d0\u4f9b\u7ed9\u6a21\u578b\uff0c\u8ba9\u5b83\u7ee7\u7eed\u601d\u8003\u4e0b\u4e00\u6b65\u3002<\/p>\n<h2><strong>\u7b2c\u516b\u90e8\u5206\uff1a\u5c06 Observation \u8fd4\u56de\u6a21\u578b\uff0c\u5f62\u6210\u95ed\u73af<\/strong><\/h2>\n<p>\u8fd9\u884c\u4ee3\u7801\u7684\u4f5c\u7528\u662f\u628a\u4e0a\u4e00\u8f6e\u5de5\u5177\u8fd0\u884c\u5f97\u5230\u7684\u7ed3\u679c\u5305\u88c5\u6210 Observation \u683c\u5f0f\uff0c\u7136\u540e\u4f5c\u4e3a\u4e0b\u4e00\u8f6e\u5bf9\u8bdd\u7684\u8f93\u5165\u5185\u5bb9\u3002<\/p>\n<pre><code>    next_prompt = &quot;Observation: {}&quot;.format(observation)<\/code><\/pre>\n<p>\u8fd9\u6837\u6a21\u578b\u5728\u4e0b\u4e00\u6b21\u8fd0\u884c\u65f6\u5c31\u80fd\u201c\u770b\u5230\u201d\u4e4b\u524d\u5de5\u5177\u7684\u6267\u884c\u7ed3\u679c\uff0c\u5f62\u6210 ReAct \u6a21\u5f0f\u4e2d\u7684\u201c\u89c2\u5bdf\u53cd\u9988\u201d\u95ed\u73af\u3002<\/p>\n<h2><strong>\u7b2c\u4e5d\u90e8\u5206\uff1a\u5982\u679c\u6ca1\u6709 Action\uff0c\u76f4\u63a5\u8fd4\u56de\u6700\u7ec8\u7ed3\u679c<\/strong><\/h2>\n<p>\u5982\u679c\u8fd9\u4e00\u8f6e\u6a21\u578b\u8f93\u51fa\u4e2d\u6ca1\u6709 Action\uff0c\u8bf4\u660e\u5b83\u8ba4\u4e3a\u5df2\u7ecf\u6709\u8db3\u591f\u4fe1\u606f\u6765\u7ed9\u51fa\u6700\u7ec8\u56de\u7b54\u4e86\u3002<\/p>\n<pre><code>else:\n    return result<\/code><\/pre>\n<p>\u6bd4\u5982\uff1a<\/p>\n<pre><code>Answer: The total weight of your dogs is 57 lbs.<\/code><\/pre>\n<p>\u8fd9\u65f6\u5019\u5c31\u4e0d\u518d\u7ee7\u7eed\u5de5\u5177\u8c03\u7528\u6d41\u7a0b\uff0c\u76f4\u63a5\u8fd4\u56de\u8fd9\u4e2a\u7ed3\u679c\u4f5c\u4e3a\u6700\u7ec8\u8f93\u51fa\u3002<\/p>\n<h1><strong>\u5b9e\u9645\u8c03\u7528<\/strong><\/h1>\n<p>\u5728\u51c6\u5907\u597d\u6574\u4f53\u7684\u8c03\u7528\u903b\u8f91\u53ca\u5de5\u5177\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u51fd\u6570\u5f00\u542f\u6211\u4eec\u624b\u6413\u7684Agent\u4e86\uff01\u8fd9\u91cc\u63d0\u51fa\u7684\u95ee\u9898\u5c31\u662f\uff1a\u6211\u6709\u4e24\u53ea\u5c0f\u72d7\uff0c\u4e00\u53ea\u662fborder collie\uff0c\u53e6\u4e00\u53ea\u662fscottish terrier\uff0c\u603b\u5171\u6709\u591a\u91cd\uff1a<\/p>\n<pre><code>question = &quot;&quot;&quot;I have 2 dogs, a border collie and a scottish terrier. \\\nWhat is their combined weight&quot;&quot;&quot;\nquery(question)<\/code><\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7query\u51fd\u6570\u91ccprint\u7684\u5185\u5bb9\u5728\u7ec8\u7aef\u770b\u5230\u5176\u4e2d\u7684\u53d8\u5316\uff1a<\/p>\n<pre><code>Thought: I need to find the average weight of a Border Collie and a Scottish Terrier, then add them together to get their combined weight.\nAction: average_dog_weight: Border Collie\nPAUSE\n -- running average_dog_weight Border Collie\nObservation: a Border Collies average weight is37 lbs\nThought: Now that I have the average weight of a Border Collie, I need to find the average weight of a Scottish Terrier.\nAction: average_dog_weight: Scottish Terrier\nPAUSE\n -- running average_dog_weight Scottish Terrier\nObservation: Scottish Terriers average 20 lbs\nThought: I now have the average weights of both a Border Collie and a Scottish Terrier. I can now add these two weights together to find their combined weight.\nAction: calculate: 37 + 20\nPAUSE\n -- running calculate 37 + 20\nObservation: 57\nAnswer: The combined weight of a Border Collie and a Scottish Terrier is57 lbs.<\/code><\/pre>\n<h2><strong>\u7b2c\u4e00\u6b65\uff1a\u7b2c\u4e00\u6b21 Thought + Action<\/strong><\/h2>\n<p>\u4ece\u6587\u672c\u53ef\u4ee5\u770b\u51fa\uff0c\u6a21\u578b\u5148\u201c\u60f3\u4e86\u60f3\u201d\uff1a\u8981\u5f97\u5230\u603b\u91cd\uff0c\u5f97\u5148\u5206\u522b\u67e5\u4e24\u53ea\u72d7\u7684\u5e73\u5747\u4f53\u91cd\uff0c\u518d\u628a\u5b83\u4eec\u52a0\u8d77\u6765\u3002\u7136\u540e\u51b3\u5b9a<strong>\u5148\u67e5 Border Collie \u7684\u4f53\u91cd<\/strong>\uff0c\u8c03\u7528\u5de5\u5177 <code>average_dog_weight<\/code> \u3002\u6700\u540e\u52a0\u4e0a <code>PAUSE<\/code>\uff0c\u610f\u5473\u7740\u5b83\u5e0c\u671b\u7cfb\u7edf\u5148\u6267\u884c\u5b8c\u5de5\u5177\u518d\u7ee7\u7eed\u3002<\/p>\n<pre><code>Thought: I need to find the average weight of a Border Collie and a Scottish Terrier, then add them together to get their combined weight.\nAction: average_dog_weight: Border Collie\nPAUSE<\/code><\/pre>\n<p>\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd0\u884c<code>result = bot(next_prompt)<\/code> \u540e\uff0c\u8fd9\u90e8\u5206\u601d\u8003\u548c\u884c\u52a8\u7684\u5185\u5bb9\u90fd\u4f1a\u4f5c\u4e3a\u804a\u5929\u8bb0\u5f55\u4fdd\u5b58\u5728<code>self.messages<\/code>\u91cc\u4e86\uff0c\u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u6a21\u578b\u540e\u7eed\u8fdb\u4e00\u6b65\u5b8c\u6210\u4efb\u52a1\u3002<\/p>\n<h2><strong>\u7b2c\u4e8c\u6b65\uff1a\u7cfb\u7edf\u6267\u884c Border Collie \u5de5\u5177<\/strong><\/h2>\n<p>\u505c\u4e0b\u540e\u5c31\u5f00\u59cb\u8fdb\u884c\u884c\u52a8\uff08action\uff09\u4e86\uff0c\u5c31\u662f\u4ece<code>known_actions<\/code> \u5b57\u5178\u4e2d\u8c03\u7528 <code>average_dog_weight(&quot;Border Collie&quot;)<\/code>\u3002\u53ef\u4ee5\u5f97\u5230\u7ed3\u679c <code>&quot;a Border Collies average weight is 37 lbs&quot;<\/code>\u3002\u6700\u540e\u7cfb\u7edf\u628a\u8fd9\u4e2a observation \u53cd\u9988\u7ed9\u6a21\u578b\uff0c\u8ba9\u5b83\u7ee7\u7eed\u63a8\u7406\u3002<\/p>\n<pre><code> -- running average_dog_weight Border Collie\nObservation: a Border Collies average weight is 37 lbs<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65f6\u5019\uff0cobservation\u4f1a\u4f5c\u4e3a\u4e0b\u4e00\u4e2a\u63d0\u95ee\u4f20\u7ed9\u6a21\u578b\uff0c\u6a21\u578b\u57fa\u4e8e\u5386\u53f2\u8bb0\u5f55\u53ca\u8fd9\u4e2a\u89c2\u5bdf\u8fdb\u4e00\u6b65\u7684\u5b8c\u6210\u4efb\u52a1\uff0c\u540c\u6837\u7684\uff0c\u5728\u5386\u53f2\u91cc\u4f1a\u628a\u8fd9\u4e2aobservation\u7684\u4fe1\u606f\u4f5c\u4e3a<code>user message<\/code>\u4f20\u5165<code>self.messages<\/code>\u3002<\/p>\n<h2><strong>\u7b2c\u4e09\u6b65\uff1a\u6a21\u578b\u7ee7\u7eed\u63a8\u7406\u4e0b\u4e00\u6b65<\/strong><\/h2>\n<p>\u5728\u63a5\u6536\u5230\u89c2\u5bdf\u7684\u4fe1\u606f\u540e\uff0c\u6a21\u578b\u77e5\u9053\u4e86\u7b2c\u4e00\u53ea\u72d7\u7684\u4f53\u91cd\u3002\u63a5\u7740\u6a21\u578b\u601d\u8003\u201c\u6211\u8fd8\u9700\u8981\u7b2c\u4e8c\u53ea\u72d7\u7684\u4f53\u91cd\u201d\u6240\u4ee5\u8fd9\u4e00\u6b65\u5b83\u8c03\u7528 <code>average_dog_weight: Scottish Terrier<\/code> \u5de5\u5177\u8fdb\u4e00\u6b65\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n<pre><code>Thought: Now that I have the average weight of a Border Collie, I need to find the average weight of a Scottish Terrier.\nAction: average_dog_weight: Scottish Terrier\nPAUSE<\/code><\/pre>\n<p>\u5f53\u7136\u8fd9\u4e2a\u4fe1\u606f\u4e5f\u56de\u4f5c\u4e3aassistant\u56de\u590d\u7684\u4fe1\u606f\u4fdd\u5b58\u5728<code>self.messages<\/code> \u4e2d\u3002<\/p>\n<h2><strong>\u7b2c\u56db\u6b65\uff1a\u7cfb\u7edf\u6267\u884c\u7b2c\u4e8c\u4e2a\u5de5\u5177<\/strong><\/h2>\n<p>\u5728\u6536\u5230action\u6307\u4ee4\u540e\uff0c\u7cfb\u7edf\u7ee7\u7eed\u8c03\u7528\u5de5\u5177\u51fd\u6570\uff1a<code>average_dog_weight(&quot;Scottish Terrier&quot;)<\/code>\u3002\u6700\u540e\u53ef\u4ee5\u5f97\u5230\u4f53\u91cd\uff1a<code>20 lbs<\/code>\u3002\u7136\u540e\u7cfb\u7edf\u518d\u6b21\u5c06\u7ed3\u679c\u4f5c\u4e3a Observation \u53cd\u9988\u7ed9\u6a21\u578b\u3002<\/p>\n<pre><code> -- running average_dog_weight Scottish Terrier\nObservation: Scottish Terriers average 20 lbs<\/code><\/pre>\n<h2><strong>\u7b2c\u4e94\u6b65\uff1a\u6a21\u578b\u601d\u8003\u5e76\u8c03\u7528\u52a0\u6cd5\u5de5\u5177<\/strong><\/h2>\n<p>\u7cfb\u7edf\u770b\u5230\u4e86\u6211\u4eec\u5df2\u7ecf\u627e\u5230\u4e86\u4e24\u4e2a\u4f53\u91cd\uff0837\u548c20\uff09\uff0c\u6240\u4ee5\u6a21\u578b\u51b3\u5b9a \u8c03\u7528 <code>calculate: 37 + 20<\/code> \u5de5\u5177\u6765\u8fdb\u884c\u8ba1\u7b97\u3002<\/p>\n<pre><code>Thought: I now have the average weights of both a Border Collie and a Scottish Terrier. I can now add these two weights together to find their combined weight.\nAction: calculate: 37 + 20\nPAUSE<\/code><\/pre>\n<h2><strong>\u7b2c\u516d\u6b65\uff1a\u7cfb\u7edf\u6267\u884c\u52a0\u6cd5\u5de5\u5177<\/strong><\/h2>\n<p>\u6839\u636e\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u89e3\u6790\uff0c\u7cfb\u7edf\u8c03\u7528 <code>calculate(&quot;37 + 20&quot;)<\/code> \u2192 <code>eval(&quot;37 + 20&quot;)<\/code> \u5f97\u51fa 57\u3002\u6700\u540e\u6211\u4eec\u5c06\u628a\u8fd9\u4e2a Observation \u8fd4\u56de\u7ed9\u6a21\u578b\u3002<\/p>\n<pre><code> -- running calculate 37 + 20\nObservation: 57<\/code><\/pre>\n<h2><strong>\u6700\u540e\u4e00\u6b65\uff1a\u6a21\u578b\u8f93\u51fa\u6700\u7ec8\u7b54\u6848<\/strong><\/h2>\n<p>\u6700\u540e\u6a21\u578b\u8f93\u51fa\u4e00\u4e2aAnswer\u7684\u7b54\u6848\u5185\u5bb9\uff0c\u7531\u4e8e\u5e76\u6ca1\u6709\u8f93\u51faAction\u7684\u4fe1\u606f\uff0c\u56e0\u6b64\u6574\u4e2a\u63a8\u7406\u94fe\u6761\u95ed\u73af\uff0c\u4efb\u52a1\u7ed3\u675f\u3002<\/p>\n<pre><code>Answer: The combined weight of a Border Collie and a Scottish Terrier is 57 lbs.<\/code><\/pre>\n<h2><strong>\u6574\u4f53\u6d41\u7a0b\u5c55\u793a<\/strong><\/h2>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u9636\u6bb5<\/th>\n<th style=\"text-align: left;\">\u6a21\u578b\u884c\u4e3a<\/th>\n<th style=\"text-align: left;\">\u5de5\u5177\u8c03\u7528<\/th>\n<th style=\"text-align: left;\">Observation<\/th>\n<th style=\"text-align: left;\">\u4e0b\u4e00\u6b65<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">1<\/td>\n<td style=\"text-align: left;\">\u601d\u8003\u9700\u8981\u4e24\u4e2a\u4f53\u91cd<\/td>\n<td style=\"text-align: left;\">\u67e5 Border Collie<\/td>\n<td style=\"text-align: left;\">\u5f97\u5230 37 lbs<\/td>\n<td style=\"text-align: left;\">\u53bb\u67e5\u7b2c\u4e8c\u53ea<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">2<\/td>\n<td style=\"text-align: left;\">\u67e5\u7b2c\u4e8c\u53ea\u72d7\u7684\u4f53\u91cd<\/td>\n<td style=\"text-align: left;\">\u67e5 Scottish Terrier<\/td>\n<td style=\"text-align: left;\">\u5f97\u5230 20 lbs<\/td>\n<td style=\"text-align: left;\">\u5f00\u59cb\u8ba1\u7b97<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">3<\/td>\n<td style=\"text-align: left;\">\u52a0\u6cd5\u8ba1\u7b97<\/td>\n<td style=\"text-align: left;\">calculate: 37 + 20<\/td>\n<td style=\"text-align: left;\">\u5f97\u5230 57<\/td>\n<td style=\"text-align: left;\">\u7ed9\u51fa\u6700\u7ec8\u7b54\u6848<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-python\">import re\nfrom openai import OpenAI\naliyun_api_key = &#039;\u4f60\u7684api_key&#039;\nclient = OpenAI(\n    api_key=aliyun_api_key,\n    base_url=&quot;https:\/\/dashscope.aliyuncs.com\/compatible-mode\/v1&quot;\n)\n\nclass Agent:\n    def __init__(self, system=&quot;&quot;):\n        self.system = system\n        self.messages = []\n        if self.system:\n            self.messages.append({&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: system})\n\n    def __call__(self, message):\n        self.messages.append({&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: message})\n        result = self.execute()\n        self.messages.append({&quot;role&quot;: &quot;assistant&quot;, &quot;content&quot;: result})\n        return result\n\n    def execute(self):\n        response = client.chat.completions.create(\n            model=&quot;qwen-max&quot;,\n            messages=self.messages\n        )\n        return response.choices[0].message.content\n\nabot = Agent(&#039;\u4f60\u662f\u4e00\u4e2a\u4e50\u4e8e\u52a9\u4eba\u7684\u673a\u5668\u4eba&#039;)\nresult = abot(&quot;\u4f60\u662f\u8c01\uff1f&quot;)\nprint(result)\n\nprompt = &quot;&quot;&quot;\nYou run in a loop of Thought, Action, PAUSE, Observation.\nAt the end of the loop you output an Answer\nUse Thought to describe your thoughts about the question you have been asked.\nUse Action to run one of the actions available to you - then return PAUSE.\nObservation will be the result of running those actions.\n\nYour available actions are:\n\ncalculate:\ne.g. calculate: 4 * 7 \/ 3\nRuns a calculation and returns the number - uses Python so be sure to use floating point syntax if necessary\n\naverage_dog_weight:\ne.g. average_dog_weight: Collie\nreturns average weight of a dog when given the breed\n\nExample session:\n\nQuestion: How much does a Bulldog weigh?\nThought: I should look the dogs weight using average_dog_weight\nAction: average_dog_weight: Bulldog\nPAUSE\n\nYou will be called again with this:\n\nObservation: A Bulldog weights 51 lbs\n\nYou then output:\n\nAnswer: A bulldog weights 51 lbs\n&quot;&quot;&quot;.strip()\n\ndef calculate(what):\n    return eval(what)\n\ndef average_dog_weight(name):\n    if name in&quot;Scottish Terrier&quot;: \n        return(&quot;Scottish Terriers average 20 lbs&quot;)\n    elif name in&quot;Border Collie&quot;:\n        return(&quot;a Border Collies average weight is 37 lbs&quot;)\n    elif name in&quot;Toy Poodle&quot;:\n        return(&quot;a toy poodles average weight is 7 lbs&quot;)\n    else:\n        return(&quot;An average dog weights 50 lbs&quot;)\n\nknown_actions = {\n    &quot;calculate&quot;: calculate,\n    &quot;average_dog_weight&quot;: average_dog_weight\n}\n\naction_re = re.compile(&#039;^Action: (\\w+): (.*)$&#039;)   # python regular expression to selection action\ndef query(question, max_turns=5):\n    i = 0\n    bot = Agent(prompt)\n    next_prompt = question\n    while i &lt; max_turns:\n        i += 1\n        result = bot(next_prompt)\n        print(result)\n        actions = [\n            action_re.match(a) \n            for a in result.split(&#039;\\n&#039;) \n            if action_re.match(a)\n        ]\n        if actions:\n            # There is an action to run\n            action, action_input = actions[0].groups()\n            if action notin known_actions:\n                raise Exception(&quot;Unknown action: {}: {}&quot;.format(action, action_input))\n            print(&quot; -- running {} {}&quot;.format(action, action_input))\n            observation = known_actions[action](action_input)\n            print(&quot;Observation:&quot;, observation)\n            next_prompt = &quot;Observation: {}&quot;.format(observation)\n        else:\n            return result\n\nquestion = &quot;&quot;&quot;I have 2 dogs, a border collie and a scottish terrier. \\\nWhat is their combined weight&quot;&quot;&quot;\nquery(question)<\/code><\/pre>\n<h1><strong>\u603b\u7ed3<\/strong><\/h1>\n<p>\u867d\u7136\u672c\u8282\u8bfe\u6211\u4eec\u6784\u5efa\u7684 Agent \u8fd8\u6bd4\u8f83\u57fa\u7840\uff0c\u4f46\u5b83\u5df2\u7ecf\u5b8c\u6574\u5730\u5c55\u793a\u4e86\u4e00\u4e2a\u667a\u80fd\u4f53\u201c\u601d\u8003-\u884c\u52a8-\u89c2\u5bdf-\u518d\u601d\u8003\u201d\u7684\u6838\u5fc3\u95ed\u73af\u6d41\u7a0b\u3002\u968f\u7740\u4efb\u52a1\u590d\u6742\u5ea6\u7684\u63d0\u5347\uff0c\u6211\u4eec\u65e0\u6cd5\u4ec5\u9760\u624b\u5de5\u63a7\u5236\u6d41\u7a0b\u6765\u652f\u6491\u66f4\u5f3a\u5927\u7684 Agent \u7cfb\u7edf\uff0c\u53ef\u80fd\u6211\u4eec\u9700\u8981\u4e00\u4e9b\u6210\u719f\u7684 Agent \u6846\u67b6\u6765\u5b9e\u73b0\u6211\u4eec\u7684\u60f3\u6cd5\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u4e0b\u4e00\u8282\u8bfe\u6211\u4eec\u5c06\u6b63\u5f0f\u8fdb\u5165 <strong>LangGraph \u6846\u67b6\u4e0b\u7684 Agent \u8bbe\u8ba1\u5b9e\u8df5<\/strong>\uff0c\u5b66\u4e60\u5982\u4f55\u901a\u8fc7\u201c\u56fe\u7ed3\u6784\u201d\u4f18\u96c5\u5730\u7ec4\u7ec7 Agent \u7684\u63a8\u7406\u903b\u8f91\u4e0e\u5de5\u5177\u8c03\u7528\uff0c\u6784\u5efa\u66f4\u5f3a\u5927\u3001\u66f4\u53ef\u63a7\u3001\u66f4\u5177\u6269\u5c55\u6027\u7684\u667a\u80fd\u4f53\u3002<strong>\u656c\u8bf7\u671f\u5f85\uff01<\/strong><\/p>\n<p>-- \u5b8c --<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb \u5176\u5b9e\u5bf9\u4e8e AI Agent \u7684\u4ecb\u7ecd\u5df2\u7ecf\u975e\u5e38\u975e\u5e38\u591a\u4e86\uff0c\u7b80\u5355\u6765\u8bf4\uff0cAI Agent \u662f\u4e00\u79cd\u5177\u5907\u201c\u611f\u77e5-\u601d\u8003-\u884c\u52a8\u201d\u80fd\u529b\u7684\u667a\u80fd\u4f53\uff0c\u5b83\u80fd\u63a5   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[286],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9437"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9437"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9437\/revisions"}],"predecessor-version":[{"id":9438,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9437\/revisions\/9438"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9437"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}