-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(product-assistant): better failover for the ReAct agent (#25903)
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
124d166
commit 1fbc799
Showing
5 changed files
with
281 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from langchain_core.messages import AIMessage as LangchainAIMessage | ||
|
||
from ee.hogai.trends.parsers import ( | ||
ReActParserMalformedJsonException, | ||
ReActParserMissingActionException, | ||
parse_react_agent_output, | ||
) | ||
from posthog.test.base import BaseTest | ||
|
||
|
||
class TestParsers(BaseTest): | ||
def test_parse_react_agent_output(self): | ||
res = parse_react_agent_output( | ||
LangchainAIMessage( | ||
content=""" | ||
Some thoughts... | ||
Action: | ||
```json | ||
{"action": "action_name", "action_input": "action_input"} | ||
``` | ||
""" | ||
) | ||
) | ||
self.assertEqual(res.tool, "action_name") | ||
self.assertEqual(res.tool_input, "action_input") | ||
|
||
res = parse_react_agent_output( | ||
LangchainAIMessage( | ||
content=""" | ||
Some thoughts... | ||
Action: | ||
``` | ||
{"action": "tool", "action_input": {"key": "value"}} | ||
``` | ||
""" | ||
) | ||
) | ||
self.assertEqual(res.tool, "tool") | ||
self.assertEqual(res.tool_input, {"key": "value"}) | ||
|
||
self.assertRaises( | ||
ReActParserMissingActionException, parse_react_agent_output, LangchainAIMessage(content="Some thoughts...") | ||
) | ||
self.assertRaises( | ||
ReActParserMalformedJsonException, | ||
parse_react_agent_output, | ||
LangchainAIMessage(content="Some thoughts...\nAction: abc"), | ||
) | ||
self.assertRaises( | ||
ReActParserMalformedJsonException, | ||
parse_react_agent_output, | ||
LangchainAIMessage(content="Some thoughts...\nAction:"), | ||
) | ||
self.assertRaises( | ||
ReActParserMalformedJsonException, | ||
parse_react_agent_output, | ||
LangchainAIMessage(content="Some thoughts...\nAction: {}"), | ||
) | ||
self.assertRaises( | ||
ReActParserMalformedJsonException, | ||
parse_react_agent_output, | ||
LangchainAIMessage(content="Some thoughts...\nAction:\n```\n{}\n```"), | ||
) | ||
self.assertRaises( | ||
ReActParserMalformedJsonException, | ||
parse_react_agent_output, | ||
LangchainAIMessage(content="Some thoughts...\nAction:\n```\n{not a json}\n```"), | ||
) | ||
self.assertRaises( | ||
ReActParserMalformedJsonException, | ||
parse_react_agent_output, | ||
LangchainAIMessage(content='Some thoughts...\nAction:\n```\n{"action":"tool"}\n```'), | ||
) | ||
self.assertRaises( | ||
ReActParserMalformedJsonException, | ||
parse_react_agent_output, | ||
LangchainAIMessage(content='Some thoughts...\nAction:\n```\n{"action_input":"input"}\n```'), | ||
) |