From 95b77a52153b10091e480be0b5e7a366d3b3f796 Mon Sep 17 00:00:00 2001 From: Nir Gazit Date: Tue, 20 Jun 2023 08:03:58 +0300 Subject: [PATCH] Fix Custom LLM Agent example (#6429) The `CustomOutputParser` needs to throw `OutputParserException` when it fails to parse the response from the agent, so that the executor can [catch it and retry](https://github.com/hwchase17/langchain/blob/be9371ca8f363bf1c748ac4af7fb4a0d75a365c5/langchain/agents/agent.py#L767) when `handle_parsing_errors=True`. #### Who can review? Tag maintainers/contributors who might be interested: @hwchase17 --- docs/snippets/modules/agents/how_to/custom_llm_agent.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/snippets/modules/agents/how_to/custom_llm_agent.mdx b/docs/snippets/modules/agents/how_to/custom_llm_agent.mdx index ac892816..eebf7429 100644 --- a/docs/snippets/modules/agents/how_to/custom_llm_agent.mdx +++ b/docs/snippets/modules/agents/how_to/custom_llm_agent.mdx @@ -22,7 +22,7 @@ from langchain.agents import Tool, AgentExecutor, LLMSingleActionAgent, AgentOut from langchain.prompts import StringPromptTemplate from langchain import OpenAI, SerpAPIWrapper, LLMChain from typing import List, Union -from langchain.schema import AgentAction, AgentFinish +from langchain.schema import AgentAction, AgentFinish, OutputParserException import re ``` @@ -135,7 +135,7 @@ class CustomOutputParser(AgentOutputParser): 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}`") + raise OutputParserException(f"Could not parse LLM output: `{llm_output}`") action = match.group(1).strip() action_input = match.group(2) # Return the action and action input