core:adds tests for partial_variables (#15427)

**Description:** Added small tests to test partial_variables in
PromptTemplate. It was missing.
This commit is contained in:
Dariusz Kajtoch 2024-01-03 00:00:06 +01:00 committed by GitHub
parent 73a628de9a
commit 15b6c049d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 2 deletions

View File

@ -1,5 +1,7 @@
_type: prompt _type: prompt
input_variables: input_variables:
["adjective", "content"] ["adjective"]
partial_variables:
content: dogs
template: template:
Tell me a {adjective} joke about {content}. Tell me a {adjective} joke about {content}.

View File

@ -28,7 +28,8 @@ def test_loading_from_YAML() -> None:
"""Test loading from yaml file.""" """Test loading from yaml file."""
prompt = load_prompt(EXAMPLE_DIR / "simple_prompt.yaml") prompt = load_prompt(EXAMPLE_DIR / "simple_prompt.yaml")
expected_prompt = PromptTemplate( expected_prompt = PromptTemplate(
input_variables=["adjective", "content"], input_variables=["adjective"],
partial_variables={"content": "dogs"},
template="Tell me a {adjective} joke about {content}.", template="Tell me a {adjective} joke about {content}.",
) )
assert prompt == expected_prompt assert prompt == expected_prompt

View File

@ -1,4 +1,6 @@
"""Test functionality related to prompts.""" """Test functionality related to prompts."""
from unittest import mock
import pytest import pytest
from langchain_core.prompts.prompt import PromptTemplate from langchain_core.prompts.prompt import PromptTemplate
@ -34,6 +36,22 @@ def test_prompt_from_template() -> None:
assert prompt == expected_prompt 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: def test_prompt_missing_input_variables() -> None:
"""Test error is raised when input variables are not provided.""" """Test error is raised when input variables are not provided."""
template = "This is a {foo} test." template = "This is a {foo} test."
@ -118,6 +136,26 @@ def test_prompt_from_file() -> None:
assert prompt.template == "Question: {question}\nAnswer:" 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: def test_partial_init_string() -> None:
"""Test prompt can be initialized with partial variables.""" """Test prompt can be initialized with partial variables."""
template = "This is a {foo} test." template = "This is a {foo} test."