From 1e655d5ffda799c937557274d6dec2a0f6c771ce Mon Sep 17 00:00:00 2001 From: Azam Iftikhar Date: Sun, 16 Apr 2023 21:46:50 +0530 Subject: [PATCH] Fixed Regular expression (#2933) ### https://github.com/hwchase17/langchain/issues/2898 Instead of `"Action" and "Action Input"` keywords, we are getting `"Action 1" and "Action 1 Input" or "Action Input 1" ` from **gpt-3.5-turbo** Updated the Regular expression to handle all these cases Attaching the screenshot of the result from the updated Regular expression. Screenshot 2023-04-16 at 1 39 00 AM --- .../agents/agents/custom_agent_with_tool_retrieval.ipynb | 2 +- docs/modules/agents/agents/custom_llm_agent.ipynb | 2 +- docs/modules/agents/agents/custom_llm_chat_agent.ipynb | 2 +- docs/use_cases/agents/custom_agent_with_plugin_retrieval.ipynb | 2 +- langchain/agents/mrkl/base.py | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/modules/agents/agents/custom_agent_with_tool_retrieval.ipynb b/docs/modules/agents/agents/custom_agent_with_tool_retrieval.ipynb index b445ec95..c81cc19e 100644 --- a/docs/modules/agents/agents/custom_agent_with_tool_retrieval.ipynb +++ b/docs/modules/agents/agents/custom_agent_with_tool_retrieval.ipynb @@ -315,7 +315,7 @@ " log=llm_output,\n", " )\n", " # Parse out the action and action input\n", - " regex = r\"Action: (.*?)[\\n]*Action Input:[\\s]*(.*)\"\n", + " regex = r\"Action\\s*\\d*\\s*:(.*?)\\nAction\\s*\\d*\\s*Input\\s*\\d*\\s*:[\\s]*(.*)\"\n", " match = re.search(regex, llm_output, re.DOTALL)\n", " if not match:\n", " raise ValueError(f\"Could not parse LLM output: `{llm_output}`\")\n", diff --git a/docs/modules/agents/agents/custom_llm_agent.ipynb b/docs/modules/agents/agents/custom_llm_agent.ipynb index cd2092d7..5b9da883 100644 --- a/docs/modules/agents/agents/custom_llm_agent.ipynb +++ b/docs/modules/agents/agents/custom_llm_agent.ipynb @@ -204,7 +204,7 @@ " log=llm_output,\n", " )\n", " # Parse out the action and action input\n", - " regex = r\"Action: (.*?)[\\n]*Action Input:[\\s]*(.*)\"\n", + " regex = r\"Action\\s*\\d*\\s*:(.*?)\\nAction\\s*\\d*\\s*Input\\s*\\d*\\s*:[\\s]*(.*)\"\n", " match = re.search(regex, llm_output, re.DOTALL)\n", " if not match:\n", " raise ValueError(f\"Could not parse LLM output: `{llm_output}`\")\n", diff --git a/docs/modules/agents/agents/custom_llm_chat_agent.ipynb b/docs/modules/agents/agents/custom_llm_chat_agent.ipynb index c4b5e2d6..571c928d 100644 --- a/docs/modules/agents/agents/custom_llm_chat_agent.ipynb +++ b/docs/modules/agents/agents/custom_llm_chat_agent.ipynb @@ -206,7 +206,7 @@ " log=llm_output,\n", " )\n", " # Parse out the action and action input\n", - " regex = r\"Action: (.*?)[\\n]*Action Input:[\\s]*(.*)\"\n", + " regex = r\"Action\\s*\\d*\\s*:(.*?)\\nAction\\s*\\d*\\s*Input\\s*\\d*\\s*:[\\s]*(.*)\"\n", " match = re.search(regex, llm_output, re.DOTALL)\n", " if not match:\n", " raise ValueError(f\"Could not parse LLM output: `{llm_output}`\")\n", diff --git a/docs/use_cases/agents/custom_agent_with_plugin_retrieval.ipynb b/docs/use_cases/agents/custom_agent_with_plugin_retrieval.ipynb index cba8f69f..c40605e0 100644 --- a/docs/use_cases/agents/custom_agent_with_plugin_retrieval.ipynb +++ b/docs/use_cases/agents/custom_agent_with_plugin_retrieval.ipynb @@ -375,7 +375,7 @@ " log=llm_output,\n", " )\n", " # Parse out the action and action input\n", - " regex = r\"Action: (.*?)[\\n]*Action Input:[\\s]*(.*)\"\n", + " regex = r\"Action\\s*\\d*\\s*:(.*?)\\nAction\\s*\\d*\\s*Input\\s*\\d*\\s*:[\\s]*(.*)\"\n", " match = re.search(regex, llm_output, re.DOTALL)\n", " if not match:\n", " raise ValueError(f\"Could not parse LLM output: `{llm_output}`\")\n", diff --git a/langchain/agents/mrkl/base.py b/langchain/agents/mrkl/base.py index 576c1bd8..c44c477b 100644 --- a/langchain/agents/mrkl/base.py +++ b/langchain/agents/mrkl/base.py @@ -42,7 +42,7 @@ def get_action_and_input(llm_output: str) -> Tuple[str, str]: if FINAL_ANSWER_ACTION in llm_output: return "Final Answer", llm_output.split(FINAL_ANSWER_ACTION)[-1].strip() # \s matches against tab/newline/whitespace - regex = r"Action: (.*?)[\n]*Action Input:[\s]*(.*)" + regex = r"Action\s*\d*\s*:(.*?)\nAction\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)" match = re.search(regex, llm_output, re.DOTALL) if not match: raise ValueError(f"Could not parse LLM output: `{llm_output}`")