mistral_parallel_tool.jinja 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. {%- if tools is defined %}
  11. {%- set parallel_tool_prompt = "You are a helpful assistant that can call tools. If you call one or more tools, format them in a single JSON array or objects, where each object is a tool call, not as separate objects outside of an array or multiple arrays. Use the format [{\"name\": tool call name, \"arguments\": tool call arguments}, additional tool calls] if you call more than one tool. If you call tools, do not attempt to interpret them or otherwise provide a response until you receive a tool call result that you can interpret for the user." %}
  12. {%- if system_message is defined %}
  13. {%- set system_message = parallel_tool_prompt + "\n\n" + system_message %}
  14. {%- else %}
  15. {%- set system_message = parallel_tool_prompt %}
  16. {%- endif %}
  17. {%- endif %}
  18. {%- set user_messages = loop_messages | selectattr("role", "equalto", "user") | list %}
  19. {%- for message in loop_messages | rejectattr("role", "equalto", "tool") | rejectattr("role", "equalto", "tool_results") | selectattr("tool_calls", "undefined") %}
  20. {%- if (message["role"] == "user") != (loop.index0 % 2 == 0) %}
  21. {{- raise_exception("After the optional system message, conversation roles must alternate user/assistant/user/assistant/...") }}
  22. {%- endif %}
  23. {%- endfor %}
  24. {{- bos_token }}
  25. {%- for message in loop_messages %}
  26. {%- if message["role"] == "user" %}
  27. {%- if tools is not none and (message == user_messages[-1]) %}
  28. {{- "[AVAILABLE_TOOLS] [" }}
  29. {%- for tool in tools %}
  30. {%- set tool = tool.function %}
  31. {{- '{"type": "function", "function": {' }}
  32. {%- for key, val in tool.items() if key != "return" %}
  33. {%- if val is string %}
  34. {{- '"' + key + '": "' + val + '"' }}
  35. {%- else %}
  36. {{- '"' + key + '": ' + val|tojson }}
  37. {%- endif %}
  38. {%- if not loop.last %}
  39. {{- ", " }}
  40. {%- endif %}
  41. {%- endfor %}
  42. {{- "}}" }}
  43. {%- if not loop.last %}
  44. {{- ", " }}
  45. {%- else %}
  46. {{- "]" }}
  47. {%- endif %}
  48. {%- endfor %}
  49. {{- "[/AVAILABLE_TOOLS]" }}
  50. {%- endif %}
  51. {%- if loop.last and system_message is defined %}
  52. {{- "[INST] " + system_message + "\n\n" + message["content"] + "[/INST]" }}
  53. {%- else %}
  54. {{- "[INST] " + message["content"] + "[/INST]" }}
  55. {%- endif %}
  56. {%- elif message["role"] == "tool_calls" or message.tool_calls is defined %}
  57. {%- if message.tool_calls is defined %}
  58. {%- set tool_calls = message.tool_calls %}
  59. {%- else %}
  60. {%- set tool_calls = message.content %}
  61. {%- endif %}
  62. {{- "[TOOL_CALLS] [" }}
  63. {%- for tool_call in tool_calls %}
  64. {%- set out = tool_call.function|tojson %}
  65. {{- out[:-1] }}
  66. {%- if not tool_call.id is defined or tool_call.id|length < 9 %}
  67. {{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9! (1)" + tool_call.id) }}
  68. {%- endif %}
  69. {{- ', "id": "' + tool_call.id[-9:] + '"}' }}
  70. {%- if not loop.last %}
  71. {{- ", " }}
  72. {%- else %}
  73. {{- "]" + eos_token }}
  74. {%- endif %}
  75. {%- endfor %}
  76. {%- elif message["role"] == "assistant" %}
  77. {{- " " + message["content"] + eos_token }}
  78. {%- elif message["role"] == "tool_results" or message["role"] == "tool" %}
  79. {%- if message.content is defined and message.content.content is defined %}
  80. {%- set content = message.content.content %}
  81. {%- else %}
  82. {%- set content = message.content %}
  83. {%- endif %}
  84. {{- '[TOOL_RESULTS] {"content": ' + content|string + ", " }}
  85. {%- if not message.tool_call_id is defined or message.tool_call_id|length < 9 %}
  86. {{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9! (2)" + message.tool_call_id) }}
  87. {%- endif %}
  88. {{- '"call_id": "' + message.tool_call_id[-9:] + '"}[/TOOL_RESULTS]' }}
  89. {%- else %}
  90. {{- raise_exception("Only user and assistant roles are supported, with the exception of an initial optional system message!") }}
  91. {%- endif %}
  92. {%- endfor %}