Skip to content

Commit

Permalink
langchain: Standardize output_parser.py across all agent types for …
Browse files Browse the repository at this point in the history
…custom `FORMAT_INSTRUCTIONS` (#17168)

- **Description:** 
This PR standardizes the `output_parser.py` file across all agent types
to ensure a uniform parsing mechanism is implemented. It introduces a
cohesive structure and common interface for output parsing, facilitating
easier modifications and extensions by users. The standardized approach
enhances maintainability and scalability of the codebase by providing a
consistent pattern for output parsing, which can be easily understood
and utilized across different agent types.

This PR builds upon the foundation set by a previously merged PR, which
focused exclusively on standardizing the `output_parser.py` for the
`conversational_agent` ([PR
#16945](#16945)). With
this new update, I extend the standardization efforts to encompass
`output_parser.py` files across all agent types. This enhancement not
only unifies the parsing mechanism across the board but also introduces
the flexibility for users to incorporate custom `FORMAT_INSTRUCTIONS`.

  - **Issue:** 
#10721
#4044

  - **Dependencies:**
No new dependencies required for this change

  - **Twitter handle:**
With my github user is enough. Thanks

I hope you accept my PR.
  • Loading branch information
hdnh2006 authored Feb 7, 2024
1 parent 1cf5a58 commit 2281f00
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
6 changes: 5 additions & 1 deletion libs/langchain/langchain/agents/chat/output_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
class ChatOutputParser(AgentOutputParser):
"""Output parser for the chat agent."""

format_instructions: str = FORMAT_INSTRUCTIONS
"""Default formatting instructions"""

pattern = re.compile(r"^.*?`{3}(?:json)?\n(.*?)`{3}.*?$", re.DOTALL)
"""Regex pattern to parse the output."""

def get_format_instructions(self) -> str:
return FORMAT_INSTRUCTIONS
"""Returns formatting instructions for the given output parser."""
return self.format_instructions

def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
includes_answer = FINAL_ANSWER_ACTION in text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ class ConvoOutputParser(AgentOutputParser):
ai_prefix: str = "AI"
"""Prefix to use before AI output."""

format_instructions: str = FORMAT_INSTRUCTIONS
"""Default formatting instructions"""

def get_format_instructions(self) -> str:
return FORMAT_INSTRUCTIONS
"""Returns formatting instructions for the given output parser."""
return self.format_instructions

def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
if f"{self.ai_prefix}:" in text:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ConvoOutputParser(AgentOutputParser):
"""Output parser for the conversational agent."""

format_instructions: str = FORMAT_INSTRUCTIONS
"""Default formatting instructions"""

def get_format_instructions(self) -> str:
"""Returns formatting instructions for the given output parser."""
Expand Down
6 changes: 5 additions & 1 deletion libs/langchain/langchain/agents/mrkl/output_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@
class MRKLOutputParser(AgentOutputParser):
"""MRKL Output parser for the chat agent."""

format_instructions: str = FORMAT_INSTRUCTIONS
"""Default formatting instructions"""

def get_format_instructions(self) -> str:
return FORMAT_INSTRUCTIONS
"""Returns formatting instructions for the given output parser."""
return self.format_instructions

def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
includes_answer = FINAL_ANSWER_ACTION in text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@
class StructuredChatOutputParser(AgentOutputParser):
"""Output parser for the structured chat agent."""

format_instructions: str = FORMAT_INSTRUCTIONS
"""Default formatting instructions"""

pattern = re.compile(r"```(?:json\s+)?(\W.*?)```", re.DOTALL)
"""Regex pattern to parse the output."""

def get_format_instructions(self) -> str:
return FORMAT_INSTRUCTIONS
"""Returns formatting instructions for the given output parser."""
return self.format_instructions

def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
try:
Expand Down

0 comments on commit 2281f00

Please sign in to comment.