-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Open
Description
Describe the bug
In the custom chat template example provided in smol-course, unit1/2.md, advanced-template-customization, the system_message variable is set inside a for loop:
custom_template = """
{%- for message in messages %}
{%- if message['role'] == 'system' %}
{%- set system_message = message['content'] %}
{%- endif %}
{%- endfor %}
{%- if system_message is defined %}
<|system|>{{ system_message }}<|end|>
{%- endif %}
... (rest omitted)
"""However, due to Jinja2 scoping rules, variables set inside a loop are not accessible outside that loop's scope. As a result, system_message will not be defined in the template body outside the loop, and the system message will not be rendered properly.
To Reproduce
add a system message to the debug_messages, and watch the formatted text in the output, no system message will be shown
# Example usage
debug_messages = [
{"role": "system", "content": "You are a helpful assistant."}, # add a system message
{"role": "user", "content": "Hello!"},
{"role": "assistant", "content": "Hi there!"}
]Screenshots
Additional context
Add any other context about the problem here.
Metadata
Metadata
Assignees
Labels
No labels