Tony20100/langchain_full huggingface dataset:
the human.
        Args:
            template: template string
            **kwargs: keyword arguments to pass to the constructor.
        Returns:
            A new instance of this class.
        """
        prompt_template = PromptTemplate.from_template(template, **kwargs)
        message = HumanMessagePromptTemplate(prompt=prompt_template)
        return cls.from_messages([message])
[docs]    @classmethod
    @deprecated("0.0.1", alternative="from_messages classmethod", pending=True)
    def from_role_strings(
        cls, string_messages: List[Tuple[str, str]]
    ) -> ChatPromptTemplate:
        """Create a chat prompt template from a list of (role, template) tuples.
        Args:
            string_messages: list of (role, template) tuples.
        Returns:
            a chat prompt template
        """
        return cls(  # type: ignore[call-arg]
            messages=[
                ChatMessagePromptTemplate.from_template(template, role=role)
                for role, template in string_messages
            ]
        )
[docs]    @classmethod
    @deprecated("0.0.1", alternative="from_messages classmethod", pending=True)
    def from_strings(
        cls, string_messages: List[Tuple[Type[BaseMessagePromptTemplate], str]]
    ) -> ChatPromptTemplate:
        """Create a chat prompt template from a list of (role class, template) tuples.
        Args:
            string_messages: list of (role class, template) tuples.
        Returns:
            a chat prompt template
        """
        return cls.from_messages(string_messages)
[docs]    @classmethod
    def from_messages(
        cls,
        messages: Sequence[MessageLikeRepresentation],
def from_messages(
        cls,
        messages: Sequence[MessageLikeRepresentation],
        template_format: Literal["f-string", "mustache"] = "f-string",
    ) -> ChatPromptTemplate:
        """Create a chat prompt template from a variety of message formats.
        Examples:
            Instantiation from a list of message templates:
            .. code-block:: python
                template = ChatPromptTemplate.from_messages([
                    ("human", "Hello, how are you?"),
                    ("ai", "I'm doing well, thanks!"),
                    ("human", "That's good to hear."),
                ])
            Instantiation from mixed message formats:
            .. code-block:: python
                template = ChatPromptTemplate.from_messages([
                    SystemMessage(content="hello"),
                    ("human", "Hello, how are you?"),
                ])
        Args:
            messages: sequence of message representations.
                  A message can be represented using the following formats:
                  (1) BaseMessagePromptTemplate, (2) BaseMessage, (3) 2-tuple of
                  (message type, template); e.g., ("human", "{user_input}"),
                  (4) 2-tuple of (message class, template), (4) a string which is
                  shorthand for ("human", template); e.g., "{user_input}"
        Returns:
            a chat prompt template
        """
        _messages = [
            _convert_to_message(message, template_format) for message in messages
        ]
        # Automatically infer input variables from messages
        input_vars: Set[str] = set()
        partial_vars: Dict[str, Any] = {}
        for _message in _messages:
            if isinstance(_message, MessagesPlaceholder) and _message.optional:
                partial_vars[_message.variable_name] = []
partial_vars[_message.variable_name] = []
            elif isinstance(
                _message, (BaseChatPromptTemplate, BaseMessagePromptTemplate)
            ):
                input_vars.update(_message.input_variables)
        return cls(
            input_variables=sorted(input_vars),
            messages=_messages,
            partial_variables=partial_vars,
        )
[docs]    def format_messages(self, **kwargs: Any) -> List[BaseMessage]:
        """Format the chat template into a list of finalized messages.
        Args:
            **kwargs: keyword arguments to use for filling in template variables
                      in all the template messages in this chat template.
        Returns:
            list of formatted messages
        """
        kwargs = self._merge_partial_and_user_variables(**kwargs)
        result = []
        for message_template in self.messages:
            if isinstance(message_template, BaseMessage):
                result.extend([message_template])
            elif isinstance(
                message_template, (BaseMessagePromptTemplate, BaseChatPromptTemplate)
            ):
                message = message_template.format_messages(**kwargs)
                result.extend(message)
            else:
                raise ValueError(f"Unexpected input: {message_template}")
        return result
[docs]    async def aformat_messages(self, **kwargs: Any) -> List[BaseMessage]:
        """Format the chat template into a list of finalized messages.
        Args:
            **kwargs: keyword arguments to use for filling in template variables
                      in all the template messages in this chat template.
        Returns:
            list of formatted messages
        """
        kwargs = self._merge_partial_and_user_variables(**kwargs)
        result = []
        for message_template in self.messages:
            if isinstance(message_template, BaseMessage):
                result.extend([message_template])
            elif isinstance(
result.extend([message_template])
            elif isinstance(
                message_template, (BaseMessagePromptTemplate, BaseChatPromptTemplate)
            ):
                message = await message_template.aformat_messages(**kwargs)
                result.extend(message)
            else:
                raise ValueError(f"Unexpected input: {message_template}")
        return result
[docs]    def partial(self, **kwargs: Any) -> ChatPromptTemplate:
        """Get a new ChatPromptTemplate with some input variables already filled in.
        Args:
            **kwargs: keyword arguments to use for filling in template variables. Ought
                        to be a subset of the input variables.
        Returns:
            A new ChatPromptTemplate.
        Example:
            .. code-block:: python
                from langchain_core.prompts import ChatPromptTemplate
                template = ChatPromptTemplate.from_messages(
                    [
                        ("system", "You are an AI assistant named {name}."),
                        ("human", "Hi I'm {user}"),
                        ("ai", "Hi there, {user}, I'm {name}."),
                        ("human", "{input}"),
                    ]
                )
                template2 = template.partial(user="Lucy", name="R2D2")
                template2.format_messages(input="hello")
        """
        prompt_dict = self.__dict__.copy()
        prompt_dict["input_variables"] = list(
            set(self.input_variables).difference(kwargs)
        )
        prompt_dict["partial_variables"] = {**self.partial_variables, **kwargs}
        return type(self)(**prompt_dict)
[docs]    def append(self, message: MessageLikeRepresentation) -> None:
        """Append message to the end of the chat template.
        Args:
            message: representation of a message to append.
        """
Args:
            message: representation of a message to append.
        """
        self.messages.append(_convert_to_message(message))
