modprompt 0.0.1 → 0.0.2

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
@@ -30,11 +30,11 @@ console.log(templateNames);
30
30
  To load a template:
31
31
 
32
32
  ```js
33
- import { templates, useTemplate } from "modprompt";
33
+ import { templates, ModTemplate } from "modprompt";
34
34
 
35
- const tpl = useTemplate(templates.alpaca);
35
+ const tpl = new ModTemplate(templates.alpaca)
36
36
  // or
37
- const tpl = useTemplate("alpaca");
37
+ const tpl = new ModTemplate("alpaca")
38
38
  ```
39
39
 
40
40
  ### Render a template
@@ -109,9 +109,9 @@ is assembled in a prompt template. Example with custom user and assistant messag
109
109
  // modify system message
110
110
  tpl.afterSystem("You are a javascript specialist");
111
111
  // modify assistant message
112
- tpl.template.assistant = tpl.template.assistant + " (answer in valid json)"
112
+ tpl.afterAssistant(" (answer in valid json)")
113
113
  // modify the prompt message
114
- tpl.template.user = tpl.template.user.replace("{prompt}", "fix this invalid json:\n\n```json\n{prompt}\n```")
114
+ tpl.replacePrompt("fix this invalid json:\n\n```json\n{prompt}\n```")
115
115
  // add a one shot example
116
116
  tpl.addShot(
117
117
  "{'a':1,}",
@@ -147,6 +147,21 @@ fix this invalid json:
147
147
  ### Response: (answer in valid json)
148
148
  ```
149
149
 
150
+ ### Chainable api
151
+
152
+ The calls can be chained. Example with the code above:
153
+
154
+ ```js
155
+ const tpl = new ModTemplate(templates.llama)
156
+ .afterSystem("You are a javascript specialist")
157
+ .afterAssistant(" (answer in valid json)")
158
+ .replacePrompt("fix this invalid json:\n\n```json\n{prompt}\n```")
159
+ .addShot(
160
+ "{'a':1,}",
161
+ '\n\n```json\n{"a":1}\n```\n',
162
+ );
163
+ ```
164
+
150
165
  ## Types
151
166
 
152
167
  Template types:
@@ -176,7 +191,6 @@ interface LmTemplate {
176
191
  shots?: Array<TurnBlock>;
177
192
  stop?: Array<string>;
178
193
  linebreaks?: SpacingSlots;
179
- spacing?: SpacingSlots;
180
194
  }
181
195
  ```
182
196
 
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}},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.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,14 @@ 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
+ },
28
57
  "wizard_vicuna": {
29
58
  "name": "Wizard Vicuna",
30
59
  "user": "### Human:\n{prompt}",
@@ -49,136 +78,148 @@ const templates = {
49
78
  },
50
79
  };
51
80
 
