forked from langchain-ai/langchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored
input
(langchain-ai#8202)
Refactored `input.py`. The same as langchain-ai#7961 langchain-ai#8098 langchain-ai#8099 input.py is in the root code folder. This creates the `langchain.input: Input` group on the API Reference navigation ToC, on the same level as Chains and Agents which is incorrect. Refactoring: - copied input.py file into utils/input.py - I added the backwards compatibility ref in the original input.py. - changed several imports to a new ref @hwchase17, @baskaryan
- Loading branch information
Showing
12 changed files
with
75 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,14 @@ | ||
"""Handle chained inputs.""" | ||
from typing import Dict, List, Optional, TextIO | ||
|
||
_TEXT_COLOR_MAPPING = { | ||
"blue": "36;1", | ||
"yellow": "33;1", | ||
"pink": "38;5;200", | ||
"green": "32;1", | ||
"red": "31;1", | ||
} | ||
|
||
|
||
def get_color_mapping( | ||
items: List[str], excluded_colors: Optional[List] = None | ||
) -> Dict[str, str]: | ||
"""Get mapping for items to a support color.""" | ||
colors = list(_TEXT_COLOR_MAPPING.keys()) | ||
if excluded_colors is not None: | ||
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)} | ||
return color_mapping | ||
|
||
|
||
def get_colored_text(text: str, color: str) -> str: | ||
"""Get colored text.""" | ||
color_str = _TEXT_COLOR_MAPPING[color] | ||
return f"\u001b[{color_str}m\033[1;3m{text}\u001b[0m" | ||
|
||
|
||
def get_bolded_text(text: str) -> str: | ||
"""Get bolded text.""" | ||
return f"\033[1m{text}\033[0m" | ||
|
||
|
||
def print_text( | ||
text: str, color: Optional[str] = None, end: str = "", file: Optional[TextIO] = None | ||
) -> None: | ||
"""Print text with highlighting and no end characters.""" | ||
text_to_print = get_colored_text(text, color) if color else text | ||
print(text_to_print, end=end, file=file) | ||
if file: | ||
file.flush() # ensure all printed content are written to file | ||
"""DEPRECATED: Kept for backwards compatibility.""" | ||
from langchain.utils.input import ( | ||
get_bolded_text, | ||
get_color_mapping, | ||
get_colored_text, | ||
print_text, | ||
) | ||
|
||
__all__ = [ | ||
"get_bolded_text", | ||
"get_color_mapping", | ||
"get_colored_text", | ||
"print_text", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"""Handle chained inputs.""" | ||
from typing import Dict, List, Optional, TextIO | ||
|
||
_TEXT_COLOR_MAPPING = { | ||
"blue": "36;1", | ||
"yellow": "33;1", | ||
"pink": "38;5;200", | ||
"green": "32;1", | ||
"red": "31;1", | ||
} | ||
|
||
|
||
def get_color_mapping( | ||
items: List[str], excluded_colors: Optional[List] = None | ||
) -> Dict[str, str]: | ||
"""Get mapping for items to a support color.""" | ||
colors = list(_TEXT_COLOR_MAPPING.keys()) | ||
if excluded_colors is not None: | ||
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)} | ||
return color_mapping | ||
|
||
|
||
def get_colored_text(text: str, color: str) -> str: | ||
"""Get colored text.""" | ||
color_str = _TEXT_COLOR_MAPPING[color] | ||
return f"\u001b[{color_str}m\033[1;3m{text}\u001b[0m" | ||
|
||
|
||
def get_bolded_text(text: str) -> str: | ||
"""Get bolded text.""" | ||
return f"\033[1m{text}\033[0m" | ||
|
||
|
||
def print_text( | ||
text: str, color: Optional[str] = None, end: str = "", file: Optional[TextIO] = None | ||
) -> None: | ||
"""Print text with highlighting and no end characters.""" | ||
text_to_print = get_colored_text(text, color) if color else text | ||
print(text_to_print, end=end, file=file) | ||
if file: | ||
file.flush() # ensure all printed content are written to file |