mirror of
https://github.com/hwchase17/langchain.git
synced 2025-05-31 20:19:43 +00:00
convert the parameter 'text' to uppercase in the function 'parse' of the class BooleanOutputParser (#5397)
when the LLMs output 'yes|no',BooleanOutputParser can parse it to 'True|False', fix the ValueError in parse(). <!-- when use the BooleanOutputParser in the chain_filter.py, the LLMs output 'yes|no',the function 'parse' will throw ValueError。 --> Fixes # (issue) #5396 https://github.com/hwchase17/langchain/issues/5396 --------- Co-authored-by: gaofeng27692 <gaofeng27692@hundsun.com>
This commit is contained in:
parent
199cc700a3
commit
e31705b5ab
@ -16,12 +16,12 @@ class BooleanOutputParser(BaseOutputParser[bool]):
|
||||
|
||||
"""
|
||||
cleaned_text = text.strip()
|
||||
if cleaned_text not in (self.true_val, self.false_val):
|
||||
if cleaned_text.upper() not in (self.true_val.upper(), self.false_val.upper()):
|
||||
raise ValueError(
|
||||
f"BooleanOutputParser expected output value to either be "
|
||||
f"{self.true_val} or {self.false_val}. Received {cleaned_text}."
|
||||
)
|
||||
return cleaned_text == self.true_val
|
||||
return cleaned_text.upper() == self.true_val.upper()
|
||||
|
||||
@property
|
||||
def _type(self) -> str:
|
||||
|
@ -12,6 +12,14 @@ def test_boolean_output_parser_parse() -> None:
|
||||
result = parser.parse("NO")
|
||||
assert result is False
|
||||
|
||||
# Test valid input
|
||||
result = parser.parse("yes")
|
||||
assert result is True
|
||||
|
||||
# Test valid input
|
||||
result = parser.parse("no")
|
||||
assert result is False
|
||||
|
||||
# Test invalid input
|
||||
try:
|
||||
parser.parse("INVALID")
|
||||
|
Loading…
Reference in New Issue
Block a user