File size: 4,092 Bytes
b41e100
8553702
 
1
2
3
{
  "chat_template": "{{- bos_token }}\n{%- for message in messages %}\n    {%- if message['role'] == 'user' %}\n        {{- '[INST] ' }}\n        {%- if message['content'] is not string %}\n            {%- for chunk in message['content'] %}\n                {%- if chunk['type'] == 'text' %}\n                    {{- chunk['content'] }}\n                {%- elif chunk['type'] == 'image' %}\n                    {{- '[IMG]' }}\n                {%- else %}\n                    {{- raise_exception('Unrecognized content type!') }}\n                {%- endif %}\n            {%- endfor %}\n        {%- else %}\n            {{- message['content'] }}\n        {%- endif %}\n        {%- if tools is defined and tools is not none and loop.last %}\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                {%- endif %}\n            {%- endfor %}\n            {{- ']' }}\n            {{- '[\/AVAILABLE_TOOLS]' }}\n        {%- endif %}\n        {{- '[\/INST]' }}\n    {%- elif message['role'] == 'system' %}\n        {{- '[SYSTEM_PROMPT] ' + message['content'] + '[\/SYSTEM_PROMPT]' }}\n    {%- elif message['role'] == 'assistant' %}\n        {{- ' ' }}\n        {%- if message['content'] is not string %}\n            {%- for chunk in message['content'] %}\n                {%- if chunk['type'] == 'text' %}\n                    {{- chunk['content']|trim }}\n                {%- elif chunk['type'] == 'image' %}\n                    {{- '[IMG]' }}\n                {%- else %}\n                    {{- raise_exception('Unrecognized content type!') }}\n                {%- endif %}\n            {%- endfor %}\n        {%- else %}\n            {{- message['content']|trim }}\n        {%- endif %}\n        {{- eos_token }}\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 message.content is mapping and 'image' in message.content %}\n            {{- '\"image\": \"[IMG]\", ' }}\n        {%- endif %}\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!\") }}\n        {%- endif %}\n        {{- '\"call_id\": \"' + message.tool_call_id + '\"}[\/TOOL_RESULTS]' }}\n    {%- elif message.tool_calls is defined and message.tool_calls is not none %}\n        {{- '[TOOL_CALLS] [' }}\n        {%- for tool_call in message.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!\") }}\n            {%- endif %}\n            {{- ', \"id\": \"' + tool_call.id + '\"}' }}\n            {%- if not loop.last %}\n                {{- ', ' }}\n            {%- endif %}\n        {%- endfor %}\n        {{- ']' + eos_token }}\n    {%- else %}\n        {{- raise_exception('Only user, system, assistant, tool, and tool_results roles are supported!') }}\n    {%- endif %}\n{%- endfor %}"
}