From 95720adff5cd45321fa5de42f303d0b51f218253 Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Tue, 17 Jan 2023 22:47:15 -0800 Subject: [PATCH] Add documentation for custom prompts for Agents (#631) (#640) - Added a comment interpreting regex for `ZeroShotAgent` - Added a note to the `Custom Agent` notebook Co-authored-by: Sam Ching --- docs/modules/agents/examples/custom_agent.ipynb | 17 ++++++++++++++--- langchain/agents/mrkl/base.py | 8 +++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/docs/modules/agents/examples/custom_agent.ipynb b/docs/modules/agents/examples/custom_agent.ipynb index 3b7bcb8b..780f2b28 100644 --- a/docs/modules/agents/examples/custom_agent.ipynb +++ b/docs/modules/agents/examples/custom_agent.ipynb @@ -133,6 +133,17 @@ "print(prompt.template)" ] }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "5e028e6d", + "metadata": {}, + "source": [ + "Note that we are able to feed agents a self-defined prompt template, i.e. not restricted to the prompt generated by the `create_prompt` function, assuming it meets the agent's requirements. \n", + "\n", + "For example, for `ZeroShotAgent`, we will need to ensure that it meets the following requirements. There should a string starting with \"Action:\" and a following string starting with \"Action Input:\", and both should be separated by a newline.\n" + ] + }, { "cell_type": "code", "execution_count": 16, @@ -350,7 +361,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3.9.0 64-bit ('llm-env')", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -364,11 +375,11 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.0" + "version": "3.8.12 (default, Feb 15 2022, 17:41:09) \n[Clang 12.0.5 (clang-1205.0.22.11)]" }, "vscode": { "interpreter": { - "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103" + "hash": "18784188d7ecd866c0586ac068b02361a6896dc3a29b64f5cc957f09c590acef" } } }, diff --git a/langchain/agents/mrkl/base.py b/langchain/agents/mrkl/base.py index 2a7e264c..76af6dc7 100644 --- a/langchain/agents/mrkl/base.py +++ b/langchain/agents/mrkl/base.py @@ -28,7 +28,13 @@ class ChainConfig(NamedTuple): def get_action_and_input(llm_output: str) -> Tuple[str, str]: - """Parse out the action and input from the LLM output.""" + """Parse out the action and input from the LLM output. + + Note: if you're specifying a custom prompt for the ZeroShotAgent, + you will need to ensure that it meets the following Regex requirements. + The string starting with "Action:" and the following string starting + with "Action Input:" should be separated by a newline. + """ if FINAL_ANSWER_ACTION in llm_output: return "Final Answer", llm_output.split(FINAL_ANSWER_ACTION)[-1].strip() regex = r"Action: (.*?)\nAction Input: (.*)"