mirror of
https://github.com/theroyallab/tabbyAPI.git
synced 2026-04-19 22:08:59 +00:00
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:
@@ -401,19 +401,30 @@ class ExllamaV2Container:
|
||||
find_template_functions = [
|
||||
lambda: PromptTemplate.from_model_json(
|
||||
pathlib.Path(self.config.model_dir) / "tokenizer_config.json",
|
||||
"chat_template",
|
||||
key="chat_template",
|
||||
),
|
||||
lambda: PromptTemplate.from_file(find_template_from_model(model_directory)),
|
||||
]
|
||||
|
||||
# Find the template in the model directory if it exists
|
||||
model_dir_template_path = (
|
||||
pathlib.Path(self.config.model_dir) / "tabby_template.jinja"
|
||||
)
|
||||
if model_dir_template_path.exists():
|
||||
find_template_functions[:0] = [
|
||||
lambda: PromptTemplate.from_file(model_dir_template_path)
|
||||
]
|
||||
|
||||
# Add lookup from prompt template name if provided
|
||||
if prompt_template_name:
|
||||
find_template_functions[:0] = [
|
||||
lambda: PromptTemplate.from_file(prompt_template_name),
|
||||
lambda: PromptTemplate.from_file(
|
||||
pathlib.Path("templates") / prompt_template_name
|
||||
),
|
||||
lambda: PromptTemplate.from_model_json(
|
||||
pathlib.Path(self.config.model_dir) / "tokenizer_config.json",
|
||||
"chat_template",
|
||||
prompt_template_name,
|
||||
key="chat_template",
|
||||
name=prompt_template_name,
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user