forked from Archives/langchain
parent
dfb81c969f
commit
4cc18d6c2a
@ -0,0 +1,35 @@
|
||||
"""Handle chained inputs."""
|
||||
from typing import Optional
|
||||
|
||||
_COLOR_MAPPING = {"blue": 104, "yellow": 103, "red": 101, "green": 102}
|
||||
|
||||
|
||||
def print_text(text: str, color: Optional[str] = None) -> None:
|
||||
"""Print text with highlighting and no end characters."""
|
||||
if color is None:
|
||||
print(text, end="")
|
||||
else:
|
||||
color_str = _COLOR_MAPPING[color]
|
||||
print(f"\x1b[{color_str}m{text}\x1b[0m", end="")
|
||||
|
||||
|
||||
class ChainedInput:
|
||||
"""Class for working with input that is the result of chains."""
|
||||
|
||||
def __init__(self, text: str, verbose: bool = False):
|
||||
"""Initialize with verbose flag and initial text."""
|
||||
self.verbose = verbose
|
||||
if self.verbose:
|
||||
print_text(text, None)
|
||||
self._input = text
|
||||
|
||||
def add(self, text: str, color: Optional[str] = None) -> None:
|
||||
"""Add text to input, print if in verbose mode."""
|
||||
if self.verbose:
|
||||
print_text(text, color)
|
||||
self._input += text
|
||||
|
||||
@property
|
||||
def input(self) -> str:
|
||||
"""Return the accumulated input."""
|
||||
return self._input
|
@ -0,0 +1,52 @@
|
||||
"""Test input manipulating logic."""
|
||||
|
||||
import sys
|
||||
from io import StringIO
|
||||
|
||||
from langchain.input import ChainedInput
|
||||
|
||||
|
||||
def test_chained_input_not_verbose() -> None:
|
||||
"""Test chained input logic."""
|
||||
old_stdout = sys.stdout
|
||||
sys.stdout = mystdout = StringIO()
|
||||
chained_input = ChainedInput("foo")
|
||||
sys.stdout = old_stdout
|
||||
output = mystdout.getvalue()
|
||||
assert output == ""
|
||||
assert chained_input.input == "foo"
|
||||
|
||||
old_stdout = sys.stdout
|
||||
sys.stdout = mystdout = StringIO()
|
||||
chained_input.add("bar")
|
||||
sys.stdout = old_stdout
|
||||
output = mystdout.getvalue()
|
||||
assert output == ""
|
||||
assert chained_input.input == "foobar"
|
||||
|
||||
|
||||
def test_chained_input_verbose() -> None:
|
||||
"""Test chained input logic, making sure verbose doesn't mess it up."""
|
||||
old_stdout = sys.stdout
|
||||
sys.stdout = mystdout = StringIO()
|
||||
chained_input = ChainedInput("foo", verbose=True)
|
||||
sys.stdout = old_stdout
|
||||
output = mystdout.getvalue()
|
||||
assert output == "foo"
|
||||
assert chained_input.input == "foo"
|
||||
|
||||
old_stdout = sys.stdout
|
||||
sys.stdout = mystdout = StringIO()
|
||||
chained_input.add("bar")
|
||||
sys.stdout = old_stdout
|
||||
output = mystdout.getvalue()
|
||||
assert output == "bar"
|
||||
assert chained_input.input == "foobar"
|
||||
|
||||
old_stdout = sys.stdout
|
||||
sys.stdout = mystdout = StringIO()
|
||||
chained_input.add("baz", color="blue")
|
||||
sys.stdout = old_stdout
|
||||
output = mystdout.getvalue()
|
||||
assert output == "\x1b[104mbaz\x1b[0m"
|
||||
assert chained_input.input == "foobarbaz"
|
Loading…
Reference in New Issue