English 中文(简体)
从_ 消息、 从_ template、 格式、 格式、 语言格式、 语言格式来发布什么信息? 有人能简化吗?
原标题:what are from_messages, from_template, format, format_messages in language? Can someone simplify?

请告诉我何时使用以及为什么使用上述功能的背景?

我试着读了医生,但无法理解太多。 任何引用博客或链接的参考都会有所帮助。

提前感谢。

问题回答

您应该去源代码执行。 在代码中, 他们评论出来, 您也可以看到正在发生什么。 这些方法实际上正在以额外的配置和验证来即时处理类实例。 例如, 如果您查看执行 < code> from_ messages 的 < messages 执行 :

@classmethod
    def from_messages(
        cls,
        messages: Sequence[
            Union[
                BaseMessagePromptTemplate,
                BaseChatPromptTemplate,
                BaseMessage,
                Tuple[str, str],
                Tuple[Type, str],
                str,
            ]
        ],
    ) -> 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) for message in messages]

        # Automatically infer input variables from messages
        input_vars = set()
        for _message in _messages:
            if isinstance(
                _message, (BaseChatPromptTemplate, BaseMessagePromptTemplate)
            ):
                input_vars.update(_message.input_variables)

        return cls(input_variables=sorted(input_vars), messages=_messages)

此方法,

  • 首先使用 帮助程序,它将每条消息转换为标准格式,供聊天PromptTemplate类使用

  • 然后使用 input_vars = set 来删除重复的信件。

  • input_evilables 是信件模板中的占位符, 当使用模板时将替换为实际值。 例如, 信件可能有“ 你好, {user_ name} ” 这样的模板, 即 {user_ name} 是一个输入变量 。

  • 然后检查他们接受何种消息 : BaseChatPhamptTemplate, baseMessagePromptTemplate

收集所有独特的输入变量后, 它返回

   return cls(input_variables=sorted(input_vars), messages=_messages)

此行创建并返回一个 ChattPromptTemplate 实例, 使用推断输入变量和已处理的信件 。

这是来自_template 方法的 :

  @classmethod
    def from_template(
        cls: Type[MessagePromptTemplateT],
        template: str,
        template_format: str = "f-string",
        **kwargs: Any,
    ) -> MessagePromptTemplateT:
        """Create a class from a string template.

        Args:
            template: a template.
            template_format: format of the template.
            **kwargs: keyword arguments to pass to the constructor.

        Returns:
            A new instance of this class.
        """
        prompt = PromptTemplate.from_template(template, template_format=template_format)
        return cls(prompt=prompt, **kwargs)

from_template 需要一个单一的模板字符串(和可选格式)来创建实例。 from_messages 需要不同格式的顺序信息来创建实例。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...