|
{%- set default_system_message = 'You are AI.' -%}{{ bos_token }} |
|
{%- if messages[0]['role'] == 'system' %} |
|
{%- set system_message = messages[0]['content'] %} |
|
{%- set loop_messages = messages[1:] %} |
|
{%- else %} |
|
{%- set system_message = default_system_message %} |
|
{%- set loop_messages = messages %} |
|
{%- endif %} |
|
{%- set user_messages = loop_messages | selectattr("role", "equalto", "user") | list %} |
|
|
|
{% if tools is not none and tools|length > 0 %} |
|
{%- set tool_instructions = |
|
'Use the available tools appropriately to fulfill your instructions and achieve your goals by returning a JSON array pre-pended with `[TOOL_CALLS]` like this: ' |
|
~ '[TOOL_CALLS][{"name": "tool1", "arguments": {"p1": "val1"}}, {"name": "tool2", "arguments": {"p2": "val2", "p3": 23}}] ' |
|
~ 'Your available tools are: ' |
|
-%} |
|
|
|
|
|
{%- set tool_instructions = tool_instructions ~ '[AVAILABLE_TOOLS]' %} |
|
{%- for tool in tools %} |
|
{%- if not loop.first %}, {% endif %} |
|
{%- set tool_definition = tool.function %} |
|
{%- set tool_instructions = tool_instructions ~ '{"type":"function","function":{' %} |
|
{%- for key, val in tool_definition.items() if key != "return" %} |
|
{%- if not loop.first %}, {% endif %} |
|
{%- if val is string %} |
|
{%- set tool_instructions = tool_instructions ~ '"' ~ key ~ '":"' ~ val ~ '"' %} |
|
{%- else %} |
|
{%- set tool_instructions = tool_instructions ~ '"' ~ key ~ '":' ~ (val|tojson) %} |
|
{%- endif %} |
|
{%- endfor %} |
|
{%- set tool_instructions = tool_instructions ~ '}}' %} |
|
{%- endfor %} |
|
{%- set tool_instructions = tool_instructions ~ '[/AVAILABLE_TOOLS]\n' %} |
|
|
|
{%- set system_message = system_message ~ tool_instructions -%} |
|
{% endif %} |
|
{{ '[SYSTEM_PROMPT]' ~ system_message ~ '[/SYSTEM_PROMPT]' }} |
|
|
|
{%- for message in loop_messages %} |
|
{%- if message['role'] == 'user' %} |
|
{{- '[INST]' + message['content'] + '[/INST]' }} |
|
|
|
{%- elif message['role'] == 'system' %} |
|
{{- '[SYSTEM_PROMPT]' + message['content'] + '[/SYSTEM_PROMPT]' }} |
|
|
|
{%- elif message['role'] == 'assistant' %} |
|
{%- if message.tool_calls is defined and message.tool_calls is not none and message.tool_calls|length > 0 -%} |
|
[TOOL_CALLS][{{ message.tool_calls|tojson }}] |
|
{%- elif message['content'] is defined and message['content'] is not none -%} |
|
{{- message['content'] + eos_token }} |
|
{%- endif %} |
|
|
|
{%- elif message['role'] == 'tool_calls' or message.tool_calls is defined %} |
|
{%- if message.tool_calls is defined %} |
|
{%- set tool_calls = message.tool_calls %} |
|
{%- else %} |
|
{%- set tool_calls = message.content %} |
|
{%- endif %} |
|
{{- "[TOOL_CALLS] [" }} |
|
{%- for tool_call in tool_calls %} |
|
{%- set out = tool_call.function|tojson %} |
|
{{- out[:-1] }} |
|
{%- if not tool_call.id is defined or tool_call.id|length < 9 %} |
|
{{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9!" + tool_call.id) }} |
|
{%- endif %} |
|
{{- ', "id": "' + tool_call.id + '"}' }} |
|
{%- if not loop.last %} |
|
{{- ", " }} |
|
{%- else %} |
|
{{- "]" + eos_token }} |
|
{%- endif %} |
|
{%- endfor %} |
|
|
|
{%- elif message["role"] == "tool" %} |
|
{%- if message.content is defined and message.content.content is defined %} |
|
{%- set content = message.content.content %} |
|
{%- else %} |
|
{%- set content = message.content %} |
|
{%- endif %} |
|
{{- '[TOOL_RESULTS] {"content": ' + content|string + ", " }} |
|
{%- if not message.tool_call_id is defined or message.tool_call_id|length < 9 %} |
|
{{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9!" + message.tool_call_id) }} |
|
{%- endif %} |
|
{{- '"call_id": "' + message.tool_call_id + '"}[/TOOL_RESULTS]' }} |
|
|
|
{%- elif message["role"] == "assistant" and message.content is defined and message.content is not none %} |
|
{{- message["content"] + eos_token }} |
|
|
|
{%- elif message['role'] == 'tool' -%} |
|
[TOOL_CONTENT] |
|
{{ {"tool_call_id": message['tool_call_id'], "tool_call": message['content']}|tojson }} |
|
[/TOOL_CONTENT] |
|
|
|
{%- else %} |
|
{{- raise_exception('Only user, system, assistant, tool_call & tool roles are supported!') }} |
|
{%- endif %} |
|
{%- endfor %} |
|
|