{{- bos_token }}\n {%- if custom_tools is defined %} {%- set tools = custom_tools %} {%- endif %} {%- if not tools_in_user_message is defined %} {%- set tools_in_user_message = true %} {%- endif %} {%- if not date_string is defined %} {%- if strftime_now is defined %} {%- set date_string = strftime_now("%d %b %Y") %} {%- else %} {%- set date_string = "26 Jul 2024" %} {%- endif %} {%- endif %} {%- if not tools is defined %} {%- set tools = none %} {%- endif %} {# Extract the system message, if present #} {%- if messages[0]['role'] == 'system' %} {%- set system_message = messages[0]['content'] | trim %} {%- set messages = messages[1:] %} {%- else %} {%- set system_message = "" %} {%- endif %} {# Identify if any messages contain images #} {% set image_ns = namespace(has_images=false) %} {%- for message in messages %} {%- for content in message['content'] %} {%- if content['type'] == 'image' %} {%- set image_ns.has_images = true %} {%- endif %} {%- endfor %} {%- endfor %} {# Raise exception if images and system message coexist #} {%- if image_ns.has_images and system_message != "" %} {{- raise_exception("Prompting with images is incompatible with system messages.") }} {%- endif %} {# Insert system message if no images are found #} {%- if not image_ns.has_images %} {{- "<|start_header_id|>system<|end_header_id|>\\n\\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, respond with JSON for a function call." }} {{- 'Use the format {"name": function name, "parameters": dictionary of arguments}.' }} {{- "Do not use variables.\\n\\n" }} {%- for t in tools %} {{- t | tojson(indent=4) }} {{- "\\n\\n" }} {%- endfor %} {%- endif %} {{- system_message }} {{- "<|eot_id|>" }} {%- endif %} {# Custom tools in user messages #} {%- if tools_in_user_message and tools is not none %} {%- if messages | length != 0 %} {%- set first_user_message = messages[0]['content'] | trim %} {%- set messages = messages[1:] %} {%- else %} {{- raise_exception("Cannot put tools in the first user message when there's no first user message!") }} {%- endif %} {{- '<|start_header_id|>user<|end_header_id|>\\n\\n' }} {{- "Given these functions, respond with a JSON for a function call " }} {{- "that best addresses the prompt.\\n\\n" }} {{- 'Format: {"name": function name, "parameters": dictionary of arguments}.' }} {{- "Do not use variables.\\n\\n" }} {%- for t in tools %} {{- t | tojson(indent=4) }} {{- "\\n\\n" }} {%- endfor %} {{- first_user_message + "<|eot_id|>" }} {%- endif %} {# Iterate over remaining messages #} {%- for message in messages %} {%- if not (message.role in ['ipython', 'tool'] or 'tool_calls' in message) %} {{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\\n\\n' }} {%- if message['content'] is string %} {{- message['content'] }} {%- else %} {%- for content in message['content'] %} {%- if content['type'] == 'image' %} {{- '<|image|>' }} {%- elif content['type'] == 'text' %} {{- content['text'] }} {%- endif %} {%- endfor %} {%- endif %} {{- '<|eot_id|>' }} {%- elif 'tool_calls' in message %} {%- if 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_header_id|>assistant<|end_header_id|>\\n\\n' }} {{- '{"name": "' + tool_call.name + '", "parameters": ' }} {{- tool_call.arguments | tojson }} {{- "}" }} {{- "<|eot_id|>" }} {%- elif message.role in ["tool", "ipython"] %} {{- "<|start_header_id|>ipython<|end_header_id|>\\n\\n" }} {%- if message.content is mapping or message.content is iterable %} {{- message.content | tojson }} {%- else %} {{- message.content }} {%- endif %} {{- "<|eot_id|>" }} {%- endif %} {%- endfor %} {# Add generation prompt if specified #} {%- if add_generation_prompt %} {{- '<|start_header_id|>assistant<|end_header_id|>\\n\\n' }} {%- endif %}