Templates: Add auto-detection from path

This replicates FastChat's model path detection.

Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
kingbri
2023-12-17 15:41:09 -05:00
committed by Brian Dashore
parent e895eaa4bd
commit 7cbc08fc72
2 changed files with 30 additions and 14 deletions

View File

@@ -1,7 +1,8 @@
import pathlib
from functools import lru_cache
from importlib.metadata import version as package_version
from packaging import version
from jinja2.sandbox import ImmutableSandboxedEnvironment
from packaging import version
from pydantic import BaseModel
# Small replication of AutoTokenizer's chat template system for efficiency
@@ -28,3 +29,10 @@ def _compile_template(template: str):
jinja_env = ImmutableSandboxedEnvironment(trim_blocks = True, lstrip_blocks = True)
jinja_template = jinja_env.from_string(template)
return jinja_template
def get_template_from_file(prompt_template_name):
with open(pathlib.Path(f"templates/{prompt_template_name}.jinja"), "r") as raw_template:
return PromptTemplate(
name = prompt_template_name,
template = raw_template.read()
)