| 1 | {{- bos_token }} |
| 2 | {%- if custom_tools is defined %} |
| 3 | {%- set tools = custom_tools %} |
| 4 | {%- endif %} |
| 5 | {%- if not tools_in_user_message is defined %} |
| 6 | {%- set tools_in_user_message = false %} |
| 7 | {%- endif %} |
| 8 | {%- if not date_string is defined %} |
| 9 | {%- if strftime_now is defined %} |
| 10 | {%- set date_string = strftime_now("%d %b %Y") %} |
| 11 | {%- else %} |
| 12 | {%- set date_string = "26 Jul 2024" %} |
| 13 | {%- endif %} |
| 14 | {%- endif %} |
| 15 | {%- if not tools is defined %} |
| 16 | {%- set tools = none %} |
| 17 | {%- endif %} |
| 18 | |
| 19 | {#- Find out if there are any images #} |
| 20 | {% set image_ns = namespace(has_images=false) %} |
| 21 | {%- for message in messages %} |
| 22 | {%- for content in message['content'] %} |
| 23 | {%- if content['type'] == 'image' %} |
| 24 | {%- set image_ns.has_images = true %} |
| 25 | {%- endif %} |
| 26 | {%- endfor %} |
| 27 | {%- endfor %} |
| 28 | |
| 29 | {#- This block extracts the system message, so we can slot it into the right place. #} |
| 30 | {%- if messages[0]['role'] == 'system' %} |
| 31 | {%- if messages[0]['content'] is string %} |
| 32 | {%- set system_message = messages[0]['content']|trim %} |
| 33 | {%- else %} |
| 34 | {%- set system_message = messages[0]['content'][0]['text']|trim %} |
| 35 | {%- endif %} |
| 36 | {%- set messages = messages[1:] %} |
| 37 | {%- else %} |
| 38 | {%- if tools is not none %} |
| 39 | {%- 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." %} |
| 40 | {%- else %} |
| 41 | {%- set system_message = "" %} |
| 42 | {%- endif %} |
| 43 | {%- endif %} |
| 44 | |
| 45 | {#- System message if there are no images, if the user supplied one, or if tools are used (default tool system message) #} |
| 46 | {%- if system_message or not image_ns.has_images %} |
| 47 | {{- "<|start_header_id|>system<|end_header_id|>\n\n" }} |
| 48 | {%- if tools is not none %} |
| 49 | {{- "Environment: ipython\n" }} |
| 50 | {%- endif %} |
| 51 | {{- "Cutting Knowledge Date: December 2023\n" }} |
| 52 | {{- "Today Date: " + date_string + "\n\n" }} |
| 53 | {%- if tools is not none and not tools_in_user_message %} |
| 54 | {{- "You have access to the following functions. To call a function, please respond with JSON for a function call. " }} |
| 55 | {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}. ' }} |
| 56 | {{- "Do not use variables.\n\n" }} |
| 57 | {%- for t in tools %} |
| 58 | {{- t | tojson(indent=4) }} |
| 59 | {{- "\n\n" }} |
| 60 | {%- endfor %} |
| 61 | {%- endif %} |
| 62 | {{- system_message }} |
| 63 | {{- "<|eot_id|>" }} |
| 64 | {%- endif %} |
| 65 | |
| 66 | {#- Custom tools are passed in a user message with some extra guidance #} |
| 67 | {%- if tools_in_user_message and not tools is none %} |
| 68 | {#- Extract the first user message so we can plug it in here #} |
| 69 | {%- if messages | length != 0 %} |
| 70 | {%- if messages[0]['content'] is string %} |
| 71 | {%- set first_user_message = messages[0]['content']|trim %} |
| 72 | {%- else %} |
| 73 | {%- set first_user_message = messages[0]['content'] | selectattr('type', 'equalto', 'text') | map(attribute='text') | map('trim') | join('\n') %} |
| 74 | {%- endif %} |
| 75 | {%- set messages = messages[1:] %} |
| 76 | {%- else %} |
| 77 | {{- raise_exception("Cannot put tools in the first user message when there's no first user message!") }} |
| 78 | {%- endif %} |
| 79 | {{- '<|start_header_id|>user<|end_header_id|>\n\n' -}} |
| 80 | {{- "Given the following functions, please respond with a JSON for a function call " }} |
| 81 | {{- "with its proper arguments that best answers the given prompt.\n\n" }} |
| 82 | {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}. ' }} |
| 83 | {{- "Do not use variables.\n\n" }} |
| 84 | {%- for t in tools %} |
| 85 | {{- t | tojson(indent=4) }} |
| 86 | {{- "\n\n" }} |
| 87 | {%- endfor %} |
| 88 | {{- first_user_message + "<|eot_id|>"}} |
| 89 | {%- endif %} |
| 90 | |
| 91 | {%- for message in messages %} |
| 92 | {%- if not (message.role == 'ipython' or message.role == 'tool' or 'tool_calls' in message) %} |
| 93 | {{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n' }} |
| 94 | {%- if message['content'] is string %} |
| 95 | {{- message['content'] | trim}} |
| 96 | {%- else %} |
| 97 | {%- for content in message['content'] %} |
| 98 | {%- if content['type'] == 'image' %} |
| 99 | {{- '<|image|>' }} |
| 100 | {%- elif content['type'] == 'text' %} |
| 101 | {{- content['text'] | trim }} |
| 102 | {%- endif %} |
| 103 | {%- endfor %} |
| 104 | {%- endif %} |
| 105 | {{- '<|eot_id|>' }} |
| 106 | {%- elif 'tool_calls' in message %} |
| 107 | {%- if not message.tool_calls|length == 1 %} |
| 108 | {{- raise_exception("This model only supports single tool-calls at once!") }} |
| 109 | {%- endif %} |
| 110 | {%- set tool_call = message.tool_calls[0].function %} |
| 111 | {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' -}} |
| 112 | {{- '{"name": "' + tool_call.name + '", ' }} |
| 113 | {{- '"parameters": ' }} |
| 114 | {{- tool_call.arguments | tojson }} |
| 115 | {{- "}" }} |
| 116 | {{- "<|eot_id|>" }} |
| 117 | {%- elif message.role == "tool" or message.role == "ipython" %} |
| 118 | {{- "<|start_header_id|>ipython<|end_header_id|>\n\n" }} |
| 119 | {%- if message.content is string %} |
| 120 | {{- { "output": message.content } | tojson }} |
| 121 | {%- else %} |
| 122 | {%- for content in message['content'] %} |
| 123 | {%- if content['type'] == 'text' %} |
| 124 | {{- { "output": content['text'] } | tojson }} |
| 125 | {%- endif %} |
| 126 | {%- endfor %} |
| 127 | {%- endif %} |
| 128 | {{- "<|eot_id|>" }} |
| 129 | {%- endif %} |
| 130 | {%- endfor %} |
| 131 | {%- if add_generation_prompt %} |
| 132 | {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' }} |
| 133 | {%- endif %} |