From e057123a1d62edbee85ecc02c243dcaec1ca297e Mon Sep 17 00:00:00 2001 From: Muspi Merol Date: Sat, 27 Apr 2024 03:04:47 +0800 Subject: [PATCH] fix: template tokens shadowing --- python/promplate/prompt/template.py | 6 ++++-- python/promplate/prompt/utils.py | 5 +---- python/pyproject.toml | 2 +- python/tests/test_template.py | 4 ++++ 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/python/promplate/prompt/template.py b/python/promplate/prompt/template.py index d5358ee..98fff42 100644 --- a/python/promplate/prompt/template.py +++ b/python/promplate/prompt/template.py @@ -24,8 +24,6 @@ def __init__(self, text: str): """Construct a Templite with the given `text`.""" self.text = text - self._buffer = [] - self._ops_stack = [] def _flush(self): for line in self._buffer: @@ -92,9 +90,13 @@ def _make_context(text: str): return f"locals() | dict({text[text.index(' ') + 1:]})" if " " in text else "locals()" def compile(self, sync=True, indent_str="\t"): + self._buffer = [] + self._ops_stack = [] self._builder = get_base_builder(sync, indent_str) for token in split_template_tokens(self.text): + if not token: + continue s_token = token.strip() if s_token.startswith("{{") and s_token.endswith("}}"): self._on_eval_token(token) diff --git a/python/promplate/prompt/utils.py b/python/promplate/prompt/utils.py index a6069c2..ad48d01 100644 --- a/python/promplate/prompt/utils.py +++ b/python/promplate/prompt/utils.py @@ -4,10 +4,7 @@ from typing import Any, Callable, ParamSpec, TypeVar split_template_tokens = compile( - r"(\s{%-.*?-%}\s|\s{{-[\s\S]*?-}}\s|\s{#-[\s\S]*?-#}\s" - r"|\s{%-.*?%}|\s{{-[\s\S]*?}}|\s{#-[\s\S]*?#}" - r"|{%.*?-%}\s|{{[\s\S]*?-}}\s|{#[\s\S]*?-#}\s" - r"|{%.*?%}|{{[\s\S]*?}}|{#[\s\S]*?#})" + r"((?:\s{%-|{%).*?(?:%}|-%}\s))|((?:\s{{-|{{)[\s\S]*?(?:}}|-}}\s))|((?:\s{#-|{#)[\s\S]*?(?:#}|-#}\s))" ).split diff --git a/python/pyproject.toml b/python/pyproject.toml index 25f4c82..f6ef3b5 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "promplate" -version = "0.3.4.2" +version = "0.3.4.4" description = "Prompt engineering framework for humans" homepage = "https://promplate.dev/" documentation = "https://docs.py.promplate.dev/" diff --git a/python/tests/test_template.py b/python/tests/test_template.py index 625898a..1447869 100644 --- a/python/tests/test_template.py +++ b/python/tests/test_template.py @@ -234,6 +234,10 @@ def test_whitespace_handling(): ) +def test_whitespace_among_tags(): + render_assert("@{{ a }} {{- b -}} {{ c }}!", {"a": 1, "b": 2, "c": 3}, "@123!") + + def test_non_ascii(): render_assert("{{where}} ollǝɥ", {"where": "ǝɹǝɥʇ"}, "ǝɹǝɥʇ ollǝɥ")