Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions code/chapter1/FirstAgentTest.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,8 @@
"def parse_action(action_str):\n",
" \"\"\"解析行动字符串\"\"\"\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",
" finish_match = re.search(r\"\\w+\\[(.+)\\]\", action_str, re.DOTALL)\n",
" return \"finish\", {\"answer\": finish_match.group(1).strip() if finish_match else \"任务完成\"}\n",
" \n",
" tool_name_match = re.search(r\"(\\w+)\\(\", action_str)\n",
" if not tool_name_match:\n",
Expand Down Expand Up @@ -664,4 +662,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
3 changes: 2 additions & 1 deletion code/chapter1/FirstAgentTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ def generate(self, prompt: str, system_prompt: str) -> str:
action_str = action_match.group(1).strip()

if action_str.startswith("Finish"):
final_answer = re.match(r"Finish\[(.*)\]", action_str).group(1)
finish_match = re.search(r"Finish\[(.+)\]", action_str, re.DOTALL)
final_answer = finish_match.group(1).strip() if finish_match else action_str
print(f"任务完成,最终答案: {final_answer}")
break

Expand Down