modprompt 0.12.6 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cls.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { HistoryTurn, ToolDefSpec, ToolCallSpec } from "@locallm/types";
2
- import { LmTemplate, PromptBlock, SpacingSlots, LmToolsDef, LmTags } from "./interfaces.js";
1
+ import type { HistoryTurn, ToolCallSpec, ToolDefSpec } from "@locallm/types";
2
+ import { LmTags, LmTemplate, LmToolsDef, PromptBlock, SpacingSlots } from "./interfaces.js";
3
3
  /**
4
4
  * Represents a modified language model template.
5
5
  *
@@ -27,6 +27,8 @@ declare class PromptTemplate {
27
27
  private _replaceSystem;
28
28
  private _toolCallStart;
29
29
  private _toolCallEnd;
30
+ private _toolCallParser;
31
+ private _beforeToolResponse;
30
32
  /**
31
33
  * Constructs a new `PromptTemplate` instance.
32
34
  *
@@ -60,7 +62,6 @@ declare class PromptTemplate {
60
62
  toolsCall: Array<ToolCallSpec>;
61
63
  error?: string;
62
64
  };
63
- encodeToolResponse(response: any): string;
64
65
  /**
65
66
  * Clones the current `PromptTemplate` instance to a new instance of `PromptTemplate`.
66
67
  *
@@ -166,7 +167,7 @@ declare class PromptTemplate {
166
167
  * @returns The rendered template with the provided message.
167
168
  *
168
169
  * @example
169
- * const prompted = tpl.prompt("list the planets in the solar system");
170
+ * const prompted = tpl.prompt("list the planets of the solar system");
170
171
  * console.log(prompted);
171
172
  */
172
173
  prompt(msg: string, skip_empty_system?: boolean): string;
@@ -183,6 +184,5 @@ declare class PromptTemplate {
183
184
  private _buildUserBlock;
184
185
  private _buildAssistantBlock;
185
186
  private _load;
186
- private _parseToolCallString;
187
187
  }
188
188
  export { PromptTemplate };
@@ -83,6 +83,8 @@ interface LmToolsDef {
83
83
  * The expected response format from the tool.
84
84
  */
85
85
  response: string;
86
+ beforeResponse?: string;
87
+ parser?: string;
86
88
  }
