|
| 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 | + {#- Llama 3.1 doesn't pass all tests if the tools are in the system prompt #} |
| 7 | + {%- set tools_in_user_message = true %} |
| 8 | +{%- endif %} |
| 9 | +{%- if not date_string is defined %} |
| 10 | + {%- if strftime_now is defined %} |
| 11 | + {%- set date_string = strftime_now("%d %b %Y") %} |
| 12 | + {%- else %} |
| 13 | + {%- set date_string = "26 Jul 2024" %} |
| 14 | + {%- endif %} |
| 15 | +{%- endif %} |
| 16 | +{%- if not tools is defined %} |
| 17 | + {%- set tools = none %} |
| 18 | +{%- endif %} |
| 19 | + |
| 20 | +{#- This block extracts the system message, so we can slot it into the right place. #} |
| 21 | +{%- if messages[0]['role'] == 'system' %} |
| 22 | + {%- set system_message = messages[0]['content']|trim %} |
| 23 | + {%- set messages = messages[1:] %} |
| 24 | +{%- else %} |
| 25 | + {%- 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." %} |
| 26 | +{%- endif %} |
| 27 | + |
| 28 | +{#- System message #} |
| 29 | +{{- "<|start_header_id|>system<|end_header_id|>\n\n" }} |
| 30 | +{%- if tools is not none %} |
| 31 | + {{- "Environment: ipython\n" }} |
| 32 | +{%- endif %} |
| 33 | +{{- "Cutting Knowledge Date: December 2023\n" }} |
| 34 | +{{- "Today Date: " + date_string + "\n\n" }} |
| 35 | +{%- if tools is not none and not tools_in_user_message %} |
| 36 | + {{- "You have access to the following functions. To call a function, please respond with JSON for a function call." }} |
| 37 | + {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }} |
| 38 | + {{- "Do not use variables.\n\n" }} |
| 39 | + {%- for t in tools %} |
| 40 | + {{- t | tojson(indent=4) }} |
| 41 | + {{- "\n\n" }} |
| 42 | + {%- endfor %} |
| 43 | +{%- endif %} |
| 44 | +{{- system_message }} |
| 45 | +{{- "<|eot_id|>" }} |
| 46 | + |
| 47 | +{#- Custom tools are passed in a user message with some extra guidance #} |
| 48 | +{%- if tools_in_user_message and not tools is none %} |
| 49 | + {#- Extract the first user message so we can plug it in here #} |
| 50 | + {%- if messages | length != 0 %} |
| 51 | + {%- set first_user_message = messages[0]['content']|trim %} |
| 52 | + {%- set messages = messages[1:] %} |
| 53 | + {%- else %} |
| 54 | + {{- raise_exception("Cannot put tools in the first user message when there's no first user message!") }} |
| 55 | + {%- endif %} |
| 56 | + {{- '<|start_header_id|>user<|end_header_id|>\n\n' -}} |
| 57 | + {{- "Given the following functions, please respond with a JSON for a function call " }} |
| 58 | + {{- "with its proper arguments that best answers the given prompt.\n\n" }} |
| 59 | + {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }} |
| 60 | + {{- "Do not use variables.\n\n" }} |
| 61 | + {%- for t in tools %} |
| 62 | + {{- t | tojson(indent=4) }} |
| 63 | + {{- "\n\n" }} |
| 64 | + {%- endfor %} |
| 65 | + {{- first_user_message + "<|eot_id|>"}} |
| 66 | +{%- endif %} |
| 67 | + |
| 68 | +{%- for message in messages %} |
| 69 | + {%- if not (message.role == 'ipython' or message.role == 'tool' or 'tool_calls' in message) %} |
| 70 | + {{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'+ message['content'] | trim + '<|eot_id|>' }} |
| 71 | + {%- elif 'tool_calls' in message %} |
| 72 | + {%- if not message.tool_calls|length == 1 %} |
| 73 | + {{- raise_exception("This model only supports single tool-calls at once!") }} |
| 74 | + {%- endif %} |
| 75 | + {%- set tool_call = message.tool_calls[0].function %} |
| 76 | + {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' -}} |
| 77 | + {{- '{"name": "' + tool_call.name + '", ' }} |
| 78 | + {{- '"parameters": ' }} |
| 79 | + {{- tool_call.arguments | tojson }} |
| 80 | + {{- "}" }} |
| 81 | + {{- "<|eot_id|>" }} |
| 82 | + {%- elif message.role == "tool" or message.role == "ipython" %} |
| 83 | + {{- "<|start_header_id|>ipython<|end_header_id|>\n\n" }} |
| 84 | + {%- if message.content is mapping %} |
| 85 | + {{- message.content | tojson }} |
| 86 | + {%- else %} |
| 87 | + {{- { "output": message.content } | tojson }} |
| 88 | + {%- endif %} |
| 89 | + {{- "<|eot_id|>" }} |
| 90 | + {%- endif %} |
| 91 | +{%- endfor %} |
| 92 | +{%- if add_generation_prompt %} |
| 93 | + {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' }} |
| 94 | +{%- endif %} |
0 commit comments