modprompt 0.0.7 → 0.0.9

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 ModTemplate implements LmTemplate {
2
+ declare class PromptTemplate {
3
+ id: string;
3
4
  name: string;
4
5
  user: string;
5
6
  assistant: string;
@@ -7,14 +8,15 @@ declare class ModTemplate implements LmTemplate {
7
8
  shots?: Array<TurnBlock>;
8
9
  stop?: Array<string>;
9
10
  linebreaks?: SpacingSlots;
10
- spacing?: SpacingSlots;
11
- private _systemBlock;
11
+ _systemBlock: string;
12
12
  constructor(template: string | LmTemplate);
13
- replaceSystem(msg: string): ModTemplate;
14
- afterSystem(msg: string): ModTemplate;
15
- afterAssistant(msg: string): ModTemplate;
16
- replacePrompt(msg: string): ModTemplate;
17
- addShot(user: string, assistant: string): ModTemplate;
13
+ cloneTo(name: string): PromptTemplate;
14
+ toJson(): LmTemplate;
15
+ replaceSystem(msg: string): PromptTemplate;
16
+ afterSystem(msg: string): PromptTemplate;
17
+ afterAssistant(msg: string): PromptTemplate;
18
+ replacePrompt(msg: string): PromptTemplate;
19
+ addShot(user: string, assistant: string): PromptTemplate;
18
20
  render(): string;
19
21
  prompt(msg: string): string;
20
22
  private _buildSystemBlock;
@@ -22,4 +24,4 @@ declare class ModTemplate implements LmTemplate {
22
24
  private _buildAssistantBlock;
23
25
  private _load;
24
26
  }
25
- export { ModTemplate };
27
+ export { PromptTemplate };
@@ -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/main.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { templates } from "./db.js";
2
- import { ModTemplate } from "./cls.js";
2
+ import { PromptTemplate } from "./cls.js";
3
3
  import { SpacingSlots, PromptBlock, TurnBlock, LmTemplate } from "./interfaces.js";
4
- export { templates, ModTemplate, SpacingSlots, PromptBlock, TurnBlock, LmTemplate };
4
+ export { templates, PromptTemplate, SpacingSlots, PromptBlock, TurnBlock, LmTemplate };
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,19 +14,21 @@ 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>>",
19
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."
20
22
  },
21
23
  "user": "{prompt}",
22
- "assistant": " [/INST]",
24
+ "assistant": " [/INST] ",
23
25
  "linebreaks": {
24
26
  "system": 2,
25
27
  "user": 0
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}",
@@ -134,10 +154,11 @@ const templates = {
134
154
  "user": 2,
135
155
  "system": 1,
136
156
  },
137
- }
157
+ },
138
158
  };
139
159
 
140
- class ModTemplate {
160
+ class PromptTemplate {
161
+ id;
141
162
  name;
142
163
  user;
143
164
  assistant;
@@ -145,7 +166,6 @@ class ModTemplate {
145
166
  shots;
146
167
  stop;
147
168
  linebreaks;
148
- spacing;
149
169
  _systemBlock = "";
150
170
  constructor(template) {
151
171
  let tpl;
@@ -155,6 +175,7 @@ class ModTemplate {
155
175
  else {
156
176
  tpl = template;
157
177
  }
178
+ this.id = tpl.id;
158
179
  this.name = tpl.name;
159
180
  this.user = tpl.user;
160
181
  this.assistant = tpl.assistant;
@@ -174,6 +195,46 @@ class ModTemplate {
174
195
  this._systemBlock = this._buildSystemBlock();
175
196
  }
176
197
  }
198
+ cloneTo(name) {
199
+ const tpl = new PromptTemplate(name);
200
+ if (this?.system) {
201
+ tpl.system = this.system;
202
+ }
203
+ if (this?.shots) {
204
+ tpl.shots = this.shots;
205
+ }
206
+ if (this?.stop) {
207
+ tpl.stop = this.stop;
208
+ }
209
+ if (this?.linebreaks) {
210
+ tpl.linebreaks = this.linebreaks;
211
+ }
212
+ if (tpl?.system) {
213
+ tpl.replaceSystem(this._buildSystemBlock());
214
+ }
215
+ return tpl;
216
+ }
217
+ toJson() {
218
+ const res = {
219
+ id: this.id,
220
+ name: this.name,
221
+ user: this.user,
222
+ assistant: this.assistant,
223
+ };
224
+ if (this?.system) {
225
+ this.system = this.system;
226
+ }
227
+ if (this?.shots) {
228
+ this.shots = this.shots;
229
+ }
230
+ if (this?.stop) {
231
+ this.stop = this.stop;
232
+ }
233
+ if (this?.linebreaks) {
234
+ this.linebreaks = this.linebreaks;
235
+ }
236
+ return res;
237
+ }
177
238
  replaceSystem(msg) {
178
239
  this._systemBlock = this._buildSystemBlock(msg);
179
240
  return this;
@@ -277,9 +338,9 @@ class ModTemplate {
277
338
  }
278
339
  }
279
340
  catch (err) {
280
- throw new Error(`Error loading template ${name}`);
341
+ throw new Error(`Error loading template ${name}: ${err}`);
281
342
  }
282
343
  }
283
344
  }
284
345
 
285
- export { ModTemplate, templates };
346
+ export { PromptTemplate, templates };
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}}};return s.ModTemplate=class{name;user;assistant;system;shots;stop;linebreaks;spacing;_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())}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}`)}}},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;_systemBlock="";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),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={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._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}({});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "modprompt",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
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",