2024-05-09 15:40:36 +02:00
|
|
|
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2023-09-05 12:22:21 +02:00
|
|
|
import pytest
|
|
|
|
|
2023-11-24 14:48:43 +01:00
|
|
|
from haystack.components.builders.prompt_builder import PromptBuilder
|
2023-09-05 12:22:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_init():
|
|
|
|
builder = PromptBuilder(template="This is a {{ variable }}")
|
|
|
|
assert builder._template_string == "This is a {{ variable }}"
|
|
|
|
|
|
|
|
|
2024-02-29 14:30:06 +01:00
|
|
|
def test_to_dict():
|
|
|
|
builder = PromptBuilder(template="This is a {{ variable }}")
|
|
|
|
res = builder.to_dict()
|
|
|
|
assert res == {
|
|
|
|
"type": "haystack.components.builders.prompt_builder.PromptBuilder",
|
|
|
|
"init_parameters": {"template": "This is a {{ variable }}"},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-05 12:22:21 +02:00
|
|
|
def test_run():
|
|
|
|
builder = PromptBuilder(template="This is a {{ variable }}")
|
|
|
|
res = builder.run(variable="test")
|
|
|
|
assert res == {"prompt": "This is a test"}
|
|
|
|
|
|
|
|
|
|
|
|
def test_run_without_input():
|
|
|
|
builder = PromptBuilder(template="This is a template without input")
|
|
|
|
res = builder.run()
|
|
|
|
assert res == {"prompt": "This is a template without input"}
|
|
|
|
|
|
|
|
|
|
|
|
def test_run_with_missing_input():
|
|
|
|
builder = PromptBuilder(template="This is a {{ variable }}")
|
|
|
|
res = builder.run()
|
|
|
|
assert res == {"prompt": "This is a "}
|
2024-04-29 20:21:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_run_with_missing_required_input():
|
|
|
|
builder = PromptBuilder(template="This is a {{ foo }}, not a {{ bar }}", required_variables=["foo", "bar"])
|
|
|
|
with pytest.raises(ValueError, match="foo"):
|
|
|
|
builder.run(bar="bar")
|
|
|
|
with pytest.raises(ValueError, match="bar"):
|
|
|
|
builder.run(foo="foo")
|
|
|
|
with pytest.raises(ValueError, match="foo, bar"):
|
|
|
|
builder.run()
|