[docs]    def extend(self, messages: Sequence[MessageLikeRepresentation]) -> None:
        """Extend the chat template with a sequence of messages."""
        self.messages.extend([_convert_to_message(message) for message in messages])
    @overload
    def __getitem__(self, index: int) -> MessageLike:
        ...
    @overload
    def __getitem__(self, index: slice) -> ChatPromptTemplate:
        ...
    def __getitem__(
        self, index: Union[int, slice]
    ) -> Union[MessageLike, ChatPromptTemplate]:
        """Use to index into the chat template."""
        if isinstance(index, slice):
            start, stop, step = index.indices(len(self.messages))
            messages = self.messages[start:stop:step]
            return ChatPromptTemplate.from_messages(messages)
        else:
            return self.messages[index]
    def __len__(self) -> int:
        """Get the length of the chat template."""
        return len(self.messages)
    @property
    def _prompt_type(self) -> str:
        """Name of prompt type."""
        return "chat"
[docs]    def save(self, file_path: Union[Path, str]) -> None:
        """Save prompt to file.
        Args:
            file_path: path to file.
        """
        raise NotImplementedError()
[docs]    def pretty_repr(self, html: bool = False) -> str:
        # TODO: handle partials
        return "\n\n".join(msg.pretty_repr(html=html) for msg in self.messages)
