meetkai-functionary-medium-v3.2.jinja 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. {# version=v3.llama3 #}{%- macro append_new_param_info(param_declaration, comment_info, examples_info, depth) -%}
  2. {%- set offset = "" -%}
  3. {%- if depth >= 1 -%}
  4. {%- set offset = " " * depth -%}
  5. {%- endif -%}
  6. {%- if comment_info != "<|NONE|>" -%}
  7. {{ "\n" + offset + comment_info }}
  8. {%- if examples_info | length > 0 -%}
  9. {# Append each example info #}
  10. {%- for example in examples_info -%}
  11. {{ "\n" + offset + "// " + example|string|replace("'", '"') }}
  12. {%- endfor -%}
  13. {%- endif -%}
  14. {%- endif -%}
  15. {{ "\n" + offset + param_declaration }}
  16. {%- endmacro -%}
  17. {%- macro convert_data_type(param_type) -%}
  18. {%- if param_type == "integer" or param_type == "float" -%}
  19. {{ "number" }}
  20. {%- else -%}
  21. {{ param_type }}
  22. {%- endif -%}
  23. {%- endmacro -%}
  24. {%- macro get_param_type(param) -%}
  25. {%- set param_type = "any" -%}
  26. {%- if "type" in param -%}
  27. {%- set raw_param_type = param["type"] -%}
  28. {%- if raw_param_type is iterable and raw_param_type is not string -%}
  29. {%- set param_type = raw_param_type | join(" | ") -%}
  30. {%- else -%}
  31. {%- set param_type = raw_param_type -%}
  32. {%- endif -%}
  33. {{ convert_data_type(param_type) }}
  34. {%- elif "oneOf" in param -%}
  35. {%- set one_of_types = param["oneOf"]|selectattr("type", "defined")|list -%}
  36. {%- set one_of_types = one_of_types|map(attribute="type")|unique|list -%}
  37. {{ convert_data_type(one_of_types | join(" | ")) }}
  38. {%- endif -%}
  39. {%- endmacro -%}
  40. {%- macro get_format_param(param) -%}
  41. {%- if "format" in param -%}
  42. {{ param["format"] }}
  43. {%- elif "oneOf" in param -%}
  44. {%- set formats = [] -%}
  45. {%- for item in param["oneOf"] -%}
  46. {%- if "format" in item -%}
  47. {%- if item["format"] == param["oneOf"][-1]["format"] -%}
  48. {{ item["format"] }}
  49. {%- else -%}
  50. {{ item["format"] + " or "}}
  51. {%- endif -%}
  52. {%- endif -%}
  53. {%- endfor -%}
  54. {%- else -%}
  55. {{ "<|NONE|>" }}
  56. {%- endif -%}
  57. {%- endmacro -%}
  58. {%- macro get_param_info(param) -%}
  59. {%- set param_type = param.get("type", "any") -%}
  60. {%- set format_param = get_format_param(param) -%}
  61. {%- if "description" in param or "default" in param or format_param != "<|NONE|>" or param["maximum"] or param["minimum"] or param["maxLength"] or param["minLength"] -%}
  62. {{ "//" }}
  63. {%- if "description" in param -%}
  64. {%- set desc = param["description"] -%}
  65. {%- if not desc.endswith(".") -%}
  66. {%- set desc = desc + "." -%}
  67. {%- endif -%}
  68. {{ " " + desc }}
  69. {%- endif -%}
  70. {%- if "default" in param -%}
  71. {%- set default_value = param["default"] -%}
  72. {%- if param_type == "string" -%}
  73. {%- set default_value = '"' ~ default_value ~ '"' -%}
  74. {%- endif -%}
  75. {{ " Default=" ~ default_value ~ "." }}
  76. {%- endif -%}
  77. {%- set format_param = get_format_param(param) -%}
  78. {%- if format_param != "<|NONE|>" -%}
  79. {{ " Format=" ~ format_param }}
  80. {%- endif -%}
  81. {%- for field, field_name in [("maximum", "Maximum"), ("minimum", "Minimum"), ("maxLength", "Maximum length"), ("minLength", "Minimum length")] -%}
  82. {%- if field in param -%}
  83. {{ " " + field_name ~ "=" ~ param[field] }}
  84. {%- endif -%}
  85. {%- endfor -%}
  86. {%- else -%}
  87. {{ "<|NONE|>"}}
  88. {%- endif -%}
  89. {%- endmacro -%}
  90. {%- macro get_enum_option_str(enum_options) -%}
  91. {%- for v in enum_options -%}
  92. {%- if v is string -%}
  93. {{ '"' + v + '"' }}
  94. {%- else -%}
  95. {{ v }}
  96. {%- endif -%}
  97. {%- if enum_options|length > 0 and v != enum_options[-1] -%}
  98. {{ " | " }}
  99. {%- endif -%}
  100. {%- endfor -%}
  101. {%- endmacro -%}
  102. {%- macro get_array_typescript(param_name, param_dic, depth) -%}
  103. {%- set offset = '' -%}
  104. {%- if depth >= 1 -%}
  105. {%- set offset = " " * depth -%}
  106. {%- endif -%}
  107. {%- set items_info = param_dic.get('items', {}) -%}
  108. {%- if items_info|length == 0 -%}
  109. {%- if param_name -%}
  110. {{ "\n" + offset + param_name + ": []" }}
  111. {%- else -%}
  112. {{ "\n" + offset + "[]" }}
  113. {%- endif -%}
  114. {%- else -%}
  115. {%- set array_type = get_param_type(items_info) -%}
  116. {%- if array_type == 'object' -%}
  117. {%- if param_name -%}
  118. {{ "\n" + offset + param_name + ": {" }}
  119. {%- else -%}
  120. {{ "\n" + offset + "{" }}
  121. {%- endif -%}
  122. {{ get_parameter_typescript(items_info.get('properties', {}), items_info.get('required', []), depth + 1) -}}
  123. {{- "\n" + offset + "}[]" }}
  124. {%- elif array_type == 'array' -%}
  125. {%- set item_info = get_array_typescript(None, items_info, depth + 1) -%}
  126. {%- if not param_name -%}
  127. {{ "\n" + item_info + "[]" }}
  128. {%- else -%}
  129. {{ "\n" + offset + param_name + ": " + item_info|trim + "[]" }}
  130. {%- endif -%}
  131. {%- else -%}
  132. {%- if 'enum' in items_info -%}
  133. {%- set item_type = get_enum_option_str(items_info['enum']) -%}
  134. {%- if param_name is none -%}
  135. {{ "(" + item_type + ")[]"}}
  136. {%- else -%}
  137. {{ "\n" + offset + param_name + ": (" + item_type + ")[]" }}
  138. {%- endif -%}
  139. {%- else -%}
  140. {%- if param_name is none -%}
  141. {{ "\n" + array_type + "[]" }}
  142. {%- else -%}
  143. {{ "\n" + offset + param_name + ": " + array_type + "[]," }}
  144. {%- endif -%}
  145. {%- endif -%}
  146. {%- endif -%}
  147. {%- endif -%}
  148. {%- endmacro -%}
  149. {%- macro get_parameter_typescript(properties, required_params, depth=0) -%}
  150. {%- set res = "" -%}
  151. {%- for param_name, param in properties.items() -%}
  152. {%- if param is mapping -%}
  153. {%- set comment_info = get_param_info(param) -%}
  154. {# Param Examples #}
  155. {%- set examples_info = [] -%}
  156. {%- if "examples" in param -%}
  157. {%- set examples_info = ["Example " + param_name + ":"] -%}
  158. {%- set examples_info = examples_info + param["examples"] -%}
  159. {%- endif -%}
  160. {# Param Name declaration #}
  161. {%- set param_declaration = param_name -%}
  162. {%- if required_params is iterable and param_name not in required_params -%}
  163. {%- set param_declaration = param_declaration + "?" -%}
  164. {%- endif -%}
  165. {%- set param_type = get_param_type(param) -%}
  166. {# Handle indentation based on depth #}
  167. {%- set offset = "" -%}
  168. {%- if depth >= 1 -%}
  169. {%- set offset = " " * depth -%}
  170. {%- endif -%}
  171. {%- if param_type == "object" -%}
  172. {%- if comment_info != "<|NONE|>" -%}
  173. {{ "\n" + offset + comment_info }}
  174. {%- endif -%}
  175. {%- if examples_info|length > 0 -%}
  176. {%- for example in examples_info -%}
  177. {{ "\n" + offset + "// " + example|string|replace("'", '"') }}
  178. {%- endfor -%}
  179. {%- endif -%}
  180. {%- set param_declaration = param_declaration + ": {" -%}
  181. {{ "\n" + offset + param_declaration -}}
  182. {{- get_parameter_typescript(param.get("properties", {}), param.get("required", []), depth + 1) -}}
  183. {{- "\n" + offset + "}," }}
  184. {%- elif param_type == "array" -%}
  185. {%- set item_info = param.get("items", {}) -%}
  186. {%- if "type" not in item_info -%}
  187. {%- set param_declaration = param_declaration + ": []," -%}
  188. {{ append_new_param_info(param_declaration, comment_info, examples_info, depth) }}
  189. {%- else -%}
  190. {%- if comment_info != "<|NONE|>" -%}
  191. {{ "\n" + offset + comment_info }}
  192. {%- endif -%}
  193. {%- if examples_info|length > 0 -%}
  194. {%- for example in examples_info -%}
  195. {{ "\n" + offset + "// " + example|string|replace("'", '"') }}
  196. {%- endfor -%}
  197. {%- endif -%}
  198. {%- set array_declaration = get_array_typescript(param_declaration, param, depth) -%}
  199. {%- if not array_declaration.endswith(",") -%}
  200. {%- set array_declaration = array_declaration + "," -%}
  201. {%- endif -%}
  202. {{ array_declaration}}
  203. {%- endif -%}
  204. {%- else -%}
  205. {%- if "enum" in param -%}
  206. {%- set param_type = get_enum_option_str(param["enum"]) -%}
  207. {%- endif -%}
  208. {%- if "nullable" in param and param["nullable"] -%}
  209. {%- set param_type = param_type + " | null" -%}
  210. {%- endif -%}
  211. {%- set param_declaration = param_declaration + ": " + param_type + "," -%}
  212. {{ append_new_param_info(param_declaration, comment_info, examples_info, depth) }}
  213. {%- endif -%}
  214. {%- endif -%}
  215. {%- endfor -%}
  216. {%- endmacro -%}
  217. {%- macro generate_schema_from_functions(functions, namespace='functions') -%}
  218. {{ "// Supported function definitions that should be called when necessary.\n" -}}
  219. {{- "namespace " + namespace + " {\n\n" -}}
  220. {%- for function in functions -%}
  221. {%- if function.get("function") -%}
  222. {%- set function = function.get("function") -%}
  223. {%- endif -%}
  224. {%- set function_name = function.get("name") -%}
  225. {%- if function_name -%}
  226. {%- set description = function.get('description', '') -%}
  227. {%- set parameters = function.get('parameters', {}) -%}
  228. {{- "// " + description + "\n" -}}
  229. {{- "type " + function_name -}}
  230. {%- if parameters and parameters.get("properties") -%}
  231. {{- " = (_: {" -}}
  232. {%- set required_params = parameters.get("required", []) -%}
  233. {{ get_parameter_typescript(parameters.get("properties"), required_params, 0) -}}
  234. {{- "\n}) => any;\n\n" }}
  235. {%- else -%}
  236. {{ " = () => any;\n\n" }}
  237. {%- endif -%}
  238. {%- endif -%}
  239. {%- endfor -%}
  240. {{ "} // namespace " + namespace }}
  241. {%- endmacro -%}
  242. {%- if not tools -%}
  243. {%- set tools = [] -%}
  244. {%- endif -%}
  245. {{ bos_token + '<|start_header_id|>system<|end_header_id|>\n\nYou are capable of executing available function(s) if required.\nOnly execute function(s) when absolutely necessary.\nAsk for the required input to:recipient==all\nUse JSON for function arguments.\nRespond in this format:\n>>>${recipient}\n${content}\nAvailable functions:\n' + generate_schema_from_functions(tools) + '<|eot_id|>' -}}
  246. {%- if tools|length > 0 and tools|selectattr("type", "equalto", "code_interpreter")|list|length > 0 -%}
  247. {{ '<|start_header_id|>system<|end_header_id|>\n\nWhen you send a message containing Python code to python, it will be executed in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 60.0 seconds. The drive at \'/mnt/data\' can be used to save and persist user files.<|eot_id|>' }}
  248. {%- endif -%}
  249. {%- for message in messages -%}
  250. {%- if message['role'] == 'user' or message['role'] == 'system' -%}
  251. {{ '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n' + message['content'] + '<|eot_id|>' }}
  252. {%- elif message['role'] == 'tool' -%}
  253. {{ '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n' + message['content'] + '<|eot_id|>' }}
  254. {%- else -%}
  255. {{ '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'}}
  256. {%- if message['content'] -%}
  257. {{ '>>>all\n' + message['content'] }}
  258. {%- endif -%}
  259. {%- if 'tool_calls' in message and message['tool_calls'] -%}
  260. {%- for tool_call in message['tool_calls'] -%}
  261. {{ '>>>' + tool_call['function']['name'] + '\n' + tool_call['function']['arguments'] }}
  262. {%- endfor -%}
  263. {%- endif -%}
  264. {{ '<|eot_id|>' }}
  265. {%- endif -%}
  266. {%- endfor -%}
  267. {% if add_generation_prompt %}{{ '<|start_header_id|>assistant<|end_header_id|>\n\n>>>' }}{% endif %}