modprompt 0.0.10 → 0.0.12

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
@@ -8,10 +8,14 @@ declare class PromptTemplate {
8
8
  shots?: Array<TurnBlock>;
9
9
  stop?: Array<string>;
10
10
  linebreaks?: SpacingSlots;
11
+ afterShot?: string;
12
+ prefix?: string;
11
13
  _extraSystem: string;
12
14
  _extraAssistant: string;
15
+ _replacePrompt: string;
16
+ _replaceSystem: string;
13
17
  constructor(template: string | LmTemplate);
14
- cloneTo(name: string): PromptTemplate;
18
+ cloneTo(template: string | LmTemplate, keepShots?: boolean): PromptTemplate;
15
19
  toJson(): LmTemplate;
16
20
  replaceSystem(msg: string): PromptTemplate;
17
21
  afterSystem(msg: string): PromptTemplate;
@@ -20,5 +20,7 @@ interface LmTemplate {
20
20
  shots?: Array<TurnBlock>;
21
21
  stop?: Array<string>;
22
22
  linebreaks?: SpacingSlots;
23
+ afterShot?: string;
24
+ prefix?: string;
23
25
  }
24
26
  export { SpacingSlots, PromptBlock, TurnBlock, LmTemplate };
package/dist/mod.es.mjs CHANGED
@@ -17,7 +17,7 @@ const templates = {
17
17
  "id": "llama",
18
18
  "name": "Llama",
19
19
  "system": {
20
- "schema": "<s>[INST] <<SYS>>\n{system}\n<</SYS>>",
20
+ "schema": "[INST] <<SYS>>\n{system}\n<</SYS>>",
21
21
  "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."
22
22
  },
23
23
  "user": "{prompt}",
@@ -25,7 +25,8 @@ const templates = {
25
25
  "linebreaks": {
26
26
  "system": 2,
27
27
  "user": 0
28
- }
28
+ },
29
+ "prefix": "<s>",
29
30
  },
30
31
  "llama_instruct": {
31
32
  "id": "llama_instruct",
@@ -39,9 +40,11 @@ const templates = {
39
40
  "mistral": {
40
41
  "id": "mistral",
41
42
  "name": "Mistral",
42
- "user": "<s>[INST] {prompt}",
43
+ "user": "[INST] {prompt}",
43
44
  "assistant": " [/INST]",
44
- "stop": ["</s>"]
45
+ "stop": ["</s>"],
46
+ "afterShot": "\n",
47
+ "prefix": "<s>",
45
48
  },
46
49
  "orca": {
47
50
  "id": "orca",
@@ -55,7 +58,7 @@ const templates = {
55
58
  "linebreaks": {
56
59
  "system": 2,
57
60
  "user": 2
58
- }
61
+ },
59
62
  },
60
63
  "vicuna": {
61
64
  "id": "vicuna",
@@ -109,7 +112,9 @@ const templates = {
109
112
  "system": 1,
110
113
  "user": 1,
111
114
  "assistant": 1,
112
- }
115
+ },
116
+ "stop": ["<|im_end|>"],
117
+ "afterShot": " <|im_end|>",
113
118
  },
114
119
  "mamba": {
115
120
  "id": "mamba",
@@ -166,8 +171,12 @@ class PromptTemplate {
166
171
  shots;
167
172
  stop;
168
173
  linebreaks;
174
+ afterShot;
175
+ prefix;
169
176
  _extraSystem = "";
170
177
  _extraAssistant = "";
178
+ _replacePrompt = "";
179
+ _replaceSystem = "";
171
180
  constructor(template) {
172
181
  let tpl;
173
182
  if (typeof template == "string") {
@@ -180,40 +189,34 @@ class PromptTemplate {
180
189
  this.name = tpl.name;
181
190
  this.user = tpl.user;
182
191
  this.assistant = tpl.assistant;
183
- if (tpl?.system) {
184
- this.system = tpl.system;
185
- }
186
- if (tpl?.shots) {
187
- this.shots = tpl.shots;
188
- }
189
- if (tpl?.stop) {
190
- this.stop = tpl.stop;
191
- }
192
- if (tpl?.linebreaks) {
193
- this.linebreaks = tpl.linebreaks;
194
- }
192
+ this.system = tpl.system;
193
+ this.shots = tpl.shots;
194
+ this.stop = tpl.stop;
195
+ this.linebreaks = tpl.linebreaks;
196
+ this.afterShot = tpl.afterShot;
197
+ this.prefix = tpl.prefix;
195
198
  }
196
- cloneTo(name) {
197
- const tpl = new PromptTemplate(name);
198
- if (this?.system) {
199
- tpl.system = this.system;
200
- }
201
- if (this?.shots) {
202
- tpl.shots = this.shots;
203
- }
204
- if (this?.stop) {
205
- tpl.stop = this.stop;
206
- }
207
- if (this?.linebreaks) {
208
- tpl.linebreaks = this.linebreaks;
199
+ cloneTo(template, keepShots = true) {
200
+ const tpl = new PromptTemplate(template);
201
+ if (keepShots) {
202
+ if (this?.shots) {
203
+ this.shots.forEach((s) => {
204
+ tpl.addShot(s.user, s.assistant);
205
+ });
206
+ }
209
207
  }
210
208
  if (this._extraSystem.length > 0) {
211
209
  tpl.afterSystem(this._extraSystem);
212
210
  }
211
+ if (this._replaceSystem.length > 0) {
212
+ tpl.replaceSystem(this._replaceSystem);
213
+ }
213
214
  if (this._extraAssistant.length > 0) {
214
215
  tpl.afterAssistant(this._extraAssistant);
215
216
  }
216
- tpl.user = this.user;
217
+ if (this._replacePrompt.length > 0) {
218
+ tpl.replacePrompt(this._replacePrompt);
219
+ }
217
220
  return tpl;
218
221
  }
219
222
  toJson() {
@@ -241,16 +244,13 @@ class PromptTemplate {
241
244
  if (!this.system) {
242
245
  return this;
243
246
  }
244
- this.system.message = msg;
247
+ this._replaceSystem = msg;
245
248
  return this;
246
249
  }
247
250
  afterSystem(msg) {
248
251
  if (!this.system) {
249
252
  return this;
250
253
  }
251
- if (!this.system?.message) {
252
- this.system.message = "";
253
- }
254
254
  this._extraSystem = msg;
255
255
  return this;
256
256
  }
@@ -259,21 +259,25 @@ class PromptTemplate {
259
259
  return this;
260
260
  }
261
261
  replacePrompt(msg) {
262
- this.user = this.user.replace("{prompt}", msg);
262
+ this._replacePrompt = msg;
263
263
  return this;
264
264
  }
265
265
  addShot(user, assistant) {
266
266
  if (!this?.shots) {
267
267
  this.shots = [];
268
268
  }
269
+ let _assistantMsg = assistant;
269
270
  this.shots.push({
270
271
  user: user,
271
- assistant: assistant,
272
+ assistant: _assistantMsg,
272
273
  });
273
274
  return this;
274
275
  }
275
276
  render() {
276
277
  const buf = new Array();
278
+ if (this.prefix) {
279
+ buf.push(this.prefix);
280
+ }
277
281
  const _systemBlock = this._buildSystemBlock();
278
282
  if (_systemBlock.length > 0) {
279
283
  buf.push(_systemBlock);
@@ -284,7 +288,14 @@ class PromptTemplate {
284
288
  if (this?.shots) {
285
289
  for (const shot of this.shots) {
286
290
  buf.push(this._buildUserBlock(shot.user));
287
- buf.push(this._buildAssistantBlock(shot.assistant));
291
+ let _assistantMsg = shot.assistant;
292
+ if (this.afterShot) {
293
+ _assistantMsg += this.afterShot;
294
+ }
295
+ else {
296
+ _assistantMsg += "\n\n";
297
+ }
298
+ buf.push(this._buildAssistantBlock(_assistantMsg));
288
299
  }
289
300
  }
290
301
  buf.push(this._buildUserBlock());
@@ -299,25 +310,29 @@ class PromptTemplate {
299
310
  if (!this?.system) {
300
311
  return "";
301
312
  }
313
+ if (this._replaceSystem) {
314
+ this.system.message = this._replaceSystem;
315
+ }
302
316
  if (this.system?.message) {
303
317
  res = this.system.schema.replace("{system}", this.system.message);
304
- }
305
- else {
306
- res = this.system.schema.replace("{system}", "");
307
- }
308
- if (this._extraSystem) {
309
- res = res + this._extraSystem;
318
+ if (this._extraSystem) {
319
+ res = res + this._extraSystem;
320
+ }
310
321
  }
311
322
  return res;
312
323
  }
313
324
  _buildUserBlock(msg) {
314
325
  let buf = [];
315
- buf.push(this.user);
326
+ let _userBlock = this.user;
327
+ if (this._replacePrompt.length > 0) {
328
+ _userBlock = _userBlock.replace("{prompt}", this._replacePrompt);
329
+ }
330
+ buf.push(_userBlock);
316
331
  if (this?.linebreaks?.user) {
317
332
  buf.push("\n".repeat(this.linebreaks.user));
318
333
  }
319
334
  if (msg) {
320
- buf[0] = buf[0].replace("{prompt}", msg);
335
+ buf[0] = _userBlock.replace("{prompt}", msg);
321
336
  }
322
337
  return buf.join("");
323
338
  }
@@ -332,7 +347,7 @@ class PromptTemplate {
332
347
  buf.push("\n".repeat(this.linebreaks.assistant));
333
348
  }
334
349
  if (msg) {
335
- buf[0] = buf[0] + msg + "\n";
350
+ buf[0] = buf[0] + msg;
336
351
  }
337
352
  return buf.join("");
338
353
  }
package/dist/mod.min.js CHANGED
@@ -1 +1 @@
1
- var $tpl=function(s){"use strict";const t={alpaca:{id:"alpaca",name:"Alpaca",system:{schema:"{system}",message:"Below is an instruction that describes a task. Write a response that appropriately completes the request."},user:"### Instruction:\n{prompt}",assistant:"### Response:",linebreaks:{system:2,user:2}},llama:{id:"llama",name:"Llama",system:{schema:"<s>[INST] <<SYS>>\n{system}\n<</SYS>>",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."},user:"{prompt}",assistant:" [/INST] ",linebreaks:{system:2,user:0}},llama_instruct:{id:"llama_instruct",name:"Llama instruct",user:"[INST] {prompt}",assistant:" [/INST]",linebreaks:{user:1}},mistral:{id:"mistral",name:"Mistral",user:"<s>[INST] {prompt}",assistant:" [/INST]",stop:["</s>"]},orca:{id:"orca",name:"Orca",system:{schema:"### System:\n{system}",message:"You are an AI assistant that follows instruction extremely well. Help as much as you can."},user:"### User:\n{prompt}",assistant:"### Response:",linebreaks:{system:2,user:2}},vicuna:{id:"vicuna",name:"Vicuna",user:"USER: {prompt}",assistant:"### ASSISTANT:",linebreaks:{user:2}},vicuna_system:{id:"vicuna_system",name:"Vicuna system",system:{schema:"SYSTEM: {system}"},user:"USER: {prompt}",assistant:"### ASSISTANT:",linebreaks:{user:2}},wizard_vicuna:{id:"wizard_vicuna",name:"Wizard Vicuna",user:"### Human:\n{prompt}",assistant:"### ASSISTANT:",linebreaks:{user:2},stop:["<|endoftext|>"]},guanaco:{id:"guanaco",name:"Guanaco",user:"### Human: {prompt}",assistant:"### Assistant:",linebreaks:{user:1}},chatml:{id:"chatml",name:"ChatMl",system:{schema:"<|im_start|>system\n{system}\n<|im_end|>"},user:"<|im_start|>user\n{prompt}<|im_end|>",assistant:"<|im_start|>assistant",linebreaks:{system:1,user:1,assistant:1}},mamba:{id:"mamba",name:"Mamba",user:"<|prompt|>{prompt}</s>",assistant:"<|answer|>",stop:["<|endoftext|>"]},wizardlm:{id:"wizardlm",name:"WizardLM",system:{schema:"{system}",message:"You are a helpful AI assistant."},user:"USER: {prompt}",assistant:"ASSISTANT:",linebreaks:{user:1}},human_response:{id:"human_response",name:"Human response",user:"### HUMAN:\n{prompt}",assistant:"### RESPONSE:",linebreaks:{user:2,assistant:1}},coding_assistant:{id:"coding_assistant",name:"Coding assistant",system:{schema:"{system}",message:"You are a coding assistant that will help the user to resolve the following instruction:"},user:"### Instruction: {prompt}",assistant:"### Solution:",linebreaks:{user:2,system:1}}};class e{id;name;user;assistant;system;shots;stop;linebreaks;_extraSystem="";_extraAssistant="";constructor(s){let t;t="string"==typeof s?this._load(s):s,this.id=t.id,this.name=t.name,this.user=t.user,this.assistant=t.assistant,t?.system&&(this.system=t.system),t?.shots&&(this.shots=t.shots),t?.stop&&(this.stop=t.stop),t?.linebreaks&&(this.linebreaks=t.linebreaks)}cloneTo(s){const t=new e(s);return this?.system&&(t.system=this.system),this?.shots&&(t.shots=this.shots),this?.stop&&(t.stop=this.stop),this?.linebreaks&&(t.linebreaks=this.linebreaks),this._extraSystem.length>0&&t.afterSystem(this._extraSystem),this._extraAssistant.length>0&&t.afterAssistant(this._extraAssistant),t.user=this.user,t}toJson(){const s={id:this.id,name:this.name,user:this.user,assistant:this.assistant};return this?.system&&(this.system=this.system),this?.shots&&(this.shots=this.shots),this?.stop&&(this.stop=this.stop),this?.linebreaks&&(this.linebreaks=this.linebreaks),s}replaceSystem(s){return this.system?(this.system.message=s,this):this}afterSystem(s){return this.system?(this.system?.message||(this.system.message=""),this._extraSystem=s,this):this}afterAssistant(s){return this._extraAssistant=s,this}replacePrompt(s){return this.user=this.user.replace("{prompt}",s),this}addShot(s,t){return this?.shots||(this.shots=[]),this.shots.push({user:s,assistant:t}),this}render(){const s=new Array,t=this._buildSystemBlock();if(t.length>0&&(s.push(t),this?.linebreaks?.system&&s.push("\n".repeat(this.linebreaks.system))),this?.shots)for(const t of this.shots)s.push(this._buildUserBlock(t.user)),s.push(this._buildAssistantBlock(t.assistant));return s.push(this._buildUserBlock()),s.push(this._buildAssistantBlock()),s.join("")}prompt(s){return this.render().replace("{prompt}",s)}_buildSystemBlock(){let s="";return this?.system?(s=this.system?.message?this.system.schema.replace("{system}",this.system.message):this.system.schema.replace("{system}",""),this._extraSystem&&(s+=this._extraSystem),s):""}_buildUserBlock(s){let t=[];return t.push(this.user),this?.linebreaks?.user&&t.push("\n".repeat(this.linebreaks.user)),s&&(t[0]=t[0].replace("{prompt}",s)),t.join("")}_buildAssistantBlock(s){let t=[],e=this.assistant;return this._extraAssistant.length>0&&(e+=this._extraAssistant),t.push(e),this?.linebreaks?.assistant&&t.push("\n".repeat(this.linebreaks.assistant)),s&&(t[0]=t[0]+s+"\n"),t.join("")}_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=e,s.templates=t,s}({});
1
+ var $tpl=function(s){"use strict";const t={alpaca:{id:"alpaca",name:"Alpaca",system:{schema:"{system}",message:"Below is an instruction that describes a task. Write a response that appropriately completes the request."},user:"### Instruction:\n{prompt}",assistant:"### Response:",linebreaks:{system:2,user:2}},llama:{id:"llama",name:"Llama",system:{schema:"[INST] <<SYS>>\n{system}\n<</SYS>>",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."},user:"{prompt}",assistant:" [/INST] ",linebreaks:{system:2,user:0},prefix:"<s>"},llama_instruct:{id:"llama_instruct",name:"Llama instruct",user:"[INST] {prompt}",assistant:" [/INST]",linebreaks:{user:1}},mistral:{id:"mistral",name:"Mistral",user:"[INST] {prompt}",assistant:" [/INST]",stop:["</s>"],afterShot:"\n",prefix:"<s>"},orca:{id:"orca",name:"Orca",system:{schema:"### System:\n{system}",message:"You are an AI assistant that follows instruction extremely well. Help as much as you can."},user:"### User:\n{prompt}",assistant:"### Response:",linebreaks:{system:2,user:2}},vicuna:{id:"vicuna",name:"Vicuna",user:"USER: {prompt}",assistant:"### ASSISTANT:",linebreaks:{user:2}},vicuna_system:{id:"vicuna_system",name:"Vicuna system",system:{schema:"SYSTEM: {system}"},user:"USER: {prompt}",assistant:"### ASSISTANT:",linebreaks:{user:2}},wizard_vicuna:{id:"wizard_vicuna",name:"Wizard Vicuna",user:"### Human:\n{prompt}",assistant:"### ASSISTANT:",linebreaks:{user:2},stop:["<|endoftext|>"]},guanaco:{id:"guanaco",name:"Guanaco",user:"### Human: {prompt}",assistant:"### Assistant:",linebreaks:{user:1}},chatml:{id:"chatml",name:"ChatMl",system:{schema:"<|im_start|>system\n{system}\n<|im_end|>"},user:"<|im_start|>user\n{prompt}<|im_end|>",assistant:"<|im_start|>assistant",linebreaks:{system:1,user:1,assistant:1},stop:["<|im_end|>"],afterShot:" <|im_end|>"},mamba:{id:"mamba",name:"Mamba",user:"<|prompt|>{prompt}</s>",assistant:"<|answer|>",stop:["<|endoftext|>"]},wizardlm:{id:"wizardlm",name:"WizardLM",system:{schema:"{system}",message:"You are a helpful AI assistant."},user:"USER: {prompt}",assistant:"ASSISTANT:",linebreaks:{user:1}},human_response:{id:"human_response",name:"Human response",user:"### HUMAN:\n{prompt}",assistant:"### RESPONSE:",linebreaks:{user:2,assistant:1}},coding_assistant:{id:"coding_assistant",name:"Coding assistant",system:{schema:"{system}",message:"You are a coding assistant that will help the user to resolve the following instruction:"},user:"### Instruction: {prompt}",assistant:"### Solution:",linebreaks:{user:2,system:1}}};class e{id;name;user;assistant;system;shots;stop;linebreaks;afterShot;prefix;_extraSystem="";_extraAssistant="";_replacePrompt="";_replaceSystem="";constructor(s){let t;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}cloneTo(s,t=!0){const a=new e(s);return t&&this?.shots&&this.shots.forEach((s=>{a.addShot(s.user,s.assistant)})),this._extraSystem.length>0&&a.afterSystem(this._extraSystem),this._replaceSystem.length>0&&a.replaceSystem(this._replaceSystem),this._extraAssistant.length>0&&a.afterAssistant(this._extraAssistant),this._replacePrompt.length>0&&a.replacePrompt(this._replacePrompt),a}toJson(){const s={id:this.id,name:this.name,user:this.user,assistant:this.assistant};return this?.system&&(this.system=this.system),this?.shots&&(this.shots=this.shots),this?.stop&&(this.stop=this.stop),this?.linebreaks&&(this.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,t){this?.shots||(this.shots=[]);let e=t;return this.shots.push({user:s,assistant:e}),this}render(){const s=new Array;this.prefix&&s.push(this.prefix);const t=this._buildSystemBlock();if(t.length>0&&(s.push(t),this?.linebreaks?.system&&s.push("\n".repeat(this.linebreaks.system))),this?.shots)for(const t of this.shots){s.push(this._buildUserBlock(t.user));let e=t.assistant;this.afterShot?e+=this.afterShot:e+="\n\n",s.push(this._buildAssistantBlock(e))}return s.push(this._buildUserBlock()),s.push(this._buildAssistantBlock()),s.join("")}prompt(s){return this.render().replace("{prompt}",s)}_buildSystemBlock(){let s="";return this?.system?(this._replaceSystem&&(this.system.message=this._replaceSystem),this.system?.message&&(s=this.system.schema.replace("{system}",this.system.message),this._extraSystem&&(s+=this._extraSystem)),s):""}_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]=e.replace("{prompt}",s)),t.join("")}_buildAssistantBlock(s){let t=[],e=this.assistant;return this._extraAssistant.length>0&&(e+=this._extraAssistant),t.push(e),this?.linebreaks?.assistant&&t.push("\n".repeat(this.linebreaks.assistant)),s&&(t[0]=t[0]+s),t.join("")}_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=e,s.templates=t,s}({});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "modprompt",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "description": "Prompt templates for language models",
5
5
  "license": "MIT",
6
6
  "scripts": {