Просмотр исходного кода

docs(agent): 更新智能体任务完成格式说明

- 将 finish(answer="...") 格式更新为 Finish[最终答案] 格式
- 修改正则表达式匹配模式以适应新的 Finish 语法
- 统一文档和代码中的任务完成标记格式
jianuo 5 месяцев назад
Родитель
Сommit
896feb157f

+ 3 - 3
code/chapter1/FirstAgentTest.ipynb

@@ -39,7 +39,7 @@
     "Action: [这里是你要调用的工具,格式为 function_name(arg_name=\"arg_value\")]\n",
     "\n",
     "# 任务完成:\n",
-    "当你收集到足够的信息,能够回答用户的最终问题时,你必须在`Action:`字段后使用 `finish(answer=\"...\")` 来输出最终答案。\n",
+    "当你收集到足够的信息,能够回答用户的最终问题时,你必须在`Action:`字段后使用 `Finish[最终答案]` 来输出最终答案。\n",
     "\n",
     "请开始吧!\n",
     "\"\"\""
@@ -244,8 +244,8 @@
     "\n",
     "def parse_action(action_str):\n",
     "    \"\"\"解析行动字符串\"\"\"\n",
-    "    if action_str.startswith(\"finish\"):\n",
-    "        match = re.search(r'finish\\(answer=\"(.*)\"\\)', action_str)\n",
+    "    if action_str.startswith(\"Finish\"):\n",
+    "        match = re.match(r\"\\w+\\[(.*)\\]\", action_str)\n",
     "        if match:\n",
     "            return \"finish\", {\"answer\": match.group(1)}\n",
     "        return \"finish\", {\"answer\": \"任务完成\"}\n",

+ 3 - 3
code/chapter1/FirstAgentTest.py

@@ -11,7 +11,7 @@ Thought: [这里是你的思考过程和下一步计划]
 Action: [这里是你要调用的工具,格式为 function_name(arg_name="arg_value")]
 
 # 任务完成:
-当你收集到足够的信息,能够回答用户的最终问题时,你必须在`Action:`字段后使用 `finish(answer="...")` 来输出最终答案。
+当你收集到足够的信息,能够回答用户的最终问题时,你必须在`Action:`字段后使用 `Finish[最终答案]` 来输出最终答案。
 
 请开始吧!
 """
@@ -178,8 +178,8 @@ for i in range(5): # 设置最大循环次数
         break
     action_str = action_match.group(1).strip()
 
-    if action_str.startswith("finish"):
-        final_answer = re.search(r'finish\(answer="(.*)"\)', action_str).group(1)
+    if action_str.startswith("Finish"):
+        final_answer = re.match(r"Finish\[(.*)\]", action_str).group(1)
         print(f"任务完成,最终答案: {final_answer}")
         break
     

+ 1 - 1
code/chapter4/ReAct.py

@@ -53,7 +53,7 @@ class ReActAgent:
             
             if action.startswith("Finish"):
                 # 如果是Finish指令,提取最终答案并结束
-                final_answer = re.match(r"Finish\[(.*)\]", action).group(1)
+                final_answer = self._parse_action_input(action)
                 print(f"🎉 最终答案: {final_answer}")
                 return final_answer
             

+ 4 - 4
docs/chapter1/Chapter1-Introduction-to-Agents.md

@@ -243,7 +243,7 @@ Thought: [Here is your thinking process and next step plan]
 Action: [Here is the tool you want to call, in the format function_name(arg_name="arg_value")]
 
 # Task Completion:
-When you have collected enough information to answer the user's final question, you must use `finish(answer="...")` after the Action: field to output the final answer.
+When you have collected enough information to answer the user's final question, you must use `Finish[final answer]` after the Action: field to output the final answer.
 
 Let's begin!
 """
@@ -435,8 +435,8 @@ for i in range(5): # Set maximum number of loops
         break
     action_str = action_match.group(1).strip()
 
-    if action_str.startswith("finish"):
-        final_answer = re.search(r'finish\(answer="(.*)"\)', action_str).group(1)
+    if action_str.startswith("Finish"):
+        final_answer = re.match(r"Finish\[(.*)\]", action_str).group(1)
         print(f"Task completed, final answer: {final_answer}")
         break
 
@@ -490,7 +490,7 @@ Calling large language model...
 Large language model responded successfully.
 Model output:
 Thought: I have obtained two attraction suggestions suitable for sunny days, now I can provide a satisfactory response to the user based on this information.
-Action: finish(answer="Today's weather in Beijing is sunny with a temperature of 26 degrees Celsius, very suitable for outdoor activities. I recommend you visit the Summer Palace to enjoy the beautiful lake views and ancient architecture, or go to the Great Wall to experience its spectacular scenery and profound historical significance. Hope you have a pleasant trip!")
+Action: Finish[Today's weather in Beijing is sunny with a temperature of 26 degrees Celsius, very suitable for outdoor activities. I recommend you visit the Summer Palace to enjoy the beautiful lake views and ancient architecture, or go to the Great Wall to experience its spectacular scenery and profound historical significance. Hope you have a pleasant trip!]
 
 Task completed, final answer: Today's weather in Beijing is sunny with a temperature of 26 degrees Celsius, very suitable for outdoor activities. I recommend you visit the Summer Palace to enjoy the beautiful lake views and ancient architecture, or go to the Great Wall to experience its spectacular scenery and profound historical significance. Hope you have a pleasant trip!
 ```

+ 4 - 5
docs/chapter1/第一章 初识智能体.md

@@ -247,7 +247,7 @@ Thought: [这里是你的思考过程和下一步计划]
 Action: [这里是你要调用的工具,格式为 function_name(arg_name="arg_value")]
 
 # 任务完成:
-当你收集到足够的信息,能够回答用户的最终问题时,你必须在`Action:`字段后使用 `finish(answer="...")` 来输出最终答案。
+当你收集到足够的信息,能够回答用户的最终问题时,你必须在`Action:`字段后使用 `Finish[最终答案]` 来输出最终答案。
 
 请开始吧!
 """
@@ -440,8 +440,8 @@ for i in range(5): # 设置最大循环次数
         break
     action_str = action_match.group(1).strip()
 
-    if action_str.startswith("finish"):
-        final_answer = re.search(r'finish\(answer="(.*)"\)', action_str).group(1)
+    if action_str.startswith("Finish"):
+        final_answer = re.match(r"Finish\[(.*)\]", action_str).group(1)
         print(f"任务完成,最终答案: {final_answer}")
         break
     
@@ -495,8 +495,7 @@ Observation: 北京在晴天最值得去的旅游景点是颐和园,因其美
 大语言模型响应成功。
 模型输出:
 Thought: 已经获得了两个适合晴天游览的景点建议,现在可以根据这些信息给用户提供满意的答复。
-Action: finish(answer="今天北京的天气是晴朗的,气温26摄氏度,非常适合外出游玩。我推荐您去颐和园欣赏美丽的湖景和古建筑,或者前往长城体验其壮观的景观和深厚的历史意义。希望您有一个愉快的旅行!
-")
+Action: Finish[今天北京的天气是晴朗的,气温26摄氏度,非常适合外出游玩。我推荐您去颐和园欣赏美丽的湖景和古建筑,或者前往长城体验其壮观的景观和深厚的历史意义。希望您有一个愉快的旅行!]
 
 任务完成,最终答案: 今天北京的天气是晴朗的,气温26摄氏度,非常适合外出游玩。我推荐您去颐和园欣赏美丽的湖景和古建筑,或者前往长城体验其壮观的景观和深厚的历史意义。希望您有一个愉快的旅行!
 ```