mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-23 15:19:33 +00:00
Enhance regex of structured_chat agents' output parser (#8965)
Current regex only extracts agent's action between '` ``` ``` `', this commit will extract action between both '` ```json ``` `' and '` ``` ``` `' This is very similar to #7511 Co-authored-by: zjl <junlinzhou@yzbigdata.com>
This commit is contained in:
parent
16bd328aab
commit
cb5fb751e9
@ -19,12 +19,14 @@ logger = logging.getLogger(__name__)
|
|||||||
class StructuredChatOutputParser(AgentOutputParser):
|
class StructuredChatOutputParser(AgentOutputParser):
|
||||||
"""Output parser for the structured chat agent."""
|
"""Output parser for the structured chat agent."""
|
||||||
|
|
||||||
|
pattern = re.compile(r"```(?:json)?\n(.*?)```", re.DOTALL)
|
||||||
|
|
||||||
def get_format_instructions(self) -> str:
|
def get_format_instructions(self) -> str:
|
||||||
return FORMAT_INSTRUCTIONS
|
return FORMAT_INSTRUCTIONS
|
||||||
|
|
||||||
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
|
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
|
||||||
try:
|
try:
|
||||||
action_match = re.search(r"```(.*?)```?", text, re.DOTALL)
|
action_match = self.pattern.search(text)
|
||||||
if action_match is not None:
|
if action_match is not None:
|
||||||
response = json.loads(action_match.group(1).strip(), strict=False)
|
response = json.loads(action_match.group(1).strip(), strict=False)
|
||||||
if isinstance(response, list):
|
if isinstance(response, list):
|
||||||
|
@ -0,0 +1,47 @@
|
|||||||
|
"""Unittests for langchain.agents.chat package."""
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
|
from langchain.agents.structured_chat.output_parser import StructuredChatOutputParser
|
||||||
|
from langchain.schema import AgentAction
|
||||||
|
|
||||||
|
output_parser = StructuredChatOutputParser()
|
||||||
|
|
||||||
|
|
||||||
|
def get_action_and_input(text: str) -> Tuple[str, str]:
|
||||||
|
output = output_parser.parse(text)
|
||||||
|
if isinstance(output, AgentAction):
|
||||||
|
return output.tool, str(output.tool_input)
|
||||||
|
else:
|
||||||
|
return "Final Answer", output.return_values["output"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_with_language() -> None:
|
||||||
|
llm_output = """I can use the `foo` tool to achieve the goal.
|
||||||
|
|
||||||
|
Action:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"action": "foo",
|
||||||
|
"action_input": "bar"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
action, action_input = get_action_and_input(llm_output)
|
||||||
|
assert action == "foo"
|
||||||
|
assert action_input == "bar"
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_without_language() -> None:
|
||||||
|
llm_output = """I can use the `foo` tool to achieve the goal.
|
||||||
|
|
||||||
|
Action:
|
||||||
|
```
|
||||||
|
{
|
||||||
|
"action": "foo",
|
||||||
|
"action_input": "bar"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
action, action_input = get_action_and_input(llm_output)
|
||||||
|
assert action == "foo"
|
||||||
|
assert action_input == "bar"
|
Loading…
Reference in New Issue
Block a user