52
- const useTemplate = (_template) => {
53
- let template = templates.alpaca;
54
- let _systemBlock = "";
55
- if (_template) {
56
- if (typeof _template == "string") {
57
- load(_template);
81
+ class ModTemplate {
82
+ name;
83
+ user;
84
+ assistant;
85
+ system;
86
+ shots;
87
+ stop;
88
+ linebreaks;
89
+ spacing;
90
+ _systemBlock = "";
91
+ constructor(template) {
92
+ let tpl;
93
+ if (typeof template == "string") {
94
+ tpl = this._load(template);
58
95
  }
59
96
  else {
60
- template = _template;
61
- if (template?.system) {
62
- _systemBlock = _buildSystemBlock();
63
- }
97
+ tpl = template;
98
+ }
99
+ this.name = tpl.name;
100
+ this.user = tpl.user;
101
+ this.assistant = tpl.assistant;
102
+ if (tpl?.system) {
103
+ this.system = tpl.system;
104
+ }
105
+ if (tpl?.shots) {
106
+ this.shots = tpl.shots;
64
107
  }
108
+ if (tpl?.stop) {
109
+ this.stop = tpl.stop;
110
+ }
111
+ if (tpl?.linebreaks) {
112
+ this.linebreaks = tpl.linebreaks;
113
+ }
114
+ this._systemBlock = this._buildSystemBlock();
65
115
  }
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
- }
116
+ replaceSystem(msg) {
117
+ this._systemBlock = this._buildSystemBlock(msg);
118
+ return this;
119
+ }
120
+ afterSystem(msg) {
121
+ if (!this.system) {
122
+ throw new Error("This template has no system var");
75
123
  }
76
- catch (err) {
77
- throw new Error(`Error loading template ${name}`);
124
+ if (!this.system?.message) {
125
+ this.system.message = "";
78
126
  }
79
- if (template?.system) {
80
- _systemBlock = _buildSystemBlock();
127
+ this._systemBlock = this._buildSystemBlock(this.system.message + " " + msg);
128
+ return this;
129
+ }
130
+ afterAssistant(msg) {
131
+ this.assistant = this.assistant + msg;
132
+ return this;
133
+ }
134
+ replacePrompt(msg) {
135
+ this.user = this.user.replace("{prompt}", msg);
136
+ return this;
137
+ }
138
+ addShot(user, assistant) {
139
+ if (!this?.shots) {
140
+ this.shots = [];
81
141
  }
142
+ this.shots.push({
143
+ user: user,
144
+ assistant: assistant,
145
+ });
146
+ return this;
147
+ }
148
+ render() {
149
+ const buf = new Array();
150
+ if (this._systemBlock.length > 0) {
151
+ buf.push(this._systemBlock);
152
+ if (this?.linebreaks?.system) {
153
+ buf.push("\n".repeat(this.linebreaks.system));
154
+ }
155
+ }
156
+ if (this?.shots) {
157
+ for (const shot of this.shots) {
158
+ buf.push(this._buildUserBlock(shot.user));
159
+ buf.push(this._buildAssistantBlock(shot.assistant));
160
+ }
161
+ }
162
+ buf.push(this._buildUserBlock());
163
+ buf.push(this._buildAssistantBlock());
164
+ return buf.join("");
82
165
  }
83
- function _buildSystemBlock(systemMsg) {
166
+ prompt(msg) {
167
+ return this.render().replace("{prompt}", msg);
168
+ }
169
+ _buildSystemBlock(systemMsg) {
84
170
  let res = "";
85
- if (!template?.system) {
86
- throw new Error(`The template ${template.name} has no system var`);
171
+ if (!this?.system) {
172
+ throw new Error(`The template ${this.name} has no system var`);
87
173
  }
88
174
  if (systemMsg) {
89
- res = template.system.schema.replace("{system}", systemMsg);
175
+ res = this.system.schema.replace("{system}", systemMsg);
90
176
  }
91
177
  else {
92
- if (template.system?.message) {
93
- res = template.system.schema.replace("{system}", template.system.message);
178
+ if (this.system?.message) {
179
+ res = this.system.schema.replace("{system}", this.system.message);
94
180
  }
95
181
  else {
96
- res = template.system.schema;
182
+ res = this.system.schema;
97
183
  }
98
184
  }
99
185
  return res;
100
186
  }
101
- function _buildUserBlock(msg) {
187
+ _buildUserBlock(msg) {
102
188
  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));
189
+ buf.push(this.user);
190
+ if (this?.linebreaks?.user) {
191
+ buf.push("\n".repeat(this.linebreaks.user));
109
192
  }
110
193
  if (msg) {
111
194
  buf[0] = buf[0].replace("{prompt}", msg);
112
195
  }
113
196
  return buf.join("");
114
197
  }
115
- function _buildAssistantBlock(msg) {
198
+ _buildAssistantBlock(msg) {
116
199
  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));
200
+ buf.push(this.assistant);
201
+ if (this?.linebreaks?.assistant) {
202
+ buf.push("\n".repeat(this.linebreaks.assistant));
123
203
  }
124
204
  if (msg) {
125
205
  buf[0] = buf[0] + msg + "\n";
126
206
  }
127
207
  return buf.join("");
128
208
  }
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));
209
+ _load(name) {
210
+ try {
211
+ if (name in templates) {
212
+ console.log("Loading", name);
213
+ return templates[name];
156
214
  }
157
- if (template?.linebreaks?.system) {
158
- buf.push("\n".repeat(template.linebreaks.system));
215
+ else {
216
+ throw new Error(`Template ${name} not found`);
159
217
  }
160
218
  }
161
- if (template?.shots) {
162
- for (const shot of template.shots) {
163
- buf.push(_buildUserBlock(shot.user));
164
- buf.push(_buildAssistantBlock(shot.assistant));
165
- }
219
+ catch (err) {
220
+ throw new Error(`Error loading template ${name}`);
166
221
  }
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
- };
222
+ }
223
+ }
183
224
 
184
- export { templates, useTemplate };
225
+ 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}},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.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.2",
4
4
  "description": "Prompt templates for language models",
5
5
  "license": "MIT",
6
6
  "scripts": {
@@ -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 };