modprompt 0.0.1 → 0.0.3

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 synw
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -4,6 +4,12 @@
4
4
 
5
5
  A collection of prompt templates for language models
6
6
 
7
+ - Classic templates formats for different models
8
+ - Easily modify and adapt templates on-the-fly
9
+ - Few shots support
10
+
11
+ :books: [Api doc](https://synw.github.io/modprompt/)
12
+
7
13
  ## Install
8
14
 
9
15
  ```bash
@@ -30,11 +36,11 @@ console.log(templateNames);
30
36
  To load a template:
31
37
 
32
38
  ```js
33
- import { templates, useTemplate } from "modprompt";
39
+ import { templates, ModTemplate } from "modprompt";
34
40
 
35
- const tpl = useTemplate(templates.alpaca);
41
+ const tpl = new ModTemplate(templates.alpaca)
36
42
  // or
37
- const tpl = useTemplate("alpaca");
43
+ const tpl = new ModTemplate("alpaca")
38
44
  ```
39
45
 
40
46
  ### Render a template
@@ -109,9 +115,9 @@ is assembled in a prompt template. Example with custom user and assistant messag
109
115
  // modify system message
110
116
  tpl.afterSystem("You are a javascript specialist");
111
117
  // modify assistant message
112
- tpl.template.assistant = tpl.template.assistant + " (answer in valid json)"
118
+ tpl.afterAssistant(" (answer in valid json)")
113
119
  // modify the prompt message
114
- tpl.template.user = tpl.template.user.replace("{prompt}", "fix this invalid json:\n\n```json\n{prompt}\n```")
120
+ tpl.replacePrompt("fix this invalid json:\n\n```json\n{prompt}\n```")
115
121
  // add a one shot example
116
122
  tpl.addShot(
117
123
  "{'a':1,}",
@@ -147,6 +153,21 @@ fix this invalid json:
147
153
  ### Response: (answer in valid json)
148
154
  ```
149
155
 
156
+ ### Chainable api
157
+
158
+ The calls can be chained. Example with the code above:
159
+
160
+ ```js
161
+ const tpl = new ModTemplate(templates.llama)
162
+ .afterSystem("You are a javascript specialist")
163
+ .afterAssistant(" (answer in valid json)")
164
+ .replacePrompt("fix this invalid json:\n\n```json\n{prompt}\n```")
165
+ .addShot(
166
+ "{'a':1,}",
167
+ '\n\n```json\n{"a":1}\n```\n',
168
+ );
169
+ ```
170
+
150
171
  ## Types
151
172
 
152
173
  Template types:
@@ -176,7 +197,6 @@ interface LmTemplate {
176
197
  shots?: Array<TurnBlock>;
177
198
  stop?: Array<string>;
178
199
  linebreaks?: SpacingSlots;
179
- spacing?: SpacingSlots;
180
200
  }
181
201
  ```
182
202
 
@@ -198,4 +218,6 @@ const orca: LmTemplate = {
198
218
  "user": 2
199
219
  }
200
220
  }
201
- ```
221
+ ```
222
+
223
+ :books: [Api doc](https://synw.github.io/modprompt/)
package/dist/cls.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ import { LmTemplate, PromptBlock, TurnBlock, SpacingSlots } from "./interfaces";
2
+ declare class ModTemplate implements LmTemplate {
3
+ name: string;
4
+ user: string;
5
+ assistant: string;
6
+ system?: PromptBlock;
7
+ shots?: Array<TurnBlock>;
8
+ stop?: Array<string>;
9
+ linebreaks?: SpacingSlots;
10
+ spacing?: SpacingSlots;
11
+ private _systemBlock;
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;
18
+ render(): string;
19
+ prompt(msg: string): string;
20
+ private _buildSystemBlock;
21
+ private _buildUserBlock;
22
+ private _buildAssistantBlock;
23
+ private _load;
24
+ }
25
+ export { ModTemplate };
@@ -19,6 +19,5 @@ interface LmTemplate {
19
19
  shots?: Array<TurnBlock>;
20
20
  stop?: Array<string>;
21
21
  linebreaks?: SpacingSlots;
22
- spacing?: SpacingSlots;
23
22
  }
24
23
  export { SpacingSlots, PromptBlock, TurnBlock, LmTemplate };
package/dist/main.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { templates } from "./db.js";
2
- import { useTemplate } from "./template.js";
2
+ import { ModTemplate } from "./cls.js";
3
3
  import type { SpacingSlots, PromptBlock, TurnBlock, LmTemplate } from "@/interfaces.js";
4
- export { templates, useTemplate };
4
+ export { templates, ModTemplate };
5
5
  export type { SpacingSlots, PromptBlock, TurnBlock, LmTemplate };
@@ -1 +1 @@
1
- "use strict";const s={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}},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}},wizard_vicuna:{name:"Wizard Vicuna",user:"### Human:\n{prompt}",assistant:"### ASSISTANT:",linebreaks:{user:2},stop:["<|endoftext|>"]},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}}};exports.templates=s,exports.useTemplate=e=>{let t=s.alpaca,a="";function r(s){let e="";if(!t?.system)throw new Error(`The template ${t.name} has no system var`);return e=s?t.system.schema.replace("{system}",s):t.system?.message?t.system.schema.replace("{system}",t.system.message):t.system.schema,e}function n(s){let e=[];return e.push(t.user),t?.spacing?.user&&e.push(" ".repeat(t.spacing.user)),t?.linebreaks?.user&&e.push("\n".repeat(t.linebreaks.user)),s&&(e[0]=e[0].replace("{prompt}",s)),e.join("")}function m(s){let e=[];return e.push(t.assistant),t?.spacing?.assistant&&e.push(" ".repeat(t.spacing.assistant)),t?.linebreaks?.assistant&&e.push("\n".repeat(t.linebreaks.assistant)),s&&(e[0]=e[0]+s+"\n"),e.join("")}e&&("string"==typeof e?function(e){try{if(!(e in s))throw new Error(`Template ${e} not found`);console.log("Loading",e),t=s[e]}catch(s){throw new Error(`Error loading template ${e}`)}t?.system&&(a=r())}(e):(t=e,t?.system&&(a=r())));const p=()=>{const s=new Array;if(a.length>0&&(s.push(a),t?.spacing?.system&&s.push(" ".repeat(t.spacing.system)),t?.linebreaks?.system&&s.push("\n".repeat(t.linebreaks.system))),t?.shots)for(const e of t.shots)s.push(n(e.user)),s.push(m(e.assistant));return s.push(n()),s.push(m()),s.join("")};return{template:t,system:s=>{a=r(s)},afterSystem:s=>{if(!t.system)throw new Error("This template has no system var");t.system?.message||(t.system.message=""),a=r(t.system.message+" "+s)},render:p,prompt:s=>p().replace("{prompt}",s),addShot:(s,e)=>{t?.shots||(t.shots=[]),t.shots.push({user:s,assistant:e})}}};
1
+ "use strict";const s={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:"Guanaco",user:"### HUMAN:\n{prompt}",assistant:"### RESPONSE:",linebreaks:{user:2,assistant:1}}};exports.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),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(t){try{if(t in s)return console.log("Loading",t),s[t];throw new Error(`Template ${t} not found`)}catch(s){throw new Error(`Error loading template ${t}`)}}},exports.templates=s;
package/dist/mod.es.js CHANGED
@@ -12,6 +12,27 @@ const templates = {
12
12
  "user": 2
13
13
  }
14
14
  },
15
+ "llama": {
16
+ "name": "Llama",
17
+ "system": {
18
+ "schema": "<s>[INST] <<SYS>>\n{system}\n<</SYS>>",
19
+ "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
+ },
21
+ "user": "{prompt}",
22
+ "assistant": " [/INST]",
23
+ "linebreaks": {
24
+ "system": 2,
25
+ "user": 0
26
+ }
27
+ },
28
+ "llama_instruct": {
29
+ "name": "Llama instruct",
30
+ "user": "[INST] {prompt}",
31
+ "assistant": " [/INST]",
32
+ "linebreaks": {
33
+ "user": 1
34
+ },
35
+ },
15
36
  "orca": {
16
37
  "name": "Orca",
17
38
  "system": {
@@ -25,6 +46,25 @@ const templates = {
25
46
  "user": 2
26
47
  }
27
48
  },
49
+ "vicuna": {
50
+ "name": "Vicuna",
51
+ "user": "USER: {prompt}",
52
+ "assistant": "### ASSISTANT:",
53
+ "linebreaks": {
54
+ "user": 2
55
+ },
56
+ },
57
+ "vicuna_system": {
58
+ "name": "Vicuna system",
59
+ "system": {
60
+ "schema": "SYSTEM: {system}",
61
+ },
62
+ "user": "USER: {prompt}",
63
+ "assistant": "### ASSISTANT:",
64
+ "linebreaks": {
65
+ "user": 2
66
+ },
67
+ },
28
68
  "wizard_vicuna": {
29
69
  "name": "Wizard Vicuna",
30
70
  "user": "### Human:\n{prompt}",
@@ -34,6 +74,14 @@ const templates = {
34
74
  },
35
75
  "stop": ["<|endoftext|>"]
36
76
  },
77
+ "guanaco": {
78
+ "name": "Guanaco",
79
+ "user": "### Human: {prompt}",
80
+ "assistant": "### Assistant:",
81
+ "linebreaks": {
82
+ "user": 1
83
+ },
84
+ },
37
85
  "chatml": {
38
86
  "name": "ChatMl",
39
87
  "system": {
@@ -47,138 +95,177 @@ const templates = {
47
95
  "assistant": 1,
48
96
  }
49
97
  },
98
+ "mamba": {
99
+ "name": "Mamba",
100
+ "user": "<|prompt|>{prompt}</s>",
101
+ "assistant": "<|answer|>",
102
+ "stop": ["<|endoftext|>"]
103
+ },
104
+ "wizardlm": {
105
+ "name": "WizardLM",
106
+ "system": {
107
+ "schema": "{system}",
108
+ "message": "You are a helpful AI assistant."
109
+ },
110
+ "user": "USER: {prompt}",
111
+ "assistant": "ASSISTANT:",
112
+ "linebreaks": {
113
+ "user": 1
114
+ },
115
+ },
116
+ "human_response": {
117
+ "name": "Guanaco",
118
+ "user": "### HUMAN:\n{prompt}",
119
+ "assistant": "### RESPONSE:",
120
+ "linebreaks": {
121
+ "user": 2,
122
+ "assistant": 1
123
+ },
124
+ },
50
125
  };
51
126
 
52
- const useTemplate = (_template) => {
53
- let template = templates.alpaca;
54
- let _systemBlock = "";
55
- if (_template) {
56
- if (typeof _template == "string") {
57
- load(_template);
127
+ class ModTemplate {
128
+ name;
129
+ user;
130
+ assistant;
131
+ system;
132
+ shots;
133
+ stop;
134
+ linebreaks;
135
+ spacing;
136
+ _systemBlock = "";
137
+ constructor(template) {
138
+ let tpl;
139
+ if (typeof template == "string") {
140
+ tpl = this._load(template);
58
141
  }
59
142
  else {
60
- template = _template;
61
- if (template?.system) {
62
- _systemBlock = _buildSystemBlock();
63
- }
143
+ tpl = template;
144
+ }
145
+ this.name = tpl.name;
146
+ this.user = tpl.user;
147
+ this.assistant = tpl.assistant;
148
+ if (tpl?.system) {
149
+ this.system = tpl.system;
150
+ }
151
+ if (tpl?.shots) {
152
+ this.shots = tpl.shots;
64
153
  }
154
+ if (tpl?.stop) {
155
+ this.stop = tpl.stop;
156
+ }
157
+ if (tpl?.linebreaks) {
158
+ this.linebreaks = tpl.linebreaks;
159
+ }
160
+ this._systemBlock = this._buildSystemBlock();
65
161
  }
66
- function load(name) {
67
- try {
68
- if (name in templates) {
69
- console.log("Loading", name);
70
- template = templates[name];
71
- }
72
- else {
73
- throw new Error(`Template ${name} not found`);
74
- }
162
+ replaceSystem(msg) {
163
+ this._systemBlock = this._buildSystemBlock(msg);
164
+ return this;
165
+ }
166
+ afterSystem(msg) {
167
+ if (!this.system) {
168
+ throw new Error("This template has no system var");
75
169
  }
76
- catch (err) {
77
- throw new Error(`Error loading template ${name}`);
170
+ if (!this.system?.message) {
171
+ this.system.message = "";
172
+ }
173
+ this._systemBlock = this._buildSystemBlock(this.system.message + " " + msg);
174
+ return this;
175
+ }
176
+ afterAssistant(msg) {
177
+ this.assistant = this.assistant + msg;
178
+ return this;
179
+ }
180
+ replacePrompt(msg) {
181
+ this.user = this.user.replace("{prompt}", msg);
182
+ return this;
183
+ }
184
+ addShot(user, assistant) {
185
+ if (!this?.shots) {
186
+ this.shots = [];
78
187
  }
79
- if (template?.system) {
80
- _systemBlock = _buildSystemBlock();
188
+ this.shots.push({
189
+ user: user,
190
+ assistant: assistant,
191
+ });
192
+ return this;
193
+ }
194
+ render() {
195
+ const buf = new Array();
196
+ if (this._systemBlock.length > 0) {
197
+ buf.push(this._systemBlock);
198
+ if (this?.linebreaks?.system) {
199
+ buf.push("\n".repeat(this.linebreaks.system));
200
+ }
201
+ }
202
+ if (this?.shots) {
203
+ for (const shot of this.shots) {
204
+ buf.push(this._buildUserBlock(shot.user));
205
+ buf.push(this._buildAssistantBlock(shot.assistant));
206
+ }
81
207
  }
208
+ buf.push(this._buildUserBlock());
209
+ buf.push(this._buildAssistantBlock());
210
+ return buf.join("");
211
+ }
212
+ prompt(msg) {
213
+ return this.render().replace("{prompt}", msg);
82
214
  }
83
- function _buildSystemBlock(systemMsg) {
215
+ _buildSystemBlock(systemMsg) {
84
216
  let res = "";
85
- if (!template?.system) {
86
- throw new Error(`The template ${template.name} has no system var`);
217
+ if (!this?.system) {
218
+ throw new Error(`The template ${this.name} has no system var`);
87
219
  }
88
220
  if (systemMsg) {
89
- res = template.system.schema.replace("{system}", systemMsg);
221
+ res = this.system.schema.replace("{system}", systemMsg);
90
222
  }
91
223
  else {
92
- if (template.system?.message) {
93
- res = template.system.schema.replace("{system}", template.system.message);
224
+ if (this.system?.message) {
225
+ res = this.system.schema.replace("{system}", this.system.message);
94
226
  }
95
227
  else {
96
- res = template.system.schema;
228
+ res = this.system.schema;
97
229
  }
98
230
  }
99
231
  return res;
100
232
  }
101
- function _buildUserBlock(msg) {
233
+ _buildUserBlock(msg) {
102
234
  let buf = [];
103
- buf.push(template.user);
104
- if (template?.spacing?.user) {
105
- buf.push(" ".repeat(template.spacing.user));
106
- }
107
- if (template?.linebreaks?.user) {
108
- buf.push("\n".repeat(template.linebreaks.user));
235
+ buf.push(this.user);
236
+ if (this?.linebreaks?.user) {
237
+ buf.push("\n".repeat(this.linebreaks.user));
109
238
  }
110
239
  if (msg) {
111
240
  buf[0] = buf[0].replace("{prompt}", msg);
112
241
  }
113
242
  return buf.join("");
114
243
  }
115
- function _buildAssistantBlock(msg) {
244
+ _buildAssistantBlock(msg) {
116
245
  let buf = [];
117
- buf.push(template.assistant);
118
- if (template?.spacing?.assistant) {
119
- buf.push(" ".repeat(template.spacing.assistant));
120
- }
121
- if (template?.linebreaks?.assistant) {
122
- buf.push("\n".repeat(template.linebreaks.assistant));
246
+ buf.push(this.assistant);
247
+ if (this?.linebreaks?.assistant) {
248
+ buf.push("\n".repeat(this.linebreaks.assistant));
123
249
  }
124
250
  if (msg) {
125
251
  buf[0] = buf[0] + msg + "\n";
126
252
  }
127
253
  return buf.join("");
128
254
  }
129
- const addShot = (user, assistant) => {
130
- if (!template?.shots) {
131
- template.shots = [];
132
- }
133
- template.shots.push({
134
- user: user,
135
- assistant: assistant,
136
- });
137
- };
138
- const system = (msg) => {
139
- _systemBlock = _buildSystemBlock(msg);
140
- };
141
- const afterSystem = (msg) => {
142
- if (!template.system) {
143
- throw new Error("This template has no system var");
144
- }
145
- if (!template.system?.message) {
146
- template.system.message = "";
147
- }
148
- _systemBlock = _buildSystemBlock(template.system.message + " " + msg);
149
- };
150
- const render = () => {
151
- const buf = new Array();
152
- if (_systemBlock.length > 0) {
153
- buf.push(_systemBlock);
154
- if (template?.spacing?.system) {
155
- buf.push(" ".repeat(template.spacing.system));
255
+ _load(name) {
256
+ try {
257
+ if (name in templates) {
258
+ console.log("Loading", name);
259
+ return templates[name];
156
260
  }
157
- if (template?.linebreaks?.system) {
158
- buf.push("\n".repeat(template.linebreaks.system));
261
+ else {
262
+ throw new Error(`Template ${name} not found`);
159
263
  }
160
264
  }
161
- if (template?.shots) {
162
- for (const shot of template.shots) {
163
- buf.push(_buildUserBlock(shot.user));
164
- buf.push(_buildAssistantBlock(shot.assistant));
165
- }
265
+ catch (err) {
266
+ throw new Error(`Error loading template ${name}`);
166
267
  }
167
- buf.push(_buildUserBlock());
168
- buf.push(_buildAssistantBlock());
169
- return buf.join("");
170
- };
171
- const prompt = (msg) => {
172
- return render().replace("{prompt}", msg);
173
- };
174
- return {
175
- template,
176
- system,
177
- afterSystem,
178
- render,
179
- prompt,
180
- addShot,
181
- };
182
- };
268
+ }
269
+ }
183
270
 
184
- export { templates, useTemplate };
271
+ export { ModTemplate, templates };
package/dist/mod.min.js CHANGED
@@ -1 +1 @@
1
- var $tpl=function(s){"use strict";const e={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}},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}},wizard_vicuna:{name:"Wizard Vicuna",user:"### Human:\n{prompt}",assistant:"### ASSISTANT:",linebreaks:{user:2},stop:["<|endoftext|>"]},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}}};return s.templates=e,s.useTemplate=s=>{let t=e.alpaca,a="";function r(s){let e="";if(!t?.system)throw new Error(`The template ${t.name} has no system var`);return e=s?t.system.schema.replace("{system}",s):t.system?.message?t.system.schema.replace("{system}",t.system.message):t.system.schema,e}function n(s){let e=[];return e.push(t.user),t?.spacing?.user&&e.push(" ".repeat(t.spacing.user)),t?.linebreaks?.user&&e.push("\n".repeat(t.linebreaks.user)),s&&(e[0]=e[0].replace("{prompt}",s)),e.join("")}function m(s){let e=[];return e.push(t.assistant),t?.spacing?.assistant&&e.push(" ".repeat(t.spacing.assistant)),t?.linebreaks?.assistant&&e.push("\n".repeat(t.linebreaks.assistant)),s&&(e[0]=e[0]+s+"\n"),e.join("")}s&&("string"==typeof s?function(s){try{if(!(s in e))throw new Error(`Template ${s} not found`);console.log("Loading",s),t=e[s]}catch(e){throw new Error(`Error loading template ${s}`)}t?.system&&(a=r())}(s):(t=s,t?.system&&(a=r())));const i=()=>{const s=new Array;if(a.length>0&&(s.push(a),t?.spacing?.system&&s.push(" ".repeat(t.spacing.system)),t?.linebreaks?.system&&s.push("\n".repeat(t.linebreaks.system))),t?.shots)for(const e of t.shots)s.push(n(e.user)),s.push(m(e.assistant));return s.push(n()),s.push(m()),s.join("")};return{template:t,system:s=>{a=r(s)},afterSystem:s=>{if(!t.system)throw new Error("This template has no system var");t.system?.message||(t.system.message=""),a=r(t.system.message+" "+s)},render:i,prompt:s=>i().replace("{prompt}",s),addShot:(s,e)=>{t?.shots||(t.shots=[]),t.shots.push({user:s,assistant:e})}}},s}({});
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:"Guanaco",user:"### HUMAN:\n{prompt}",assistant:"### RESPONSE:",linebreaks:{user:2,assistant: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),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 console.log("Loading",s),t[s];throw new Error(`Template ${s} not found`)}catch(t){throw new Error(`Error loading template ${s}`)}}},s.templates=t,s}({});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "modprompt",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "Prompt templates for language models",
5
5
  "license": "MIT",
6
6
  "scripts": {
@@ -14,10 +14,13 @@
14
14
  "@rollup/plugin-node-resolve": "^15.2.1",
15
15
  "@rollup/plugin-typescript": "^11.1.3",
16
16
  "@types/node": "^20.6.0",
17
- "rollup": "^3.29.0",
17
+ "rollup": "^3.29.1",
18
18
  "rollup-plugin-terser": "^7.0.2",
19
19
  "tslib": "^2.6.2",
20
- "typescript": "^4.9.5"
20
+ "typedoc": "^0.25.1",
21
+ "typedoc-plugin-markdown": "^3.15.4",
22
+ "typedoc-plugin-rename-defaults": "^0.6.5",
23
+ "typescript": "^5.2.2"
21
24
  },
22
25
  "files": [
23
26
  "dist"
@@ -1,10 +0,0 @@
1
- import { LmTemplate } from "./interfaces.js";
2
- declare const useTemplate: (_template?: string | LmTemplate) => {
3
- template: LmTemplate;
4
- system: (msg: string) => void;
5
- afterSystem: (msg: string) => void;
6
- render: () => string;
7
- prompt: (msg: string) => string;
8
- addShot: (user: string, assistant: string) => void;
9
- };
10
- export { useTemplate };