mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-25 16:13:25 +00:00
chore: update ascii colors to work with dark mode (#152)
This commit is contained in:
parent
ca4b10bb74
commit
0c3ae78ec1
@ -9,7 +9,7 @@ class Chain(BaseModel, ABC):
|
|||||||
"""Base interface that all chains should implement."""
|
"""Base interface that all chains should implement."""
|
||||||
|
|
||||||
verbose: bool = False
|
verbose: bool = False
|
||||||
"""Whether to print out the code that was executed."""
|
"""Whether to print out response text."""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
|
@ -1,14 +1,19 @@
|
|||||||
"""Handle chained inputs."""
|
"""Handle chained inputs."""
|
||||||
from typing import Dict, List, Optional
|
from typing import Dict, List, Optional
|
||||||
|
|
||||||
_COLOR_MAPPING = {"blue": 51, "yellow": 229, "pink": 219, "green": 85}
|
_TEXT_COLOR_MAPPING = {
|
||||||
|
"blue": "36;1",
|
||||||
|
"yellow": "33;1",
|
||||||
|
"pink": "38;5;200",
|
||||||
|
"green": "32;1",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def get_color_mapping(
|
def get_color_mapping(
|
||||||
items: List[str], excluded_colors: Optional[List] = None
|
items: List[str], excluded_colors: Optional[List] = None
|
||||||
) -> Dict[str, str]:
|
) -> Dict[str, str]:
|
||||||
"""Get mapping for items to a support color."""
|
"""Get mapping for items to a support color."""
|
||||||
colors = list(_COLOR_MAPPING.keys())
|
colors = list(_TEXT_COLOR_MAPPING.keys())
|
||||||
if excluded_colors is not None:
|
if excluded_colors is not None:
|
||||||
colors = [c for c in colors if c not in excluded_colors]
|
colors = [c for c in colors if c not in excluded_colors]
|
||||||
color_mapping = {item: colors[i % len(colors)] for i, item in enumerate(items)}
|
color_mapping = {item: colors[i % len(colors)] for i, item in enumerate(items)}
|
||||||
@ -20,8 +25,8 @@ def print_text(text: str, color: Optional[str] = None, end: str = "") -> None:
|
|||||||
if color is None:
|
if color is None:
|
||||||
print(text, end=end)
|
print(text, end=end)
|
||||||
else:
|
else:
|
||||||
color_str = _COLOR_MAPPING[color]
|
color_str = _TEXT_COLOR_MAPPING[color]
|
||||||
print(f"\u001b[48;5;{color_str}m{text}\x1b[0m", end=end)
|
print(f"\u001b[{color_str}m\033[1;3m{text}\u001b[0m", end=end)
|
||||||
|
|
||||||
|
|
||||||
class ChainedInput:
|
class ChainedInput:
|
||||||
@ -29,14 +34,14 @@ class ChainedInput:
|
|||||||
|
|
||||||
def __init__(self, text: str, verbose: bool = False):
|
def __init__(self, text: str, verbose: bool = False):
|
||||||
"""Initialize with verbose flag and initial text."""
|
"""Initialize with verbose flag and initial text."""
|
||||||
self.verbose = verbose
|
self._verbose = verbose
|
||||||
if self.verbose:
|
if self._verbose:
|
||||||
print_text(text, None)
|
print_text(text, None)
|
||||||
self._input = text
|
self._input = text
|
||||||
|
|
||||||
def add(self, text: str, color: Optional[str] = None) -> None:
|
def add(self, text: str, color: Optional[str] = None) -> None:
|
||||||
"""Add text to input, print if in verbose mode."""
|
"""Add text to input, print if in verbose mode."""
|
||||||
if self.verbose:
|
if self._verbose:
|
||||||
print_text(text, color)
|
print_text(text, color)
|
||||||
self._input += text
|
self._input += text
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ def test_chained_input_verbose() -> None:
|
|||||||
chained_input.add("baz", color="blue")
|
chained_input.add("baz", color="blue")
|
||||||
sys.stdout = old_stdout
|
sys.stdout = old_stdout
|
||||||
output = mystdout.getvalue()
|
output = mystdout.getvalue()
|
||||||
assert output == "\x1b[48;5;51mbaz\x1b[0m"
|
assert output == "\x1b[36;1m\x1b[1;3mbaz\x1b[0m"
|
||||||
assert chained_input.input == "foobarbaz"
|
assert chained_input.input == "foobarbaz"
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user