fishsure commited on
Commit
54c40cd
·
verified ·
1 Parent(s): 6f34252

Upload chat_template.jinja with huggingface_hub

Browse files
Files changed (1) hide show
  1. chat_template.jinja +119 -0
chat_template.jinja ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {{- bos_token }}
2
+ {%- if custom_tools is defined %}
3
+ {%- set tools = custom_tools %}
4
+ {%- endif %}
5
+ {%- if not tools_in_user_message is defined %}
6
+ {%- set tools_in_user_message = true %}
7
+ {%- endif %}
8
+ {%- if not date_string is defined %}
9
+ {%- if strftime_now is defined %}
10
+ {%- set date_string = strftime_now("%d %b %Y") %}
11
+ {%- else %}
12
+ {%- set date_string = "26 Jul 2024" %}
13
+ {%- endif %}
14
+ {%- endif %}
15
+ {%- if not tools is defined %}
16
+ {%- set tools = none %}
17
+ {%- endif %}
18
+
19
+ {#- This block extracts the system message, so we can slot it into the right place. #}
20
+ {%- if messages[0]['role'] == 'system' %}
21
+ {%- set system_message = messages[0]['content']|trim %}
22
+ {%- set messages = messages[1:] %}
23
+ {%- set user_supplied_system_message = true %}
24
+ {%- else %}
25
+ {%- set system_message = "" %}
26
+ {%- set user_supplied_system_message = false %}
27
+ {%- endif %}
28
+
29
+ {#- Find out if there are any images #}
30
+ {% set image_ns = namespace(has_images=false) %}
31
+ {%- for message in messages %}
32
+ {%- for content in message['content'] %}
33
+ {%- if content['type'] == 'image' %}
34
+ {%- set image_ns.has_images = true %}
35
+ {%- endif %}
36
+ {%- endfor %}
37
+ {%- endfor %}
38
+
39
+ {#- System message if there are no images, or if the user supplied one #}
40
+ {%- if user_supplied_system_message or not image_ns.has_images %}
41
+ {{- "<|start_header_id|>system<|end_header_id|>\n\n" }}
42
+ {%- if tools is not none %}
43
+ {{- "Environment: ipython\n" }}
44
+ {%- endif %}
45
+ {{- "Cutting Knowledge Date: December 2023\n" }}
46
+ {{- "Today Date: " + date_string + "\n\n" }}
47
+ {%- if tools is not none and not tools_in_user_message %}
48
+ {{- "You have access to the following functions. To call a function, please respond with JSON for a function call." }}
49
+ {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }}
50
+ {{- "Do not use variables.\n\n" }}
51
+ {%- for t in tools %}
52
+ {{- t | tojson(indent=4) }}
53
+ {{- "\n\n" }}
54
+ {%- endfor %}
55
+ {%- endif %}
56
+ {{- system_message }}
57
+ {{- "<|eot_id|>" }}
58
+ {%- endif %}
59
+
60
+ {#- Custom tools are passed in a user message with some extra guidance #}
61
+ {%- if tools_in_user_message and not tools is none %}
62
+ {#- Extract the first user message so we can plug it in here #}
63
+ {%- if messages | length != 0 %}
64
+ {%- set first_user_message = messages[0]['content']|trim %}
65
+ {%- set messages = messages[1:] %}
66
+ {%- else %}
67
+ {{- raise_exception("Cannot put tools in the first user message when there's no first user message!") }}
68
+ {%- endif %}
69
+ {{- '<|start_header_id|>user<|end_header_id|>\n\n' -}}
70
+ {{- "Given the following functions, please respond with a JSON for a function call " }}
71
+ {{- "with its proper arguments that best answers the given prompt.\n\n" }}
72
+ {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }}
73
+ {{- "Do not use variables.\n\n" }}
74
+ {%- for t in tools %}
75
+ {{- t | tojson(indent=4) }}
76
+ {{- "\n\n" }}
77
+ {%- endfor %}
78
+ {{- first_user_message + "<|eot_id|>"}}
79
+ {%- endif %}
80
+
81
+ {%- for message in messages %}
82
+ {%- if not (message.role == 'ipython' or message.role == 'tool' or 'tool_calls' in message) %}
83
+ {{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n' }}
84
+ {%- if message['content'] is string %}
85
+ {{- message['content'] }}
86
+ {%- else %}
87
+ {%- for content in message['content'] %}
88
+ {%- if content['type'] == 'image' %}
89
+ {{- '<|image|>' }}
90
+ {%- elif content['type'] == 'text' %}
91
+ {{- content['text'] }}
92
+ {%- endif %}
93
+ {%- endfor %}
94
+ {%- endif %}
95
+ {{- '<|eot_id|>' }}
96
+ {%- elif 'tool_calls' in message %}
97
+ {%- if not message.tool_calls|length == 1 %}
98
+ {{- raise_exception("This model only supports single tool-calls at once!") }}
99
+ {%- endif %}
100
+ {%- set tool_call = message.tool_calls[0].function %}
101
+ {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' -}}
102
+ {{- '{"name": "' + tool_call.name + '", ' }}
103
+ {{- '"parameters": ' }}
104
+ {{- tool_call.arguments | tojson }}
105
+ {{- "}" }}
106
+ {{- "<|eot_id|>" }}
107
+ {%- elif message.role == "tool" or message.role == "ipython" %}
108
+ {{- "<|start_header_id|>ipython<|end_header_id|>\n\n" }}
109
+ {%- if message.content is mapping or message.content is iterable %}
110
+ {{- message.content | tojson }}
111
+ {%- else %}
112
+ {{- message.content }}
113
+ {%- endif %}
114
+ {{- "<|eot_id|>" }}
115
+ {%- endif %}
116
+ {%- endfor %}
117
+ {%- if add_generation_prompt %}
118
+ {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' }}
119
+ {%- endif %}