Allow -t and --system to be used together, refs #916

This commit is contained in:
Simon Willison 2025-04-13 20:27:41 -07:00
parent 6273bc79ff
commit 4279df23a4
2 changed files with 18 additions and 10 deletions

View file

@ -574,8 +574,6 @@ def prompt(
if template:
params = dict(param)
# Cannot be used with system
if system:
raise click.ClickException("Cannot use -t/--template and --system together")
try:
template_obj = load_template(template)
except LoadTemplateError as ex:
@ -601,13 +599,15 @@ def prompt(
if "input" in template_obj.vars():
input_ = read_prompt()
try:
template_prompt, system = template_obj.evaluate(input_, params)
template_prompt, template_system = template_obj.evaluate(input_, params)
if template_prompt:
# Combine with user prompt
if prompt and "input" not in template_obj.vars():
prompt = template_prompt + "\n" + prompt
else:
prompt = template_prompt
if template_system and not system:
system = template_system
except Template.MissingVariables as ex:
raise click.ClickException(str(ex))
if model_id is None and template_obj.model:
@ -853,9 +853,6 @@ def chat(
template_obj = None
if template:
params = dict(param)
# Cannot be used with system
if system:
raise click.ClickException("Cannot use -t/--template and --system together")
try:
template_obj = load_template(template)
except LoadTemplateError as ex:
@ -929,9 +926,16 @@ def chat(
continue
if template_obj:
try:
prompt, system = template_obj.evaluate(prompt, params)
template_prompt, template_system = template_obj.evaluate(prompt, params)
except Template.MissingVariables as ex:
raise click.ClickException(str(ex))
if template_system and not system:
system = template_system
if template_prompt:
new_prompt = template_prompt
if prompt:
new_prompt += "\n" + prompt
prompt = new_prompt
if prompt.strip() in ("exit", "quit"):
break
response = conversation.prompt(prompt, system=system, **kwargs)

View file

@ -211,14 +211,18 @@ def test_templates_error_on_missing_schema(templates_path):
None,
None,
),
# -s system prompt should over-ride template system prompt
pytest.param(
"boo",
"Input text",
["-s", "s"],
["-s", "custom system"],
"gpt-4o-mini",
[
{"role": "system", "content": "custom system"},
{"role": "user", "content": "boo\nInput text"},
],
None,
None,
"Error: Cannot use -t/--template and --system together",
None,
marks=pytest.mark.httpx_mock(),
),
pytest.param(