Upload chat_template.jinja with huggingface_hub
Browse files- chat_template.jinja +130 -0
chat_template.jinja
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{{- bos_token }}\n
|
2 |
+
|
3 |
+
{%- if custom_tools is defined %}
|
4 |
+
{%- set tools = custom_tools %}
|
5 |
+
{%- endif %}
|
6 |
+
|
7 |
+
{%- if not tools_in_user_message is defined %}
|
8 |
+
{%- set tools_in_user_message = true %}
|
9 |
+
{%- endif %}
|
10 |
+
|
11 |
+
{%- if not date_string is defined %}
|
12 |
+
{%- if strftime_now is defined %}
|
13 |
+
{%- set date_string = strftime_now("%d %b %Y") %}
|
14 |
+
{%- else %}
|
15 |
+
{%- set date_string = "26 Jul 2024" %}
|
16 |
+
{%- endif %}
|
17 |
+
{%- endif %}
|
18 |
+
|
19 |
+
{%- if not tools is defined %}
|
20 |
+
{%- set tools = none %}
|
21 |
+
{%- endif %}
|
22 |
+
|
23 |
+
{# Extract the system message, if present #}
|
24 |
+
{%- if messages[0]['role'] == 'system' %}
|
25 |
+
{%- set system_message = messages[0]['content'] | trim %}
|
26 |
+
{%- set messages = messages[1:] %}
|
27 |
+
{%- else %}
|
28 |
+
{%- set system_message = "" %}
|
29 |
+
{%- endif %}
|
30 |
+
|
31 |
+
{# Identify if any messages contain images #}
|
32 |
+
{% set image_ns = namespace(has_images=false) %}
|
33 |
+
{%- for message in messages %}
|
34 |
+
{%- for content in message['content'] %}
|
35 |
+
{%- if content['type'] == 'image' %}
|
36 |
+
{%- set image_ns.has_images = true %}
|
37 |
+
{%- endif %}
|
38 |
+
{%- endfor %}
|
39 |
+
{%- endfor %}
|
40 |
+
|
41 |
+
{# Raise exception if images and system message coexist #}
|
42 |
+
{%- if image_ns.has_images and system_message != "" %}
|
43 |
+
{{- raise_exception("Prompting with images is incompatible with system messages.") }}
|
44 |
+
{%- endif %}
|
45 |
+
|
46 |
+
{# Insert system message if no images are found #}
|
47 |
+
{%- if not image_ns.has_images %}
|
48 |
+
{{- "<|start_header_id|>system<|end_header_id|>\\n\\n" }}
|
49 |
+
{%- if tools is not none %}
|
50 |
+
{{- "Environment: ipython\\n" }}
|
51 |
+
{%- endif %}
|
52 |
+
{{- "Cutting Knowledge Date: December 2023\\n" }}
|
53 |
+
{{- "Today Date: " + date_string + "\\n\\n" }}
|
54 |
+
|
55 |
+
{%- if tools is not none and not tools_in_user_message %}
|
56 |
+
{{- "You have access to the following functions. " }}
|
57 |
+
{{- "To call a function, respond with JSON for a function call." }}
|
58 |
+
{{- 'Use the format {"name": function name, "parameters": dictionary of arguments}.' }}
|
59 |
+
{{- "Do not use variables.\\n\\n" }}
|
60 |
+
{%- for t in tools %}
|
61 |
+
{{- t | tojson(indent=4) }}
|
62 |
+
{{- "\\n\\n" }}
|
63 |
+
{%- endfor %}
|
64 |
+
{%- endif %}
|
65 |
+
{{- system_message }}
|
66 |
+
{{- "<|eot_id|>" }}
|
67 |
+
{%- endif %}
|
68 |
+
|
69 |
+
{# Custom tools in user messages #}
|
70 |
+
{%- if tools_in_user_message and tools is not none %}
|
71 |
+
{%- if messages | length != 0 %}
|
72 |
+
{%- set first_user_message = messages[0]['content'] | trim %}
|
73 |
+
{%- set messages = messages[1:] %}
|
74 |
+
{%- else %}
|
75 |
+
{{- raise_exception("Cannot put tools in the first user message when there's no first user message!") }}
|
76 |
+
{%- endif %}
|
77 |
+
|
78 |
+
{{- '<|start_header_id|>user<|end_header_id|>\\n\\n' }}
|
79 |
+
{{- "Given these functions, respond with a JSON for a function call " }}
|
80 |
+
{{- "that best addresses the prompt.\\n\\n" }}
|
81 |
+
{{- 'Format: {"name": function name, "parameters": dictionary of arguments}.' }}
|
82 |
+
{{- "Do not use variables.\\n\\n" }}
|
83 |
+
{%- for t in tools %}
|
84 |
+
{{- t | tojson(indent=4) }}
|
85 |
+
{{- "\\n\\n" }}
|
86 |
+
{%- endfor %}
|
87 |
+
{{- first_user_message + "<|eot_id|>" }}
|
88 |
+
{%- endif %}
|
89 |
+
|
90 |
+
{# Iterate over remaining messages #}
|
91 |
+
{%- for message in messages %}
|
92 |
+
{%- if not (message.role in ['ipython', 'tool'] or 'tool_calls' in message) %}
|
93 |
+
{{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\\n\\n' }}
|
94 |
+
{%- if message['content'] is string %}
|
95 |
+
{{- message['content'] }}
|
96 |
+
{%- else %}
|
97 |
+
{%- for content in message['content'] %}
|
98 |
+
{%- if content['type'] == 'image' %}
|
99 |
+
{{- '<|image|>' }}
|
100 |
+
{%- elif content['type'] == 'text' %}
|
101 |
+
{{- content['text'] }}
|
102 |
+
{%- endif %}
|
103 |
+
{%- endfor %}
|
104 |
+
{%- endif %}
|
105 |
+
{{- '<|eot_id|>' }}
|
106 |
+
{%- elif 'tool_calls' in message %}
|
107 |
+
{%- if message.tool_calls | length != 1 %}
|
108 |
+
{{- raise_exception("This model only supports single tool-calls at once!") }}
|
109 |
+
{%- endif %}
|
110 |
+
{%- set tool_call = message.tool_calls[0].function %}
|
111 |
+
{{- '<|start_header_id|>assistant<|end_header_id|>\\n\\n' }}
|
112 |
+
{{- '{"name": "' + tool_call.name + '", "parameters": ' }}
|
113 |
+
{{- tool_call.arguments | tojson }}
|
114 |
+
{{- "}" }}
|
115 |
+
{{- "<|eot_id|>" }}
|
116 |
+
{%- elif message.role in ["tool", "ipython"] %}
|
117 |
+
{{- "<|start_header_id|>ipython<|end_header_id|>\\n\\n" }}
|
118 |
+
{%- if message.content is mapping or message.content is iterable %}
|
119 |
+
{{- message.content | tojson }}
|
120 |
+
{%- else %}
|
121 |
+
{{- message.content }}
|
122 |
+
{%- endif %}
|
123 |
+
{{- "<|eot_id|>" }}
|
124 |
+
{%- endif %}
|
125 |
+
{%- endfor %}
|
126 |
+
|
127 |
+
{# Add generation prompt if specified #}
|
128 |
+
{%- if add_generation_prompt %}
|
129 |
+
{{- '<|start_header_id|>assistant<|end_header_id|>\\n\\n' }}
|
130 |
+
{%- endif %}
|