mistralai-Mistral-Nemo-Instruct-2407.jinja 3.9 KB

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