| 1 | {{- bos_token }} |
| 2 | |
| 3 | {%- if custom_tools is defined %} |
| 4 | {%- set tools = custom_tools %} |
| 5 | {%- endif %} |
| 6 | {%- if not tools is defined %} |
| 7 | {%- set tools = none %} |
| 8 | {%- endif %} |
| 9 | |
| 10 | {#- This block extracts the system message, so we can slot it into the right place. #} |
| 11 | {%- if messages[0]['role'] == 'system' %} |
| 12 | {%- set system_message = messages[0]['content']|trim %} |
| 13 | {%- set messages = messages[1:] %} |
| 14 | {%- else %} |
| 15 | {%- 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." %} |
| 16 | {%- endif %} |
| 17 | |
| 18 | {{- "<|start_header_id|>system<|end_header_id|>\n\n" }} |
| 19 | {%- if tools is not none and not tools_in_user_message %} |
| 20 | {{- "You are an expert in composing functions. You are given a question and a set of possible functions. Based on the question, you will need to make one or more function/tool calls to achieve the purpose.\n" }} |
| 21 | {{- "If none of the function can be used, point it out. If the given question lacks the parameters required by the function, also point it out.\n" }} |
| 22 | {{- "You should only return the function call in tools call sections.\n\n" }} |
| 23 | {{- "If you decide to invoke any of the function(s), you MUST put it in the format of [func_name1(params_name1=params_value1, params_name2=params_value2...), func_name2(params)]\n" }} |
| 24 | {{- "You SHOULD NOT include any other text in the response.\n" }} |
| 25 | {{- "Here is a list of functions in JSON format that you can invoke.\n" }} |
| 26 | {%- for t in tools %} |
| 27 | {{- t | tojson(indent=4) }} |
| 28 | {{- "\n\n" }} |
| 29 | {%- endfor %} |
| 30 | {{- "\n" }} |
| 31 | {%- endif %} |
| 32 | {{- system_message }} |
| 33 | {{- "<|eot_id|>" }} |
| 34 | |
| 35 | {%- for message in messages %} |
| 36 | {%- if not (message.role == 'ipython' or message.role == 'tool' or 'tool_calls' in message) %} |
| 37 | {{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'+ message['content'] | trim + '<|eot_id|>' }} |
| 38 | {%- elif 'tool_calls' in message %} |
| 39 | {{- '<|start_header_id|>assistant<|end_header_id|>\n\n[' -}} |
| 40 | {%- for tool_call in message.tool_calls %} |
| 41 | {%- if tool_call.function is defined %} |
| 42 | {%- set tool_call = tool_call.function %} |
| 43 | {%- endif %} |
| 44 | {{- tool_call.name + '(' -}} |
| 45 | {%- for param in tool_call.arguments %} |
| 46 | {{- param + '=' -}} |
| 47 | {{- "%s" | format(tool_call.arguments[param]) -}} |
| 48 | {% if not loop.last %}, {% endif %} |
| 49 | {%- endfor %} |
| 50 | {{- ')' -}} |
| 51 | {% if not loop.last %}, {% endif %} |
| 52 | {%- endfor %} |
| 53 | {{- ']<|eot_id|>' -}} |
| 54 | {%- elif message.role == "tool" or message.role == "ipython" %} |
| 55 | {{- "<|start_header_id|>ipython<|end_header_id|>\n\n" }} |
| 56 | {%- if message.content is mapping %} |
| 57 | {{- message.content | tojson }} |
| 58 | {%- else %} |
| 59 | {{- { "output": message.content } | tojson }} |
| 60 | {%- endif %} |
| 61 | {{- "<|eot_id|>" }} |
| 62 | {%- endif %} |
| 63 | {%- endfor %} |
| 64 | |
| 65 | {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' }} |