tool_chat_template_llama3.1_json.jinja 5.1 KB

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