tool_chat_template_llama3.2_json.jinja 5.7 KB

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