| 1 | {%- if tools %} |
| 2 | {%- if messages and messages[0]['role'] != 'system' %} |
| 3 | {{- '<|start_of_role|>system<|end_of_role|>You are a helpful AI assistant with access to the following tools. When a tool is required to answer the user query, respond with <|tool_call|> followed by a JSON list of tools used.<|end_of_text|> |
| 4 | ' }} |
| 5 | {%- endif %} |
| 6 | {{- '<|start_of_role|>available_tools<|end_of_role|> |
| 7 | ' }} |
| 8 | {%- for tool in tools %} |
| 9 | {{- tool | tojson(indent=4) }} |
| 10 | {%- if not loop.last %} |
| 11 | {{- ' |
| 12 | |
| 13 | ' }} |
| 14 | {%- endif %} |
| 15 | {%- endfor %} |
| 16 | {{- '<|end_of_text|> |
| 17 | ' }} |
| 18 | {%- endif %} |
| 19 | |
| 20 | {%- for message in messages %} |
| 21 | {%- if message['role'] == 'system' %} |
| 22 | {{- '<|start_of_role|>system<|end_of_role|>' + message['content'] + '<|end_of_text|> |
| 23 | ' }} |
| 24 | {%- elif message['role'] == 'user' %} |
| 25 | {{- '<|start_of_role|>user<|end_of_role|>' + message['content'] + '<|end_of_text|> |
| 26 | ' }} |
| 27 | {%- elif message['role'] == 'assistant_tool_call' or (message['role'] == 'assistant' and message.tool_calls is defined) %} |
| 28 | {{- '<|start_of_role|>assistant<|end_of_role|><|tool_call|>' + message.tool_calls|map(attribute='function')|list|tojson(indent=4) + '<|end_of_text|> |
| 29 | ' }} |
| 30 | {%- elif message['role'] == 'assistant' %} |
| 31 | {{- '<|start_of_role|>assistant<|end_of_role|>' + message['content'] + '<|end_of_text|> |
| 32 | ' }} |
| 33 | {%- elif message['role'] == 'tool_response' or message['role'] == 'tool' %} |
| 34 | {{- '<|start_of_role|>tool_response<|end_of_role|>' + message['content'] + '<|end_of_text|> |
| 35 | ' }} |
| 36 | {%- endif %} |
| 37 | {%- if loop.last and add_generation_prompt %} |
| 38 | {{- '<|start_of_role|>assistant<|end_of_role|>' }} |
| 39 | {%- endif %} |
| 40 | {%- endfor %} |