| 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 | {%- if not tools is defined %} |
| 8 | {%- set tools = none %} |
| 9 | {%- elif tools is not none %} |
| 10 | {%- set parallel_tool_prompt = "You are a helpful assistant that can call tools. If you call one or more tools, format them in a single JSON array or objects, where each object is a tool call, not as separate objects outside of an array or multiple arrays. Use the format [{\"name\": tool call name, \"arguments\": tool call arguments}, additional tool calls] if you call more than one tool. If you call tools, do not attempt to interpret them or otherwise provide a response until you receive a tool call result that you can interpret for the user." %} |
| 11 | {%- if system_message is defined %} |
| 12 | {%- set system_message = parallel_tool_prompt + "\n\n" + system_message %} |
| 13 | {%- else %} |
| 14 | {%- set system_message = parallel_tool_prompt %} |
| 15 | {%- endif %} |
| 16 | {%- endif %} |
| 17 | {%- set user_messages = loop_messages | selectattr("role", "equalto", "user") | list %} |
| 18 | |
| 19 | {%- for message in loop_messages | rejectattr("role", "equalto", "tool") | rejectattr("role", "equalto", "tool_results") | selectattr("tool_calls", "undefined") %} |
| 20 | {%- if (message["role"] == "user") != (loop.index0 % 2 == 0) %} |
| 21 | {{- raise_exception("After the optional system message, conversation roles must alternate user/assistant/user/assistant/...") }} |
| 22 | {%- endif %} |
| 23 | {%- endfor %} |
| 24 | |
| 25 | {{- bos_token }} |
| 26 | {%- for message in loop_messages %} |
| 27 | {%- if message["role"] == "user" %} |
| 28 | {%- if tools is not none and (message == user_messages[-1]) %} |
| 29 | {{- "[AVAILABLE_TOOLS] [" }} |
| 30 | {%- for tool in tools %} |
| 31 | {%- set tool = tool.function %} |
| 32 | {{- '{"type": "function", "function": {' }} |
| 33 | {%- for key, val in tool.items() if key != "return" %} |
| 34 | {%- if val is string %} |
| 35 | {{- '"' + key + '": "' + val + '"' }} |
| 36 | {%- else %} |
| 37 | {{- '"' + key + '": ' + val|tojson }} |
| 38 | {%- endif %} |
| 39 | {%- if not loop.last %} |
| 40 | {{- ", " }} |
| 41 | {%- endif %} |
| 42 | {%- endfor %} |
| 43 | {{- "}}" }} |
| 44 | {%- if not loop.last %} |
| 45 | {{- ", " }} |
| 46 | {%- else %} |
| 47 | {{- "]" }} |
| 48 | {%- endif %} |
| 49 | {%- endfor %} |
| 50 | {{- "[/AVAILABLE_TOOLS]" }} |
| 51 | {%- endif %} |
| 52 | {%- if loop.last and system_message is defined %} |
| 53 | {{- "[INST] " + system_message + "\n\n" + message["content"] + "[/INST]" }} |
| 54 | {%- else %} |
| 55 | {{- "[INST] " + message["content"] + "[/INST]" }} |
| 56 | {%- endif %} |
| 57 | {%- elif message["role"] == "tool_calls" or message.tool_calls is defined %} |
| 58 | {%- if message.tool_calls is defined %} |
| 59 | {%- set tool_calls = message.tool_calls %} |
| 60 | {%- else %} |
| 61 | {%- set tool_calls = message.content %} |
| 62 | {%- endif %} |
| 63 | {{- "[TOOL_CALLS] [" }} |
| 64 | {%- for tool_call in tool_calls %} |
| 65 | {%- set out = tool_call.function|tojson %} |
| 66 | {{- out[:-1] }} |
| 67 | {%- if not tool_call.id is defined or tool_call.id|length < 9 %} |
| 68 | {{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9! (1)" + tool_call.id) }} |
| 69 | {%- endif %} |
| 70 | {{- ', "id": "' + tool_call.id[-9:] + '"}' }} |
| 71 | {%- if not loop.last %} |
| 72 | {{- ", " }} |
| 73 | {%- else %} |
| 74 | {{- "]" + eos_token }} |
| 75 | {%- endif %} |
| 76 | {%- endfor %} |
| 77 | {%- elif message["role"] == "assistant" %} |
| 78 | {{- " " + message["content"] + eos_token }} |
| 79 | {%- elif message["role"] == "tool_results" or message["role"] == "tool" %} |
| 80 | {%- if message.content is defined and message.content.content is defined %} |
| 81 | {%- set content = message.content.content %} |
| 82 | {%- else %} |
| 83 | {%- set content = message.content %} |
| 84 | {%- endif %} |
| 85 | {{- '[TOOL_RESULTS] {"content": ' + content|string + ", " }} |
| 86 | {%- if not message.tool_call_id is defined or message.tool_call_id|length < 9 %} |
| 87 | {{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9! (2)" + message.tool_call_id) }} |
| 88 | {%- endif %} |
| 89 | {{- '"call_id": "' + message.tool_call_id[-9:] + '"}[/TOOL_RESULTS]' }} |
| 90 | {%- else %} |
| 91 | {{- raise_exception("Only user and assistant roles are supported, with the exception of an initial optional system message!") }} |
| 92 | {%- endif %} |
| 93 | {%- endfor %} |