def _create_template_from_message_type(
    message_type: str,
def _create_template_from_message_type(
    message_type: str,
    template: Union[str, list],
    template_format: Literal["f-string", "mustache"] = "f-string",
) -> BaseMessagePromptTemplate:
    """Create a message prompt template from a message type and template string.
    Args:
        message_type: str the type of the message template (e.g., "human", "ai", etc.)
        template: str the template string.
    Returns:
        a message prompt template of the appropriate type.
    """
    if message_type in ("human", "user"):
        message: BaseMessagePromptTemplate = HumanMessagePromptTemplate.from_template(
            template, template_format=template_format
        )
    elif message_type in ("ai", "assistant"):
        message = AIMessagePromptTemplate.from_template(
            cast(str, template), template_format=template_format
        )
    elif message_type == "system":
        message = SystemMessagePromptTemplate.from_template(
            cast(str, template), template_format=template_format
        )
    elif message_type == "placeholder":
        if isinstance(template, str):
            if template[0] != "{" or template[-1] != "}":
                raise ValueError(
                    f"Invalid placeholder template: {template}."
                    " Expected a variable name surrounded by curly braces."
                )
            var_name = template[1:-1]
            message = MessagesPlaceholder(variable_name=var_name, optional=True)
        elif len(template) == 2 and isinstance(template[1], bool):
            var_name_wrapped, is_optional = template
            if not isinstance(var_name_wrapped, str):
                raise ValueError(
                    "Expected variable name to be a string." f" Got: {var_name_wrapped}"
                )
)
            if var_name_wrapped[0] != "{" or var_name_wrapped[-1] != "}":
                raise ValueError(
                    f"Invalid placeholder template: {var_name_wrapped}."
                    " Expected a variable name surrounded by curly braces."
                )
            var_name = var_name_wrapped[1:-1]
            message = MessagesPlaceholder(variable_name=var_name, optional=is_optional)
        else:
            raise ValueError(
                "Unexpected arguments for placeholder message type."
                " Expected either a single string variable name"
                " or a list of [variable_name: str, is_optional: bool]."
                f" Got: {template}"
            )
    else:
        raise ValueError(
            f"Unexpected message type: {message_type}. Use one of 'human',"
            f" 'user', 'ai', 'assistant', or 'system'."
        )
    return message
def _convert_to_message(
    message: MessageLikeRepresentation,
    template_format: Literal["f-string", "mustache"] = "f-string",
) -> Union[BaseMessage, BaseMessagePromptTemplate, BaseChatPromptTemplate]:
    """Instantiate a message from a variety of message formats.
    The message format can be one of the following:
    - BaseMessagePromptTemplate
    - BaseMessage
    - 2-tuple of (role string, template); e.g., ("human", "{user_input}")
    - 2-tuple of (message class, template)
    - string: shorthand for ("human", template); e.g., "{user_input}"
    Args:
        message: a representation of a message in one of the supported formats
    Returns:
        an instance of a message or a message template
    """
