Skip to content

Commit

Permalink
fix: template tokens shadowing
Browse files Browse the repository at this point in the history
  • Loading branch information
CNSeniorious000 committed Apr 26, 2024
1 parent f9cb165 commit e057123
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
6 changes: 4 additions & 2 deletions python/promplate/prompt/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions python/promplate/prompt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -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/"
Expand Down
4 changes: 4 additions & 0 deletions python/tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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ǝɥ")

Expand Down

0 comments on commit e057123

Please sign in to comment.