87
89
  interface LmTags {
88
90
  think?: {
package/dist/main.js CHANGED
@@ -250,6 +250,12 @@ const templates = {
250
250
  "system": {
251
251
  "schema": "<|system|>{system}"
252
252
  },
253
+ "tags": {
254
+ "think": {
255
+ "end": "</think>",
256
+ "start": "<think>"
257
+ }
258
+ },
253
259
  "user": "<|user|>\n{prompt}"
254
260
  },
255
261
  "glm-tools": {
@@ -265,10 +271,22 @@ const templates = {
265
271
  "message": "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>\n{tools}\n</tools>\n\nFor each function call, output the function name and arguments within the following XML format:\n<tool_call>{function-name}\n<arg_key>{arg-key-1}</arg_key>\n<arg_value>{arg-value-1}</arg_value>\n<arg_key>{arg-key-2}</arg_key>\n<arg_value>{arg-value-2}</arg_value>\n...\n</tool_call>",
266
272
  "schema": "<|system|>{system}"
267
273
  },
274
+ "tags": {
275
+ "think": {
276
+ "end": "</think>",
277
+ "start": "<think>"
278
+ },
279
+ "toolCall": {
280
+ "end": "</tool_call>",
281
+ "start": "<tool_call>"
282
+ }
283
+ },
268
284
  "tools": {
285
+ "beforeResponse": "<|observation|>\n",
269
286
  "call": "<tool_call>\n{tools}\n</tool_call>",
270
287
  "def": "{system}",
271
- "response": "<tool_response>\n{tools_response}\n</tool_response>"
288
+ "parser": "glm",
289
+ "response": "<tool_response>{tools_response}</tool_response>"
272
290
  },
273
291
  "user": "<|user|>\n{prompt}"
274
292
  },
@@ -778,6 +796,58 @@ const templates = {
778
796
  }
779
797
  };
780
798
 
799
+ function extractGlmToolSpec(text, startTag, endTag) {
800
+ try {
801
+ const _rtcs = text.trim().split("</tool_call>").map(t => t.replace("<tool_call>", ""));
802
+ const rtcs = new Array();
803
+ _rtcs.forEach(r => {
804
+ if (r.length > 0) {
805
+ rtcs.push(r.trim().replace("<tool_call>", ""));
806
+ }
807
+ });
808
+ const tcs = new Array();
809
+ for (const rtc of rtcs) {
810
+ if (!rtc.includes("<arg_key>")) {
811
+ const tc = {
812
+ id: "",
813
+ name: rtc
814
+ };
815
+ tcs.push(tc);
816
+ }
817
+ else {
818
+ const idx = rtc.indexOf("<arg_key>");
819
+ const name = rtc.slice(0, idx);
820
+ const rawtc = rtc.slice(idx);
821
+ //console.log("RAWTC", rawtc);
822
+ const args = {};
823
+ const parts = rawtc.split(/<\/arg_key>|<\/arg_value>/).filter(part => part.trim());
824
+ let currentKey = '';
825
+ for (const part of parts) {
826
+ if (part.includes('<arg_key>')) {
827
+ currentKey = part.replace(/<arg_key>/g, '').trim();
828
+ }
829
+ else {
830
+ const value = part.replace(/<arg_value>/g, '').trim();
831
+ args[currentKey] = value;
832
+ }
833
+ }
834
+ tcs.push({
835
+ id: `call_${Date.now()}`,
836
+ name,
837
+ arguments: Object.keys(args).length > 0 ? args : undefined
838
+ });
839
+ }
840
+ }
841
+ /*console.log("----------- FINAL TCS");
842
+ console.log(tcs);
843
+ console.log("----------- END");*/
844
+ return tcs;
845
+ }
846
+ catch (error) {
847
+ throw new Error(`tool call parsing error: ${error}`);
848
+ }
849
+ }
850
+
781
851
  function extractBetweenTags(text, startTag, endTag) {
782
852
  try {
783
853
  // Find start position
@@ -805,14 +875,11 @@ function extractBetweenTags(text, startTag, endTag) {
805
875
  throw new Error(`Error parsing content between tags ${startTag} ${endTag}: ${error}`);
806
876
  }
807
877
  }
808
- function extractToolSpec(text, startTag, endTag) {
878
+
879
+ function extractJsonToolSpec(text, startTag, endTag) {
809
880
  try {
810
881
  // Extract content
811
882
  let content = extractBetweenTags(text, startTag, endTag);
812
- // try to patch malformed tool c
813
- if (content.startsWith("[") && !content.endsWith("]")) {
814
- content = content + "]";
815
- }
816
883
  // Parse JSON content
817
884
  let parsed = JSON.parse(content);
818
885
  if (!Array.isArray(parsed)) {
@@ -825,6 +892,18 @@ function extractToolSpec(text, startTag, endTag) {
825
892
  }
826
893
  }
827
894
 
895
+ function routeToolResponseParsing(text, startTag, endTag, parser) {
896
+ if (!parser) {
897
+ return extractJsonToolSpec(text, startTag, endTag);
898
+ }
899
+ else if (parser == "glm") {
900
+ return extractGlmToolSpec(text);
901
+ }
902
+ else {
903
+ throw new Error(`unknown tool response parser ${parser}`);
904
+ }
905
+ }
906
+
828
907
  /**
829
908
  * Represents a modified language model template.
830
909
  *
@@ -853,6 +932,8 @@ class PromptTemplate {
853
932
  _replaceSystem = "";
854
933
  _toolCallStart = "";
855
934
  _toolCallEnd = null;
935
+ _toolCallParser = null;
936
+ _beforeToolResponse = null;
856
937
  /**
857
938
  * Constructs a new `PromptTemplate` instance.
858
939
  *
@@ -892,10 +973,10 @@ class PromptTemplate {
892
973
  if (toolCallStartEnd.length == 0) {
893
974
  throw new Error(`Tool definition malformed in template ${this.name}: no start tool call definition`);
894
975
  }
895
- this._toolCallStart = toolCallStartEnd[0];
896
- if (toolCallStartEnd.length > 1) {
897
- this._toolCallEnd = toolCallStartEnd[1];
898
- }
976
+ this._toolCallStart = this.tags?.toolCall?.start || "";
977
+ this._toolCallEnd = this.tags?.toolCall?.end || null;
978
+ this._toolCallParser = tpl?.tools?.parser ?? null;
979
+ this._beforeToolResponse = tpl?.tools?.beforeResponse ?? null;
899
980
  }
900
981
  }
901
982
  get hasTools() {
@@ -1011,12 +1092,23 @@ class PromptTemplate {
1011
1092
  let isToolCall = false;
1012
1093
  let toolsCall = [];
1013
1094
  const ans = answer.trim();
1014
- //console.log("\nTC ANSWER", ans);
1015
- //console.log("TC SW", this._toolCallStart, ans.startsWith(this._toolCallStart));
1095
+ console.log("\nTC ANSWER", ans);
1096
+ console.log("TC SW", this._toolCallStart + "||", ans.includes(this._toolCallStart));
1016
1097
  if (ans.includes(this._toolCallStart)) {
1017
1098
  isToolCall = true;
1099
+ //console.log("TCS", tc);
1100
+ let rawTc;
1101
+ if (ans.startsWith(this._toolCallStart)) {
1102
+ rawTc = ans;
1103
+ }
1104
+ else {
1105
+ const index = ans.indexOf(this._toolCallStart);
1106
+ //const answerText = ans.slice(0, index);
1107
+ //console.log("Answer text:", answerText);
1108
+ rawTc = ans.slice(index);
1109
+ }
1018
1110
  try {
1019
- const tc = this._parseToolCallString(answer);
1111
+ const tc = routeToolResponseParsing(rawTc, this._toolCallStart, this._toolCallEnd ?? undefined, this._toolCallParser ?? undefined);
1020
1112
  //const tc = JSON.parse(tcs);
1021
1113
  if (!Array.isArray(tc)) {
1022
1114
  throw new Error(`error parsing tool call response from model: the response object is not an Array:\n${tc}`);
@@ -1037,16 +1129,16 @@ class PromptTemplate {
1037
1129
  //console.log("FTC", isToolCall, toolsCall);
1038
1130
  return { isToolCall: isToolCall, toolsCall: toolsCall };
1039
1131
  }
1040
- encodeToolResponse(response) {
1041
- if (!this.toolsDef) {
1042
- throw new Error("can not encode tool response: the template has no tools definition");
1043
- }
1044
- if (!this.toolsDef.response.includes("{tools_response}")) {
1045
- throw new Error(`Template ${this.name} has invalid tool response format`);
1046
- }
1047
- const resp = typeof response == "string" ? response : JSON.stringify(response);
1048
- return this.toolsDef.response.replace("{tools_response}", resp);
1049
- }
1132
+ /*encodeToolResponse(response: any): string {
1133
+ if (!this.toolsDef) {
1134
+ throw new Error("can not encode tool response: the template has no tools definition")
1135
+ }
1136
+ if (!this.toolsDef.response.includes("{tools_response}")) {
1137
+ throw new Error(`Template ${this.name} has invalid tool response format`);
1138
+ }
1139
+ const resp = typeof response == "string" ? response : JSON.stringify(response);
1140
+ return this.toolsDef.response.replace("{tools_response}", resp)
1141
+ }*/
1050
1142
  /**
1051
1143
  * Clones the current `PromptTemplate` instance to a new instance of `PromptTemplate`.
1052
1144
  *
@@ -1218,7 +1310,7 @@ class PromptTemplate {
1218
1310
  * @returns The rendered template with the provided message.
1219
1311
  *
1220
1312
  * @example
1221
- * const prompted = tpl.prompt("list the planets in the solar system");
1313
+ * const prompted = tpl.prompt("list the planets of the solar system");
1222
1314
  * console.log(prompted);
1223
1315
  */
1224
1316
  prompt(msg, skip_empty_system = true) {
@@ -1276,7 +1368,11 @@ class PromptTemplate {
1276
1368
  for (const tt of toolTurns) {
1277
1369
  buf.push(this.toolsDef.response.replace("{tools_response}", JSON.stringify(tt.response)));
1278
1370
  }
1279
- return buf.join("");
1371
+ let tr = buf.join("");
1372
+ if (this._beforeToolResponse) {
1373
+ tr = this._beforeToolResponse + tr;
1374
+ }
1375
+ return tr;
1280
1376
  }
1281
1377
  _buildToolsBlock(raw = false) {
1282
1378
  if (!this.toolsDef) {
@@ -1343,9 +1439,6 @@ class PromptTemplate {
1343
1439
  throw new Error(`Error loading template ${name}: ${err}`);
1344
1440
  }
1345
1441
  }
1346
- _parseToolCallString(raw) {
1347
- return extractToolSpec(raw, this._toolCallStart, this._toolCallEnd ?? undefined);
1348
- }
1349
1442
  }
1350
1443
 
1351
1444
  export { PromptTemplate, templates };
package/dist/main.min.js CHANGED
@@ -1 +1 @@
1
- var $tpl=function(s){"use strict";const t={alpaca:{assistant:"### Response:",id:"alpaca",linebreaks:{system:2,user:2},name:"Alpaca",system:{message:"Below is an instruction that describes a task. Write a response that appropriately completes the request.",schema:"{system}"},user:"### Instruction:\n{prompt}"},chatml:{afterShot:"<|im_end|>\n",assistant:"<|im_start|>assistant",id:"chatml",linebreaks:{assistant:1,system:1,user:1},name:"ChatMl",stop:["<|im_end|>"],system:{schema:"<|im_start|>system\n{system}<|im_end|>"},tags:{think:{end:"</think>",start:"<think>"}},user:"<|im_start|>user\n{prompt}<|im_end|>"},"chatml-tools":{afterShot:"<|im_end|>\n",assistant:"<|im_start|>assistant",id:"chatml-tools",linebreaks:{assistant:1,system:1,tools:1,user:1},name:"ChatMl tools",stop:["<|im_end|>"],system:{message:'You are a helpful assistant with tool calling capabilities. You may call one or more functions to assist with the user query.\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>\n{tools}\n</tools>\n\nFor function calls, return a json array of objects with function names and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n[{"name": <function-name>, "arguments": <args-json-object>}, {"name": <function-name>, "arguments": <args-json-object>}]\n</tool_call>',schema:"<|im_start|>system\n{system}<|im_end|>"},tags:{think:{end:"</think>",start:"<think>"},toolCall:{end:"</tool_call>",start:"<tool_call>"}},tools:{call:"<tool_call>\n{tools}\n</tool_call>",def:"{system}",response:"<|im_start|>user\n<tool_response>\n{tools_response}\n</tool_response><|im_end|>"},user:"<|im_start|>user\n{prompt}<|im_end|>"},codestral:{afterShot:"\n",assistant:" [/INST]",id:"codestral",linebreaks:{system:2},name:"Codestral",stop:["</s>"],system:{schema:"<<SYS>>\n{system}\n<</SYS>>"},user:"[INST] {prompt}"},"command-r":{afterShot:"<|END_OF_TURN_TOKEN|>",assistant:"<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>",id:"command-r",name:"Command-R",stop:["<|END_OF_TURN_TOKEN|>"],system:{schema:"<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{system}<|END_OF_TURN_TOKEN|>"},user:"<|START_OF_TURN_TOKEN|><|USER_TOKEN|>{prompt}<|END_OF_TURN_TOKEN|>"},deephermes:{afterShot:"<|eot_id|>\n\n",assistant:"<|start_header_id|>assistant<|end_header_id|>",id:"deephermes",name:"Deephermes",stop:["<|eot_id|>","<|end_of_text|>"],system:{message:'You 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> {tools} </tools>. For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:\n<tool_call>\n[{"arguments": <args-dict>, "name": <function-name>}]\n</tool_call>',schema:"<|start_header_id|>system<|end_header_id|>\n\n{system}<|eot_id|>"},tools:{call:"<tool_call>\n{tools}\n</tool_call>",def:"{system}",response:"<|start_header_id|>user<|end_header_id|>\n<tool_response>\n{tools_response}\n</tool_response><|eot_id|>"},user:"<|start_header_id|>user<|end_header_id|>\n{prompt}<|eot_id|>"},deepseek:{afterShot:"\n",assistant:"### Response:",id:"deepseek",linebreaks:{system:1,user:1},name:"Deepseek",stop:["<|EOT|>","### Instruction:"],system:{message:"You are an AI programming assistant, utilizing the DeepSeek Coder model, developed by DeepSeek Company, and you only answer questions related to computer science. For politically sensitive questions, security and privacy issues, and other non-computer science questions, you will refuse to answer.",schema:"{system}"},user:"### Instruction:\n{prompt}"},deepseek2:{assistant:"Assistant:",id:"deepseek2",linebreaks:{system:2,user:2},name:"Deepseek 2",stop:["<|end▁of▁sentence|>","<|tool▁calls▁end|>"],system:{schema:"<|begin▁of▁sentence|>{system}"},user:"User: {prompt}"},deepseek3:{afterShot:"<|end▁of▁sentence|>",assistant:"<|Assistant|>",id:"deepseek3",linebreaks:{system:2,user:2},name:"Deepseek 3",stop:["<|end▁of▁sentence|>","<|tool▁calls▁end|>"],system:{schema:"<|begin▁of▁sentence|>{system}"},user:"<|User|>{prompt}"},"deepseek3-tools":{afterShot:"<|end▁of▁sentence|>",assistant:"<|Assistant|>",id:"deepseek3-tools",linebreaks:{system:2,user:2},name:"Deepseek 3 tools",stop:["<|end▁of▁sentence|>","<|tool▁calls▁end|>"],system:{message:"## Tools\nYou have access to the following tools:\n\n{tools}\n\nIMPORTANT: ALWAYS adhere to this exact format for tool use:\n<|tool▁calls▁begin|><|tool▁call▁begin|>tool_call_name<|tool▁sep|>tool_call_arguments<|tool▁call▁end|>{{additional_tool_calls}}<|tool▁calls▁end|>\n\nWhere:\n- `tool_call_name` must be an exact match to one of the available tools\n- `tool_call_arguments` must be valid JSON that strictly follows the tool's Parameters Schema\n- For multiple tool calls, chain them directly without separators or spaces",schema:"<|begin▁of▁sentence|>{system}"},tags:{think:{end:"</think>",start:"<think>"},toolCall:{end:"<|tool▁call▁end|>",start:"<|tool▁call▁begin|>"}},tools:{call:"<|tool▁calls▁begin|>{tools}<|tool▁calls▁end|>",def:"{system}",response:"<|tool▁output▁begin|>{tools_response}<|tool▁output▁end|>"},user:"<|User|>{prompt}"},exaone:{afterShot:"[|endofturn|]",assistant:"[|assistant|]",id:"exaone",linebreaks:{system:1,user:1},name:"Exaone",stop:["[|endofturn|]"],system:{message:"You are EXAONE model from LG AI Research, a helpful assistant.",schema:"[|system|]{system}[|endofturn|]"},user:"[|user|]{prompt}[|endofturn|]"},gemma:{afterShot:"<end_of_turn>",assistant:"<start_of_turn>model",id:"gemma",name:"Gemma",stop:["<end_of_turn>"],user:"<start_of_turn>user\n{prompt}\n <end_of_turn>\n "},glm:{afterShot:"\n",assistant:"<|assistant|>",id:"glm",name:"Glm",prefix:"[gMASK]<sop>",stop:["<sop>"],system:{schema:"<|system|>{system}"},user:"<|user|>\n{prompt}"},"glm-tools":{afterShot:"\n",assistant:"<|assistant|>",id:"glm-tools",name:"Glm tools",prefix:"[gMASK]<sop>",stop:["<sop>"],system:{message:"# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>\n{tools}\n</tools>\n\nFor each function call, output the function name and arguments within the following XML format:\n<tool_call>{function-name}\n<arg_key>{arg-key-1}</arg_key>\n<arg_value>{arg-value-1}</arg_value>\n<arg_key>{arg-key-2}</arg_key>\n<arg_value>{arg-value-2}</arg_value>\n...\n</tool_call>",schema:"<|system|>{system}"},tools:{call:"<tool_call>\n{tools}\n</tool_call>",def:"{system}",response:"<tool_response>\n{tools_response}\n</tool_response>"},user:"<|user|>\n{prompt}"},gptoss:{assistant:"",id:"gptoss",linebreaks:{assistant:1,system:1,user:1},name:"Gpt Oss",system:{schema:"<|start|>system<|message|>\n{system}\n<|end|>"},tags:{think:{end:"<|end|><|start|>assistant<|channel|>final<|message|>",start:"<|channel|>analysis<|message|>"}},user:"<|start|>user<|message|>\n{prompt}\n<|end|>"},granite:{afterShot:"<|end_of_text|>\n",assistant:"<|start_of_role|>assistant<|end_of_role|>",id:"granite",linebreaks:{system:1,user:1},name:"Granite",stop:["<|end_of_text|>","<|start_of_role|>"],system:{message:"You are Granite, developed by IBM. You are a helpful AI assistant.",schema:"<|start_of_role|>system<|end_of_role|>{system}<|end_of_text|>"},user:"<|start_of_role|>user<|end_of_role|>{prompt}<|end_of_text|>"},"granite-think":{afterShot:"<|end_of_text|>\n",assistant:"<|start_of_role|>assistant<|end_of_role|>",id:"granite-think",linebreaks:{system:1,user:1},name:"Granite think",stop:["<|end_of_text|>","<|start_of_role|>"],system:{message:"You are Granite, developed by IBM. You are a helpful AI assistant. Respond to every user query in a comprehensive and detailed way. You can write down your thoughts and reasoning process before responding. In the thought process, engage in a comprehensive cycle of analysis, summarization, exploration, reassessment, reflection, backtracing, and iteration to develop well-considered thinking process. In the response section, based on various attempts, explorations, and reflections from the thoughts section, systematically present the final solution that you deem correct. The response should summarize the thought process. Write your thoughts after 'Here is my thought process:' and write your response after 'Here is my response:' for each user query.",schema:"<|start_of_role|>system<|end_of_role|>{system}<|end_of_text|>"},user:"<|start_of_role|>user<|end_of_role|>{prompt}<|end_of_text|>"},"granite-tools":{afterShot:"<|end_of_text|>\n",assistant:"<|start_of_role|>assistant<|end_of_role|>",id:"granite-tools",linebreaks:{system:1,tools:1,user:1},name:"Granite tools",stop:["<|end_of_text|>","<|start_of_role|>"],system:{message:"You are Granite, developed by IBM. You are a helpful AI assistant with access to the following tools. When a tool is required to answer the user's query, respond with <|tool_call|> followed by a JSON list of tools used. If a tool does not exist in the provided list of tools, notify the user that you do not have the ability to fulfill the request.",schema:"<|start_of_role|>system<|end_of_role|>{system}<|end_of_text|>"},tags:{toolCall:{end:"<|start_of_role|>",start:"<|tool_call|>"}},tools:{call:"<|tool_call|>{tools}",def:"<|start_of_role|>tools<|end_of_role|>{tools}<|end_of_text|>",response:"<|start_of_role|>tool_response<|end_of_role|>{tools_response}<|end_of_text|>\n"},user:"<|start_of_role|>user<|end_of_role|>{prompt}<|end_of_text|>"},lfm:{afterShot:"<|im_end|>\n",assistant:"<|im_start|>assistant",id:"lfm",linebreaks:{assistant:1,system:1,tools:1,user:1},name:"Lfm 2",stop:["<|im_end|>"],system:{schema:"<|im_start|>system\n{system}<|im_end|>"},tags:{think:{end:"</think>",start:"<think>"}},user:"<|im_start|>user\n{prompt}<|im_end|>"},"lfm-tools":{afterShot:"<|im_end|>\n",assistant:"<|im_start|>assistant",id:"lfm-tools",linebreaks:{assistant:1,system:1,tools:1,user:1},name:"Lfm 2 tools",stop:["<|im_end|>"],system:{message:"List of tools: <|tool_list_start|>{tools}<|tool_list_end|>",schema:"<|im_start|>system\n{system}<|im_end|>"},tags:{think:{end:"</think>",start:"<think>"},toolCall:{end:"<|tool_call_end|>",start:"<|tool_call_start|>"}},tools:{call:"<|tool_call_start|>\n{tools}\n<|tool_call_end|>",def:"{system}",response:"<|im_start|>tool\n<|tool_response_start|>\n{tools_response}\n<|tool_response_end|><|im_end|>"},user:"<|im_start|>user\n{prompt}<|im_end|>"},ling:{assistant:"<role>ASSISTANT</role>",id:"ling",linebreaks:{assistant:1,user:1},name:"Ling",stop:["<|role_end|>"],user:"<role>HUMAN</role>{prompt}<|role_end|>"},"ling-tools":{assistant:"<role>ASSISTANT</role>",id:"ling-tools",name:"Ling tools",stop:["<|role_end|>"],system:{message:'You are a helpful assistant with tool calling capabilities. You may call one or more functions to assist with the user query.\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>\n{tools}\n</tools>\n\nFor function calls, return a json array of objects with function names and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n[{"name": <function-name>, "arguments": <args-json-object>}, {"name": <function-name>, "arguments": <args-json-object>}]\n</tool_call>',schema:"<role>SYSTEM</role>{system}<|role_end|>"},tools:{call:"<tool_call>\n{tools}\n</tool_call>",def:"{system}",response:"<role>OBSERVATION</role>\n<tool_response>\n{tools_response}\n</tool_response><|role_end|>"},user:"<role>HUMAN</role>{prompt}<|role_end|>"},llama:{assistant:" [/INST] ",id:"llama",linebreaks:{system:2,user:0},name:"Llama",prefix:"<s>",stop:["</s>"],system:{message:"You are a helpful, respectful and honest assistant. Always answer as helpfully as possible\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.",schema:"[INST] <<SYS>>\n{system}\n<</SYS>>"},user:"{prompt}"},llama3:{afterShot:"<|eot_id|>\n\n",assistant:"<|start_header_id|>assistant<|end_header_id|>",id:"llama3",name:"Llama 3",stop:["<|eot_id|>","<|end_of_text|>"],system:{schema:"<|start_header_id|>system<|end_header_id|>\n\n{system}<|eot_id|>"},user:"<|start_header_id|>user<|end_header_id|>\n\n{prompt}<|eot_id|>"},"llama3-think":{afterShot:"<|eot_id|>\n\n",assistant:"<|start_header_id|>assistant<|end_header_id|>",id:"llama3-think",name:"Llama 3 think",stop:["<|eot_id|>","<|end_of_text|>"],system:{message:"You are a deep thinking AI, you may use extremely long chains of thought to deeply consider the problem and deliberate with yourself via systematic reasoning processes to help come to a correct solution prior to answering. You should enclose your thoughts and internal monologue inside <think> </think> tags, and then provide your solution or response to the problem.",schema:"<|start_header_id|>system<|end_header_id|>\n\n{system}<|eot_id|>"},tags:{think:{end:"</think>",start:"<think>"}},user:"<|start_header_id|>user<|end_header_id|>\n\n{prompt}<|eot_id|>"},llava:{assistant:"ASSISTANT:",id:"llava",linebreaks:{user:1},name:"Llava",user:"USER: {prompt}"},minichat:{afterShot:"\n",assistant:"[|Assistant|]",id:"minichat",name:"Minichat",prefix:"<s> ",stop:["</s>","[|User|]"],user:"[|User|] {prompt} </s>"},minimax:{afterShot:"\n",assistant:"]~b]ai",id:"minimax",name:"Minimax",prefix:"]~!b[",stop:["[e~[","]~b]user"],system:{message:'You are a helpful assistant.\n\n# Tools\nYou may call one or more tools to assist with the user query.\nHere are the tools available in JSONSchema format:\n\n<tools>\n{tools}\n</tools>\n\nWhen making tool calls, use XML format to invoke tools and pass parameters:\n\n<minimax:tool_call>\n<invoke name="tool-name-1">\n<parameter name="param-key-1">param-value-1</parameter>\n<parameter name="param-key-2">param-value-2</parameter>\n...\n</invoke>',schema:"]~b]system"},tools:{call:"<minimax:tool_call>\n{tools}\n</minimax:tool_call>",def:"\n{system}",response:"<minimax:tool_call>\n{tools_response}\n</minimax:tool_call>"},user:"]~b]user\n{prompt}[e~["},mistral:{afterShot:"\n",assistant:" [/INST]",id:"mistral",name:"Mistral",stop:["</s>"],user:"[INST] {prompt}"},"mistral-system":{afterShot:"\n",assistant:" [/INST]",id:"mistral-system",name:"Mistral system",stop:["</s>"],system:{schema:"[SYSTEM_PROMPT]{system}[/SYSTEM_PROMPT] "},user:"[INST] {prompt}"},"mistral-system-tools":{afterShot:"\n",assistant:"",id:"mistral-system-tools",name:"Mistral system tools",stop:["</s>"],system:{schema:"[SYSTEM_PROMPT]{system}[/SYSTEM_PROMPT] "},tags:{toolCall:{end:"[/TOOL_RESULTS]",start:"[TOOL_CALLS]"}},tools:{call:"[TOOL_CALLS]{tools}",def:"[AVAILABLE_TOOLS]{tools}[/AVAILABLE_TOOLS]",response:"[TOOL_RESULTS]{tools_response}[/TOOL_RESULTS]"},user:"[INST] {prompt} [/INST]"},nemotron:{afterShot:"\n\n",assistant:"<extra_id_1>Assistant\n",id:"nemotron",linebreaks:{system:2,user:1},name:"Nemotron",system:{schema:"<extra_id_0>System\n{system}"},user:"<extra_id_1>User\n{prompt}"},none:{assistant:"",id:"none",name:"No template",user:"{prompt}"},openchat:{assistant:"GPT4 Assistant:",id:"openchat",name:"OpenChat",stop:["<|end_of_turn|>"],user:"GPT4 User: {prompt}<|end_of_turn|>"},"openchat-correct":{assistant:"GPT4 Correct Assistant:",id:"openchat-correct",name:"OpenChat correct",stop:["<|end_of_turn|>"],user:"GPT4 Correct User: {prompt}<|end_of_turn|>"},orca:{assistant:"### Response:",id:"orca",linebreaks:{system:2,user:2},name:"Orca",system:{message:"You are an AI assistant that follows instruction extremely well. Help as much as you can.",schema:"### System:\n{system}"},user:"### User:\n{prompt}"},phi3:{afterShot:"<|end|>\n",assistant:"<|assistant|>",id:"phi3",name:"Phi 3",stop:["<|end|>","<|user|>"],system:{schema:"<|system|> {system}<|end|>"},user:"<|user|> {prompt}<|end|>"},phi4:{afterShot:"<|im_end|>\n",assistant:"<|im_start|>assistant<|im_sep|>",id:"phi4",name:"Phi 4",stop:["<|im_end|>","<|im_sep|>"],system:{schema:"<|im_start|>system<|im_sep|>{system}<|im_end|>"},user:"<|im_start|>user<|im_sep|>{prompt}<|im_end|>"},"phi4-tools":{afterShot:"<|im_end|>\n",assistant:"<|im_start|>assistant<|im_sep|>",id:"phi4-tools",name:"Phi 4 tools",stop:["<|im_end|>","<|im_sep|>"],system:{message:"You are a helpful assistant with some tools.\n<|tool|>\n{tools}\n<|/tool|>",schema:"<|im_start|>system<|im_sep|>{system}<|im_end|>"},tags:{toolCall:{end:"<|/tool_call|>",start:"<|tool_call|>"}},tools:{call:"<|tool_call|>\n{tools}\n<|/tool_call|>",def:"{system}",response:"<|im_start|>user\n<|tool_response|>\n{tools_response}\n<|/tool_response|><|im_end|>"},user:"<|im_start|>user<|im_sep|>{prompt}<|im_end|>"},reka:{afterShot:" <sep> ",assistant:"assistant:",id:"reka",name:"Reka",stop:["<sep>","<|endoftext|>"],user:"human: {prompt} <sep> "},vicuna:{assistant:"### ASSISTANT:",id:"vicuna",linebreaks:{user:2},name:"Vicuna",user:"USER: {prompt}"},vicuna_system:{assistant:"### ASSISTANT:",id:"vicuna_system",linebreaks:{system:2,user:2},name:"Vicuna system",system:{schema:"SYSTEM: {system}"},user:"USER: {prompt}"},wizard_vicuna:{assistant:"### ASSISTANT:",id:"wizard_vicuna",linebreaks:{user:2},name:"Wizard Vicuna",stop:["<|endoftext|>"],user:"### Human:\n{prompt}"},wizardlm:{assistant:"ASSISTANT:",id:"wizardlm",linebreaks:{user:1},name:"WizardLM",system:{message:"You are a helpful AI assistant.",schema:"{system}"},user:"USER: {prompt}"},zephyr:{afterShot:"\n",assistant:"<|assistant|>",id:"zephyr",linebreaks:{assistant:1,system:1,user:1},name:"Zephyr",stop:["<|endoftext|>"],system:{schema:"<|system|>\n{system}<|endoftext|>"},user:"<|user|>\n{prompt}<|endoftext|>"}};function e(s,t,e){try{const o=s.indexOf(t);if(-1===o)return s;let n,a=o+t.length;if(e){if(n=s.indexOf(e,a),-1===n)return s}else n=s.indexOf("\n",a),-1===n&&(n=s.length);return s.substring(a,n).trim()}catch(s){throw new Error(`Error parsing content between tags ${t} ${e}: ${s}`)}}class o{id;name;user;assistant;history=[];toolsDef=null;tools=[];tags={};system;shots;stop;linebreaks;afterShot;prefix;_extraSystem="";_extraAssistant="";_replacePrompt="";_replaceSystem="";_toolCallStart="";_toolCallEnd=null;constructor(s){let t;if(t="string"==typeof s?this._load(s):s,this.id=t.id,this.name=t.name,this.user=t.user,this.assistant=t.assistant,this.system=t?.system,this.shots=t?.shots,this.stop=t?.stop,this.linebreaks=t?.linebreaks,this.afterShot=t?.afterShot,this.prefix=t?.prefix,t?.tags&&(this.tags=t?.tags),t?.tools){this.toolsDef=t.tools;const s=this.toolsDef?.call.split("{tools}");if(!s)throw new Error(`Tool definition malformed in template ${this.name}`);if(0==s.length)throw new Error(`Tool definition malformed in template ${this.name}: no start tool call definition`);this._toolCallStart=s[0],s.length>1&&(this._toolCallEnd=s[1])}}get hasTools(){return this.tools.length>0}renderShot(s){const t=[];if(s?.user&&t.push(this._buildUserBlock(s.user)),s?.assistant){let e=s.assistant;this.afterShot&&(e+=this.afterShot),t.push(this._buildAssistantBlock(e))}if(s?.tools){const e=this._buildToolsResponse(s.tools);t.push(e)}return t.join("")}render(s=!0){const t=new Array;this.prefix&&t.push(this.prefix);const e="{system}"==this?.toolsDef?.def,o=this._buildSystemBlock(s,e);if(o.length>0&&(t.push(o),this?.linebreaks?.system&&t.push("\n".repeat(this.linebreaks.system))),this.toolsDef&&!e){const s=this._buildToolsBlock();s.length>0&&(t.push(s),this?.linebreaks?.tools&&t.push("\n".repeat(this.linebreaks.tools)))}if(this?.shots)for(const s of this.shots)t.push(this.renderShot(s));let n=!1;if(this.history.length>0){for(const s of this.history)t.push(this.renderShot(s));this.history[this.history.length-1]?.tools&&(n=!0)}return n?this?.linebreaks?.tools&&t.push("\n".repeat(this.linebreaks.tools)):t.push(this._buildUserBlock()),t.push(this._buildAssistantBlock()),t.join("")}addTool(s){if(!this?.toolsDef)throw new Error("This template does not support tools");return this.tools.push(s),this}processAnswer(s){if(!this.hasTools)return{isToolCall:!1,toolsCall:[]};let t=!1,e=[];if(s.trim().includes(this._toolCallStart)){t=!0;try{const t=this._parseToolCallString(s);if(!Array.isArray(t))throw new Error(`error parsing tool call response from model: the response object is not an Array:\n${t}`);e=t}catch(t){const e=new Array;throw e.push("error parsing tool call from model:"),e.push("------------- tool call ---------------"),e.push(s),e.push("----------- parsing error --------------"),e.push(`${t}`),new Error(e.join("\n"))}}return{isToolCall:t,toolsCall:e}}encodeToolResponse(s){if(!this.toolsDef)throw new Error("can not encode tool response: the template has no tools definition");if(!this.toolsDef.response.includes("{tools_response}"))throw new Error(`Template ${this.name} has invalid tool response format`);const t="string"==typeof s?s:JSON.stringify(s);return this.toolsDef.response.replace("{tools_response}",t)}cloneTo(s,t=!0){const e=new o(s);return Object.assign(e,this),t?e.history=this.history.map(s=>({...s})):e.shots=[],e}toJson(){const s={id:this.id,name:this.name,user:this.user,assistant:this.assistant};return this?.prefix&&(s.prefix=this.prefix),this?.system&&(s.system=this.system),this?.shots&&(s.shots=this.shots),this?.afterShot&&(s.afterShot=this.afterShot),this?.stop&&(s.stop=this.stop),this?.linebreaks&&(s.linebreaks=this.linebreaks),s}replaceSystem(s){return this.system?(this._replaceSystem=s,this):this}afterSystem(s){return this.system?(this._extraSystem=s,this):this}afterAssistant(s){return this._extraAssistant=s,this}replacePrompt(s){return this._replacePrompt=s,this}addShot(s){if(s.tools&&!this.toolsDef)throw new Error("This template does not support tools");return this.shots||(this.shots=[]),this.shots.push(s),this}addShots(s){return s.forEach(s=>this.addShot(s)),this}prompt(s,t=!0){return this.render(t).replace("{prompt}",s)}pushToHistory(s,t=!0){if(t&&s?.assistant&&this.tags?.think){const t=s.assistant.split(this.tags.think.end);t.length>1&&(s.think=e(s.assistant,this.tags.think.start,this.tags.think.end),s.assistant=t[1].trim())}return this.history.push(s),this}_buildSystemBlock(s,t=!1){let e="";if(!this?.system)return"";let o=this._replaceSystem||this.system.message||"";return this._extraSystem&&(o+=this._extraSystem),o?e=this.system.schema.replace("{system}",o):s||(e=this.system.schema.replace("{system}","")),t&&this.tools.length>0&&(e=e.replace("{tools}",this._buildToolsBlock(!0))),e}_buildToolsResponse(s){if(!this.toolsDef)throw new Error("No tools def in template to build tool response");const t=new Array;for(const e of s)t.push(this.toolsDef.response.replace("{tools_response}",JSON.stringify(e.response)));return t.join("")}_buildToolsBlock(s=!1){if(!this.toolsDef)throw new Error("Can not build tools block: no tools definition found in template");let t="";if(0==this.tools.length)return"";const e=JSON.stringify(this.tools);return s?e:(t+=this.toolsDef.def.replace("{tools}",e),t)}_buildUserBlock(s){let t=[],e=this.user;return this._replacePrompt.length>0&&(e=e.replace("{prompt}",this._replacePrompt)),t.push(e),this?.linebreaks?.user&&t.push("\n".repeat(this.linebreaks.user)),s&&(t[0]=this.user.replace("{prompt}",s)),t.join("")}_buildAssistantBlock(s){let t="",e=this.assistant;return this?.linebreaks?.assistant&&(e+="\n".repeat(this.linebreaks.assistant)),this._extraAssistant.length>0&&(e+=this._extraAssistant),t+=e,s&&(t+=s),t}_load(s){try{if(s in t)return t[s];throw new Error(`Template ${s} not found`)}catch(t){throw new Error(`Error loading template ${s}: ${t}`)}}_parseToolCallString(s){return function(s,t,o){try{let n=e(s,t,o);n.startsWith("[")&&!n.endsWith("]")&&(n+="]");let a=JSON.parse(n);return Array.isArray(a)||(a=[a]),a}catch(s){throw new Error(`tool call parsing error: ${s}`)}}(s,this._toolCallStart,this._toolCallEnd??void 0)}}return s.PromptTemplate=o,s.templates=t,s}({});
1
+ var $tpl=function(s){"use strict";const t={alpaca:{assistant:"### Response:",id:"alpaca",linebreaks:{system:2,user:2},name:"Alpaca",system:{message:"Below is an instruction that describes a task. Write a response that appropriately completes the request.",schema:"{system}"},user:"### Instruction:\n{prompt}"},chatml:{afterShot:"<|im_end|>\n",assistant:"<|im_start|>assistant",id:"chatml",linebreaks:{assistant:1,system:1,user:1},name:"ChatMl",stop:["<|im_end|>"],system:{schema:"<|im_start|>system\n{system}<|im_end|>"},tags:{think:{end:"</think>",start:"<think>"}},user:"<|im_start|>user\n{prompt}<|im_end|>"},"chatml-tools":{afterShot:"<|im_end|>\n",assistant:"<|im_start|>assistant",id:"chatml-tools",linebreaks:{assistant:1,system:1,tools:1,user:1},name:"ChatMl tools",stop:["<|im_end|>"],system:{message:'You are a helpful assistant with tool calling capabilities. You may call one or more functions to assist with the user query.\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>\n{tools}\n</tools>\n\nFor function calls, return a json array of objects with function names and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n[{"name": <function-name>, "arguments": <args-json-object>}, {"name": <function-name>, "arguments": <args-json-object>}]\n</tool_call>',schema:"<|im_start|>system\n{system}<|im_end|>"},tags:{think:{end:"</think>",start:"<think>"},toolCall:{end:"</tool_call>",start:"<tool_call>"}},tools:{call:"<tool_call>\n{tools}\n</tool_call>",def:"{system}",response:"<|im_start|>user\n<tool_response>\n{tools_response}\n</tool_response><|im_end|>"},user:"<|im_start|>user\n{prompt}<|im_end|>"},codestral:{afterShot:"\n",assistant:" [/INST]",id:"codestral",linebreaks:{system:2},name:"Codestral",stop:["</s>"],system:{schema:"<<SYS>>\n{system}\n<</SYS>>"},user:"[INST] {prompt}"},"command-r":{afterShot:"<|END_OF_TURN_TOKEN|>",assistant:"<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>",id:"command-r",name:"Command-R",stop:["<|END_OF_TURN_TOKEN|>"],system:{schema:"<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{system}<|END_OF_TURN_TOKEN|>"},user:"<|START_OF_TURN_TOKEN|><|USER_TOKEN|>{prompt}<|END_OF_TURN_TOKEN|>"},deephermes:{afterShot:"<|eot_id|>\n\n",assistant:"<|start_header_id|>assistant<|end_header_id|>",id:"deephermes",name:"Deephermes",stop:["<|eot_id|>","<|end_of_text|>"],system:{message:'You 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> {tools} </tools>. For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:\n<tool_call>\n[{"arguments": <args-dict>, "name": <function-name>}]\n</tool_call>',schema:"<|start_header_id|>system<|end_header_id|>\n\n{system}<|eot_id|>"},tools:{call:"<tool_call>\n{tools}\n</tool_call>",def:"{system}",response:"<|start_header_id|>user<|end_header_id|>\n<tool_response>\n{tools_response}\n</tool_response><|eot_id|>"},user:"<|start_header_id|>user<|end_header_id|>\n{prompt}<|eot_id|>"},deepseek:{afterShot:"\n",assistant:"### Response:",id:"deepseek",linebreaks:{system:1,user:1},name:"Deepseek",stop:["<|EOT|>","### Instruction:"],system:{message:"You are an AI programming assistant, utilizing the DeepSeek Coder model, developed by DeepSeek Company, and you only answer questions related to computer science. For politically sensitive questions, security and privacy issues, and other non-computer science questions, you will refuse to answer.",schema:"{system}"},user:"### Instruction:\n{prompt}"},deepseek2:{assistant:"Assistant:",id:"deepseek2",linebreaks:{system:2,user:2},name:"Deepseek 2",stop:["<|end▁of▁sentence|>","<|tool▁calls▁end|>"],system:{schema:"<|begin▁of▁sentence|>{system}"},user:"User: {prompt}"},deepseek3:{afterShot:"<|end▁of▁sentence|>",assistant:"<|Assistant|>",id:"deepseek3",linebreaks:{system:2,user:2},name:"Deepseek 3",stop:["<|end▁of▁sentence|>","<|tool▁calls▁end|>"],system:{schema:"<|begin▁of▁sentence|>{system}"},user:"<|User|>{prompt}"},"deepseek3-tools":{afterShot:"<|end▁of▁sentence|>",assistant:"<|Assistant|>",id:"deepseek3-tools",linebreaks:{system:2,user:2},name:"Deepseek 3 tools",stop:["<|end▁of▁sentence|>","<|tool▁calls▁end|>"],system:{message:"## Tools\nYou have access to the following tools:\n\n{tools}\n\nIMPORTANT: ALWAYS adhere to this exact format for tool use:\n<|tool▁calls▁begin|><|tool▁call▁begin|>tool_call_name<|tool▁sep|>tool_call_arguments<|tool▁call▁end|>{{additional_tool_calls}}<|tool▁calls▁end|>\n\nWhere:\n- `tool_call_name` must be an exact match to one of the available tools\n- `tool_call_arguments` must be valid JSON that strictly follows the tool's Parameters Schema\n- For multiple tool calls, chain them directly without separators or spaces",schema:"<|begin▁of▁sentence|>{system}"},tags:{think:{end:"</think>",start:"<think>"},toolCall:{end:"<|tool▁call▁end|>",start:"<|tool▁call▁begin|>"}},tools:{call:"<|tool▁calls▁begin|>{tools}<|tool▁calls▁end|>",def:"{system}",response:"<|tool▁output▁begin|>{tools_response}<|tool▁output▁end|>"},user:"<|User|>{prompt}"},exaone:{afterShot:"[|endofturn|]",assistant:"[|assistant|]",id:"exaone",linebreaks:{system:1,user:1},name:"Exaone",stop:["[|endofturn|]"],system:{message:"You are EXAONE model from LG AI Research, a helpful assistant.",schema:"[|system|]{system}[|endofturn|]"},user:"[|user|]{prompt}[|endofturn|]"},gemma:{afterShot:"<end_of_turn>",assistant:"<start_of_turn>model",id:"gemma",name:"Gemma",stop:["<end_of_turn>"],user:"<start_of_turn>user\n{prompt}\n <end_of_turn>\n "},glm:{afterShot:"\n",assistant:"<|assistant|>",id:"glm",name:"Glm",prefix:"[gMASK]<sop>",stop:["<sop>"],system:{schema:"<|system|>{system}"},tags:{think:{end:"</think>",start:"<think>"}},user:"<|user|>\n{prompt}"},"glm-tools":{afterShot:"\n",assistant:"<|assistant|>",id:"glm-tools",name:"Glm tools",prefix:"[gMASK]<sop>",stop:["<sop>"],system:{message:"# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>\n{tools}\n</tools>\n\nFor each function call, output the function name and arguments within the following XML format:\n<tool_call>{function-name}\n<arg_key>{arg-key-1}</arg_key>\n<arg_value>{arg-value-1}</arg_value>\n<arg_key>{arg-key-2}</arg_key>\n<arg_value>{arg-value-2}</arg_value>\n...\n</tool_call>",schema:"<|system|>{system}"},tags:{think:{end:"</think>",start:"<think>"},toolCall:{end:"</tool_call>",start:"<tool_call>"}},tools:{beforeResponse:"<|observation|>\n",call:"<tool_call>\n{tools}\n</tool_call>",def:"{system}",parser:"glm",response:"<tool_response>{tools_response}</tool_response>"},user:"<|user|>\n{prompt}"},gptoss:{assistant:"",id:"gptoss",linebreaks:{assistant:1,system:1,user:1},name:"Gpt Oss",system:{schema:"<|start|>system<|message|>\n{system}\n<|end|>"},tags:{think:{end:"<|end|><|start|>assistant<|channel|>final<|message|>",start:"<|channel|>analysis<|message|>"}},user:"<|start|>user<|message|>\n{prompt}\n<|end|>"},granite:{afterShot:"<|end_of_text|>\n",assistant:"<|start_of_role|>assistant<|end_of_role|>",id:"granite",linebreaks:{system:1,user:1},name:"Granite",stop:["<|end_of_text|>","<|start_of_role|>"],system:{message:"You are Granite, developed by IBM. You are a helpful AI assistant.",schema:"<|start_of_role|>system<|end_of_role|>{system}<|end_of_text|>"},user:"<|start_of_role|>user<|end_of_role|>{prompt}<|end_of_text|>"},"granite-think":{afterShot:"<|end_of_text|>\n",assistant:"<|start_of_role|>assistant<|end_of_role|>",id:"granite-think",linebreaks:{system:1,user:1},name:"Granite think",stop:["<|end_of_text|>","<|start_of_role|>"],system:{message:"You are Granite, developed by IBM. You are a helpful AI assistant. Respond to every user query in a comprehensive and detailed way. You can write down your thoughts and reasoning process before responding. In the thought process, engage in a comprehensive cycle of analysis, summarization, exploration, reassessment, reflection, backtracing, and iteration to develop well-considered thinking process. In the response section, based on various attempts, explorations, and reflections from the thoughts section, systematically present the final solution that you deem correct. The response should summarize the thought process. Write your thoughts after 'Here is my thought process:' and write your response after 'Here is my response:' for each user query.",schema:"<|start_of_role|>system<|end_of_role|>{system}<|end_of_text|>"},user:"<|start_of_role|>user<|end_of_role|>{prompt}<|end_of_text|>"},"granite-tools":{afterShot:"<|end_of_text|>\n",assistant:"<|start_of_role|>assistant<|end_of_role|>",id:"granite-tools",linebreaks:{system:1,tools:1,user:1},name:"Granite tools",stop:["<|end_of_text|>","<|start_of_role|>"],system:{message:"You are Granite, developed by IBM. You are a helpful AI assistant with access to the following tools. When a tool is required to answer the user's query, respond with <|tool_call|> followed by a JSON list of tools used. If a tool does not exist in the provided list of tools, notify the user that you do not have the ability to fulfill the request.",schema:"<|start_of_role|>system<|end_of_role|>{system}<|end_of_text|>"},tags:{toolCall:{end:"<|start_of_role|>",start:"<|tool_call|>"}},tools:{call:"<|tool_call|>{tools}",def:"<|start_of_role|>tools<|end_of_role|>{tools}<|end_of_text|>",response:"<|start_of_role|>tool_response<|end_of_role|>{tools_response}<|end_of_text|>\n"},user:"<|start_of_role|>user<|end_of_role|>{prompt}<|end_of_text|>"},lfm:{afterShot:"<|im_end|>\n",assistant:"<|im_start|>assistant",id:"lfm",linebreaks:{assistant:1,system:1,tools:1,user:1},name:"Lfm 2",stop:["<|im_end|>"],system:{schema:"<|im_start|>system\n{system}<|im_end|>"},tags:{think:{end:"</think>",start:"<think>"}},user:"<|im_start|>user\n{prompt}<|im_end|>"},"lfm-tools":{afterShot:"<|im_end|>\n",assistant:"<|im_start|>assistant",id:"lfm-tools",linebreaks:{assistant:1,system:1,tools:1,user:1},name:"Lfm 2 tools",stop:["<|im_end|>"],system:{message:"List of tools: <|tool_list_start|>{tools}<|tool_list_end|>",schema:"<|im_start|>system\n{system}<|im_end|>"},tags:{think:{end:"</think>",start:"<think>"},toolCall:{end:"<|tool_call_end|>",start:"<|tool_call_start|>"}},tools:{call:"<|tool_call_start|>\n{tools}\n<|tool_call_end|>",def:"{system}",response:"<|im_start|>tool\n<|tool_response_start|>\n{tools_response}\n<|tool_response_end|><|im_end|>"},user:"<|im_start|>user\n{prompt}<|im_end|>"},ling:{assistant:"<role>ASSISTANT</role>",id:"ling",linebreaks:{assistant:1,user:1},name:"Ling",stop:["<|role_end|>"],user:"<role>HUMAN</role>{prompt}<|role_end|>"},"ling-tools":{assistant:"<role>ASSISTANT</role>",id:"ling-tools",name:"Ling tools",stop:["<|role_end|>"],system:{message:'You are a helpful assistant with tool calling capabilities. You may call one or more functions to assist with the user query.\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>\n{tools}\n</tools>\n\nFor function calls, return a json array of objects with function names and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n[{"name": <function-name>, "arguments": <args-json-object>}, {"name": <function-name>, "arguments": <args-json-object>}]\n</tool_call>',schema:"<role>SYSTEM</role>{system}<|role_end|>"},tools:{call:"<tool_call>\n{tools}\n</tool_call>",def:"{system}",response:"<role>OBSERVATION</role>\n<tool_response>\n{tools_response}\n</tool_response><|role_end|>"},user:"<role>HUMAN</role>{prompt}<|role_end|>"},llama:{assistant:" [/INST] ",id:"llama",linebreaks:{system:2,user:0},name:"Llama",prefix:"<s>",stop:["</s>"],system:{message:"You are a helpful, respectful and honest assistant. Always answer as helpfully as possible\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.",schema:"[INST] <<SYS>>\n{system}\n<</SYS>>"},user:"{prompt}"},llama3:{afterShot:"<|eot_id|>\n\n",assistant:"<|start_header_id|>assistant<|end_header_id|>",id:"llama3",name:"Llama 3",stop:["<|eot_id|>","<|end_of_text|>"],system:{schema:"<|start_header_id|>system<|end_header_id|>\n\n{system}<|eot_id|>"},user:"<|start_header_id|>user<|end_header_id|>\n\n{prompt}<|eot_id|>"},"llama3-think":{afterShot:"<|eot_id|>\n\n",assistant:"<|start_header_id|>assistant<|end_header_id|>",id:"llama3-think",name:"Llama 3 think",stop:["<|eot_id|>","<|end_of_text|>"],system:{message:"You are a deep thinking AI, you may use extremely long chains of thought to deeply consider the problem and deliberate with yourself via systematic reasoning processes to help come to a correct solution prior to answering. You should enclose your thoughts and internal monologue inside <think> </think> tags, and then provide your solution or response to the problem.",schema:"<|start_header_id|>system<|end_header_id|>\n\n{system}<|eot_id|>"},tags:{think:{end:"</think>",start:"<think>"}},user:"<|start_header_id|>user<|end_header_id|>\n\n{prompt}<|eot_id|>"},llava:{assistant:"ASSISTANT:",id:"llava",linebreaks:{user:1},name:"Llava",user:"USER: {prompt}"},minichat:{afterShot:"\n",assistant:"[|Assistant|]",id:"minichat",name:"Minichat",prefix:"<s> ",stop:["</s>","[|User|]"],user:"[|User|] {prompt} </s>"},minimax:{afterShot:"\n",assistant:"]~b]ai",id:"minimax",name:"Minimax",prefix:"]~!b[",stop:["[e~[","]~b]user"],system:{message:'You are a helpful assistant.\n\n# Tools\nYou may call one or more tools to assist with the user query.\nHere are the tools available in JSONSchema format:\n\n<tools>\n{tools}\n</tools>\n\nWhen making tool calls, use XML format to invoke tools and pass parameters:\n\n<minimax:tool_call>\n<invoke name="tool-name-1">\n<parameter name="param-key-1">param-value-1</parameter>\n<parameter name="param-key-2">param-value-2</parameter>\n...\n</invoke>',schema:"]~b]system"},tools:{call:"<minimax:tool_call>\n{tools}\n</minimax:tool_call>",def:"\n{system}",response:"<minimax:tool_call>\n{tools_response}\n</minimax:tool_call>"},user:"]~b]user\n{prompt}[e~["},mistral:{afterShot:"\n",assistant:" [/INST]",id:"mistral",name:"Mistral",stop:["</s>"],user:"[INST] {prompt}"},"mistral-system":{afterShot:"\n",assistant:" [/INST]",id:"mistral-system",name:"Mistral system",stop:["</s>"],system:{schema:"[SYSTEM_PROMPT]{system}[/SYSTEM_PROMPT] "},user:"[INST] {prompt}"},"mistral-system-tools":{afterShot:"\n",assistant:"",id:"mistral-system-tools",name:"Mistral system tools",stop:["</s>"],system:{schema:"[SYSTEM_PROMPT]{system}[/SYSTEM_PROMPT] "},tags:{toolCall:{end:"[/TOOL_RESULTS]",start:"[TOOL_CALLS]"}},tools:{call:"[TOOL_CALLS]{tools}",def:"[AVAILABLE_TOOLS]{tools}[/AVAILABLE_TOOLS]",response:"[TOOL_RESULTS]{tools_response}[/TOOL_RESULTS]"},user:"[INST] {prompt} [/INST]"},nemotron:{afterShot:"\n\n",assistant:"<extra_id_1>Assistant\n",id:"nemotron",linebreaks:{system:2,user:1},name:"Nemotron",system:{schema:"<extra_id_0>System\n{system}"},user:"<extra_id_1>User\n{prompt}"},none:{assistant:"",id:"none",name:"No template",user:"{prompt}"},openchat:{assistant:"GPT4 Assistant:",id:"openchat",name:"OpenChat",stop:["<|end_of_turn|>"],user:"GPT4 User: {prompt}<|end_of_turn|>"},"openchat-correct":{assistant:"GPT4 Correct Assistant:",id:"openchat-correct",name:"OpenChat correct",stop:["<|end_of_turn|>"],user:"GPT4 Correct User: {prompt}<|end_of_turn|>"},orca:{assistant:"### Response:",id:"orca",linebreaks:{system:2,user:2},name:"Orca",system:{message:"You are an AI assistant that follows instruction extremely well. Help as much as you can.",schema:"### System:\n{system}"},user:"### User:\n{prompt}"},phi3:{afterShot:"<|end|>\n",assistant:"<|assistant|>",id:"phi3",name:"Phi 3",stop:["<|end|>","<|user|>"],system:{schema:"<|system|> {system}<|end|>"},user:"<|user|> {prompt}<|end|>"},phi4:{afterShot:"<|im_end|>\n",assistant:"<|im_start|>assistant<|im_sep|>",id:"phi4",name:"Phi 4",stop:["<|im_end|>","<|im_sep|>"],system:{schema:"<|im_start|>system<|im_sep|>{system}<|im_end|>"},user:"<|im_start|>user<|im_sep|>{prompt}<|im_end|>"},"phi4-tools":{afterShot:"<|im_end|>\n",assistant:"<|im_start|>assistant<|im_sep|>",id:"phi4-tools",name:"Phi 4 tools",stop:["<|im_end|>","<|im_sep|>"],system:{message:"You are a helpful assistant with some tools.\n<|tool|>\n{tools}\n<|/tool|>",schema:"<|im_start|>system<|im_sep|>{system}<|im_end|>"},tags:{toolCall:{end:"<|/tool_call|>",start:"<|tool_call|>"}},tools:{call:"<|tool_call|>\n{tools}\n<|/tool_call|>",def:"{system}",response:"<|im_start|>user\n<|tool_response|>\n{tools_response}\n<|/tool_response|><|im_end|>"},user:"<|im_start|>user<|im_sep|>{prompt}<|im_end|>"},reka:{afterShot:" <sep> ",assistant:"assistant:",id:"reka",name:"Reka",stop:["<sep>","<|endoftext|>"],user:"human: {prompt} <sep> "},vicuna:{assistant:"### ASSISTANT:",id:"vicuna",linebreaks:{user:2},name:"Vicuna",user:"USER: {prompt}"},vicuna_system:{assistant:"### ASSISTANT:",id:"vicuna_system",linebreaks:{system:2,user:2},name:"Vicuna system",system:{schema:"SYSTEM: {system}"},user:"USER: {prompt}"},wizard_vicuna:{assistant:"### ASSISTANT:",id:"wizard_vicuna",linebreaks:{user:2},name:"Wizard Vicuna",stop:["<|endoftext|>"],user:"### Human:\n{prompt}"},wizardlm:{assistant:"ASSISTANT:",id:"wizardlm",linebreaks:{user:1},name:"WizardLM",system:{message:"You are a helpful AI assistant.",schema:"{system}"},user:"USER: {prompt}"},zephyr:{afterShot:"\n",assistant:"<|assistant|>",id:"zephyr",linebreaks:{assistant:1,system:1,user:1},name:"Zephyr",stop:["<|endoftext|>"],system:{schema:"<|system|>\n{system}<|endoftext|>"},user:"<|user|>\n{prompt}<|endoftext|>"}};function e(s,t,e){try{const o=s.indexOf(t);if(-1===o)return s;let a,n=o+t.length;if(e){if(a=s.indexOf(e,n),-1===a)return s}else a=s.indexOf("\n",n),-1===a&&(a=s.length);return s.substring(n,a).trim()}catch(s){throw new Error(`Error parsing content between tags ${t} ${e}: ${s}`)}}function o(s,t,o,a){if(a){if("glm"==a)return function(s){try{const t=s.trim().split("</tool_call>").map(s=>s.replace("<tool_call>","")),e=new Array;t.forEach(s=>{s.length>0&&e.push(s.trim().replace("<tool_call>",""))});const o=new Array;for(const s of e)if(s.includes("<arg_key>")){const t=s.indexOf("<arg_key>"),e=s.slice(0,t),a=s.slice(t),n={},r=a.split(/<\/arg_key>|<\/arg_value>/).filter(s=>s.trim());let l="";for(const s of r)if(s.includes("<arg_key>"))l=s.replace(/<arg_key>/g,"").trim();else{const t=s.replace(/<arg_value>/g,"").trim();n[l]=t}o.push({id:`call_${Date.now()}`,name:e,arguments:Object.keys(n).length>0?n:void 0})}else{const t={id:"",name:s};o.push(t)}return o}catch(s){throw new Error(`tool call parsing error: ${s}`)}}(s);throw new Error(`unknown tool response parser ${a}`)}return function(s,t,o){try{let a=e(s,t,o),n=JSON.parse(a);return Array.isArray(n)||(n=[n]),n}catch(s){throw new Error(`tool call parsing error: ${s}`)}}(s,t,o)}class a{id;name;user;assistant;history=[];toolsDef=null;tools=[];tags={};system;shots;stop;linebreaks;afterShot;prefix;_extraSystem="";_extraAssistant="";_replacePrompt="";_replaceSystem="";_toolCallStart="";_toolCallEnd=null;_toolCallParser=null;_beforeToolResponse=null;constructor(s){let t;if(t="string"==typeof s?this._load(s):s,this.id=t.id,this.name=t.name,this.user=t.user,this.assistant=t.assistant,this.system=t?.system,this.shots=t?.shots,this.stop=t?.stop,this.linebreaks=t?.linebreaks,this.afterShot=t?.afterShot,this.prefix=t?.prefix,t?.tags&&(this.tags=t?.tags),t?.tools){this.toolsDef=t.tools;const s=this.toolsDef?.call.split("{tools}");if(!s)throw new Error(`Tool definition malformed in template ${this.name}`);if(0==s.length)throw new Error(`Tool definition malformed in template ${this.name}: no start tool call definition`);this._toolCallStart=this.tags?.toolCall?.start||"",this._toolCallEnd=this.tags?.toolCall?.end||null,this._toolCallParser=t?.tools?.parser??null,this._beforeToolResponse=t?.tools?.beforeResponse??null}}get hasTools(){return this.tools.length>0}renderShot(s){const t=[];if(s?.user&&t.push(this._buildUserBlock(s.user)),s?.assistant){let e=s.assistant;this.afterShot&&(e+=this.afterShot),t.push(this._buildAssistantBlock(e))}if(s?.tools){const e=this._buildToolsResponse(s.tools);t.push(e)}return t.join("")}render(s=!0){const t=new Array;this.prefix&&t.push(this.prefix);const e="{system}"==this?.toolsDef?.def,o=this._buildSystemBlock(s,e);if(o.length>0&&(t.push(o),this?.linebreaks?.system&&t.push("\n".repeat(this.linebreaks.system))),this.toolsDef&&!e){const s=this._buildToolsBlock();s.length>0&&(t.push(s),this?.linebreaks?.tools&&t.push("\n".repeat(this.linebreaks.tools)))}if(this?.shots)for(const s of this.shots)t.push(this.renderShot(s));let a=!1;if(this.history.length>0){for(const s of this.history)t.push(this.renderShot(s));this.history[this.history.length-1]?.tools&&(a=!0)}return a?this?.linebreaks?.tools&&t.push("\n".repeat(this.linebreaks.tools)):t.push(this._buildUserBlock()),t.push(this._buildAssistantBlock()),t.join("")}addTool(s){if(!this?.toolsDef)throw new Error("This template does not support tools");return this.tools.push(s),this}processAnswer(s){if(!this.hasTools)return{isToolCall:!1,toolsCall:[]};let t=!1,e=[];const a=s.trim();if(console.log("\nTC ANSWER",a),console.log("TC SW",this._toolCallStart+"||",a.includes(this._toolCallStart)),a.includes(this._toolCallStart)){let n;if(t=!0,a.startsWith(this._toolCallStart))n=a;else{const s=a.indexOf(this._toolCallStart);n=a.slice(s)}try{const s=o(n,this._toolCallStart,this._toolCallEnd??void 0,this._toolCallParser??void 0);if(!Array.isArray(s))throw new Error(`error parsing tool call response from model: the response object is not an Array:\n${s}`);e=s}catch(t){const e=new Array;throw e.push("error parsing tool call from model:"),e.push("------------- tool call ---------------"),e.push(s),e.push("----------- parsing error --------------"),e.push(`${t}`),new Error(e.join("\n"))}}return{isToolCall:t,toolsCall:e}}cloneTo(s,t=!0){const e=new a(s);return Object.assign(e,this),t?e.history=this.history.map(s=>({...s})):e.shots=[],e}toJson(){const s={id:this.id,name:this.name,user:this.user,assistant:this.assistant};return this?.prefix&&(s.prefix=this.prefix),this?.system&&(s.system=this.system),this?.shots&&(s.shots=this.shots),this?.afterShot&&(s.afterShot=this.afterShot),this?.stop&&(s.stop=this.stop),this?.linebreaks&&(s.linebreaks=this.linebreaks),s}replaceSystem(s){return this.system?(this._replaceSystem=s,this):this}afterSystem(s){return this.system?(this._extraSystem=s,this):this}afterAssistant(s){return this._extraAssistant=s,this}replacePrompt(s){return this._replacePrompt=s,this}addShot(s){if(s.tools&&!this.toolsDef)throw new Error("This template does not support tools");return this.shots||(this.shots=[]),this.shots.push(s),this}addShots(s){return s.forEach(s=>this.addShot(s)),this}prompt(s,t=!0){return this.render(t).replace("{prompt}",s)}pushToHistory(s,t=!0){if(t&&s?.assistant&&this.tags?.think){const t=s.assistant.split(this.tags.think.end);t.length>1&&(s.think=e(s.assistant,this.tags.think.start,this.tags.think.end),s.assistant=t[1].trim())}return this.history.push(s),this}_buildSystemBlock(s,t=!1){let e="";if(!this?.system)return"";let o=this._replaceSystem||this.system.message||"";return this._extraSystem&&(o+=this._extraSystem),o?e=this.system.schema.replace("{system}",o):s||(e=this.system.schema.replace("{system}","")),t&&this.tools.length>0&&(e=e.replace("{tools}",this._buildToolsBlock(!0))),e}_buildToolsResponse(s){if(!this.toolsDef)throw new Error("No tools def in template to build tool response");const t=new Array;for(const e of s)t.push(this.toolsDef.response.replace("{tools_response}",JSON.stringify(e.response)));let e=t.join("");return this._beforeToolResponse&&(e=this._beforeToolResponse+e),e}_buildToolsBlock(s=!1){if(!this.toolsDef)throw new Error("Can not build tools block: no tools definition found in template");let t="";if(0==this.tools.length)return"";const e=JSON.stringify(this.tools);return s?e:(t+=this.toolsDef.def.replace("{tools}",e),t)}_buildUserBlock(s){let t=[],e=this.user;return this._replacePrompt.length>0&&(e=e.replace("{prompt}",this._replacePrompt)),t.push(e),this?.linebreaks?.user&&t.push("\n".repeat(this.linebreaks.user)),s&&(t[0]=this.user.replace("{prompt}",s)),t.join("")}_buildAssistantBlock(s){let t="",e=this.assistant;return this?.linebreaks?.assistant&&(e+="\n".repeat(this.linebreaks.assistant)),this._extraAssistant.length>0&&(e+=this._extraAssistant),t+=e,s&&(t+=s),t}_load(s){try{if(s in t)return t[s];throw new Error(`Template ${s} not found`)}catch(t){throw new Error(`Error loading template ${s}: ${t}`)}}}return s.PromptTemplate=a,s.templates=t,s}({});
@@ -0,0 +1,3 @@
1
+ import type { ToolCallSpec } from "@locallm/types";
2
+ declare function extractGlmToolSpec(text: string, startTag: string, endTag?: string): ToolCallSpec[];
3
+ export { extractGlmToolSpec, };
@@ -0,0 +1,3 @@
1
+ import type { ToolCallSpec } from "@locallm/types";
2
+ declare function extractJsonToolSpec(text: string, startTag: string, endTag?: string): ToolCallSpec[];
3
+ export { extractJsonToolSpec, };
@@ -0,0 +1,3 @@
1
+ import type { ToolCallSpec } from "@locallm/types";
2
+ declare function routeToolResponseParsing(text: string, startTag: string, endTag?: string, parser?: string): ToolCallSpec[];
3
+ export { routeToolResponseParsing, };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "modprompt",
3
- "version": "0.12.6",
3
+ "version": "0.13.0",
4
4
  "description": "Prompt templates for language models",
5
5
  "license": "MIT",
6
6
  "scripts": {
@@ -9,22 +9,22 @@
9
9
  "docs": "typedoc --entryPointStrategy expand"
10
10
  },
11
11
  "devDependencies": {
12
- "@locallm/types": "^0.4.2",
13
- "@rollup/plugin-node-resolve": "^16.0.1",
12
+ "@locallm/types": "^0.6.7",
13
+ "@rollup/plugin-node-resolve": "^16.0.3",
14
14
  "@rollup/plugin-terser": "^0.4.4",
15
- "@rollup/plugin-typescript": "^12.1.4",
15
+ "@rollup/plugin-typescript": "^12.3.0",
16
16
  "@types/expect": "^24.3.2",
17
17
  "@types/jest": "^30.0.0",
18
- "@types/node": "^24.3.1",
19
- "jest": "^30.1.3",
20
- "rollup": "^4.50.1",
21
- "ts-jest": "^29.4.1",
18
+ "@types/node": "^25.0.10",
19
+ "jest": "^30.2.0",
20
+ "rollup": "^4.56.0",
21
+ "ts-jest": "^29.4.6",
22
22
  "ts-node": "^10.9.2",
23
23
  "tslib": "^2.8.1",
24
- "typedoc": "^0.28.12",
25
- "typedoc-plugin-markdown": "^4.8.1",
24
+ "typedoc": "^0.28.16",
25
+ "typedoc-plugin-markdown": "^4.9.0",
26
26
  "typedoc-plugin-rename-defaults": "^0.7.3",
27
- "typescript": "^5.9.2"
27
+ "typescript": "^5.9.3"
28
28
  },
29
29
  "type": "module",
30
30
  "files": [