| 10 | """Output parser for the ReAct agent.""" |
| 11 | |
| 12 | def parse(self, text: str) -> Union[AgentAction, AgentFinish]: |
| 13 | action_prefix = "Action: " |
| 14 | if not text.strip().split("\n")[-1].startswith(action_prefix): |
| 15 | raise OutputParserException(f"Could not parse LLM Output: {text}") |
| 16 | action_block = text.strip().split("\n")[-1] |
| 17 | |
| 18 | action_str = action_block[len(action_prefix) :] |
| 19 | # Parse out the action and the directive. |
| 20 | re_matches = re.search(r"(.*?)\[(.*?)\]", action_str) |
| 21 | if re_matches is None: |
| 22 | raise OutputParserException( |
| 23 | f"Could not parse action directive: {action_str}" |
| 24 | ) |
| 25 | action, action_input = re_matches.group(1), re_matches.group(2) |
| 26 | if action == "Finish": |
| 27 | return AgentFinish({"output": action_input}, text) |
| 28 | else: |
| 29 | return AgentAction(action, action_input, text) |
| 30 | |
| 31 | @property |
| 32 | def _type(self) -> str: |