1
+ {{- bos_token }}
2
+ {% - for message in messages %}
3
+ {% - if message ['role' ] == 'user' %}
4
+ {{- '[INST] ' }}
5
+ {% - if message ['content' ] is not string %}
6
+ {% - for chunk in message ['content' ] %}
7
+ {% - if chunk ['type' ] == 'text' %}
8
+ {{- chunk['content'] }}
9
+ {% - elif chunk ['type' ] == 'image' %}
10
+ {{- '[IMG] ' }}
11
+ {% - else %}
12
+ {{- raise_exception('Unrecognized content type!') }}
13
+ {% - endif %}
14
+ {% - endfor %}
15
+ {% - else %}
16
+ {{- message['content'] }}
17
+ {% - endif %}
18
+ {% - if tools is defined and tools is not none and loop .last %}
19
+ {{- '[AVAILABLE_TOOLS] [' }}
20
+ {% - for tool in tools %}
21
+ {% - set tool = tool .function %}
22
+ {{- '{"type": "function", "function": {' }}
23
+ {% - for key , val in tool .items () if key != "return" %}
24
+ {% - if val is string %}
25
+ {{- '"' + key + '": "' + val + '"' }}
26
+ {% - else %}
27
+ {{- '"' + key + '": ' + val|tojson }}
28
+ {% - endif %}
29
+ {% - if not loop .last %}
30
+ {{- ', ' }}
31
+ {% - endif %}
32
+ {% - endfor %}
33
+ {{- '}}' }}
34
+ {% - if not loop .last %}
35
+ {{- ', ' }}
36
+ {% - endif %}
37
+ {% - endfor %}
38
+ {{- '] ' }}
39
+ {{- '[/AVAILABLE_TOOLS] ' }}
40
+ {% - endif %}
41
+ {{- '[/INST] ' }}
42
+ {% - elif message ['role' ] == 'system' %}
43
+ {{- '[SYSTEM_PROMPT] ' + message['content'] + '[/SYSTEM_PROMPT] ' }}
44
+ {% - elif message ['role' ] == 'assistant' %}
45
+ {% - if message ['content' ] is not string %}
46
+ {% - for chunk in message ['content' ] %}
47
+ {% - if chunk ['type' ] == 'text' %}
48
+ {{- chunk['content'] |trim }}
49
+ {% - elif chunk ['type' ] == 'image' %}
50
+ {{- '[IMG] ' }}
51
+ {% - else %}
52
+ {{- raise_exception('Unrecognized content type!') }}
53
+ {% - endif %}
54
+ {% - endfor %}
55
+ {% - else %}
56
+ {{- message['content'] |trim }}
57
+ {% - endif %}
58
+ {{- eos_token }}
59
+ {% - elif message ['role' ] == 'tool_results' or message ['role' ] == 'tool' %}
60
+ {% - if message .content is defined and message .content .content is defined %}
61
+ {% - set content = message .content .content %}
62
+ {% - else %}
63
+ {% - set content = message .content %}
64
+ {% - endif %}
65
+ {{- '[TOOL_RESULTS] {"content": ' + content|string + ', ' }}
66
+ {% - if message .content is mapping and 'image' in message .content %}
67
+ {{- '"image": "[IMG] ", ' }}
68
+ {% - endif %}
69
+ {% - if not message .tool_call_id is defined or message .tool_call_id |length != 9 %}
70
+ {{- raise_exception("Tool call IDs should be alphanumeric strings with length 9!") }}
71
+ {% - endif %}
72
+ {{- '"call_id": "' + message.tool_call_id + '"}[/TOOL_RESULTS] ' }}
73
+ {% - elif message .tool_calls is defined and message .tool_calls is not none %}
74
+ {{- '[TOOL_CALLS] [' }}
75
+ {% - for tool_call in message .tool_calls %}
76
+ {% - set out = tool_call .function |tojson %}
77
+ {{- out[:-1] }}
78
+ {% - if not tool_call .id is defined or tool_call .id |length != 9 %}
79
+ {{- raise_exception("Tool call IDs should be alphanumeric strings with length 9!") }}
80
+ {% - endif %}
81
+ {{- ', "id": "' + tool_call.id + '"}' }}
82
+ {% - if not loop .last %}
83
+ {{- ', ' }}
84
+ {% - endif %}
85
+ {% - endfor %}
86
+ {{- '] ' + eos_token }}
87
+ {% - else %}
88
+ {{- raise_exception('Only user, system, assistant, tool, and tool_results roles are supported!') }}
89
+ {% - endif %}
90
+ {% - endfor %}
0 commit comments