Add --chat-template-file flag to override the default instruction template for API requests

Matches llama.cpp's flag name. Supports .jinja, .jinja2, and .yaml files.
Priority: per-request params > --chat-template-file > model's built-in template.
This commit is contained in:
oobabooga 2026-03-06 13:33:24 -03:00
parent 3531069824
commit f5acf55207
3 changed files with 26 additions and 3 deletions

View file

@ -215,6 +215,7 @@ group.add_argument('--sampler-priority', type=str, default=_d['sampler_priority'
group.add_argument('--dry-sequence-breakers', type=str, default=_d['dry_sequence_breakers'], metavar='N', help='DRY sequence breakers')
group.add_argument('--enable-thinking', action=argparse.BooleanOptionalAction, default=True, help='Enable thinking')
group.add_argument('--reasoning-effort', type=str, default='medium', metavar='N', help='Reasoning effort')
group.add_argument('--chat-template-file', type=str, default=None, help='Path to a chat template file (.jinja, .jinja2, or .yaml) to use as the default instruction template for API requests. Overrides the model\'s built-in template.')
# Handle CMD_FLAGS.txt
cmd_flags_path = user_data_dir / "CMD_FLAGS.txt"
@ -376,6 +377,11 @@ default_settings = copy.deepcopy(settings)
def do_cmd_flags_warnings():
# Validate --chat-template-file
if args.chat_template_file and not Path(args.chat_template_file).is_file():
logger.error(f"--chat-template-file: file not found: {args.chat_template_file}")
sys.exit(1)
# Security warnings
if args.trust_remote_code:
logger.warning(

View file

@ -226,7 +226,7 @@ def clean_path(base_path: str, path: str):
def get_instruction_templates():
path = shared.user_data_dir / 'instruction-templates'
names = set()
for ext in ['yaml', 'yml', 'jinja']:
for ext in ['yaml', 'yml', 'jinja', 'jinja2']:
for f in path.glob(f'*.{ext}'):
names.add(f.stem)
return ['None', 'Chat Template'] + sorted(names, key=utils.natural_keys)
@ -235,10 +235,10 @@ def get_instruction_templates():
def load_template(name):
"""Load a Jinja2 template string from {user_data_dir}/instruction-templates/."""
path = shared.user_data_dir / 'instruction-templates'
for ext in ['jinja', 'yaml', 'yml']:
for ext in ['jinja', 'jinja2', 'yaml', 'yml']:
filepath = path / f'{name}.{ext}'
if filepath.exists():
if ext == 'jinja':
if ext in ['jinja', 'jinja2']:
return filepath.read_text(encoding='utf-8')
else:
data = yaml.safe_load(filepath.read_text(encoding='utf-8'))