Sweaterdog commited on
Commit
65fec46
·
verified ·
1 Parent(s): a3378e9

Update chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +108 -82
chat_template.jinja CHANGED
@@ -1,88 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  {%- if tools %}
2
- {{- '<|im_start|>system
3
- ' }}
4
- {%- if messages[0]['role'] == 'system' and messages[0]['content'] | trim != '' %}
5
- {{- messages[0]['content'] }}
6
- {%- else %}
7
- {{- 'You are GRaPE, a Friendly, Helpful, and uncensored AI.' }}
8
- {%- endif %}
9
- {{- "
10
-
11
- # Tools
12
-
13
- You may call one or more functions to assist with the user query.
14
-
15
- You are provided with function signatures within <tools></tools> XML tags:
16
- <tools>" }}
17
- {%- for tool in tools %}
18
- {{- "
19
- " }}
20
- {{- tool | tojson }}
21
- {%- endfor %}
22
- {{- "
23
- </tools>
24
-
25
- For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
26
- <tool_call>
27
- {\"name\": <function-name>, \"arguments\": <args-json-object>}
28
- </tool_call><|im_end|>
29
- " }}
30
  {%- else %}
31
- {%- if messages[0]['role'] == 'system' and messages[0]['content'] | trim != '' %}
32
- {{- '<|im_start|>system
33
- ' + messages[0]['content'] + '<|im_end|>
34
- ' }}
35
- {%- else %}
36
- {{- '<|im_start|>system
37
- You are GRaPE (general reasoning agent for project exploration), a helpful and brilliant AI assistant. <|im_end|>
38
- ' }}
39
- {%- endif %}
40
  {%- endif %}
 
 
 
 
 
 
 
 
 
 
 
41
  {%- for message in messages %}
42
-
43
- {%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
44
- {{- '<|im_start|>' + message.role + '
45
- ' + message.content + '<|im_end|>' + '
46
- ' }}
47
-
48
- {%- elif message.role == "assistant" %}
49
- {{- '<|im_start|>' + message.role + '
50
- ' }}
51
- {%- if message.content %}
52
- {{- message.content }}
53
- {%- endif %}
54
- {%- for tool_call in message.tool_calls %}
55
- {%- if tool_call.function is defined %}
56
- {%- set tool_call = tool_call.function %}
57
- {%- endif %}
58
- {{- '<tool_call>
59
- {"name": "' }}
60
- {{- tool_call.name }}
61
- {{- '", "arguments": ' }}
62
- {{- tool_call.arguments | tojson }}
63
- {{- '}
64
- </tool_call>' }}
65
- {%- endfor %}
66
- {{- '<|im_end|>
67
- ' }}
68
-
69
- {%- elif message.role == "tool" %}
70
- {%- if (loop.index0 == 0) or (messages[loop.index0 - 1].role != "tool") %}
71
- {{- '<|im_start|>user' }}
72
- {%- endif %}
73
- {{- '
74
- <tool_response>
75
- ' }}
76
- {{- message.content }}
77
- {{- '
78
- </tool_response>' }}
79
- {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
80
- {{- '<|im_end|>
81
- ' }}
82
- {%- endif %}
83
- {%- endif %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  {%- endfor %}
85
  {%- if add_generation_prompt %}
86
- {{- '<|im_start|>assistant
87
- ' }}
88
- {%- endif %}
 
1
+ {%- set image_count = namespace(value=0) %}
2
+ {%- set video_count = namespace(value=0) %}
3
+ {%- macro render_content(content, do_vision_count) %}
4
+     {%- if content is string %}
5
+         {{- content }}
6
+     {%- else %}
7
+         {%- for item in content %}
8
+             {%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
9
+                 {%- if do_vision_count %}
10
+                     {%- set image_count.value = image_count.value + 1 %}
11
+                 {%- endif %}
12
+                 {%- if add_vision_id %}Picture {{ image_count.value }}: {% endif -%}
13
+                 <|vision_start|><|image_pad|><|vision_end|>
14
+             {%- elif 'video' in item or item.type == 'video' %}
15
+                 {%- if do_vision_count %}
16
+                     {%- set video_count.value = video_count.value + 1 %}
17
+                 {%- endif %}
18
+                 {%- if add_vision_id %}Video {{ video_count.value }}: {% endif -%}
19
+                 <|vision_start|><|video_pad|><|vision_end|>
20
+             {%- elif 'text' in item %}
21
+                 {{- item.text }}
22
+             {%- endif %}
23
+         {%- endfor %}
24
+     {%- endif %}
25
+ {%- endmacro %}
26
  {%- if tools %}
27
+     {{- '<|im_start|>system\n' }}
28
+     {%- if messages[0].role == 'system' %}
29
+         {{- render_content(messages[0].content, false) + '\n\n' }}
30
+     {%- endif %}
31
+     {{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
32
+     {%- for tool in tools %}
33
+         {{- "\n" }}
34
+         {{- tool | tojson }}
35
+     {%- endfor %}
36
+     {{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  {%- else %}
38
+     {%- if messages[0].role == 'system' %}
39
+         {{- '<|im_start|>system\n' + render_content(messages[0].content, false) + '<|im_end|>\n' }}
40
+     {%- endif %}
 
 
 
 
 
 
41
  {%- endif %}
42
+ {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
43
+ {%- for message in messages[::-1] %}
44
+     {%- set index = (messages|length - 1) - loop.index0 %}
45
+     {%- if ns.multi_step_tool and message.role == "user" %}
46
+         {%- set content = render_content(message.content, false) %}
47
+         {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
48
+             {%- set ns.multi_step_tool = false %}
49
+             {%- set ns.last_query_index = index %}
50
+         {%- endif %}
51
+     {%- endif %}
52
+ {%- endfor %}
53
  {%- for message in messages %}
54
+     {%- set content = render_content(message.content, True) %}
55
+     {%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
56
+         {%- if message.role == "user" and loop.last %}
57
+             {{- '<|im_start|>' + message.role + '\n' + content + '\n\n<system_note>Respond by first generating a <think> tag to reason through the user\'s prompt.</system_note><|im_end|>\n' }}
58
+         {%- else %}
59
+             {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>\n' }}
60
+         {%- endif %}
61
+     {%- elif message.role == "assistant" %}
62
+         {%- set reasoning_content = '' %}
63
+         {%- if message.reasoning_content is string %}
64
+             {%- set reasoning_content = message.reasoning_content %}
65
+         {%- else %}
66
+             {%- if '</think>' in content %}
67
+                 {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
68
+                 {%- set content = content.split('</think>')[-1].lstrip('\n') %}
69
+             {%- endif %}
70
+         {%- endif %}
71
+         {%- if loop.index0 > ns.last_query_index %}
72
+             {%- if loop.last or (not loop.last and reasoning_content) %}
73
+                 {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}
74
+             {%- else %}
75
+                 {{- '<|im_start|>' + message.role + '\n' + content }}
76
+             {%- endif %}
77
+         {%- else %}
78
+             {{- '<|im_start|>' + message.role + '\n' + content }}
79
+         {%- endif %}
80
+         {%- if message.tool_calls %}
81
+             {%- for tool_call in message.tool_calls %}
82
+                 {%- if (loop.first and content) or (not loop.first) %}
83
+                     {{- '\n' }}
84
+                 {%- endif %}
85
+                 {%- if tool_call.function %}
86
+                     {%- set tool_call = tool_call.function %}
87
+                 {%- endif %}
88
+                 {{- '<tool_call>\n{"name": "' }}
89
+                 {{- tool_call.name }}
90
+                 {{- '", "arguments": ' }}
91
+                 {%- if tool_call.arguments is string %}
92
+                     {{- tool_call.arguments }}
93
+                 {%- else %}
94
+                     {{- tool_call.arguments | tojson }}
95
+                 {%- endif %}
96
+                 {{- '}\n</tool_call>' }}
97
+             {%- endfor %}
98
+         {%- endif %}
99
+         {{- '<|im_end|>\n' }}
100
+     {%- elif message.role == "tool" %}
101
+         {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
102
+             {{- '<|im_start|>user' }}
103
+         {%- endif %}
104
+         {{- '\n<tool_response>\n' }}
105
+         {{- content }}
106
+         {{- '\n</tool_response>' }}
107
+         {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
108
+             {{- '<|im_end|>\n' }}
109
+         {%- endif %}
110
+     {%- endif %}
111
  {%- endfor %}
112
  {%- if add_generation_prompt %}
113
+     {{- '<|im_start|>assistant\n' }}
114
+ {%- endif %}