Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

langchain: Standardize output_parser.py across all agent types for custom FORMAT_INSTRUCTIONS #17168

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading