Enhance the error logging in PromptTemplate variable resolution (#4730)

* Enhance the error logging in PromptTemplate variable resolution

* Revert change Daria made

* Silvano PR feedback
This commit is contained in:
Vladimir Blagojevic 2023-04-26 18:09:20 +02:00 committed by GitHub
parent 2f7104704a
commit 41b6e33f64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 3 deletions

View File

@ -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():

View File

@ -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}")