From 41b6e33f6463dfa9d53062c54a502dc1480a73cd Mon Sep 17 00:00:00 2001 From: Vladimir Blagojevic Date: Wed, 26 Apr 2023 18:09:20 +0200 Subject: [PATCH] Enhance the error logging in PromptTemplate variable resolution (#4730) * Enhance the error logging in PromptTemplate variable resolution * Revert change Daria made * Silvano PR feedback --- haystack/nodes/prompt/prompt_template.py | 11 ++++++++--- test/prompt/test_prompt_template.py | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/haystack/nodes/prompt/prompt_template.py b/haystack/nodes/prompt/prompt_template.py index 55bcc211c..871b8eb21 100644 --- a/haystack/nodes/prompt/prompt_template.py +++ b/haystack/nodes/prompt/prompt_template.py @@ -268,9 +268,14 @@ class PromptTemplate(BasePromptTemplate, ABC): if param in kwargs: params_dict[param] = kwargs[param] - if set(params_dict.keys()) != set(self.prompt_params): - available_params = set(list(params_dict.keys()) + list(set(kwargs.keys()))) - raise ValueError(f"Expected prompt parameters {self.prompt_params} but got {list(available_params)}.") + if not set(self.prompt_params).issubset(params_dict.keys()): + available_params = {*params_dict.keys(), *kwargs.keys()} + provided = set(self.prompt_params).intersection(available_params) + message = f"only {list(provided)}" if provided else "none of these parameters" + raise ValueError( + f"Expected prompt parameters {self.prompt_params} to be provided but got " + f"{message}. Make sure to provide all template parameters." + ) template_dict = {"_at_least_one_prompt": True} for id, call in self._prompt_params_functions.items(): diff --git a/test/prompt/test_prompt_template.py b/test/prompt/test_prompt_template.py index 92ec3a284..d86eba6c3 100644 --- a/test/prompt/test_prompt_template.py +++ b/test/prompt/test_prompt_template.py @@ -39,6 +39,27 @@ def test_prompt_templates(): assert p.prompt_text == "Here is some fake template with variable {baz}" +@pytest.mark.unit +def test_missing_prompt_template_params(): + template = PromptTemplate("missing_params", "Here is some fake template with variable {foo} and {bar}") + + # both params provided - ok + template.prepare(foo="foo", bar="bar") + + # missing one param + with pytest.raises(ValueError, match=r".*parameters \['bar', 'foo'\] to be provided but got only \['foo'\].*"): + template.prepare(foo="foo") + + # missing both params + with pytest.raises( + ValueError, match=r".*parameters \['bar', 'foo'\] to be provided but got none of these parameters.*" + ): + template.prepare(lets="go") + + # more than both params provided - also ok + template.prepare(foo="foo", bar="bar", lets="go") + + @pytest.mark.unit def test_prompt_template_repr(): p = PromptTemplate("t", "Here is variable {baz}")