langchain/tests/unit_tests/agents
Nathan Azrak 26ec845921
Raise an exception in MKRL and Chat Output Parsers if parsing text which contains both an action and a final answer (#5609)
Raises exception if OutputParsers receive a response with both a valid
action and a final answer

Currently, if an OutputParser receives a response which includes both an
action and a final answer, they return a FinalAnswer object. This allows
the parser to accept responses which propose an action and hallucinate
an answer without the action being parsed or taken by the agent.

This PR changes the logic to:
1. store a variable checking whether a response contains the
`FINAL_ANSWER_ACTION` (this is the easier condition to check).
2. store a variable checking whether the response contains a valid
action
3. if both are present, raise a new exception stating that both are
present
4. if an action is present, return an AgentAction
5. if an answer is present, return an AgentAnswer
6. if neither is present, raise the relevant exception based around the
action format (these have been kept consistent with the prior exception
messages)

Disclaimer:
* Existing mock data included strings which did include an action and an
answer. This might indicate that prioritising returning AgentAnswer was
always correct, and I am patching out desired behaviour? @hwchase17 to
advice. Curious if there are allowed cases where this is not
hallucinating, and we do want the LLM to output an action which isn't
taken.
* I have not passed `send_to_llm` through this new exception

Fixes #5601 

## Who can review?

Community members can review the PR once tests pass. Tag
maintainers/contributors who might be interested:

@hwchase17 - project lead
@vowelparrot
2023-06-04 14:40:49 -07:00
..
__init__.py (WIP) agents (#171) 2022-11-22 06:16:26 -08:00
test_agent.py Callbacks Refactor [base] (#3256) 2023-04-30 11:14:09 -07:00
test_mrkl.py Raise an exception in MKRL and Chat Output Parsers if parsing text which contains both an action and a final answer (#5609) 2023-06-04 14:40:49 -07:00
test_public_api.py Add Spark SQL support (#4602) (#4956) 2023-05-18 20:53:08 -07:00
test_react.py Callbacks Refactor [base] (#3256) 2023-04-30 11:14:09 -07:00
test_serialization.py agent serialization (#4642) 2023-05-13 21:47:10 -07:00
test_sql.py Harrison/fix create sql agent (#2870) 2023-04-13 22:07:58 -07:00
test_tools.py Pass Callbacks through load_tools (#4298) 2023-05-08 08:44:26 -07:00
test_types.py [annotation improvement] Make AgentType->Class Conversion More Scalable (#3749) 2023-04-28 21:17:28 -07:00