modprompt 0.0.8 → 0.0.10

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/README.md CHANGED
@@ -36,11 +36,11 @@ console.log(templateNames);
36
36
  To load a template:
37
37
 
38
38
  ```js
39
- import { templates, ModTemplate } from "modprompt";
39
+ import { templates, PromptTemplate } from "modprompt";
40
40
 
41
- const tpl = new ModTemplate(templates.alpaca)
41
+ const tpl = new PromptTemplate(templates.alpaca)
42
42
  // or
43
- const tpl = new ModTemplate("alpaca")
43
+ const tpl = new PromptTemplate("alpaca")
44
44
  ```
45
45
 
46
46
  ### Render a template
@@ -158,7 +158,7 @@ fix this invalid json:
158
158
  The calls can be chained. Example with the code above:
159
159
 
160
160
  ```js
161
- const tpl = new ModTemplate(templates.llama)
161
+ const tpl = new PromptTemplate(templates.llama)
162
162
  .afterSystem("You are a javascript specialist")
163
163
  .afterAssistant(" (answer in valid json)")
164
164
  .replacePrompt("fix this invalid json:\n\n```json\n{prompt}\n```")
package/dist/cls.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { LmTemplate, PromptBlock, TurnBlock, SpacingSlots } from "./interfaces";
2
- declare class PromptTemplate implements LmTemplate {
2
+ declare class PromptTemplate {
3
+ id: string;
3
4
  name: string;
4
5
  user: string;
5
6
  assistant: string;
@@ -7,7 +8,8 @@ declare class PromptTemplate implements LmTemplate {
7
8
  shots?: Array<TurnBlock>;
8
9
  stop?: Array<string>;
9
10
  linebreaks?: SpacingSlots;
10
- private _systemBlock;
11
+ _extraSystem: string;
12
+ _extraAssistant: string;
11
13
  constructor(template: string | LmTemplate);
12
14
  cloneTo(name: string): PromptTemplate;
13
15
  toJson(): LmTemplate;
@@ -12,6 +12,7 @@ interface TurnBlock {
12
12
  assistant: string;
13
13
  }
14
14
  interface LmTemplate {
15
+ id: string;
15
16
  name: string;
16
17
  user: string;
17
18
  assistant: string;
package/dist/mod.es.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  const templates = {
2
2
  "alpaca": {
3
+ "id": "alpaca",
3
4
  "name": "Alpaca",
4
5
  "system": {
5
6
  "schema": "{system}",
@@ -13,6 +14,7 @@ const templates = {
13
14
  }
14
15
  },
15
16
  "llama": {
17
+ "id": "llama",
16
18
  "name": "Llama",
17
19
  "system": {
18
20
  "schema": "<s>[INST] <<SYS>>\n{system}\n<</SYS>>",
@@ -26,6 +28,7 @@ const templates = {
26
28
  }
27
29
  },
28
30
  "llama_instruct": {
31
+ "id": "llama_instruct",
29
32
  "name": "Llama instruct",
30
33
  "user": "[INST] {prompt}",
31
34
  "assistant": " [/INST]",
@@ -33,7 +36,15 @@ const templates = {
33
36
  "user": 1
34
37
  },
35
38
  },
39
+ "mistral": {
40
+ "id": "mistral",
41
+ "name": "Mistral",
42
+ "user": "<s>[INST] {prompt}",
43
+ "assistant": " [/INST]",
44
+ "stop": ["</s>"]
45
+ },
36
46
  "orca": {
47
+ "id": "orca",
37
48
  "name": "Orca",
38
49
  "system": {
39
50
  "schema": "### System:\n{system}",
@@ -47,6 +58,7 @@ const templates = {
47
58
  }
48
59
  },
49
60
  "vicuna": {
61
+ "id": "vicuna",
50
62
  "name": "Vicuna",
51
63
  "user": "USER: {prompt}",
52
64
  "assistant": "### ASSISTANT:",
@@ -55,6 +67,7 @@ const templates = {
55
67
  },
56
68
  },
57
69
  "vicuna_system": {
70
+ "id": "vicuna_system",
58
71
  "name": "Vicuna system",
59
72
  "system": {
60
73
  "schema": "SYSTEM: {system}",
@@ -66,6 +79,7 @@ const templates = {
66
79
  },
67
80
  },
68
81
  "wizard_vicuna": {
82
+ "id": "wizard_vicuna",
69
83
  "name": "Wizard Vicuna",
70
84
  "user": "### Human:\n{prompt}",
71
85
  "assistant": "### ASSISTANT:",
@@ -75,6 +89,7 @@ const templates = {
75
89
  "stop": ["<|endoftext|>"]
76
90
  },
77
91
  "guanaco": {
92
+ "id": "guanaco",
78
93
  "name": "Guanaco",
79
94
  "user": "### Human: {prompt}",
80
95
  "assistant": "### Assistant:",
@@ -83,6 +98,7 @@ const templates = {
83
98
  },
84
99
  },
85
100
  "chatml": {
101
+ "id": "chatml",
86
102
  "name": "ChatMl",
87
103
  "system": {
88
104
  "schema": "<|im_start|>system\n{system}\n<|im_end|>",
@@ -96,12 +112,14 @@ const templates = {
96
112
  }
97
113
  },
98
114
  "mamba": {
115
+ "id": "mamba",
99
116
  "name": "Mamba",
100
117
  "user": "<|prompt|>{prompt}</s>",
101
118
  "assistant": "<|answer|>",
102
119
  "stop": ["<|endoftext|>"]
103
120
  },
104
121
  "wizardlm": {
122
+ "id": "wizardlm",
105
123
  "name": "WizardLM",
106
124
  "system": {
107
125
  "schema": "{system}",
@@ -114,6 +132,7 @@ const templates = {
114
132
  },
115
133
  },
116
134
  "human_response": {
135
+ "id": "human_response",
117
136
  "name": "Human response",
118
137
  "user": "### HUMAN:\n{prompt}",
119
138
  "assistant": "### RESPONSE:",
@@ -123,6 +142,7 @@ const templates = {
123
142
  },
124
143
  },
125
144
  "coding_assistant": {
145
+ "id": "coding_assistant",
126
146
  "name": "Coding assistant",
127
147
  "system": {
128
148
  "schema": "{system}",
@@ -138,6 +158,7 @@ const templates = {
138
158
  };
139
159
 
140
160
  class PromptTemplate {
161
+ id;
141
162
  name;
142
163
  user;
143
164
  assistant;
@@ -145,7 +166,8 @@ class PromptTemplate {
145
166
  shots;
146
167
  stop;
147
168
  linebreaks;
148
- _systemBlock = "";
169
+ _extraSystem = "";
170
+ _extraAssistant = "";
149
171
  constructor(template) {
150
172
  let tpl;
151
173
  if (typeof template == "string") {
@@ -154,6 +176,7 @@ class PromptTemplate {
154
176
  else {
155
177
  tpl = template;
156
178
  }
179
+ this.id = tpl.id;
157
180
  this.name = tpl.name;
158
181
  this.user = tpl.user;
159
182
  this.assistant = tpl.assistant;
@@ -169,9 +192,6 @@ class PromptTemplate {
169
192
  if (tpl?.linebreaks) {
170
193
  this.linebreaks = tpl.linebreaks;
171
194
  }
172
- if (tpl?.system) {
173
- this._systemBlock = this._buildSystemBlock();
174
- }
175
195
  }
176
196
  cloneTo(name) {
177
197
  const tpl = new PromptTemplate(name);
@@ -187,13 +207,18 @@ class PromptTemplate {
187
207
  if (this?.linebreaks) {
188
208
  tpl.linebreaks = this.linebreaks;
189
209
  }
190
- if (tpl?.system) {
191
- tpl.replaceSystem(this._buildSystemBlock());
210
+ if (this._extraSystem.length > 0) {
211
+ tpl.afterSystem(this._extraSystem);
212
+ }
213
+ if (this._extraAssistant.length > 0) {
214
+ tpl.afterAssistant(this._extraAssistant);
192
215
  }
216
+ tpl.user = this.user;
193
217
  return tpl;
194
218
  }
195
219
  toJson() {
196
220
  const res = {
221
+ id: this.id,
197
222
  name: this.name,
198
223
  user: this.user,
199
224
  assistant: this.assistant,
@@ -213,21 +238,24 @@ class PromptTemplate {
213
238
  return res;
214
239
  }
215
240
  replaceSystem(msg) {
216
- this._systemBlock = this._buildSystemBlock(msg);
241
+ if (!this.system) {
242
+ return this;
243
+ }
244
+ this.system.message = msg;
217
245
  return this;
218
246
  }
219
247
  afterSystem(msg) {
220
248
  if (!this.system) {
221
- throw new Error("This template has no system var");
249
+ return this;
222
250
  }
223
251
  if (!this.system?.message) {
224
252
  this.system.message = "";
225
253
  }
226
- this._systemBlock = this._buildSystemBlock(this.system.message + " " + msg);
254
+ this._extraSystem = msg;
227
255
  return this;
228
256
  }
229
257
  afterAssistant(msg) {
230
- this.assistant = this.assistant + msg;
258
+ this._extraAssistant = msg;
231
259
  return this;
232
260
  }
233
261
  replacePrompt(msg) {
@@ -246,8 +274,9 @@ class PromptTemplate {
246
274
  }
247
275
  render() {
248
276
  const buf = new Array();
249
- if (this._systemBlock.length > 0) {
250
- buf.push(this._systemBlock);
277
+ const _systemBlock = this._buildSystemBlock();
278
+ if (_systemBlock.length > 0) {
279
+ buf.push(_systemBlock);
251
280
  if (this?.linebreaks?.system) {
252
281
  buf.push("\n".repeat(this.linebreaks.system));
253
282
  }
@@ -265,21 +294,19 @@ class PromptTemplate {
265
294
  prompt(msg) {
266
295
  return this.render().replace("{prompt}", msg);
267
296
  }
268
- _buildSystemBlock(systemMsg) {
297
+ _buildSystemBlock() {
269
298
  let res = "";
270
299
  if (!this?.system) {
271
- throw new Error(`The template ${this.name} has no system var`);
300
+ return "";
272
301
  }
273
- if (systemMsg) {
274
- res = this.system.schema.replace("{system}", systemMsg);
302
+ if (this.system?.message) {
303
+ res = this.system.schema.replace("{system}", this.system.message);
275
304
  }
276
305
  else {
277
- if (this.system?.message) {
278
- res = this.system.schema.replace("{system}", this.system.message);
279
- }
280
- else {
281
- res = this.system.schema;
282
- }
306
+ res = this.system.schema.replace("{system}", "");
307
+ }
308
+ if (this._extraSystem) {
309
+ res = res + this._extraSystem;
283
310
  }
284
311
  return res;
285
312
  }
@@ -296,7 +323,11 @@ class PromptTemplate {
296
323
  }
297
324
  _buildAssistantBlock(msg) {
298
325
  let buf = [];
299
- buf.push(this.assistant);
326
+ let amsg = this.assistant;
327
+ if (this._extraAssistant.length > 0) {
328
+ amsg += this._extraAssistant;
329
+ }
330
+ buf.push(amsg);
300
331
  if (this?.linebreaks?.assistant) {
301
332
  buf.push("\n".repeat(this.linebreaks.assistant));
302
333
  }
package/dist/mod.min.js CHANGED
@@ -1 +1 @@
1
- var $tpl=function(s){"use strict";const t={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:{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:{name:"Llama instruct",user:"[INST] {prompt}",assistant:" [/INST]",linebreaks:{user:1}},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:{name:"Vicuna",user:"USER: {prompt}",assistant:"### ASSISTANT:",linebreaks:{user:2}},vicuna_system:{name:"Vicuna system",system:{schema:"SYSTEM: {system}"},user:"USER: {prompt}",assistant:"### ASSISTANT:",linebreaks:{user:2}},wizard_vicuna:{name:"Wizard Vicuna",user:"### Human:\n{prompt}",assistant:"### ASSISTANT:",linebreaks:{user:2},stop:["<|endoftext|>"]},guanaco:{name:"Guanaco",user:"### Human: {prompt}",assistant:"### Assistant:",linebreaks:{user:1}},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:{name:"Mamba",user:"<|prompt|>{prompt}</s>",assistant:"<|answer|>",stop:["<|endoftext|>"]},wizardlm:{name:"WizardLM",system:{schema:"{system}",message:"You are a helpful AI assistant."},user:"USER: {prompt}",assistant:"ASSISTANT:",linebreaks:{user:1}},human_response:{name:"Human response",user:"### HUMAN:\n{prompt}",assistant:"### RESPONSE:",linebreaks:{user:2,assistant:1}},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{name;user;assistant;system;shots;stop;linebreaks;_systemBlock="";constructor(s){let t;t="string"==typeof s?this._load(s):s,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),t?.system&&(this._systemBlock=this._buildSystemBlock())}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),t?.system&&t.replaceSystem(this._buildSystemBlock()),t}toJson(){const s={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._systemBlock=this._buildSystemBlock(s),this}afterSystem(s){if(!this.system)throw new Error("This template has no system var");return this.system?.message||(this.system.message=""),this._systemBlock=this._buildSystemBlock(this.system.message+" "+s),this}afterAssistant(s){return this.assistant=this.assistant+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;if(this._systemBlock.length>0&&(s.push(this._systemBlock),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(s){let t="";if(!this?.system)throw new Error(`The template ${this.name} has no system var`);return t=s?this.system.schema.replace("{system}",s):this.system?.message?this.system.schema.replace("{system}",this.system.message):this.system.schema,t}_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=[];return t.push(this.assistant),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:"<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}({});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "modprompt",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "description": "Prompt templates for language models",
5
5
  "license": "MIT",
6
6
  "scripts": {
@@ -10,7 +10,6 @@
10
10
  },
11
11
  "dependencies": {},
12
12
  "devDependencies": {
13
- "@rollup/plugin-commonjs": "^25.0.4",
14
13
  "@rollup/plugin-node-resolve": "^15.2.1",
15
14
  "@rollup/plugin-typescript": "^11.1.3",
16
15
  "@types/node": "^20.6.0",