Model: Add model folder template support

Like tabby_config.yml in the model's folder, a custom template can
also be provided via tabby_template.yml in addition to the existing
templates folder. The config.yml always takes priority.

Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
kingbri
2024-09-07 22:15:42 -04:00
parent b576a2f116
commit acd3eb1140
2 changed files with 25 additions and 8 deletions

View File

@@ -106,20 +106,26 @@ class PromptTemplate:
self.template = self.compile(raw_template)
@classmethod
def from_file(self, prompt_template_name: str):
def from_file(self, template_path: pathlib.Path):
"""Get a template from a jinja file."""
template_path = pathlib.Path(f"templates/{prompt_template_name}.jinja")
# Add the jinja extension if it isn't provided
if template_path.suffix.endswith(".jinja"):
template_name = template_path.name.split(".jinja")[0]
else:
template_name = template_path.name
template_path = template_path.with_suffix(".jinja")
if template_path.exists():
with open(template_path, "r", encoding="utf8") as raw_template_stream:
return PromptTemplate(
name=prompt_template_name,
name=template_name,
raw_template=raw_template_stream.read(),
)
else:
# Let the user know if the template file isn't found
raise TemplateLoadError(
f'Chat template "{prompt_template_name}" not found in files.'
f'Chat template "{template_name}" not found in files.'
)
@classmethod