| 1 | {%- if messages[0]["role"] == "system" %} |
| 2 | {%- set system_message = messages[0]["content"] %} |
| 3 | {%- set loop_messages = messages[1:] %} |
| 4 | {%- else %} |
| 5 | {%- set loop_messages = messages %} |
| 6 | {%- endif %} |
| 7 | |
| 8 | {%- if not tools is defined %} |
| 9 | {%- set tools = none %} |
| 10 | {%- endif %} |
| 11 | |
| 12 | {{- bos_token }} |
| 13 | {%- if system_message is defined %} |
| 14 | {{- "<|im_start|>system\n" + system_message + "<|im_end|>\n" }} |
| 15 | {%- endif %} |
| 16 | |
| 17 | {%- if tools is not none %} |
| 18 | {{- "<|im_start|>system name=<|plugin|>\n[" }} |
| 19 | {%- for tool in tools %} |
| 20 | {{- tool.function|tojson }} |
| 21 | {%- if not loop.last %} |
| 22 | {{- ", " }} |
| 23 | {%- else %} |
| 24 | {{- "]" }} |
| 25 | {%- endif %} |
| 26 | {%- endfor %} |
| 27 | {{- "<|im_end|>\n" }} |
| 28 | {%- endif %} |
| 29 | |
| 30 | {%- for message in loop_messages %} |
| 31 | {%- if message["role"] == "user" %} |
| 32 | {{- "<|im_start|>user\n" + message["content"] + "<|im_end|>\n"}} |
| 33 | {%- elif message.tool_calls is defined and message.tool_calls is not none %} |
| 34 | {%- set content = message["content"] if message["content"] else "" %} |
| 35 | {{- "<|im_start|>assistant\n" + content }} |
| 36 | {%- for tool_call in message.tool_calls %} |
| 37 | {%- set function=tool_call.function %} |
| 38 | {{- "<|action_start|><|plugin|>\n" }} |
| 39 | {{- '{"name": "' + function.name + '", '}} |
| 40 | {{- '"arguments": ' + function.arguments|tojson + '}' }} |
| 41 | {{- "<|action_end|>" }} |
| 42 | {%- endfor %} |
| 43 | {{- "<|im_end|>\n" }} |
| 44 | {%- elif message["role"] == "assistant" %} |
| 45 | {{- "<|im_start|>assistant\n" + message["content"] + "<|im_end|>\n"}} |
| 46 | {%- elif message["role"] == "tool_results" or message["role"] == "tool" or message["role"] == "function" %} |
| 47 | {%- if message.content is defined and message.content.content is defined %} |
| 48 | {%- set content = message.content.content %} |
| 49 | {%- else %} |
| 50 | {%- set content = message.content %} |
| 51 | {%- endif %} |
| 52 | {{- "<|im_start|>environment name=<|plugin|>\n" + content|string + "<|im_end|>\n" }} |
| 53 | {%- else %} |
| 54 | {{- raise_exception("Only user and assistant and tool_results and tool and function roles are supported, with the exception of an initial optional system message!") }} |
| 55 | {%- endif %} |
| 56 | {%- endfor %} |
| 57 | |
| 58 | {%- if add_generation_prompt %} |
| 59 | {{- '<|im_start|>assistant\n' }} |
| 60 | {%- endif %} |