Fix Custom LLM Agent example (#6429)

<!--
Thank you for contributing to LangChain! Your PR will appear in our
release under the title you set. Please make sure it highlights your
valuable contribution.

Replace this with a description of the change, the issue it fixes (if
applicable), and relevant context. List any dependencies required for
this change.

After you're done, someone will review your PR. They may suggest
improvements. If no one reviews your PR within a few days, feel free to
@-mention the same people again, as notifications can get lost.

Finally, we'd love to show appreciation for your contribution - if you'd
like us to shout you out on Twitter, please also include your handle!
-->

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](be9371ca8f/langchain/agents/agent.py (L767))
when `handle_parsing_errors=True`.

<!-- Remove if not applicable -->

#### Who can review?

Tag maintainers/contributors who might be interested: @hwchase17

<!-- For a quicker response, figure out the right person to tag with @

  @hwchase17 - project lead

  Tracing / Callbacks
  - @agola11

  Async
  - @agola11

  DataLoaders
  - @eyurtsev

  Models
  - @hwchase17
  - @agola11

  Agents / Tools / Toolkits
  - @hwchase17

  VectorStores / Retrievers / Memory
  - @dev2049

 -->
This commit is contained in:
Nir Gazit 2023-06-20 08:03:58 +03:00 committed by GitHub
parent b697bbb5b5
commit 95b77a5215
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,7 +22,7 @@ from langchain.agents import Tool, AgentExecutor, LLMSingleActionAgent, AgentOut
from langchain.prompts import StringPromptTemplate from langchain.prompts import StringPromptTemplate
from langchain import OpenAI, SerpAPIWrapper, LLMChain from langchain import OpenAI, SerpAPIWrapper, LLMChain
from typing import List, Union from typing import List, Union
from langchain.schema import AgentAction, AgentFinish from langchain.schema import AgentAction, AgentFinish, OutputParserException
import re import re
``` ```
@ -135,7 +135,7 @@ class CustomOutputParser(AgentOutputParser):
regex = r"Action\s*\d*\s*:(.*?)\nAction\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)" regex = r"Action\s*\d*\s*:(.*?)\nAction\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)"
match = re.search(regex, llm_output, re.DOTALL) match = re.search(regex, llm_output, re.DOTALL)
if not match: 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 = match.group(1).strip()
action_input = match.group(2) action_input = match.group(2)
# Return the action and action input # Return the action and action input