tool_calling.jinja 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. {{- bos_token }}
  2. {%- if custom_tools is defined %}
  3. {%- set tools = custom_tools %}
  4. {%- endif %}
  5. {#- This block extracts the system message, so we can slot it into the right place. #}
  6. {%- if messages[0]['role'] == 'system' %}
  7. {%- set system_message = messages[0]['content']|trim %}
  8. {%- set messages = messages[1:] %}
  9. {%- else %}
  10. {%- set system_message = "" %}
  11. {%- endif %}
  12. {#- System message + builtin tools #}
  13. {{- "<|start_header_id|>system<|end_header_id|>\n\n" }}
  14. {%- if builtin_tools is defined %}
  15. {{- "Environment: ipython\n" }}
  16. {{- "Tools: " + builtin_tools | reject('equalto', 'code_interpreter') | join(", ") + "\n\n"}}
  17. {%- endif %}
  18. {{- "Cutting Knowledge Date: December 2023\n" }}
  19. {{- "Today Date: 23 Jul 2024\n\n" }}
  20. {{- system_message }}
  21. {{- "<|eot_id|>" }}
  22. {#- Custom tools are passed in a user message with some extra guidance #}
  23. {%- if tools is defined and not tools is none %}
  24. {#- Extract the first user message so we can plug it in here #}
  25. {%- if messages | length != 0 %}
  26. {%- set first_user_message = messages[0]['content']|trim %}
  27. {%- set messages = messages[1:] %}
  28. {%- else %}
  29. {{- raise_exception("Cannot put tools in the first user message when there's no first user message!") }}
  30. {%- endif %}
  31. {{- '<|start_header_id|>user<|end_header_id|>\n\n' -}}
  32. {{- "Given the following functions, please respond with a JSON for a function call " }}
  33. {{- "with its proper arguments that best answers the given prompt.\n\n" }}
  34. {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }}
  35. {{- "Do not use variables.\n\n" }}
  36. {%- for t in tools %}
  37. {{- t | tojson(indent=4) }}
  38. {{- "\n\n" }}
  39. {%- endfor %}
  40. {{- "Question: " + first_user_message + "<|eot_id|>"}}
  41. {%- endif %}
  42. {%- for message in messages %}
  43. {%- if not (message.role == 'ipython' or message.role == 'tool' or 'tool_calls' in message) %}
  44. {{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'+ message['content'] | trim + '<|eot_id|>' }}
  45. {%- elif 'tool_calls' in message %}
  46. {%- if not message.tool_calls|length == 1 %}
  47. {{- raise_exception("This model only supports single tool-calls at once!") }}
  48. {%- endif %}
  49. {%- set tool_call = message.tool_calls[0].function %}
  50. {%- if builtin_tools is defined and tool_call.name in builtin_tools %}
  51. {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' -}}
  52. {{- "<|python_tag|>" + tool_call.name + ".call(" }}
  53. {%- for arg_name, arg_val in tool_call.arguments | items %}
  54. {{- arg_name + '="' + arg_val + '"' }}
  55. {%- if not loop.last %}
  56. {{- ", " }}
  57. {%- endif %}
  58. {%- endfor %}
  59. {{- ")" }}
  60. {%- else %}
  61. {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' -}}
  62. {{- '{"name": "' + tool_call.name + '", ' }}
  63. {{- '"parameters": ' }}
  64. {{- tool_call.arguments | tojson }}
  65. {{- "}" }}
  66. {%- endif %}
  67. {%- if builtin_tools is defined %}
  68. {#- This means we're in ipython mode #}
  69. {{- "<|eom_id|>" }}
  70. {%- else %}
  71. {{- "<|eot_id|>" }}
  72. {%- endif %}
  73. {%- elif message.role == "tool" or message.role == "ipython" %}
  74. {{- "<|start_header_id|>ipython<|end_header_id|>\n\n" }}
  75. {%- if message.content is mapping or message.content is iterable %}
  76. {{- message.content | tojson }}
  77. {%- else %}
  78. {{- message.content }}
  79. {%- endif %}
  80. {{- "<|eot_id|>" }}
  81. {%- endif %}
  82. {%- endfor %}
  83. {%- if add_generation_prompt %}
  84. {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' }}
  85. {%- endif %}