From b9b625a70d20ee477f88c08ec28b9d60fd592776 Mon Sep 17 00:00:00 2001 From: "Brandon Hancock (bhancock_ai)" <109994880+bhancockio@users.noreply.github.com> Date: Wed, 26 Feb 2025 14:51:46 -0500 Subject: [PATCH] Improve extract thought (#2223) Co-authored-by: Lorenze Jay <63378463+lorenzejay@users.noreply.github.com> --- src/crewai/agents/parser.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/crewai/agents/parser.py b/src/crewai/agents/parser.py index 71444a20a..1bda4df5c 100644 --- a/src/crewai/agents/parser.py +++ b/src/crewai/agents/parser.py @@ -124,14 +124,15 @@ class CrewAgentParser: ) def _extract_thought(self, text: str) -> str: - regex = r"(.*?)(?:\n\nAction|\n\nFinal Answer)" - thought_match = re.search(regex, text, re.DOTALL) - if thought_match: - thought = thought_match.group(1).strip() - # Remove any triple backticks from the thought string - thought = thought.replace("```", "").strip() - return thought - return "" + thought_index = text.find("\n\nAction") + if thought_index == -1: + thought_index = text.find("\n\nFinal Answer") + if thought_index == -1: + return "" + thought = text[:thought_index].strip() + # Remove any triple backticks from the thought string + thought = thought.replace("```", "").strip() + return thought def _clean_action(self, text: str) -> str: """Clean action string by removing non-essential formatting characters."""