Returns:
        an instance of a message or a message template
    """
    if isinstance(message, (BaseMessagePromptTemplate, BaseChatPromptTemplate)):
        _message: Union[
            BaseMessage, BaseMessagePromptTemplate, BaseChatPromptTemplate
        ] = message
    elif isinstance(message, BaseMessage):
        _message = message
    elif isinstance(message, str):
        _message = _create_template_from_message_type(
            "human", message, template_format=template_format
        )
    elif isinstance(message, tuple):
        if len(message) != 2:
            raise ValueError(f"Expected 2-tuple of (role, template), got {message}")
        message_type_str, template = message
        if isinstance(message_type_str, str):
            _message = _create_template_from_message_type(
                message_type_str, template, template_format=template_format
            )
        else:
            _message = message_type_str(
                prompt=PromptTemplate.from_template(
                    cast(str, template), template_format=template_format
                )
            )
    else:
        raise NotImplementedError(f"Unsupported message type: {type(message)}")
    return _message
Source code for langchain_core.utils.mustache
"""
Adapted from https://github.com/noahmorrison/chevron
MIT License
"""
import logging
from typing import (
    Any,
    Dict,
    Iterator,
    List,
    Literal,
    Optional,
    Sequence,
    Tuple,
    Union,
    cast,
)
from typing_extensions import TypeAlias
logger = logging.getLogger(__name__)
Scopes: TypeAlias = List[Union[Literal[False, 0], Dict[str, Any]]]
# Globals
_CURRENT_LINE = 1
_LAST_TAG_LINE = None
[docs]class ChevronError(SyntaxError):
    """Custom exception for Chevron errors."""
    pass
#
# Helper functions
#
[docs]def grab_literal(template: str, l_del: str) -> Tuple[str, str]:
    """Parse a literal from the template."""
    global _CURRENT_LINE
    try:
        # Look for the next tag and move the template to it
        literal, template = template.split(l_del, 1)
        _CURRENT_LINE += literal.count("\n")
        return (literal, template)
    # There are no more tags in the template?
    except ValueError:
        # Then the rest of the template is a literal
        return (template, "")
[docs]def l_sa_check(template: str, literal: str, is_standalone: bool) -> bool:
    """Do a preliminary check to see if a tag could be a standalone."""
    # If there is a newline, or the previous tag was a standalone
    if literal.find("\n") != -1 or is_standalone:
        padding = literal.split("\n")[-1]
        # If all the characters since the last newline are spaces
# If all the characters since the last newline are spaces
        if padding.isspace() or padding == "":
            # Then the next tag could be a standalone
            return True
        else:
            # Otherwise it can't be
            return False
    else:
        return False
[docs]def r_sa_check(template: str, tag_type: str, is_standalone: bool) -> bool:
    """Do a final check to see if a tag could be a standalone."""
    # Check right side if we might be a standalone
    if is_standalone and tag_type not in ["variable", "no escape"]:
        on_newline = template.split("\n", 1)
        # If the stuff to the right of us are spaces we're a standalone
        if on_newline[0].isspace() or not on_newline[0]:
            return True
        else:
            return False
    # If we're a tag can't be a standalone
    else:
        return False
[docs]def parse_tag(template: str, l_del: str, r_del: str) -> Tuple[Tuple[str, str], str]:
    """Parse a tag from a template."""
    global _CURRENT_LINE
    global _LAST_TAG_LINE
    tag_types = {
        "!": "comment",
        "#": "section",
        "^": "inverted section",
        "/": "end",
        ">": "partial",
        "=": "set delimiter?",
        "{": "no escape?",
        "&": "no escape",
    }
    # Get the tag
    try:
        tag, template = template.split(r_del, 1)
    except ValueError:
        raise ChevronError("unclosed tag " "at line {0}".format(_CURRENT_LINE))
# Find the type meaning of the first character
    tag_type = tag_types.get(tag[0], "variable")
    # If the type is not a variable
    if tag_type != "variable":
        # Then that first character is not needed
        tag = tag[1:]
    # If we might be a set delimiter tag
    if tag_type == "set delimiter?":
        # Double check to make sure we are
        if tag.endswith("="):
            tag_type = "set delimiter"
            # Remove the equal sign
            tag = tag[:-1]
        # Otherwise we should complain
        else:
            raise ChevronError(
                "unclosed set delimiter tag\n" "at line {0}".format(_CURRENT_LINE)
            )
    # If we might be a no html escape tag
    elif tag_type == "no escape?":
        # And we have a third curly brace
        # (And are using curly braces as delimiters)
        if l_del == "{{" and r_del == "}}" and template.startswith("}"):
            # Then we are a no html escape tag
            template = template[1:]
            tag_type = "no escape"
    # Strip the whitespace off the key and return
    return ((tag_type, tag.strip()), template)
#
# The main tokenizing function
#
[docs]def tokenize(
    template: str, def_ldel: str = "{{", def_rdel: str = "}}"
) -> Iterator[Tuple[str, str]]:
    """Tokenize a mustache template.
    Tokenizes a mustache template in a generator fashion,
    using file-like objects. It also accepts a string containing
    the template.
    Arguments:
    template -- a file-like object, or a string of a mustache template
