Interprompt: Add location to prompt templates, allow to retrieve template

This commit is contained in:
Dominik Jain committed 2026-04-27 12:19:36 +02:00
1 parent d4c75224d3
commit 621ca5cf44
2 files changed
+11 -6

No files matched your search

+5 -3
View File
@@ -12,8 +12,9 @@ log = logging.getLogger(__name__)
class PromptTemplate(ToStringMixin, ParameterizedTemplateInterface):
def __init__(self, name: str, jinja_template_string: str) -> None:
def __init__(self, name: str, jinja_template_string: str, path: str) -> None:
self.name = name
self.path = path
self._jinja_template = JinjaTemplate(jinja_template_string.strip())
def _tostring_exclude_private(self) -> bool:
@@ -252,6 +253,7 @@ class MultiLangPromptCollection:
self,
name: str,
template_str: str,
path: str,
lang_code: str = DEFAULT_LANG_CODE,
on_name_collision: Literal["skip", "overwrite", "raise"] = "raise",
) -> None:
@@ -262,7 +264,7 @@ class MultiLangPromptCollection:
:param on_name_collision: how to deal with name/lang_code collisions
"""
allow_overwrite = False
prompt_template = PromptTemplate(name, template_str)
prompt_template = PromptTemplate(name, template_str, path=path)
mlpt = self._multi_lang_prompt_templates.get(name)
if mlpt is None:
mlpt = MultiLangPromptTemplate(name)
@@ -330,7 +332,7 @@ class MultiLangPromptCollection:
self._add_prompt_list(prompt_name, prompt_template_or_list, lang_code=lang_code, on_name_collision=on_name_collision)
elif isinstance(prompt_template_or_list, str):
self._add_prompt_template(
prompt_name, prompt_template_or_list, lang_code=lang_code, on_name_collision=on_name_collision
prompt_name, prompt_template_or_list, lang_code=lang_code, path=path, on_name_collision=on_name_collision
)
else:
raise ValueError(
+6 -3
View File
@@ -23,15 +23,18 @@ class PromptFactoryBase:
self.lang_code = lang_code
self._prompt_collection = MultiLangPromptCollection(prompts_dir, fallback_mode=fallback_mode)
def _get_prompt_template(self, prompt_name: str) -> PromptTemplate:
def get_prompt_names(self) -> list[str]:
return self._prompt_collection.get_prompt_template_names()
def get_prompt_template(self, prompt_name: str) -> PromptTemplate:
return self._prompt_collection.get_prompt_template(prompt_name, lang_code=self.lang_code)
def get_prompt_template_string(self, prompt_name: str) -> str:
return self._get_prompt_template(prompt_name).get_template_string()
return self.get_prompt_template(prompt_name).get_template_string()
def _render_prompt(self, prompt_name: str, params: dict[str, Any]) -> str:
del params["self"]
return self._get_prompt_template(prompt_name).render(**params)
return self.get_prompt_template(prompt_name).render(**params)
def _get_prompt_list(self, prompt_name: str) -> PromptList:
return self._prompt_collection.get_prompt_list(prompt_name, self.lang_code)