Apertus-8B-Instruct.jinja 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. {%- macro render_typescript_type(param_spec, required_params, is_nullable=false) -%}
  2. {%- if param_spec.type == "array" -%}
  3. {%- if param_spec['items'] -%}
  4. {%- if param_spec['items']['type'] == "string" -%}
  5. {{- "string[]" }}
  6. {%- elif param_spec['items']['type'] == "number" -%}
  7. {{- "number[]" }}
  8. {%- elif param_spec['items']['type'] == "integer" -%}
  9. {{- "number[]" }}
  10. {%- elif param_spec['items']['type'] == "boolean" -%}
  11. {{- "boolean[]" }}
  12. {%- else -%}
  13. {%- set inner_type = render_typescript_type(param_spec['items'], required_params) -%}
  14. {%- if inner_type == "object | object" or inner_type|length > 50 -%}
  15. {{- "any[]" }}
  16. {%- else -%}
  17. {{- inner_type + "[]" }}
  18. {%- endif -%}
  19. {%- endif -%}
  20. {%- if param_spec.nullable -%}
  21. {{- " | null" }}
  22. {%- endif -%}
  23. {%- else -%}
  24. {{- "any[]" }}
  25. {%- if param_spec.nullable -%}
  26. {{- " | null" }}
  27. {%- endif -%}
  28. {%- endif -%}
  29. {%- elif param_spec.type is defined and param_spec.type is iterable and param_spec.type is not string and param_spec.type is not mapping and param_spec.type[0] is defined -%}
  30. {#- Handle array of types like ["object", "object"] from Union[dict, list] #}
  31. {%- if param_spec.type | length > 1 -%}
  32. {{- param_spec.type | join(" | ") }}
  33. {%- else -%}
  34. {{- param_spec.type[0] }}
  35. {%- endif -%}
  36. {%- elif param_spec.oneOf -%}
  37. {#- Handle oneOf schemas - check for complex unions and fallback to any #}
  38. {%- set has_object_variants = false -%}
  39. {%- for variant in param_spec.oneOf -%}
  40. {%- if variant.type == "object" -%}
  41. {%- set has_object_variants = true -%}
  42. {%- endif -%}
  43. {%- endfor -%}
  44. {%- if has_object_variants and param_spec.oneOf|length > 1 -%}
  45. {{- "any" }}
  46. {%- else -%}
  47. {%- for variant in param_spec.oneOf -%}
  48. {{- render_typescript_type(variant, required_params) -}}
  49. {%- if variant.description %}
  50. {{- "// " + variant.description }}
  51. {%- endif -%}
  52. {%- if variant.default is defined %}
  53. {{ "// default: " + variant.default|tojson }}
  54. {%- endif -%}
  55. {%- if not loop.last %}
  56. {{- " | " }}
  57. {% endif -%}
  58. {%- endfor -%}
  59. {%- endif -%}
  60. {%- elif param_spec.type == "string" -%}
  61. {%- if param_spec.enum -%}
  62. {{- '"' + param_spec.enum|join('" | "') + '"' -}}
  63. {%- else -%}
  64. {{- "string" }}
  65. {%- if param_spec.nullable %}
  66. {{- " | null" }}
  67. {%- endif -%}
  68. {%- endif -%}
  69. {%- elif param_spec.type == "number" -%}
  70. {{- "number" }}
  71. {%- elif param_spec.type == "integer" -%}
  72. {{- "number" }}
  73. {%- elif param_spec.type == "boolean" -%}
  74. {{- "boolean" }}
  75. {%- elif param_spec.type == "object" -%}
  76. {%- if param_spec.properties -%}
  77. {{- "{\n" }}
  78. {%- for prop_name, prop_spec in param_spec.properties.items() -%}
  79. {{- prop_name -}}
  80. {%- if prop_name not in (param_spec.required or []) -%}
  81. {{- "?" }}
  82. {%- endif -%}
  83. {{- ": " }}
  84. {{ render_typescript_type(prop_spec, param_spec.required or []) }}
  85. {%- if not loop.last -%}
  86. {{-", " }}
  87. {%- endif -%}
  88. {%- endfor -%}
  89. {{- "}" }}
  90. {%- else -%}
  91. {{- "object" }}
  92. {%- endif -%}
  93. {%- else -%}
  94. {{- "any" }}
  95. {%- endif -%}
  96. {%- endmacro -%}
  97. {%- macro render_tools(tools) -%}
  98. {%- for tool in tools %}
  99. {{- "// " + tool.description + "\n" }}
  100. {{- "type "+ tool.name + " = " }}
  101. {%- if tool.parameters and tool.parameters.properties %}
  102. {{- "(_: {\n" }}
  103. {%- for param_name, param_spec in tool.parameters.properties.items() %}
  104. {%- if param_spec.description %}
  105. {{- "// " + param_spec.description + "\n" }}
  106. {%- endif %}
  107. {{- param_name }}
  108. {%- if param_name not in (tool.parameters.required or []) -%}
  109. {{- "?" }}
  110. {%- endif -%}
  111. {{- ": " }}
  112. {{- render_typescript_type(param_spec, tool.parameters.required or []) }}
  113. {%- if param_spec.default is defined -%}
  114. {%- if param_spec.enum %}
  115. {{- ", // default: " + param_spec.default }}
  116. {%- elif param_spec.oneOf %}
  117. {{- "// default: " + param_spec.default }}
  118. {%- else %}
  119. {{- ", // default: " + param_spec.default|tojson }}
  120. {%- endif -%}
  121. {%- endif -%}
  122. {%- if not loop.last %}
  123. {{- ",\n" }}
  124. {%- else %}
  125. {{- "\n" }}
  126. {%- endif -%}
  127. {%- endfor %}
  128. {{- "}) => any;" }}
  129. {%- else -%}
  130. {{- "() => any;" }}
  131. {%- endif -%}
  132. {%- if not loop.last -%}
  133. {{- "\n" }}
  134. {%- endif -%}
  135. {%- endfor %}
  136. {%- endmacro -%}
  137. {{ bos_token }}
  138. {%- set system_token = '<|system_start|>' -%}
  139. {%- set end_system_token = '<|system_end|>' -%}
  140. {%- set developer_token = '<|developer_start|>' -%}
  141. {%- set end_developer_token = '<|developer_end|>' -%}
  142. {%- set user_token = '<|user_start|>' -%}
  143. {%- set end_user_token = '<|user_end|>' -%}
  144. {%- set assistant_token = '<|assistant_start|>' -%}
  145. {%- set end_assistant_token = '<|assistant_end|>' -%}
  146. {%- set inner_token = '<|inner_prefix|>' -%}
  147. {%- set outer_token = '<|inner_suffix|>' -%}
  148. {%- set tool_calls_token = '<|tools_prefix|>' -%}
  149. {%- set end_tool_calls_token = '<|tools_suffix|>' -%}
  150. {%- set ns = namespace(in_assistant=false, in_tool=false, in_inner=false, assistant_format=none) -%}
  151. {%- if messages and messages[0].role == 'system' -%}
  152. {%- if "content" in messages[0] -%}
  153. {%- if messages[0].content is string -%}
  154. {{ system_token + messages[0].content + end_system_token }}
  155. {%- elif messages[0].content is mapping and "text" in messages[0].content -%}
  156. {{ system_token + messages[0].content.text + end_system_token }}
  157. {%- else -%}
  158. {{- raise_exception("Invalid system message") -}}
  159. {%- endif -%}
  160. {%- else -%}
  161. {{- raise_exception("Invalid system message") -}}
  162. {%- endif -%}
  163. {%- set loop_messages = messages[1:] -%}
  164. {%- else -%}
  165. {{ system_token + 'You are Apertus, a helpful assistant created by the SwissAI initiative.\nKnowledge cutoff: 2024-04\nCurrent date: ' + strftime_now('%Y-%m-%d') + end_system_token }}
  166. {%- set loop_messages = messages -%}
  167. {%- endif -%}
  168. {{ developer_token + 'Deliberation: ' }}
  169. {%- if enable_thinking is defined and enable_thinking -%}
  170. {{ 'enabled\n' }}
  171. {%- else -%}
  172. {{ 'disabled\n' }}
  173. {%- endif -%}
  174. {%- if tools is defined and tools -%}
  175. {{ 'Tool Capabilities:\n' + render_tools(tools) }}
  176. {%- else -%}
  177. {{ 'Tool Capabilities: disabled' }}
  178. {%- endif -%}
  179. {{ end_developer_token }}
  180. {%- for message in loop_messages -%}
  181. {%- if message.role == 'user' -%}
  182. {%- set ns.in_inner = false -%}
  183. {%- if ns.in_tool -%}
  184. {{ ']' }}
  185. {%- set ns.in_tool = false -%}
  186. {%- endif -%}
  187. {%- if ns.in_assistant -%}
  188. {{ end_assistant_token }}
  189. {%- set ns.in_assistant = false -%}
  190. {%- endif -%}
  191. {%- if "content" in message -%}
  192. {{ user_token }}
  193. {%- if message.content is string -%}
  194. {{ message.content }}
  195. {%- elif message.content is mapping and "parts" in message.content -%}
  196. {%- set parts = message.content.parts -%}
  197. {%- for part in parts -%}
  198. {%- if part.type == "text" -%}
  199. {{ part.text }}
  200. {%- else -%}
  201. {{- raise_exception("Invalid user part: " + part.type) -}}
  202. {%- endif -%}
  203. {%- endfor -%}
  204. {%- else -%}
  205. {{- raise_exception("Invalid user message: " + message.role) -}}
  206. {%- endif -%}
  207. {{ end_user_token }}
  208. {%- endif -%}
  209. {%- elif message.role == 'assistant' -%}
  210. {%- if not ns.in_assistant -%}
  211. {{ assistant_token }}
  212. {%- set ns.in_assistant = true -%}
  213. {%- endif -%}
  214. {%- if "content" in message and message.content is not none -%}
  215. {%- if message.content is string and (ns.assistant_format is none or ns.assistant_format == "string") -%}
  216. {%- if ns.in_tool -%}
  217. {{ ']' }}
  218. {%- set ns.in_tool = false -%}
  219. {%- endif -%}
  220. {%- set ns.assistant_format = "string" -%}
  221. {{ message.content }}
  222. {%- elif message.content is mapping and "blocks" in message.content and (ns.assistant_format is none or ns.assistant_format == "mapping") -%}
  223. {%- set ns.assistant_format = "mapping" -%}
  224. {%- set blocks = message.content.blocks -%}
  225. {%- for block in blocks -%}
  226. {%- if block.type == 'thoughts' -%}
  227. {%- if ns.in_tool -%}
  228. {{ ']' }}
  229. {%- set ns.in_tool = false -%}
  230. {%- endif -%}
  231. {%- if not ns.in_inner -%}
  232. {%- set ns.in_inner = true -%}
  233. {{ inner_token }}
  234. {%- endif -%}
  235. {{ block.text }}
  236. {%- elif block.type == 'tool_calls' -%}
  237. {%- if ns.in_tool -%}
  238. {{ ']' }}
  239. {%- set ns.in_tool = false -%}
  240. {%- endif -%}
  241. {%- if ns.in_inner and not loop.first and block.calls|length == 1 and block.calls[0].name == 'display_answers' -%}
  242. {%- set ns.in_inner = false -%}
  243. {{ outer_token }}
  244. {%- endif -%}
  245. {{ tool_calls_token + '[' }}
  246. {%- for tool_call in block.calls -%}
  247. {{- '{"' + tool_call.name + '": ' + tool_call.arguments + '}' }}
  248. {%- if not loop.last -%}
  249. {{- ", " }}
  250. {%- endif -%}
  251. {%- endfor -%}
  252. {{ ']' + end_tool_calls_token }}
  253. {%- elif block.type == 'tool_outputs' -%}
  254. {%- if ns.in_tool -%}
  255. {{- raise_exception("Cannot have both tool outputs as separate messages and tool outputs as blocks") -}}
  256. {%- endif -%}
  257. {{ '[' }}
  258. {%- for tool_output in block.outputs -%}
  259. {{- tool_output.output }}
  260. {%- if not loop.last -%}
  261. {{- ", " }}
  262. {%- endif -%}
  263. {%- endfor -%}
  264. {{- ']' }}
  265. {%- elif block.type == 'response' -%}
  266. {%- if ns.in_tool -%}
  267. {{ ']' }}
  268. {%- set ns.in_tool = false -%}
  269. {%- endif -%}
  270. {%- if (not loop.first and ns.in_inner) or (ns.in_assistant and ns.in_inner) -%}
  271. {%- set ns.in_inner = false -%}
  272. {{ outer_token }}
  273. {%- endif -%}
  274. {{ block.text }}
  275. {%- else -%}
  276. {{- raise_exception("Invalid assistant block type: " + block.type) -}}
  277. {%- endif -%}
  278. {%- endfor -%}
  279. {%- else -%}
  280. {{- raise_exception("Invalid assistant content '" + message.content + "', expected " + ns.assistant_format) -}}
  281. {%- endif -%}
  282. {%- elif "tool_calls" not in message -%}
  283. {{- raise_exception("Invalid assistant message " + message) -}}
  284. {%- endif -%}
  285. {%- if "tool_calls" in message and message.tool_calls -%}
  286. {{ tool_calls_token + '[' }}
  287. {%- for tool_call in message.tool_calls -%}
  288. {%- if tool_call.type == 'function' -%}
  289. {%- set function = tool_call.function -%}
  290. {{- '{"' + function.name + '": ' + function.arguments + '}' }}
  291. {%- if not loop.last -%}
  292. {{- ", " }}
  293. {%- endif -%}
  294. {%- else -%}
  295. {{- raise_exception("Invalid tool call type: " + tool_call.type) -}}
  296. {%- endif -%}
  297. {%- endfor -%}
  298. {{ ']' + end_tool_calls_token }}
  299. {%- endif -%}
  300. {%- elif message.role == 'tool' -%}
  301. {%- if not ns.in_assistant -%}
  302. {{- raise_exception("Tool message outside of assistant") -}}
  303. {%- endif -%}
  304. {%- if not ns.in_tool -%}
  305. {{ '[' }}
  306. {%- set ns.in_tool = true -%}
  307. {%- else -%}
  308. {{ ", "}}
  309. {%- endif -%}
  310. {{ message.content }}
  311. {%- else -%}
  312. {{- raise_exception("Invalid message role") -}}
  313. {%- endif -%}
  314. {%- endfor -%}
  315. {%- if ns.in_tool -%}
  316. {{ ']' }}
  317. {%- endif -%}
  318. {%- if add_generation_prompt -%}
  319. {{ assistant_token }}
  320. {%- endif -%}