main
jinja 86 lines 3.87 KB
Raw
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 {%- endif %}
10 {%- set user_messages = loop_messages | selectattr("role", "equalto", "user") | list %}
11
12 {%- for message in loop_messages | rejectattr("role", "equalto", "tool") | rejectattr("role", "equalto", "tool_results") | selectattr("tool_calls", "undefined") %}
13 {%- if (message["role"] == "user") != (loop.index0 % 2 == 0) %}
14 {{- raise_exception("After the optional system message, conversation roles must alternate user/assistant/user/assistant/...") }}
15 {%- endif %}
16 {%- endfor %}
17
18 {{- bos_token }}
19 {%- for message in loop_messages %}
20 {%- if message["role"] == "user" %}
21 {%- if tools is not none and (message == user_messages[-1]) %}
22 {{- "[AVAILABLE_TOOLS] [" }}
23 {%- for tool in tools %}
24 {%- set tool = tool.function %}
25 {{- '{"type": "function", "function": {' }}
26 {%- for key, val in tool.items() if key != "return" %}
27 {%- if val is string %}
28 {{- '"' + key + '": "' + val + '"' }}
29 {%- else %}
30 {{- '"' + key + '": ' + val|tojson }}
31 {%- endif %}
32 {%- if not loop.last %}
33 {{- ", " }}
34 {%- endif %}
35 {%- endfor %}
36 {{- "}}" }}
37 {%- if not loop.last %}
38 {{- ", " }}
39 {%- else %}
40 {{- "]" }}
41 {%- endif %}
42 {%- endfor %}
43 {{- "[/AVAILABLE_TOOLS]" }}
44 {%- endif %}
45 {%- if loop.last and system_message is defined %}
46 {{- "[INST] " + system_message + "\n\n" + message["content"] + "[/INST]" }}
47 {%- else %}
48 {{- "[INST] " + message["content"] + "[/INST]" }}
49 {%- endif %}
50 {%- elif message["role"] == "tool_calls" or message.tool_calls is defined %}
51 {%- if message.tool_calls is defined %}
52 {%- set tool_calls = message.tool_calls %}
53 {%- else %}
54 {%- set tool_calls = message.content %}
55 {%- endif %}
56 {{- "[TOOL_CALLS] [" }}
57 {%- for tool_call in tool_calls %}
58 {%- set out = tool_call.function|tojson %}
59 {{- out[:-1] }}
60 {%- if not tool_call.id is defined or tool_call.id|length < 9 %}
61 {{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9! (1)" + tool_call.id) }}
62 {%- endif %}
63 {{- ', "id": "' + tool_call.id[-9:] + '"}' }}
64 {%- if not loop.last %}
65 {{- ", " }}
66 {%- else %}
67 {{- "]" + eos_token }}
68 {%- endif %}
69 {%- endfor %}
70 {%- elif message["role"] == "assistant" %}
71 {{- " " + message["content"] + eos_token }}
72 {%- elif message["role"] == "tool_results" or message["role"] == "tool" %}
73 {%- if message.content is defined and message.content.content is defined %}
74 {%- set content = message.content.content %}
75 {%- else %}
76 {%- set content = message.content %}
77 {%- endif %}
78 {{- '[TOOL_RESULTS] {"content": ' + content|string + ", " }}
79 {%- if not message.tool_call_id is defined or message.tool_call_id|length < 9 %}
80 {{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9! (2)" + message.tool_call_id) }}
81 {%- endif %}
82 {{- '"call_id": "' + message.tool_call_id[-9:] + '"}[/TOOL_RESULTS]' }}
83 {%- else %}
84 {{- raise_exception("Only user and assistant roles are supported, with the exception of an initial optional system message!") }}
85 {%- endif %}
86 {%- endfor %}