Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion llm/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def interpolate(cls, text: str | None, params: dict[str, Any]) -> str | None:
# Confirm all variables in text are provided
string_template = string.Template(text)
vars = cls.extract_vars(string_template)
missing = [p for p in vars if p not in params]
missing = list(dict.fromkeys(p for p in vars if p not in params))
if missing:
raise cls.MissingVariables(
"Missing variables: {}".format(", ".join(missing))
Expand Down
3 changes: 3 additions & 0 deletions tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
("S: $input", "system", None, {}, "S: input", "system", None),
("No vars", None, None, {}, "No vars", None, None),
("$one and $two", None, None, {}, None, None, "Missing variables: one, two"),
# A variable reused in the template is reported once, not once per use.
("$name and $name", None, None, {}, None, None, "Missing variables: name"),
("$a $b $a", None, None, {}, None, None, "Missing variables: a, b"),
("$one and $two", None, None, {"one": 1, "two": 2}, "1 and 2", None, None),
("$one and $two", None, {"one": 1}, {"two": 2}, "1 and 2", None, None),
("$one and $$2", None, None, {"one": 1}, "1 and $2", None, None),
Expand Down