|
|
|
@ -5,7 +5,6 @@ from abc import ABC, abstractmethod
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from typing import (
|
|
|
|
|
Any,
|
|
|
|
|
Callable,
|
|
|
|
|
Dict,
|
|
|
|
|
List,
|
|
|
|
|
Optional,
|
|
|
|
@ -130,13 +129,7 @@ class MessagesPlaceholder(BaseMessagePromptTemplate):
|
|
|
|
|
f"variable {self.variable_name} should be a list of base messages, "
|
|
|
|
|
f"got {value}"
|
|
|
|
|
)
|
|
|
|
|
for v in convert_to_messages(value):
|
|
|
|
|
if not isinstance(v, BaseMessage):
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f"variable {self.variable_name} should be a list of base messages,"
|
|
|
|
|
f" got {value}"
|
|
|
|
|
)
|
|
|
|
|
return value
|
|
|
|
|
return convert_to_messages(value)
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def input_variables(self) -> List[str]:
|
|
|
|
@ -755,13 +748,20 @@ class ChatPromptTemplate(BaseChatPromptTemplate):
|
|
|
|
|
|
|
|
|
|
# Automatically infer input variables from messages
|
|
|
|
|
input_vars: Set[str] = set()
|
|
|
|
|
partial_vars: Dict[str, Any] = {}
|
|
|
|
|
for _message in _messages:
|
|
|
|
|
if isinstance(
|
|
|
|
|
if isinstance(_message, MessagesPlaceholder) and _message.optional:
|
|
|
|
|
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)
|
|
|
|
|
return cls(
|
|
|
|
|
input_variables=sorted(input_vars),
|
|
|
|
|
messages=_messages,
|
|
|
|
|
partial_variables=partial_vars,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def format(self, **kwargs: Any) -> str:
|
|
|
|
|
"""Format the chat template into a string.
|
|
|
|
@ -799,7 +799,7 @@ class ChatPromptTemplate(BaseChatPromptTemplate):
|
|
|
|
|
raise ValueError(f"Unexpected input: {message_template}")
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
def partial(self, **kwargs: Union[str, Callable[[], str]]) -> ChatPromptTemplate:
|
|
|
|
|
def partial(self, **kwargs: Any) -> ChatPromptTemplate:
|
|
|
|
|
"""Get a new ChatPromptTemplate with some input variables already filled in.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|