|
| 1 | +{% if not add_generation_prompt is defined %} |
| 2 | + {% set add_generation_prompt = false %} |
| 3 | +{% endif %} |
| 4 | + |
| 5 | +{% set ns = namespace(is_first=false, is_tool=false, is_output_first=true, system_prompt='', is_first_sp=true, is_last_user=false) %} |
| 6 | +{%- for message in messages %} |
| 7 | + {%- if message['role'] == 'system' %} |
| 8 | + {%- if ns.is_first_sp %} |
| 9 | + {% set ns.system_prompt = ns.system_prompt + message['content'] %} |
| 10 | + {% set ns.is_first_sp = false %} |
| 11 | + {%- else %} |
| 12 | + {% set ns.system_prompt = ns.system_prompt + '\n\n' + message['content'] %} |
| 13 | + {%- endif %} |
| 14 | + {%- endif %} |
| 15 | +{%- endfor %} |
| 16 | + |
| 17 | +{# --- Append tool descriptions if tools are defined --- #} |
| 18 | +{% if tools is defined and tools is not none %} |
| 19 | + {% set tool_ns = namespace(text='You are a helpful assistant with tool calling capabilities. ' |
| 20 | + 'When a tool call is needed, you MUST use the following format to issue the call:\n' |
| 21 | + '<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>FUNCTION_NAME\n' |
| 22 | + '```json\n{"param1": "value1", "param2": "value2"}\n```<|tool▁call▁end|><|tool▁calls▁end|>\n\n' |
| 23 | + 'Make sure the JSON is valid.' |
| 24 | + '## Tools\n\n### Function\n\nYou have the following functions available:\n\n') %} |
| 25 | + {% for tool in tools %} |
| 26 | + {% set tool_ns.text = tool_ns.text + '- `' + tool['name'] + '`:\n```json\n' + (tool | tojson) + '\n```\n' %} |
| 27 | + {% endfor %} |
| 28 | + {% set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %} |
| 29 | +{% endif %} |
| 30 | + |
| 31 | +{{ bos_token }} |
| 32 | +{{ ns.system_prompt }} |
| 33 | + |
| 34 | +{%- for message in messages %} |
| 35 | + {%- if message['role'] == 'user' %} |
| 36 | + {%- set ns.is_tool = false -%} |
| 37 | + {%- set ns.is_first = false -%} |
| 38 | + {%- set ns.is_last_user = true -%} |
| 39 | + {{'<|User|>' + message['content'] + '<|Assistant|>'}} |
| 40 | + {%- endif %} |
| 41 | + {%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %} |
| 42 | + {%- set ns.is_last_user = false -%} |
| 43 | + {%- if ns.is_tool %} |
| 44 | + {{'<|tool▁outputs▁end|>'}} |
| 45 | + {%- endif %} |
| 46 | + {%- set ns.is_first = false %} |
| 47 | + {%- set ns.is_tool = false -%} |
| 48 | + {%- set ns.is_output_first = true %} |
| 49 | + {%- for tool in message['tool_calls'] %} |
| 50 | + {%- if not ns.is_first %} |
| 51 | + {%- if message['content'] is none %} |
| 52 | + {{'<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}} |
| 53 | + {%- else %} |
| 54 | + {{message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}} |
| 55 | + {%- endif %} |
| 56 | + {%- set ns.is_first = true -%} |
| 57 | + {%- else %} |
| 58 | + {{'\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}} |
| 59 | + {%- endif %} |
| 60 | + {%- endfor %} |
| 61 | + {{'<|tool▁calls▁end|><|end▁of▁sentence|>'}} |
| 62 | + {%- endif %} |
| 63 | + {%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none)%} |
| 64 | + {%- set ns.is_last_user = false -%} |
| 65 | + {%- if ns.is_tool %} |
| 66 | + {{'<|tool▁outputs▁end|>' + message['content'] + '<|end▁of▁sentence|>'}} |
| 67 | + {%- set ns.is_tool = false -%} |
| 68 | + {%- else %} |
| 69 | + {% set content = message['content'] %} |
| 70 | + {{content + '<|end▁of▁sentence|>'}} |
| 71 | + {%- endif %} |
| 72 | + {%- endif %} |
| 73 | + {%- if message['role'] == 'tool' %} |
| 74 | + {%- set ns.is_last_user = false -%} |
| 75 | + {%- set ns.is_tool = true -%} |
| 76 | + {%- if ns.is_output_first %} |
| 77 | + {{ 'Use the results below to formulate an answer to the user question unless additional information is needed.' }} |
| 78 | + {{'<|tool▁outputs▁begin|><|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}} |
| 79 | + {%- set ns.is_output_first = false %} |
| 80 | + {%- else %} |
| 81 | + {{'\n<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}} |
| 82 | + {%- endif %} |
| 83 | + {%- endif %} |
| 84 | +{%- endfor -%} |
| 85 | + |
| 86 | +{% if ns.is_tool %} |
| 87 | + {{"<|tool▁outputs▁end|>"}} |
| 88 | +{% endif %} |
| 89 | +{% if add_generation_prompt and not ns.is_last_user and not ns.is_tool %} |
| 90 | + {{'<|Assistant|>'}} |
| 91 | +{% endif %} |
0 commit comments