hermes_tool.jinja 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. {%- macro json_to_python_type(json_spec) %}
  2. {%- set basic_type_map = {
  3. "string": "str",
  4. "number": "float",
  5. "integer": "int",
  6. "boolean": "bool"
  7. } %}
  8. {%- if basic_type_map[json_spec.type] is defined %}
  9. {{- basic_type_map[json_spec.type] }}
  10. {%- elif json_spec.type == "array" %}
  11. {{- "list[" + json_to_python_type(json_spec|items) + "]" }}
  12. {%- elif json_spec.type == "object" %}
  13. {%- if json_spec.additionalProperties is defined %}
  14. {{- "dict[str, " + json_to_python_type(json_spec.additionalProperties) + ']' }}
  15. {%- else %}
  16. {{- "dict" }}
  17. {%- endif %}
  18. {%- elif json_spec.type is iterable %}
  19. {{- "Union[" }}
  20. {%- for t in json_spec.type %}
  21. {{- json_to_python_type({"type": t}) }}
  22. {%- if not loop.last %}
  23. {{- "," }}
  24. {%- endif %}
  25. {%- endfor %}
  26. {{- "]" }}
  27. {%- else %}
  28. {{- "Any" }}
  29. {%- endif %}
  30. {%- endmacro %}
  31. {{- bos_token }}
  32. {{- "<|im_start|>system\nYou are a function calling AI model. You are provided with function signatures within <tools></tools> XML tags. You may call one or more functions to assist with the user query. Don't make assumptions about what values to plug into functions. Here are the available tools: <tools> " }}
  33. {%- if tools is iterable and tools | length > 0 %}
  34. {%- for tool in tools %}
  35. {%- if tool.function is defined %}
  36. {%- set tool = tool.function %}
  37. {%- endif %}
  38. {{- '{"type": "function", "function": ' }}
  39. {{- '{"name": "' + tool.name + '", ' }}
  40. {{- '"description": "' + tool.name + '(' }}
  41. {%- for param_name, param_fields in tool.parameters.properties|items %}
  42. {{- param_name + ": " + json_to_python_type(param_fields) }}
  43. {%- if not loop.last %}
  44. {{- ", " }}
  45. {%- endif %}
  46. {%- endfor %}
  47. {{- ")" }}
  48. {%- if tool.return is defined %}
  49. {{- " -> " + json_to_python_type(tool.return) }}
  50. {%- endif %}
  51. {{- " - " + tool.description + "\n\n" }}
  52. {%- for param_name, param_fields in tool.parameters.properties|items %}
  53. {%- if loop.first %}
  54. {{- " Args:\n" }}
  55. {%- endif %}
  56. {{- " " + param_name + "(" + json_to_python_type(param_fields) + "): " + param_fields.description|trim }}
  57. {%- endfor %}
  58. {%- if tool.return is defined and tool.return.description is defined %}
  59. {{- "\n Returns:\n " + tool.return.description }}
  60. {%- endif %}
  61. {{- '"' }}
  62. {{- ', "parameters": ' }}
  63. {%- if tool.parameters.properties | length == 0 %}
  64. {{- "{}" }}
  65. {%- else %}
  66. {{- tool.parameters|tojson }}
  67. {%- endif %}
  68. {{- "}" }}
  69. {%- if not loop.last %}
  70. {{- "\n" }}
  71. {%- endif %}
  72. {%- endfor %}
  73. {%- endif %}
  74. {{- " </tools>" }}
  75. {{- 'Use the following pydantic model json schema for each tool call you will make: {"properties": {"name": {"title": "Name", "type": "string"}, "arguments": {"title": "Arguments", "type": "object"}}, "required": ["name", "arguments"], "title": "FunctionCall", "type": "object"}}
  76. ' }}
  77. {{- "For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:
  78. " }}
  79. {{- "<tool_call>
  80. " }}
  81. {{- '{"name": <function-name>, "arguments": <args-dict>}
  82. ' }}
  83. {{- '</tool_call><|im_end|>' }}
  84. {%- for message in messages %}
  85. {%- if message.role == "user" or message.role == "system" or (message.role == "assistant" and message.tool_calls is not defined) %}
  86. {{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }}
  87. {%- elif message.role == "assistant" and message.tool_calls is defined %}
  88. {{- '<|im_start|>' + message.role }}
  89. {%- for tool_call in message.tool_calls %}
  90. {{- '\n<tool_call>\n' }}
  91. {%- if tool_call.function is defined %}
  92. {%- set tool_call = tool_call.function %}
  93. {%- endif %}
  94. {{- '{' }}
  95. {{- '"name": "' }}
  96. {{- tool_call.name }}
  97. {{- '"' }}
  98. {%- if tool_call.arguments is defined %}
  99. {{- ', ' }}
  100. {{- '"arguments": ' }}
  101. {{- tool_call.arguments|tojson }}
  102. {%- endif %}
  103. {{- '}' }}
  104. {{- '\n</tool_call>' }}
  105. {%- endfor %}
  106. {{- '<|im_end|>\n' }}
  107. {%- elif message.role == "tool" %}
  108. {%- if loop.previtem and loop.previtem.role != "tool" %}
  109. {{- '<|im_start|>tool\n' }}
  110. {%- endif %}
  111. {{- '<tool_response>\n' }}
  112. {{- message.content }}
  113. {%- if not loop.last %}
  114. {{- '\n</tool_response>\n' }}
  115. {%- else %}
  116. {{- '\n</tool_response>' }}
  117. {%- endif %}
  118. {%- if not loop.last and loop.nextitem.role != "tool" %}
  119. {{- '<|im_end|>' }}
  120. {%- elif loop.last %}
  121. {{- '<|im_end|>' }}
  122. {%- endif %}
  123. {%- endif %}
  124. {%- endfor %}
  125. {%- if add_generation_prompt %}
  126. {{- '<|im_start|>assistant\n' }}
  127. {%- endif %}