mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-03 19:57:51 +00:00
langchain[patch]: make BooleanOutputParser check words not substrings (#20064)
- **Description**: fixes BooleanOutputParser detecting sub-words ("NOW this is likely (YES)" -> `True`, not `AmbiguousError`) - **Issue(s)**: fixes #11408 (follow-up to #17810) - **Dependencies**: None - **GitHub handle**: @casperdcl <!-- if unreviewd after a few days, @-mention one of baskaryan, efriis, eyurtsev, hwchase17 --> - [x] **Add tests and docs**: If you're adding a new integration, please include 1. a test for the integration, preferably unit tests that do not rely on network access, 2. an example notebook showing its use. It lives in `docs/docs/integrations` directory. - [ ] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/ --------- Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
This commit is contained in:
parent
add31f46d0
commit
b972f394c8
@ -1,3 +1,5 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
from langchain_core.output_parsers import BaseOutputParser
|
from langchain_core.output_parsers import BaseOutputParser
|
||||||
|
|
||||||
|
|
||||||
@ -17,22 +19,27 @@ class BooleanOutputParser(BaseOutputParser[bool]):
|
|||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
boolean
|
boolean
|
||||||
|
|
||||||
"""
|
"""
|
||||||
cleaned_upper_text = text.strip().upper()
|
regexp = rf"\b({self.true_val}|{self.false_val})\b"
|
||||||
if (
|
|
||||||
self.true_val.upper() in cleaned_upper_text
|
truthy = {
|
||||||
and self.false_val.upper() in cleaned_upper_text
|
val.upper()
|
||||||
):
|
for val in re.findall(regexp, text, flags=re.IGNORECASE | re.MULTILINE)
|
||||||
|
}
|
||||||
|
if self.true_val.upper() in truthy:
|
||||||
|
if self.false_val.upper() in truthy:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Ambiguous response. Both {self.true_val} and {self.false_val} in "
|
f"Ambiguous response. Both {self.true_val} and {self.false_val} "
|
||||||
f"received: {text}."
|
f"in received: {text}."
|
||||||
)
|
)
|
||||||
elif self.true_val.upper() in cleaned_upper_text:
|
|
||||||
return True
|
return True
|
||||||
elif self.false_val.upper() in cleaned_upper_text:
|
elif self.false_val.upper() in truthy:
|
||||||
|
if self.true_val.upper() in truthy:
|
||||||
|
raise ValueError(
|
||||||
|
f"Ambiguous response. Both {self.true_val} and {self.false_val} "
|
||||||
|
f"in received: {text}."
|
||||||
|
)
|
||||||
return False
|
return False
|
||||||
else:
|
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"BooleanOutputParser expected output value to include either "
|
f"BooleanOutputParser expected output value to include either "
|
||||||
f"{self.true_val} or {self.false_val}. Received {text}."
|
f"{self.true_val} or {self.false_val}. Received {text}."
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
from langchain.output_parsers.boolean import BooleanOutputParser
|
from langchain.output_parsers.boolean import BooleanOutputParser
|
||||||
|
|
||||||
|
|
||||||
@ -24,16 +26,16 @@ def test_boolean_output_parser_parse() -> None:
|
|||||||
result = parser.parse("Not relevant (NO)")
|
result = parser.parse("Not relevant (NO)")
|
||||||
assert result is False
|
assert result is False
|
||||||
|
|
||||||
# Test ambiguous input
|
# Test valid input
|
||||||
try:
|
result = parser.parse("NOW this is relevant (YES)")
|
||||||
parser.parse("yes and no")
|
assert result is True
|
||||||
assert False, "Should have raised ValueError"
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Test invalid input
|
# Test ambiguous input
|
||||||
try:
|
with pytest.raises(ValueError):
|
||||||
parser.parse("INVALID")
|
parser.parse("YES NO")
|
||||||
assert False, "Should have raised ValueError"
|
|
||||||
except ValueError:
|
with pytest.raises(ValueError):
|
||||||
pass
|
parser.parse("NO YES")
|
||||||
|
# Bad input
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
parser.parse("BOOM")
|
||||||
|
Loading…
Reference in New Issue
Block a user