|
| 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 tools is defined %} |
| 9 | + {%- set tools = none %} |
| 10 | +{%- endif %} |
| 11 | + |
| 12 | +{#- This block extracts the system message, so we can slot it into the right place. #} |
| 13 | +{%- if messages[0]['role'] == 'system' %} |
| 14 | + {%- if messages[0]['content'] is string %} |
| 15 | + {%- set system_message = messages[0]['content']|trim %} |
| 16 | + {%- else %} |
| 17 | + {%- set system_message = messages[0]['content'][0]['text']|trim %} |
| 18 | + {%- endif %} |
| 19 | + {%- set messages = messages[1:] %} |
| 20 | +{%- else %} |
| 21 | + {%- if tools is not none %} |
| 22 | + {#- Add default tool system message when tools are provided #} |
| 23 | + {%- set system_message = "You are a helpful assistant with tool calling " |
| 24 | + "capabilities. Only reply with a tool call if the function exists in the " |
| 25 | + "library provided by the user. If it doesn't exist, just reply directly in " |
| 26 | + "natural language. When you receive a tool call response, use the output to " |
| 27 | + "format an answer to the original user question." %} |
| 28 | + {%- else %} |
| 29 | + {%- set system_message = "" %} |
| 30 | + {%- endif %} |
| 31 | +{%- endif %} |
| 32 | + |
| 33 | +{#- System message if the user supplied one, or if tools are used (default tool system message) #} |
| 34 | +{%- if system_message %} |
| 35 | + {#- always use user provided system message to override default tool system message #} |
| 36 | + {{- "<|header_start|>system<|header_end|>\n\n" }} |
| 37 | + {{- system_message }} |
| 38 | + {%- if tools is not none and not tools_in_user_message %} |
| 39 | + {{- "Tools: You have access to the following tools. You might need to use one " |
| 40 | + "or more function/tool calls to fulfill the task. \n" |
| 41 | + "If none are needed, then proceed to the response.\n\n" |
| 42 | + "Tool Call Syntax: You can call tools using the following syntax:\n" |
| 43 | + "[func_name1(params_name1=params_value1, params_name2=params_value2, ...), ...]\n" |
| 44 | + "Do not include anything else when calling the tools with the syntax above.\n\n" |
| 45 | + "Here is a list of functions in JSON format that you can invoke.\n " }} |
| 46 | + {%- for t in tools %} |
| 47 | + {{- t | tojson(indent=4) }} |
| 48 | + {{- "\n\n" }} |
| 49 | + {%- endfor %} |
| 50 | + {%- endif %} |
| 51 | + {{- "<|eot|>" }} |
| 52 | +{%- endif %} |
| 53 | + |
| 54 | +{#- Custom tools are passed in a user message with some extra guidance #} |
| 55 | +{%- if tools_in_user_message and tools is not none %} |
| 56 | + {#- Extract the first user message so we can plug it in here #} |
| 57 | + {%- if messages | length != 0 %} |
| 58 | + {%- if messages[0]['content'] is string %} |
| 59 | + {%- set first_user_message = messages[0]['content']|trim %} |
| 60 | + {%- else %} |
| 61 | + {%- set first_user_message = messages[0]['content'] | selectattr('type', 'equalto', 'text') | map(attribute='text') | map('trim') | join('\n') %} |
| 62 | + {%- endif %} |
| 63 | + {%- set messages = messages[1:] %} |
| 64 | + {%- else %} |
| 65 | + {{- raise_exception("Cannot put tools in the first user message when there's no first user message!") }} |
| 66 | + {%- endif %} |
| 67 | + {{- '<|header_start|>user<|header_end|>\n\n' -}} |
| 68 | + {{- first_user_message}} |
| 69 | + {{- "\nHere is a list of functions in JSON format that you can invoke:"}} |
| 70 | + {%- for t in tools %} |
| 71 | + {{- t | tojson(indent=4) }} |
| 72 | + {{- "\n\n" }} |
| 73 | + {%- endfor %} |
| 74 | + {{- "Should you decide to return the function call(s), put them in the format " |
| 75 | + "of [func_name1(params_name1=params_value1, params_name2=params_value2, " |
| 76 | + "...), ...]\nDo not include anything else when calling the tools with the " |
| 77 | + "syntax above." }} |
| 78 | +{%- endif %} |
| 79 | + |
| 80 | +{%- for message in messages %} |
| 81 | + {%- if not (message.role == 'ipython' or message.role == 'tool' or 'tool_calls' in message) %} |
| 82 | + {{- '<|header_start|>' + message['role'] + '<|header_end|>\n\n' }} |
| 83 | + {%- if message['content'] is string %} |
| 84 | + {{- message['content'] }} |
| 85 | + {%- else %} |
| 86 | + {%- for content in message['content'] %} |
| 87 | + {%- if content['type'] == 'image' %} |
| 88 | + {{- '<|image|>' }} |
| 89 | + {%- elif content['type'] == 'text' %} |
| 90 | + {{- content['text'] | trim }} |
| 91 | + {%- endif %} |
| 92 | + {%- endfor %} |
| 93 | + {%- endif %} |
| 94 | + {{- "<|eot|>" }} |
| 95 | + {%- elif 'tool_calls' in message and message.tool_calls|length > 0 %} |
| 96 | + {%- set tool_call = message.tool_calls[0].function %} |
| 97 | + {{- '<|header_start|>assistant<|header_end|>\n\n' -}} |
| 98 | + {%- if message['content'] is string %} |
| 99 | + {{- message['content'] }} |
| 100 | + {%- else %} |
| 101 | + {%- for content in message['content'] %} |
| 102 | + {%- if content['type'] == 'image' %} |
| 103 | + {{- '<|image|>' }} |
| 104 | + {%- elif content['type'] == 'text' %} |
| 105 | + {{- content['text'] }} |
| 106 | + {%- endif %} |
| 107 | + {%- endfor %} |
| 108 | + {%- endif %} |
| 109 | + {%- for tool_call in message.tool_calls %} |
| 110 | + {%- if tool_call.function is defined %} |
| 111 | + {%- set tool_call = tool_call.function %} |
| 112 | + {%- endif %} |
| 113 | + {{- tool_call.name + '(' -}} |
| 114 | + {%- for param in tool_call.arguments %} |
| 115 | + {{- param + '=' -}} |
| 116 | + {{- "%s" | format(tool_call.arguments[param]) -}} |
| 117 | + {% if not loop.last %}, {% endif %} |
| 118 | + {%- endfor %} |
| 119 | + {{- ')' -}} |
| 120 | + {% if not loop.last %}, {% endif %} |
| 121 | + {%- endfor %} |
| 122 | + {{- "<|eom|>" }} |
| 123 | + {%- elif message.role == "tool" or message.role == "ipython" %} |
| 124 | + {{- "<|header_start|>ipython<|header_end|>\n\n" }} |
| 125 | + {%- if message.content is string %} |
| 126 | + {{- message.content | tojson }} |
| 127 | + {%- else %} |
| 128 | + {%- for content in message['content'] %} |
| 129 | + {%- if content['type'] == 'text' %} |
| 130 | + {{- content['text'] | tojson }} |
| 131 | + {%- endif %} |
| 132 | + {%- endfor %} |
| 133 | + {%- endif %} |
| 134 | + {{- "<|eom|>" }} |
| 135 | + {%- endif %} |
| 136 | +{%- endfor %} |
| 137 | +{%- if add_generation_prompt %} |
| 138 | + {{- '<|header_start|>assistant<|header_end|>\n\n' }} |
| 139 | +{%- endif %} |
0 commit comments