The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
Add from_file method to message prompt template (#4713)
charosen75fe9d3555d1
diff --git a/langchain/prompts/chat.py b/langchain/prompts/chat.pyindex af1fbef453..9df7cece57 100644--- a/langchain/prompts/chat.py+++ b/langchain/prompts/chat.py@@ -74,6 +74,16 @@ class BaseStringMessagePromptTemplate(BaseMessagePromptTemplate, ABC):prompt = PromptTemplate.from_template(template)return cls(prompt=prompt, **kwargs)+ @classmethod+ def from_template_file(+ cls: Type[MessagePromptTemplateT],+ template_file: Union[str, Path],+ input_variables: List[str],+ **kwargs: Any,+ ) -> MessagePromptTemplateT:+ prompt = PromptTemplate.from_file(template_file, input_variables)+ return cls(prompt=prompt, **kwargs)+@abstractmethoddef format(self, **kwargs: Any) -> BaseMessage:"""To a BaseMessage."""diff --git a/tests/unit_tests/prompts/test_chat.py b/tests/unit_tests/prompts/test_chat.pyindex 87f64c9599..a9844a780f 100644--- a/tests/unit_tests/prompts/test_chat.py+++ b/tests/unit_tests/prompts/test_chat.py@@ -1,3 +1,4 @@+from pathlib import Pathfrom typing import Listfrom langchain.prompts import PromptTemplate@@ -80,6 +81,21 @@ def test_create_chat_prompt_template_from_template_partial() -> None:assert output_prompt.prompt == expected_prompt+def test_message_prompt_template_from_template_file() -> None:+ expected = ChatMessagePromptTemplate(+ prompt=PromptTemplate(+ template="Question: {question}\nAnswer:", input_variables=["question"]+ ),+ role="human",+ )+ actual = ChatMessagePromptTemplate.from_template_file(+ Path(__file__).parent.parent / "data" / "prompt_file.txt",+ ["question"],+ role="human",+ )+ assert expected == actual++def test_chat_prompt_template() -> None:"""Test chat prompt template."""prompt_template = create_chat_prompt_template()