|
{{- bos_token }} |
|
{%- if custom_tools is defined %} |
|
{%- set tools = custom_tools %} |
|
{%- endif %} |
|
{%- if not tools_in_user_message is defined %} |
|
{%- set tools_in_user_message = false %} |
|
{%- endif %} |
|
{%- if not date_string is defined %} |
|
{%- if strftime_now is defined %} |
|
{%- set date_string = strftime_now("%d %b %Y") %} |
|
{%- else %} |
|
{%- set date_string = "15 Mar 2025" %} |
|
{%- endif %} |
|
{%- endif %} |
|
{%- if not tools is defined %} |
|
{%- set tools = none %} |
|
{%- endif %} |
|
|
|
|
|
{% set image_ns = namespace(has_images=false) %} |
|
{%- for message in messages %} |
|
{%- if message['content'] is not string %} |
|
{%- for content in message['content'] %} |
|
{%- if content['type'] == 'image' %} |
|
{%- set image_ns.has_images = true %} |
|
{%- endif %} |
|
{%- endfor %} |
|
{%- endif %} |
|
{%- endfor %} |
|
|
|
|
|
{%- if messages[0]['role'] == 'system' %} |
|
{%- if messages[0]['content'] is string %} |
|
{%- set system_message = messages[0]['content']|trim %} |
|
{%- else %} |
|
{%- set system_message = messages[0]['content'][0]['text']|trim %} |
|
{%- endif %} |
|
{%- set messages = messages[1:] %} |
|
{%- else %} |
|
{%- if tools is not none %} |
|
{%- set system_message = "You are a helpful assistant with tool calling capabilities. Only reply with a tool call if the function exists in the library provided by the user. If it doesn't exist, just reply directly in natural language. When you receive a tool call response, use the output to format an answer to the original user question." %} |
|
{%- else %} |
|
{%- set system_message = "" %} |
|
{%- endif %} |
|
{%- endif %} |
|
|
|
|
|
{%- if system_message or not image_ns.has_images %} |
|
{{- "<start_of_turn>system\n" }} |
|
{%- if tools is not none %} |
|
{{- "Environment: ipython\n" }} |
|
{%- endif %} |
|
{{- "Cutting Knowledge Date: December 2023\n" }} |
|
{{- "Today Date: " + date_string + "\n\n" }} |
|
{%- if tools is not none and not tools_in_user_message %} |
|
{{- "You have access to the following functions. To call a function, please respond with JSON for a function call. " }} |
|
{{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}. ' }} |
|
{{- "Do not use variables.\n\n" }} |
|
{%- for t in tools %} |
|
{{- t | tojson(indent=4) }} |
|
{{- "\n\n" }} |
|
{%- endfor %} |
|
{%- endif %} |
|
{{- system_message }} |
|
{{- "<end_of_turn>\n" }} |
|
{%- endif %} |
|
|
|
|
|
{%- if tools_in_user_message and not tools is none %} |
|
|
|
{%- if messages | length != 0 %} |
|
{%- if messages[0]['content'] is string %} |
|
{%- set first_user_message = messages[0]['content']|trim %} |
|
{%- else %} |
|
{%- set first_user_message = messages[0]['content'] | selectattr('type', 'equalto', 'text') | map(attribute='text') | map('trim') | join('\n') %} |
|
{%- endif %} |
|
{%- set messages = messages[1:] %} |
|
{%- else %} |
|
{{- raise_exception("Cannot put tools in the first user message when there's no first user message!") }} |
|
{%- endif %} |
|
{{- '<start_of_turn>user\n' -}} |
|
{{- "Given the following functions, please respond with a JSON for a function call " }} |
|
{{- "with its proper arguments that best answers the given prompt.\n\n" }} |
|
{{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}. ' }} |
|
{{- "Do not use variables.\n\n" }} |
|
{%- for t in tools %} |
|
{{- t | tojson(indent=4) }} |
|
{{- "\n\n" }} |
|
{%- endfor %} |
|
{{- first_user_message + "<end_of_turn>\n"}} |
|
{%- endif %} |
|
|
|
{%- for message in messages %} |
|
{%- if not (message.role == 'ipython' or message.role == 'tool' or 'tool_calls' in message) %} |
|
{%- if message.role == 'assistant' %} |
|
{{- '<start_of_turn>model\n' }} |
|
{%- else %} |
|
{{- '<start_of_turn>' + message['role'] + '\n' }} |
|
{%- endif %} |
|
{%- if message['content'] is string %} |
|
{{- message['content'] | trim}} |
|
{%- else %} |
|
{%- for content in message['content'] %} |
|
{%- if content['type'] == 'image' %} |
|
{{- '<start_of_image>' }} |
|
{%- elif content['type'] == 'text' %} |
|
{{- content['text'] | trim }} |
|
{%- endif %} |
|
{%- endfor %} |
|
{%- endif %} |
|
{{- '<end_of_turn>\n' }} |
|
{%- elif 'tool_calls' in message %} |
|
{%- if not message.tool_calls|length == 1 %} |
|
{{- raise_exception("This model only supports single tool-calls at once!") }} |
|
{%- endif %} |
|
{%- set tool_call = message.tool_calls[0].function %} |
|
{{- '<start_of_turn>model\n' -}} |
|
{{- '{"name": "' + tool_call.name + '", ' }} |
|
{{- '"parameters": ' }} |
|
{{- tool_call.arguments | tojson }} |
|
{{- "}" }} |
|
{{- "<end_of_turn>\n" }} |
|
{%- elif message.role == "tool" or message.role == "ipython" %} |
|
{{- "<start_of_turn>ipython\n" }} |
|
{%- if message.content is string %} |
|
{{- { "output": message.content } | tojson }} |
|
{%- else %} |
|
{%- for content in message['content'] %} |
|
{%- if content['type'] == 'text' %} |
|
{{- { "output": content['text'] } | tojson }} |
|
{%- endif %} |
|
{%- endfor %} |
|
{%- endif %} |
|
{{- "<end_of_turn>\n" }} |
|
{%- endif %} |
|
{%- endfor %} |
|
{%- if add_generation_prompt %} |
|
{{- '<start_of_turn>model\n' }} |
|
{%- endif %} |