template -- a file-like object, or a string of a mustache template
    def_ldel -- The default left delimiter
                ("{{" by default, as in spec compliant mustache)
    def_rdel -- The default right delimiter
                ("}}" by default, as in spec compliant mustache)
    Returns:
    A generator of mustache tags in the form of a tuple
    -- (tag_type, tag_key)
    Where tag_type is one of:
     * literal
     * section
     * inverted section
     * end
     * partial
     * no escape
    And tag_key is either the key or in the case of a literal tag,
    the literal itself.
    """
    global _CURRENT_LINE, _LAST_TAG_LINE
    _CURRENT_LINE = 1
    _LAST_TAG_LINE = None
    is_standalone = True
    open_sections = []
    l_del = def_ldel
    r_del = def_rdel
    while template:
        literal, template = grab_literal(template, l_del)
        # If the template is completed
        if not template:
            # Then yield the literal and leave
            yield ("literal", literal)
            break
        # Do the first check to see if we could be a standalone
        is_standalone = l_sa_check(template, literal, is_standalone)
        # Parse the tag
        tag, template = parse_tag(template, l_del, r_del)
        tag_type, tag_key = tag
        # Special tag logic
        # If we are a set delimiter tag
        if tag_type == "set delimiter":
            # Then get and set the delimiters
            dels = tag_key.strip().split(" ")
            l_del, r_del = dels[0], dels[-1]
l_del, r_del = dels[0], dels[-1]
        # If we are a section tag
        elif tag_type in ["section", "inverted section"]:
            # Then open a new section
            open_sections.append(tag_key)
            _LAST_TAG_LINE = _CURRENT_LINE
        # If we are an end tag
        elif tag_type == "end":
            # Then check to see if the last opened section
            # is the same as us
            try:
                last_section = open_sections.pop()
            except IndexError:
                raise ChevronError(
                    'Trying to close tag "{0}"\n'
                    "Looks like it was not opened.\n"
                    "line {1}".format(tag_key, _CURRENT_LINE + 1)
                )
            if tag_key != last_section:
                # Otherwise we need to complain
                raise ChevronError(
                    'Trying to close tag "{0}"\n'
                    'last open tag is "{1}"\n'
                    "line {2}".format(tag_key, last_section, _CURRENT_LINE + 1)
                )
        # Do the second check to see if we're a standalone
        is_standalone = r_sa_check(template, tag_type, is_standalone)
        # Which if we are
        if is_standalone:
            # Remove the stuff before the newline
            template = template.split("\n", 1)[-1]
            # Partials need to keep the spaces on their left
            if tag_type != "partial":
                # But other tags don't
                literal = literal.rstrip(" ")
        # Start yielding
        # Ignore literals that are empty
        if literal != "":
            yield ("literal", literal)
        # Ignore comments and set delimiters
yield ("literal", literal)
        # Ignore comments and set delimiters
        if tag_type not in ["comment", "set delimiter?"]:
            yield (tag_type, tag_key)
    # If there are any open sections when we're done
    if open_sections:
        # Then we need to complain
        raise ChevronError(
            "Unexpected EOF\n"
            'the tag "{0}" was never closed\n'
            "was opened at line {1}".format(open_sections[-1], _LAST_TAG_LINE)
        )
#
# Helper functions
#
def _html_escape(string: str) -> str:
    """HTML escape all of these " & < >"""
    html_codes = {
        '"': "&quot;",
        "<": "&lt;",
        ">": "&gt;",
    }
    # & must be handled first
    string = string.replace("&", "&amp;")
    for char in html_codes:
        string = string.replace(char, html_codes[char])
    return string
def _get_key(
    key: str,
    scopes: Scopes,
    warn: bool,
    keep: bool,
    def_ldel: str,
    def_rdel: str,
) -> Any:
    """Get a key from the current scope"""
    # If the key is a dot
    if key == ".":
        # Then just return the current scope
        return scopes[0]
    # Loop through the scopes
    for scope in scopes:
        try:
            # Return an empty string if falsy, with two exceptions
            # 0 should return 0, and False should return False
            if scope in (0, False):
                return scope
            # For every dot separated key
            for child in key.split("."):