Skip to content

Commit

Permalink
refactor: make jinja2 default populator for simplicity; remove unnece…
Browse files Browse the repository at this point in the history
…ssary autodetect functionality
  • Loading branch information
MoritzLaurer committed Jan 2, 2025
1 parent 985a084 commit 8d52847
Show file tree
Hide file tree
Showing 5 changed files with 895 additions and 125 deletions.
2 changes: 2 additions & 0 deletions docs/standard_prompt_format.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ prompt:
**Naming convention:** We call a file a *"prompt template"*, when it has placeholders ({{...}}) for dynamically populating the template similar to an f-string. This makes files more useful and reusable by others for different use-cases. Once the placeholders in the template are populated with specific variables, we call it a *"prompt"*.
**Templating:** [Jinja2](https://jinja.palletsprojects.com/en/stable/) is the default templating engine for populating the variables in the template.
The following example illustrates how the prompt template becomes a prompt.
```python
Expand Down
2 changes: 1 addition & 1 deletion prompt_templates/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
VALID_TOOL_EXTENSIONS = (".py",)

# Template types
PopulatorType = Literal["double_brace", "single_brace", "jinja2", "autodetect"]
PopulatorType = Literal["jinja2", "double_brace_regex", "single_brace_regex"]
Jinja2SecurityLevel = Literal["strict", "standard", "relaxed"]
22 changes: 11 additions & 11 deletions prompt_templates/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class PromptTemplateLoader:
... filename="code_teacher.yaml"
... )
>>> print(prompt_template)
ChatPromptTemplate(template=[{'role': 'system', 'content': 'You are a coding a..., template_variables=['concept', 'programming_language'], metadata={'name': 'Code Teacher', 'description': 'A simple ..., custom_data={}, populator_type='double_brace', populator=<prompt_templates.prompt_templates.DoubleBracePopula...)
ChatPromptTemplate(template=[{'role': 'system', 'content': 'You are a coding a..., template_variables=['concept', 'programming_language'], metadata={'name': 'Code Teacher', 'description': 'A simple ..., custom_data={}, populator='jinja2')
>>> prompt_template.template
[{'role': 'system', 'content': 'You are a coding assistant...'}, ...]
>>> prompt_template.metadata["name"]
Expand All @@ -43,7 +43,7 @@ class PromptTemplateLoader:
Load a template from a local file:
>>> prompt_template = PromptTemplateLoader.from_local("./tests/test_data/translate.yaml")
>>> print(template)
TextPromptTemplate(template='Translate the following text to {{language}}:\\n{{..., template_variables=['language', 'text'], metadata={'name': 'Simple Translator', 'description': 'A si..., custom_data={}, populator_type='double_brace', populator=<prompt_templates.prompt_templates.DoubleBracePopula...)
TextPromptTemplate(template='Translate the following text to {{language}}:\\n{{..., template_variables=['language', 'text'], metadata={'name': 'Simple Translator', 'description': 'A si..., custom_data={}, populator='jinja2')
>>> prompt_template.template
'Translate the following text to {language}:\\n{text}'
>>> prompt_template.template_variables
Expand All @@ -54,15 +54,15 @@ class PromptTemplateLoader:
def from_local(
cls,
path: Union[str, Path],
populator: PopulatorType = "double_brace",
populator: PopulatorType = "jinja2",
jinja2_security_level: Literal["strict", "standard", "relaxed"] = "standard",
yaml_library: str = "ruamel",
) -> Union[TextPromptTemplate, ChatPromptTemplate]:
"""Load a prompt template from a local YAML file.
Args:
path (Union[str, Path]): Path to the YAML file containing the prompt template
populator ([PopulatorType]): The populator type to use among Literal["double_brace", "single_brace", "jinja2", "autodetect"]. Defaults to "double_brace".
populator ([PopulatorType]): The populator type to use among Literal["double_brace_regex", "single_brace_regex", "jinja2"]. Defaults to "jinja2".
jinja2_security_level (Literal["strict", "standard", "relaxed"], optional): The security level for the Jinja2 populator. Defaults to "standard".
yaml_library (str, optional): The YAML library to use ("ruamel" or "pyyaml"). Defaults to "ruamel".
Expand All @@ -79,7 +79,7 @@ def from_local(
>>> from prompt_templates import PromptTemplateLoader
>>> prompt_template = PromptTemplateLoader.from_local("./tests/test_data/translate.yaml")
>>> print(prompt_template)
TextPromptTemplate(template='Translate the following text to {{language}}:\\n{{..., template_variables=['language', 'text'], metadata={'name': 'Simple Translator', 'description': 'A si..., custom_data={}, populator_type='double_brace', populator=<prompt_templates.prompt_templates.DoubleBracePopula...)
TextPromptTemplate(template='Translate the following text to {{language}}:\\n{{..., template_variables=['language', 'text'], metadata={'name': 'Simple Translator', 'description': 'A si..., custom_data={}, populator='jinja2')
>>> prompt_template.template
'Translate the following text to {language}:\\n{text}'
>>> prompt_template.template_variables
Expand All @@ -90,7 +90,7 @@ def from_local(
Download a chat prompt template:
>>> prompt_template = PromptTemplateLoader.from_local("./tests/test_data/code_teacher.yaml")
>>> print(prompt_template)
ChatPromptTemplate(template=[{'role': 'system', 'content': 'You are a coding assistant who explains concepts clearly and provides short examples.'}, {'role': 'user', 'content': 'Explain what {concept} is in {programming_language}.'}], template_variables=['concept', 'programming_language'], metadata={'name': 'Code Teacher', 'description': 'A simple ..., custom_data={}, populator_type='double_brace', populator=<prompt_templates.prompt_templates.DoubleBracePopula...)
ChatPromptTemplate(template=[{'role': 'system', 'content': 'You are a coding assistant who explains concepts clearly and provides short examples.'}, {'role': 'user', 'content': 'Explain what {concept} is in {programming_language}.'}], template_variables=['concept', 'programming_language'], metadata={'name': 'Code Teacher', 'description': 'A simple ..., custom_data={}, populator='jinja2')
>>> prompt_template.template
[{'role': 'system', 'content': 'You are a coding assistant who explains concepts clearly and provides short examples.'}, {'role': 'user', 'content': 'Explain what {concept} is in {programming_language}.'}]
>>> prompt_template.template_variables
Expand Down Expand Up @@ -130,7 +130,7 @@ def from_hub(
filename: str,
repo_type: str = "dataset",
revision: Optional[str] = None,
populator: PopulatorType = "double_brace",
populator: PopulatorType = "jinja2",
jinja2_security_level: Literal["strict", "standard", "relaxed"] = "standard",
yaml_library: str = "ruamel",
) -> Union[TextPromptTemplate, ChatPromptTemplate]:
Expand All @@ -146,7 +146,7 @@ def from_hub(
['dataset', 'model', 'space']. Defaults to "dataset"
revision (Optional[str], optional): Git revision to download from.
Can be a branch name, tag, or commit hash. Defaults to None
populator ([PopulatorType]): The populator type to use among Literal["double_brace", "single_brace", "jinja2", "autodetect"]. Defaults to "double_brace".
populator ([PopulatorType]): The populator type to use among Literal["double_brace_regex", "single_brace_regex", "jinja2"]. Defaults to "jinja2".
jinja2_security_level (Literal["strict", "standard", "relaxed"], optional): The security level for the Jinja2 populator. Defaults to "standard".
yaml_library (str, optional): The YAML library to use ("ruamel" or "pyyaml"). Defaults to "ruamel".
Expand All @@ -168,7 +168,7 @@ def from_hub(
... filename="translate.yaml"
... )
>>> print(prompt_template)
TextPromptTemplate(template='Translate the following text to {{language}}:\\n{{..., template_variables=['language', 'text'], metadata={'name': 'Simple Translator', 'description': 'A si..., custom_data={}, populator_type='double_brace', populator=<prompt_templates.prompt_templates.DoubleBracePopula...)
TextPromptTemplate(template='Translate the following text to {{language}}:\\n{{..., template_variables=['language', 'text'], metadata={'name': 'Simple Translator', 'description': 'A si..., custom_data={}, populator='jinja2')
>>> prompt_template.template
'Translate the following text to {language}:\\n{text}'
>>> prompt_template.template_variables
Expand All @@ -182,7 +182,7 @@ def from_hub(
... filename="code_teacher.yaml"
... )
>>> print(prompt_template)
ChatPromptTemplate(template=[{'role': 'system', 'content': 'You are a coding assistant who explains concepts clearly and provides short examples.'}, {'role': 'user', 'content': 'Explain what {concept} is in {programming_language}.'}], template_variables=['concept', 'programming_language'], metadata={'name': 'Code Teacher', 'description': 'A simple ..., custom_data={}, populator_type='double_brace', populator=<prompt_templates.prompt_templates.DoubleBracePopula...)
ChatPromptTemplate(template=[{'role': 'system', 'content': 'You are a coding assistant who explains concepts clearly and provides short examples.'}, {'role': 'user', 'content': 'Explain what {concept} is in {programming_language}.'}], template_variables=['concept', 'programming_language'], metadata={'name': 'Code Teacher', 'description': 'A simple ..., custom_data={}, populator='jinja2')
>>> prompt_template.template
[{'role': 'system', 'content': 'You are a coding assistant who explains concepts clearly and provides short examples.'}, {'role': 'user', 'content': 'Explain what {concept} is in {programming_language}.'}]
>>> prompt_template.template_variables
Expand Down Expand Up @@ -229,7 +229,7 @@ def from_hub(
@staticmethod
def _load_template_from_yaml(
prompt_file: Dict[str, Any],
populator: PopulatorType = "double_brace",
populator: PopulatorType = "jinja2",
jinja2_security_level: Literal["strict", "standard", "relaxed"] = "standard",
) -> Union[TextPromptTemplate, ChatPromptTemplate]:
"""Internal method to load a template from parsed YAML data.
Expand Down
Loading

0 comments on commit 8d52847

Please sign in to comment.