From 15b6c049d4d86bfec7eb2bb8d66f0a52324f16d1 Mon Sep 17 00:00:00 2001 From: Dariusz Kajtoch <32985207+dkajtoch@users.noreply.github.com> Date: Wed, 3 Jan 2024 00:00:06 +0100 Subject: [PATCH] core:adds tests for partial_variables (#15427) **Description:** Added small tests to test partial_variables in PromptTemplate. It was missing. --- .../unit_tests/examples/simple_prompt.yaml | 4 +- .../tests/unit_tests/prompts/test_loading.py | 3 +- .../tests/unit_tests/prompts/test_prompt.py | 38 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/libs/core/tests/unit_tests/examples/simple_prompt.yaml b/libs/core/tests/unit_tests/examples/simple_prompt.yaml index 5377b92f20f..ffb3acd3393 100644 --- a/libs/core/tests/unit_tests/examples/simple_prompt.yaml +++ b/libs/core/tests/unit_tests/examples/simple_prompt.yaml @@ -1,5 +1,7 @@ _type: prompt input_variables: - ["adjective", "content"] + ["adjective"] +partial_variables: + content: dogs template: Tell me a {adjective} joke about {content}. diff --git a/libs/core/tests/unit_tests/prompts/test_loading.py b/libs/core/tests/unit_tests/prompts/test_loading.py index a89ffaf186c..afad88766a8 100644 --- a/libs/core/tests/unit_tests/prompts/test_loading.py +++ b/libs/core/tests/unit_tests/prompts/test_loading.py @@ -28,7 +28,8 @@ def test_loading_from_YAML() -> None: """Test loading from yaml file.""" prompt = load_prompt(EXAMPLE_DIR / "simple_prompt.yaml") expected_prompt = PromptTemplate( - input_variables=["adjective", "content"], + input_variables=["adjective"], + partial_variables={"content": "dogs"}, template="Tell me a {adjective} joke about {content}.", ) assert prompt == expected_prompt diff --git a/libs/core/tests/unit_tests/prompts/test_prompt.py b/libs/core/tests/unit_tests/prompts/test_prompt.py index ec01e426f78..f50b71bea80 100644 --- a/libs/core/tests/unit_tests/prompts/test_prompt.py +++ b/libs/core/tests/unit_tests/prompts/test_prompt.py @@ -1,4 +1,6 @@ """Test functionality related to prompts.""" +from unittest import mock + import pytest from langchain_core.prompts.prompt import PromptTemplate @@ -34,6 +36,22 @@ def test_prompt_from_template() -> None: assert prompt == expected_prompt +def test_prompt_from_template_with_partial_variables() -> None: + """Test prompts can be constructed from a template with partial variables.""" + # given + template = "This is a {foo} test {bar}." + partial_variables = {"bar": "baz"} + # when + prompt = PromptTemplate.from_template(template, partial_variables=partial_variables) + # then + expected_prompt = PromptTemplate( + template=template, + input_variables=["foo"], + partial_variables=partial_variables, + ) + assert prompt == expected_prompt + + def test_prompt_missing_input_variables() -> None: """Test error is raised when input variables are not provided.""" template = "This is a {foo} test." @@ -118,6 +136,26 @@ def test_prompt_from_file() -> None: assert prompt.template == "Question: {question}\nAnswer:" +def test_prompt_from_file_with_partial_variables() -> None: + """Test prompt can be successfully constructed from a file + with partial variables.""" + # given + template = "This is a {foo} test {bar}." + partial_variables = {"bar": "baz"} + # when + with mock.patch("builtins.open", mock.mock_open(read_data=template)): + prompt = PromptTemplate.from_file( + "mock_file_name", partial_variables=partial_variables + ) + # then + expected_prompt = PromptTemplate( + template=template, + input_variables=["foo"], + partial_variables=partial_variables, + ) + assert prompt == expected_prompt + + def test_partial_init_string() -> None: """Test prompt can be initialized with partial variables.""" template = "This is a {foo} test."