mistral_tool.jinja 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. {%- if messages[0]["role"] == "system" %}
  2. {%- set system_message = messages[0]["content"] %}
  3. {%- set loop_messages = messages[1:] %}
  4. {%- else %}
  5. {%- set loop_messages = messages %}
  6. {%- endif %}
  7. {%- if not tools is defined %}
  8. {%- set tools = none %}
  9. {%- endif %}
  10. {%- set user_messages = loop_messages | selectattr("role", "equalto", "user") | list %}
  11. {%- for message in loop_messages | rejectattr("role", "equalto", "tool") | rejectattr("role", "equalto", "tool_results") | selectattr("tool_calls", "undefined") %}
  12. {%- if (message["role"] == "user") != (loop.index0 % 2 == 0) %}
  13. {{- raise_exception("After the optional system message, conversation roles must alternate user/assistant/user/assistant/...") }}
  14. {%- endif %}
  15. {%- endfor %}
  16. {{- bos_token }}
  17. {%- for message in loop_messages %}
  18. {%- if message["role"] == "user" %}
  19. {%- if tools is not none and (message == user_messages[-1]) %}
  20. {{- "[AVAILABLE_TOOLS] [" }}
  21. {%- for tool in tools %}
  22. {%- set tool = tool.function %}
  23. {{- '{"type": "function", "function": {' }}
  24. {%- for key, val in tool.items() if key != "return" %}
  25. {%- if val is string %}
  26. {{- '"' + key + '": "' + val + '"' }}
  27. {%- else %}
  28. {{- '"' + key + '": ' + val|tojson }}
  29. {%- endif %}
  30. {%- if not loop.last %}
  31. {{- ", " }}
  32. {%- endif %}
  33. {%- endfor %}
  34. {{- "}}" }}
  35. {%- if not loop.last %}
  36. {{- ", " }}
  37. {%- else %}
  38. {{- "]" }}
  39. {%- endif %}
  40. {%- endfor %}
  41. {{- "[/AVAILABLE_TOOLS]" }}
  42. {%- endif %}
  43. {%- if loop.last and system_message is defined %}
  44. {{- "[INST] " + system_message + "\n\n" + message["content"] + "[/INST]" }}
  45. {%- else %}
  46. {{- "[INST] " + message["content"] + "[/INST]" }}
  47. {%- endif %}
  48. {%- elif message["role"] == "tool_calls" or message.tool_calls is defined %}
  49. {%- if message.tool_calls is defined %}
  50. {%- set tool_calls = message.tool_calls %}
  51. {%- else %}
  52. {%- set tool_calls = message.content %}
  53. {%- endif %}
  54. {{- "[TOOL_CALLS] [" }}
  55. {%- for tool_call in tool_calls %}
  56. {%- set out = tool_call.function|tojson %}
  57. {{- out[:-1] }}
  58. {%- if not tool_call.id is defined or tool_call.id|length < 9 %}
  59. {{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9! (1)" + tool_call.id) }}
  60. {%- endif %}
  61. {{- ', "id": "' + tool_call.id[-9:] + '"}' }}
  62. {%- if not loop.last %}
  63. {{- ", " }}
  64. {%- else %}
  65. {{- "]" + eos_token }}
  66. {%- endif %}
  67. {%- endfor %}
  68. {%- elif message["role"] == "assistant" %}
  69. {{- " " + message["content"] + eos_token }}
  70. {%- elif message["role"] == "tool_results" or message["role"] == "tool" %}
  71. {%- if message.content is defined and message.content.content is defined %}
  72. {%- set content = message.content.content %}
  73. {%- else %}
  74. {%- set content = message.content %}
  75. {%- endif %}
  76. {{- '[TOOL_RESULTS] {"content": ' + content|string + ", " }}
  77. {%- if not message.tool_call_id is defined or message.tool_call_id|length < 9 %}
  78. {{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9! (2)" + message.tool_call_id) }}
  79. {%- endif %}
  80. {{- '"call_id": "' + message.tool_call_id[-9:] + '"}[/TOOL_RESULTS]' }}
  81. {%- else %}
  82. {{- raise_exception("Only user and assistant roles are supported, with the exception of an initial optional system message!") }}
  83. {%- endif %}
  84. {%- endfor %}