patch: Handle how templates are rendered if no system prompt is allowed
This commit is contained in:
parent
4a018e706f
commit
8b9ed888bc
1 changed files with 24 additions and 13 deletions
|
@ -59,21 +59,32 @@ def display_chat_template(chat_template: str, format_template: bool = False):
|
||||||
if format_template:
|
if format_template:
|
||||||
# Render the formatted template using Jinja2 with a context that includes 'bos_token' and 'eos_token'
|
# Render the formatted template using Jinja2 with a context that includes 'bos_token' and 'eos_token'
|
||||||
env = jinja2.Environment(
|
env = jinja2.Environment(
|
||||||
loader=jinja2.BaseLoader(),
|
loader=jinja2.BaseLoader(), trim_blocks=True, lstrip_blocks=True
|
||||||
trim_blocks=True,
|
|
||||||
lstrip_blocks=True,
|
|
||||||
)
|
)
|
||||||
logger.info(chat_template)
|
|
||||||
template = env.from_string(chat_template)
|
template = env.from_string(chat_template)
|
||||||
formatted_template = template.render(
|
|
||||||
messages = [
|
messages = [
|
||||||
{"role": "system", "content": "I am a helpful assistant."},
|
{"role": "system", "content": "I am a helpful assistant."},
|
||||||
{"role": "user", "content": "Hello!"},
|
{"role": "user", "content": "Hello!"},
|
||||||
{"role": "assistant", "content": "Hello! How may I assist you today?"},
|
{"role": "assistant", "content": "Hello! How may I assist you today?"},
|
||||||
],
|
]
|
||||||
bos_token="[BOS]",
|
bos_token = "<s>"
|
||||||
eos_token="[EOS]",
|
eos_token = "</s>"
|
||||||
|
|
||||||
|
try:
|
||||||
|
formatted_template = template.render(
|
||||||
|
messages=messages,
|
||||||
|
bos_token=bos_token,
|
||||||
|
eos_token=eos_token,
|
||||||
)
|
)
|
||||||
|
except jinja2.exceptions.UndefinedError:
|
||||||
|
# system message is incompatible with set format
|
||||||
|
formatted_template = template.render(
|
||||||
|
messages=messages[1:],
|
||||||
|
bos_token=bos_token,
|
||||||
|
eos_token=eos_token,
|
||||||
|
)
|
||||||
|
|
||||||
print(formatted_template)
|
print(formatted_template)
|
||||||
else:
|
else:
|
||||||
# Display the raw template
|
# Display the raw template
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue