Compare commits

...

3 Commits

Author SHA1 Message Date
Erick Friis
d00f473703 x 2024-03-21 14:41:08 -07:00
Erick Friis
4e75c0228c x 2024-03-21 14:35:08 -07:00
Erick Friis
56f7f84ee4 core[patch]: placeholder message shorthand 2024-03-21 14:34:52 -07:00
2 changed files with 34 additions and 1 deletions

View File

@@ -949,6 +949,13 @@ def _create_template_from_message_type(
message = AIMessagePromptTemplate.from_template(cast(str, template))
elif message_type == "system":
message = SystemMessagePromptTemplate.from_template(cast(str, template))
elif message_type == "placeholder":
if not isinstance(template, str) or template[0] != "{" or template[-1] != "}":
raise ValueError(
r"Expected a placeholder variable of the format {variable_name},"
f" got {template}."
)
message = MessagesPlaceholder(variable_name=template[1:-1])
else:
raise ValueError(
f"Unexpected message type: {message_type}. Use one of 'human',"

View File

@@ -151,11 +151,14 @@ def test_chat_prompt_template_from_messages_using_role_strings() -> None:
("system", "You are a helpful AI bot. Your name is {name}."),
("human", "Hello, how are you doing?"),
("ai", "I'm doing well, thanks!"),
("placeholder", "{chat_history}"),
("human", "{user_input}"),
]
)
messages = template.format_messages(name="Bob", user_input="What is your name?")
messages = template.format_messages(
name="Bob", user_input="What is your name?", chat_history=[]
)
assert messages == [
SystemMessage(
@@ -170,6 +173,29 @@ def test_chat_prompt_template_from_messages_using_role_strings() -> None:
HumanMessage(content="What is your name?", additional_kwargs={}, example=False),
]
messages = template.format_messages(
name="Bob",
user_input="What is your name?",
chat_history=[("human", "That's great to hear"), ("ai", "Thanks!")],
)
assert messages == [
SystemMessage(
content="You are a helpful AI bot. Your name is Bob.", additional_kwargs={}
),
HumanMessage(
content="Hello, how are you doing?", additional_kwargs={}, example=False
),
AIMessage(
content="I'm doing well, thanks!", additional_kwargs={}, example=False
),
HumanMessage(
content="That's great to hear", additional_kwargs={}, example=False
),
AIMessage(content="Thanks!", additional_kwargs={}, example=False),
HumanMessage(content="What is your name?", additional_kwargs={}, example=False),
]
def test_chat_prompt_template_with_messages() -> None:
messages: List[Union[BaseMessagePromptTemplate, BaseMessage]] = (