| {{ bos_token }} |
| |
| {%- if messages[0]['role'] == 'system' -%} |
| {%- set sys_content = (messages[0]['content'] if messages[0]['content'] is string else messages[0]['content'][0]['text']) -%} |
| {%- if tools is defined and tools is not none -%} |
| {%- set sys_content = sys_content + '\n\nTools (functions) are available. Respond with a python list *inside* a ```tool_code``` block when you decide to call them.\nExample:\n```tool_code\n[func_name(arg="value")]\n```\n\nAvailable tools (Schemas):\n' + tools | tojson(indent=4) -%} |
| {%- endif -%} |
| {%- set first_user_prefix = sys_content + '\n\n' -%} |
| {%- set loop_messages = messages[1:] -%} |
| {%- else -%} |
| {%- set first_user_prefix = '' -%} |
| {%- set loop_messages = messages -%} |
| {%- endif -%} |
| |
| {%- for m in loop_messages -%} |
| {%- if not loop.first -%} |
| {%- set prev_msg = loop_messages[loop.index0-1] -%} |
| |
| {%- if prev_msg['role'] == 'user' -%} |
| {%- if m['role'] != 'assistant' -%} |
| {{ raise_exception('User message must be followed by assistant') }} |
| {%- endif -%} |
| {%- elif prev_msg['role'] == 'assistant' and 'tool_calls' in prev_msg -%} |
| {%- if m['role'] != 'tool' -%} |
| {{ raise_exception('Assistant with tool_calls must be followed by tool') }} |
| {%- endif -%} |
| {%- elif prev_msg['role'] == 'assistant' and 'tool_calls' not in prev_msg -%} |
| {%- if m['role'] not in ['user', 'tool'] -%} |
| {{ raise_exception('Assistant without tool_calls must be followed by user or tool') }} |
| {%- endif -%} |
| {%- elif prev_msg['role'] == 'tool' -%} |
| {%- if m['role'] not in ['assistant', 'user'] -%} |
| {{ raise_exception('Tool message must be followed by assistant or user') }} |
| {%- endif -%} |
| {%- endif -%} |
| {%- endif -%} |
| {%- endfor -%} |
| |
| {%- for message in loop_messages -%} |
| {%- set role = 'model' if message['role'] == 'assistant' else ('user' if message['role']=='tool' else message['role']) -%} |
| {{ '<start_of_turn>' ~ role ~ '\n' }} |
| |
| {%- if loop.first -%} |
| {{ first_user_prefix }} |
| {%- endif -%} |
| |
| {%- if message['role'] in ['user','assistant'] and 'tool_calls' not in message -%} |
| {%- if message['content'] is string -%} |
| {{ message['content'] | trim }} |
| {%- elif message['content'] is iterable -%} |
| {%- for item in message['content'] -%} |
| {%- if item['type']=='image' -%}<start_of_image>{%- elif item['type']=='text' -%}{{ item['text'] | trim }}{%- endif -%} |
| {%- endfor -%} |
| {%- endif -%} |
| {%- endif -%} |
| |
| {%- if message['role'] == 'tool' -%} |
| ```tool_response |
| {{ message['content'] | trim }} |
| ``` |
| {%- endif -%} |
| |
| {%- if 'tool_calls' in message -%} |
| ```tool_code |
| [ |
| {%- for tc in message.tool_calls -%} |
| {%- set fc = tc.function if tc.function is defined else tc -%} |
| {{ fc.name }}( |
| {%- if fc.arguments is iterable and fc.arguments is mapping -%} |
| {%- set first=true -%} |
| {%- for k,v in fc.arguments.items() -%} |
| {%- if not first %}, {% endif -%} |
| {{ k }}={{ v | tojson }}{%- set first=false -%} |
| {%- endfor -%} |
| {%- elif fc.arguments is iterable -%} |
| {{ fc.arguments | map('tojson') | join(', ') }} |
| {%- else -%} |
| {{ fc.arguments | tojson }} |
| {%- endif -%} |
| ) |
| {%- if not loop.last %}, |
| {%- endif -%} |
| {%- endfor -%} |
| ] |
| ``` |
| {%- endif -%} |
| |
| {{ '<end_of_turn>\n' }} |
| {%- endfor -%} |
| |
| {%- if add_generation_prompt -%} |
| <start_of_turn>model\n |
| {%- endif -%} |
| |
| |