jjyaoao 4 місяців тому
батько
коміт
2f06e74337

+ 6 - 4
code/chapter4/ReAct.py

@@ -73,18 +73,20 @@ class ReActAgent:
         return None
 
     def _parse_output(self, text: str):
-        thought_match = re.search(r"Thought: (.*)", text)
-        action_match = re.search(r"Action: (.*)", text)
+        # Thought: 匹配到 Action: 或文本末尾
+        thought_match = re.search(r"Thought:\s*(.*?)(?=\nAction:|$)", text, re.DOTALL)
+        # Action: 匹配到文本末尾
+        action_match = re.search(r"Action:\s*(.*?)$", text, re.DOTALL)
         thought = thought_match.group(1).strip() if thought_match else None
         action = action_match.group(1).strip() if action_match else None
         return thought, action
 
     def _parse_action(self, action_text: str):
-        match = re.match(r"(\w+)\[(.*)\]", action_text)
+        match = re.match(r"(\w+)\[(.*)\]", action_text, re.DOTALL)
         return (match.group(1), match.group(2)) if match else (None, None)
 
     def _parse_action_input(self, action_text: str):
-        match = re.match(r"\w+\[(.*)\]", action_text)
+        match = re.match(r"\w+\[(.*)\]", action_text, re.DOTALL)
         return match.group(1) if match else ""
 
 if __name__ == '__main__':

+ 9 - 5
docs/chapter4/Chapter4-Building-Classic-Agent-Paradigms.md

@@ -434,16 +434,20 @@ The LLM returns plain text, and we need to precisely extract `Thought` and `Acti
 ```python
 # (These methods are part of the ReActAgent class)
     def _parse_output(self, text: str):
-        """Parse LLM output to extract Thought and Action."""
-        thought_match = re.search(r"Thought: (.*)", text)
-        action_match = re.search(r"Action: (.*)", text)
+        """Parse LLM output to extract Thought and Action.
+        """
+        # Thought: match until Action: or end of text
+        thought_match = re.search(r"Thought:\s*(.*?)(?=\nAction:|$)", text, re.DOTALL)
+        # Action: match until end of text
+        action_match = re.search(r"Action:\s*(.*?)$", text, re.DOTALL)
         thought = thought_match.group(1).strip() if thought_match else None
         action = action_match.group(1).strip() if action_match else None
         return thought, action
 
     def _parse_action(self, action_text: str):
-        """Parse Action string to extract tool name and input."""
-        match = re.match(r"(\w+)\[(.*)\]", action_text)
+        """Parse Action string to extract tool name and input.
+        """
+        match = re.match(r"(\w+)\[(.*)\]", action_text, re.DOTALL)
         if match:
             return match.group(1), match.group(2)
         return None, None

+ 9 - 5
docs/chapter4/第四章 智能体经典范式构建.md

@@ -434,16 +434,20 @@ LLM 返回的是纯文本,我们需要从中精确地提取出`Thought`和`Act
 ```python
 # (这些方法是 ReActAgent 类的一部分)
     def _parse_output(self, text: str):
-        """解析LLM的输出,提取Thought和Action。"""
-        thought_match = re.search(r"Thought: (.*)", text)
-        action_match = re.search(r"Action: (.*)", text)
+        """解析LLM的输出,提取Thought和Action。
+        """
+        # Thought: 匹配到 Action: 或文本末尾
+        thought_match = re.search(r"Thought:\s*(.*?)(?=\nAction:|$)", text, re.DOTALL)
+        # Action: 匹配到文本末尾
+        action_match = re.search(r"Action:\s*(.*?)$", text, re.DOTALL)
         thought = thought_match.group(1).strip() if thought_match else None
         action = action_match.group(1).strip() if action_match else None
         return thought, action
 
     def _parse_action(self, action_text: str):
-        """解析Action字符串,提取工具名称和输入。"""
-        match = re.match(r"(\w+)\[(.*)\]", action_text)
+        """解析Action字符串,提取工具名称和输入。
+        """
+        match = re.match(r"(\w+)\[(.*)\]", action_text, re.DOTALL)
         if match:
             return match.group(1), match.group(2)
         return None, None