Skip to content

Commit

Permalink
langchain[patch]: make BooleanOutputParser check words not substrings
Browse files Browse the repository at this point in the history
- follow-up to #17810
- fixes #11408
  • Loading branch information
casperdcl committed Apr 5, 2024
1 parent ebd24bb commit 932d234
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
34 changes: 19 additions & 15 deletions libs/langchain/langchain/output_parsers/boolean.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

from langchain_core.output_parsers import BaseOutputParser


Expand All @@ -19,24 +21,26 @@ def parse(self, text: str) -> bool:
boolean
"""
cleaned_upper_text = text.strip().upper()
if (
self.true_val.upper() in cleaned_upper_text
and self.false_val.upper() in cleaned_upper_text
):
raise ValueError(
f"Ambiguous response. Both {self.true_val} and {self.false_val} in "
f"received: {text}."
truthy = {
val.upper() for val in re.findall(
rf"\b({self.true_val}|{self.false_val})\b",
text,
flags=re.IGNORECASE | re.MULTILINE
)
elif self.true_val.upper() in cleaned_upper_text:
}
if self.true_val.upper() in truthy:
if self.false_val.upper() in truthy:
raise ValueError(
f"Ambiguous response. Both {self.true_val} and {self.false_val} "
f"in received: {text}."
)
return True
elif self.false_val.upper() in cleaned_upper_text:
elif self.false_val.upper() in truthy:
return False
else:
raise ValueError(
f"BooleanOutputParser expected output value to include either "
f"{self.true_val} or {self.false_val}. Received {text}."
)
raise ValueError(
f"BooleanOutputParser expected output value to include either "
f"{self.true_val} or {self.false_val}. Received {text}."
)

@property
def _type(self) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def test_boolean_output_parser_parse() -> None:
result = parser.parse("Not relevant (NO)")
assert result is False

# Test valid input
result = parser.parse("NOW this is relevant (YES)")
assert result is True

# Test ambiguous input
try:
parser.parse("yes and no")
Expand Down

0 comments on commit 932d234

Please sign in to comment.