AlexHung29629 commited on
Commit
559fd00
·
verified ·
1 Parent(s): e79e207

Rename chat_template.json to chat_template.jinja

Browse files
Files changed (2) hide show
  1. chat_template.jinja +109 -0
  2. chat_template.json +0 -3
chat_template.jinja ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- set today = strftime_now("%Y-%m-%d") %}
2
+ {%- set default_system_message = "detailed thinking on" %}
3
+ {{- bos_token }}
4
+ {%- if messages[0]['role'] == 'system' %}
5
+ {%- if messages[0]['content'] is string %}
6
+ {%- set system_message = messages[0]['content'] %}
7
+ {%- set loop_messages = messages[1:] %}
8
+ {%- else %}
9
+ {%- set system_message = messages[0]['content'][0]['text'] %}
10
+ {%- set loop_messages = messages[1:] %}
11
+ {%- endif %}
12
+ {%- else %}
13
+ {%- set system_message = default_system_message %}
14
+ {%- set loop_messages = messages %}
15
+ {%- endif %}
16
+ {%- if not tools is defined %}
17
+ {%- set tools = none %}
18
+ {%- elif tools is not none %}
19
+ {%- 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." %}
20
+ {%- if system_message is defined %}
21
+ {%- set system_message = system_message + "\n\n" + parallel_tool_prompt %}
22
+ {%- else %}
23
+ {%- set system_message = parallel_tool_prompt %}
24
+ {%- endif %}
25
+ {%- endif %}
26
+ {{- '[SYSTEM_PROMPT]' + system_message + '[/SYSTEM_PROMPT]' }}
27
+ {%- set user_messages = loop_messages | selectattr("role", "equalto", "user") | list %}
28
+ {%- for message in loop_messages %}
29
+ {%- if message["role"] == "user" %}
30
+ {%- if tools is not none and (message == user_messages[-1]) %}
31
+ {{- "[AVAILABLE_TOOLS] [" }}
32
+ {%- for tool in tools %}
33
+ {%- set tool = tool.function %}
34
+ {{- '{"type": "function", "function": {' }}
35
+ {%- for key, val in tool.items() if key != "return" %}
36
+ {%- if val is string %}
37
+ {{- '"' + key + '": "' + val + '"' }}
38
+ {%- else %}
39
+ {{- '"' + key + '": ' + val|tojson }}
40
+ {%- endif %}
41
+ {%- if not loop.last %}
42
+ {{- ", " }}
43
+ {%- endif %}
44
+ {%- endfor %}
45
+ {{- "}}" }}
46
+ {%- if not loop.last %}
47
+ {{- ", " }}
48
+ {%- else %}
49
+ {{- "]" }}
50
+ {%- endif %}
51
+ {%- endfor %}
52
+ {{- "[/AVAILABLE_TOOLS]" }}
53
+ {%- endif %}
54
+ {%- if message['content'] is string %}
55
+ {{- '[INST]' + message['content'] + '[/INST]' }}
56
+ {%- else %}
57
+ {{- '[INST]' }}
58
+ {%- for block in message['content'] %}
59
+ {%- if block['type'] == 'text' %}
60
+ {{- block['text'] }}
61
+ {%- elif block['type'] == 'image' or block['type'] == 'image_url' %}
62
+ {{- '[IMG]' }}
63
+ {%- else %}
64
+ {{- raise_exception('Only text and image blocks are supported in message content!') }}
65
+ {%- endif %}
66
+ {%- endfor %}
67
+ {{- '[/INST]' }}
68
+ {%- endif %}
69
+ {%- elif message["role"] == "tool_calls" or message.tool_calls is defined %}
70
+ {%- if message.tool_calls is defined %}
71
+ {%- set tool_calls = message.tool_calls %}
72
+ {%- else %}
73
+ {%- set tool_calls = message.content %}
74
+ {%- endif %}
75
+ {{- "[TOOL_CALLS] [" }}
76
+ {%- for tool_call in tool_calls %}
77
+ {%- set out = tool_call.function|tojson %}
78
+ {{- out[:-1] }}
79
+ {%- if not tool_call.id is defined or tool_call.id|length < 9 %}
80
+ {{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9! (1)" + tool_call.id) }}
81
+ {%- endif %}
82
+ {{- ', "id": "' + tool_call.id[-9:] + '"}' }}
83
+ {%- if not loop.last %}
84
+ {{- ", " }}
85
+ {%- else %}
86
+ {{- "]" + eos_token }}
87
+ {%- endif %}
88
+ {%- endfor %}
89
+ {%- elif message['role'] == 'assistant' %}
90
+ {%- if message['content'] is string %}
91
+ {{- message['content'] + eos_token }}
92
+ {%- else %}
93
+ {{- message['content'][0]['text'] + eos_token }}
94
+ {%- endif %}
95
+ {%- elif message["role"] == "tool_results" or message["role"] == "tool" %}
96
+ {%- if message.content is defined and message.content.content is defined %}
97
+ {%- set content = message.content.content %}
98
+ {%- else %}
99
+ {%- set content = message.content %}
100
+ {%- endif %}
101
+ {{- '[TOOL_RESULTS] {"content": ' + content|string + ", " }}
102
+ {%- if not message.tool_call_id is defined or message.tool_call_id|length < 9 %}
103
+ {{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9! (2)" + message.tool_call_id) }}
104
+ {%- endif %}
105
+ {{- '"call_id": "' + message.tool_call_id[-9:] + '"}[/TOOL_RESULTS]' }}
106
+ {%- else %}
107
+ {{- raise_exception("Only user and assistant roles are supported, with the exception of an initial optional system message!") }}
108
+ {%- endif %}
109
+ {%- endfor %}
chat_template.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "chat_template": "{%- set today = strftime_now(\"%Y-%m-%d\") %}\n{%- set default_system_message = \"detailed thinking on\" %}\n{{- bos_token }}\n{%- if messages[0]['role'] == 'system' %}\n {%- if messages[0]['content'] is string %}\n {%- set system_message = messages[0]['content'] %}\n {%- set loop_messages = messages[1:] %}\n {%- else %}\n {%- set system_message = messages[0]['content'][0]['text'] %}\n {%- set loop_messages = messages[1:] %}\n {%- endif %}\n{%- else %}\n {%- set system_message = default_system_message %}\n {%- set loop_messages = messages %}\n{%- endif %}\n{%- if not tools is defined %}\n {%- set tools = none %}\n{%- elif tools is not none %}\n {%- 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.\" %}\n {%- if system_message is defined %}\n {%- set system_message = system_message + \"\\n\\n\" + parallel_tool_prompt %}\n {%- else %}\n {%- set system_message = parallel_tool_prompt %}\n {%- endif %}\n{%- endif %}\n{{- '[SYSTEM_PROMPT]' + system_message + '[/SYSTEM_PROMPT]' }}\n{%- set user_messages = loop_messages | selectattr(\"role\", \"equalto\", \"user\") | list %}\n{%- for message in loop_messages %}\n {%- if message[\"role\"] == \"user\" %}\n {%- if tools is not none and (message == user_messages[-1]) %}\n {{- \"[AVAILABLE_TOOLS] [\" }}\n {%- for tool in tools %}\n {%- set tool = tool.function %}\n {{- '{\"type\": \"function\", \"function\": {' }}\n {%- for key, val in tool.items() if key != \"return\" %}\n {%- if val is string %}\n {{- '\"' + key + '\": \"' + val + '\"' }}\n {%- else %}\n {{- '\"' + key + '\": ' + val|tojson }}\n {%- endif %}\n {%- if not loop.last %}\n {{- \", \" }}\n {%- endif %}\n {%- endfor %}\n {{- \"}}\" }}\n {%- if not loop.last %}\n {{- \", \" }}\n {%- else %}\n {{- \"]\" }}\n {%- endif %}\n {%- endfor %}\n {{- \"[/AVAILABLE_TOOLS]\" }}\n {%- endif %}\n {%- if message['content'] is string %}\n {{- '[INST]' + message['content'] + '[/INST]' }}\n {%- else %}\n {{- '[INST]' }}\n {%- for block in message['content'] %}\n {%- if block['type'] == 'text' %}\n {{- block['text'] }}\n {%- elif block['type'] == 'image' or block['type'] == 'image_url' %}\n {{- '[IMG]' }}\n {%- else %}\n {{- raise_exception('Only text and image blocks are supported in message content!') }}\n {%- endif %}\n {%- endfor %}\n {{- '[/INST]' }}\n {%- endif %}\n {%- elif message[\"role\"] == \"tool_calls\" or message.tool_calls is defined %}\n {%- if message.tool_calls is defined %}\n {%- set tool_calls = message.tool_calls %}\n {%- else %}\n {%- set tool_calls = message.content %}\n {%- endif %}\n {{- \"[TOOL_CALLS] [\" }}\n {%- for tool_call in tool_calls %}\n {%- set out = tool_call.function|tojson %}\n {{- out[:-1] }}\n {%- if not tool_call.id is defined or tool_call.id|length < 9 %}\n {{- raise_exception(\"Tool call IDs should be alphanumeric strings with length >= 9! (1)\" + tool_call.id) }}\n {%- endif %}\n {{- ', \"id\": \"' + tool_call.id[-9:] + '\"}' }}\n {%- if not loop.last %}\n {{- \", \" }}\n {%- else %}\n {{- \"]\" + eos_token }}\n {%- endif %}\n {%- endfor %}\n {%- elif message['role'] == 'assistant' %}\n {%- if message['content'] is string %}\n {{- message['content'] + eos_token }}\n {%- else %}\n {{- message['content'][0]['text'] + eos_token }}\n {%- endif %}\n {%- elif message[\"role\"] == \"tool_results\" or message[\"role\"] == \"tool\" %}\n {%- if message.content is defined and message.content.content is defined %}\n {%- set content = message.content.content %}\n {%- else %}\n {%- set content = message.content %}\n {%- endif %}\n {{- '[TOOL_RESULTS] {\"content\": ' + content|string + \", \" }}\n {%- if not message.tool_call_id is defined or message.tool_call_id|length < 9 %}\n {{- raise_exception(\"Tool call IDs should be alphanumeric strings with length >= 9! (2)\" + message.tool_call_id) }}\n {%- endif %}\n {{- '\"call_id\": \"' + message.tool_call_id[-9:] + '\"}[/TOOL_RESULTS]' }}\n {%- else %}\n {{- raise_exception(\"Only user and assistant roles are supported, with the exception of an initial optional system message!\") }}\n {%- endif %}\n{%- endfor %}"
3
- }