modprompt 0.1.0 → 0.1.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/README.md +20 -0
- package/dist/cls.d.ts +1 -1
- package/dist/mod.es.mjs +23 -31
- package/dist/mod.min.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -97,6 +97,26 @@ To append to a system message:
|
|
|
97
97
|
tpl.afterSystem("You are a javascript specialist");
|
|
98
98
|
```
|
|
99
99
|
|
|
100
|
+
Note: some templates does have a system schema but no default system message. Some templates
|
|
101
|
+
don't even have a system block. The default `render` will show the system schema: exemple for the Vicuna system template:
|
|
102
|
+
|
|
103
|
+
```
|
|
104
|
+
SYSTEM: {system}
|
|
105
|
+
|
|
106
|
+
USER: {prompt}
|
|
107
|
+
|
|
108
|
+
### ASSISTANT:
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
In case of empty system message it is possible to skip it using the
|
|
112
|
+
`skip_empty_system` option: outptut of `tpl.render(true)`:
|
|
113
|
+
|
|
114
|
+
```
|
|
115
|
+
USER: {prompt}
|
|
116
|
+
|
|
117
|
+
### ASSISTANT:
|
|
118
|
+
```
|
|
119
|
+
|
|
100
120
|
### Example shots
|
|
101
121
|
|
|
102
122
|
The templates have support for example shots. Add one shot:
|
package/dist/cls.d.ts
CHANGED
|
@@ -22,7 +22,7 @@ declare class PromptTemplate {
|
|
|
22
22
|
afterAssistant(msg: string): PromptTemplate;
|
|
23
23
|
replacePrompt(msg: string): PromptTemplate;
|
|
24
24
|
addShot(user: string, assistant: string): PromptTemplate;
|
|
25
|
-
render(): string;
|
|
25
|
+
render(skip_empty_system?: boolean): string;
|
|
26
26
|
prompt(msg: string): string;
|
|
27
27
|
private _buildSystemBlock;
|
|
28
28
|
private _buildUserBlock;
|
package/dist/mod.es.mjs
CHANGED
|
@@ -33,15 +33,7 @@ const templates = {
|
|
|
33
33
|
"user": 0
|
|
34
34
|
},
|
|
35
35
|
"prefix": "<s>",
|
|
36
|
-
|
|
37
|
-
"llama_instruct": {
|
|
38
|
-
"id": "llama_instruct",
|
|
39
|
-
"name": "Llama instruct",
|
|
40
|
-
"user": "[INST] {prompt}",
|
|
41
|
-
"assistant": " [/INST]",
|
|
42
|
-
"linebreaks": {
|
|
43
|
-
"user": 1
|
|
44
|
-
},
|
|
36
|
+
"stop": ["</s>"],
|
|
45
37
|
},
|
|
46
38
|
"amazon": {
|
|
47
39
|
"id": "amazon",
|
|
@@ -92,6 +84,7 @@ const templates = {
|
|
|
92
84
|
"user": "USER: {prompt}",
|
|
93
85
|
"assistant": "### ASSISTANT:",
|
|
94
86
|
"linebreaks": {
|
|
87
|
+
"system": 2,
|
|
95
88
|
"user": 2
|
|
96
89
|
},
|
|
97
90
|
},
|
|
@@ -154,6 +147,10 @@ const templates = {
|
|
|
154
147
|
},
|
|
155
148
|
"user": "USER: {prompt}",
|
|
156
149
|
"assistant": "ASSISTANT:",
|
|
150
|
+
"linebreaks": {
|
|
151
|
+
"system": 1,
|
|
152
|
+
"user": 1,
|
|
153
|
+
},
|
|
157
154
|
},
|
|
158
155
|
"wizardlm": {
|
|
159
156
|
"id": "wizardlm",
|
|
@@ -177,21 +174,7 @@ const templates = {
|
|
|
177
174
|
"user": 2,
|
|
178
175
|
"assistant": 1
|
|
179
176
|
},
|
|
180
|
-
}
|
|
181
|
-
"coding_assistant": {
|
|
182
|
-
"id": "coding_assistant",
|
|
183
|
-
"name": "Coding assistant",
|
|
184
|
-
"system": {
|
|
185
|
-
"schema": "{system}",
|
|
186
|
-
"message": "You are a coding assistant that will help the user to resolve the following instruction:"
|
|
187
|
-
},
|
|
188
|
-
"user": "### Instruction: {prompt}",
|
|
189
|
-
"assistant": "### Solution:",
|
|
190
|
-
"linebreaks": {
|
|
191
|
-
"user": 2,
|
|
192
|
-
"system": 1,
|
|
193
|
-
},
|
|
194
|
-
},
|
|
177
|
+
}
|
|
195
178
|
};
|
|
196
179
|
|
|
197
180
|
class PromptTemplate {
|
|
@@ -258,17 +241,23 @@ class PromptTemplate {
|
|
|
258
241
|
user: this.user,
|
|
259
242
|
assistant: this.assistant,
|
|
260
243
|
};
|
|
244
|
+
if (this?.prefix) {
|
|
245
|
+
res.prefix = this.prefix;
|
|
246
|
+
}
|
|
261
247
|
if (this?.system) {
|
|
262
|
-
|
|
248
|
+
res.system = this.system;
|
|
263
249
|
}
|
|
264
250
|
if (this?.shots) {
|
|
265
|
-
|
|
251
|
+
res.shots = this.shots;
|
|
252
|
+
}
|
|
253
|
+
if (this?.afterShot) {
|
|
254
|
+
res.afterShot = this.afterShot;
|
|
266
255
|
}
|
|
267
256
|
if (this?.stop) {
|
|
268
|
-
|
|
257
|
+
res.stop = this.stop;
|
|
269
258
|
}
|
|
270
259
|
if (this?.linebreaks) {
|
|
271
|
-
|
|
260
|
+
res.linebreaks = this.linebreaks;
|
|
272
261
|
}
|
|
273
262
|
return res;
|
|
274
263
|
}
|
|
@@ -305,12 +294,12 @@ class PromptTemplate {
|
|
|
305
294
|
});
|
|
306
295
|
return this;
|
|
307
296
|
}
|
|
308
|
-
render() {
|
|
297
|
+
render(skip_empty_system = false) {
|
|
309
298
|
const buf = new Array();
|
|
310
299
|
if (this.prefix) {
|
|
311
300
|
buf.push(this.prefix);
|
|
312
301
|
}
|
|
313
|
-
const _systemBlock = this._buildSystemBlock();
|
|
302
|
+
const _systemBlock = this._buildSystemBlock(skip_empty_system);
|
|
314
303
|
if (_systemBlock.length > 0) {
|
|
315
304
|
buf.push(_systemBlock);
|
|
316
305
|
if (this?.linebreaks?.system) {
|
|
@@ -337,7 +326,7 @@ class PromptTemplate {
|
|
|
337
326
|
prompt(msg) {
|
|
338
327
|
return this.render().replace("{prompt}", msg);
|
|
339
328
|
}
|
|
340
|
-
_buildSystemBlock() {
|
|
329
|
+
_buildSystemBlock(skip_empty_system = false) {
|
|
341
330
|
let res = "";
|
|
342
331
|
if (!this?.system) {
|
|
343
332
|
return "";
|
|
@@ -351,6 +340,9 @@ class PromptTemplate {
|
|
|
351
340
|
res = res + this._extraSystem;
|
|
352
341
|
}
|
|
353
342
|
}
|
|
343
|
+
else if (!skip_empty_system) {
|
|
344
|
+
res = this.system.schema;
|
|
345
|
+
}
|
|
354
346
|
return res;
|
|
355
347
|
}
|
|
356
348
|
_buildUserBlock(msg) {
|
package/dist/mod.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var $tpl=function(s){"use strict";const t={none:{id:"none",name:"No template",user:"{prompt}",assistant:""},alpaca:{id:"alpaca",name:"Alpaca",system:{schema:"{system}",message:"Below is an instruction that describes a task. Write a response that appropriately completes the request."},user:"### Instruction:\n{prompt}",assistant:"### Response:",linebreaks:{system:2,user:2}},llama:{id:"llama",name:"Llama",system:{schema:"[INST] <<SYS>>\n{system}\n<</SYS>>",message:"You are a helpful, respectful and honest assistant. Always answer as helpfully as possible\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."},user:"{prompt}",assistant:" [/INST] ",linebreaks:{system:2,user:0},prefix:"<s>"
|
|
1
|
+
var $tpl=function(s){"use strict";const t={none:{id:"none",name:"No template",user:"{prompt}",assistant:""},alpaca:{id:"alpaca",name:"Alpaca",system:{schema:"{system}",message:"Below is an instruction that describes a task. Write a response that appropriately completes the request."},user:"### Instruction:\n{prompt}",assistant:"### Response:",linebreaks:{system:2,user:2}},llama:{id:"llama",name:"Llama",system:{schema:"[INST] <<SYS>>\n{system}\n<</SYS>>",message:"You are a helpful, respectful and honest assistant. Always answer as helpfully as possible\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."},user:"{prompt}",assistant:" [/INST] ",linebreaks:{system:2,user:0},prefix:"<s>",stop:["</s>"]},amazon:{id:"amazon",name:"Amazon",user:"<|prompter|>{prompt}</s>",assistant:"<|assistant|>",afterShot:"\n",stop:["</s>"]},mistral:{id:"mistral",name:"Mistral",user:"[INST] {prompt}",assistant:" [/INST]",stop:["</s>"],afterShot:"\n",prefix:"<s>"},orca:{id:"orca",name:"Orca",system:{schema:"### System:\n{system}",message:"You are an AI assistant that follows instruction extremely well. Help as much as you can."},user:"### User:\n{prompt}",assistant:"### Response:",linebreaks:{system:2,user:2}},vicuna:{id:"vicuna",name:"Vicuna",user:"USER: {prompt}",assistant:"### ASSISTANT:",linebreaks:{user:2}},vicuna_system:{id:"vicuna_system",name:"Vicuna system",system:{schema:"SYSTEM: {system}"},user:"USER: {prompt}",assistant:"### ASSISTANT:",linebreaks:{system:2,user:2}},wizard_vicuna:{id:"wizard_vicuna",name:"Wizard Vicuna",user:"### Human:\n{prompt}",assistant:"### ASSISTANT:",linebreaks:{user:2},stop:["<|endoftext|>"]},guanaco:{id:"guanaco",name:"Guanaco",user:"### Human: {prompt}",assistant:"### Assistant:",linebreaks:{user:1}},chatml:{id:"chatml",name:"ChatMl",system:{schema:"<|im_start|>system\n{system}\n<|im_end|>"},user:"<|im_start|>user\n{prompt}<|im_end|>",assistant:"<|im_start|>assistant",linebreaks:{system:1,user:1,assistant:1},stop:["<|im_end|>"],afterShot:" <|im_end|>"},zephyr:{id:"zephyr",name:"Zephyr",system:{schema:"<|system|>\n{system}</s>"},user:"<|user|>\n{prompt}</s>",assistant:"<|assistant|>",linebreaks:{system:1,user:1,assistant:1},afterShot:"\n"},"synthia-cot":{id:"synthia-cot",name:"Synthia CoT",system:{schema:"SYSTEM: {system}",message:"Elaborate on the topic using a Tree of Thoughts and backtrack when necessary to construct a clear, cohesive Chain of Thought reasoning. Always answer without hesitation."},user:"USER: {prompt}",assistant:"ASSISTANT:",linebreaks:{system:1,user:1}},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}}};class e{id;name;user;assistant;system;shots;stop;linebreaks;afterShot;prefix;_extraSystem="";_extraAssistant="";_replacePrompt="";_replaceSystem="";constructor(s){let t;t="string"==typeof s?this._load(s):s,this.id=t.id,this.name=t.name,this.user=t.user,this.assistant=t.assistant,this.system=t.system,this.shots=t.shots,this.stop=t.stop,this.linebreaks=t.linebreaks,this.afterShot=t.afterShot,this.prefix=t.prefix}cloneTo(s,t=!0){const a=new e(s);return t&&this?.shots&&this.shots.forEach((s=>{a.addShot(s.user,s.assistant)})),this._extraSystem.length>0&&a.afterSystem(this._extraSystem),this._replaceSystem.length>0&&a.replaceSystem(this._replaceSystem),this._extraAssistant.length>0&&a.afterAssistant(this._extraAssistant),this._replacePrompt.length>0&&a.replacePrompt(this._replacePrompt),a}toJson(){const s={id:this.id,name:this.name,user:this.user,assistant:this.assistant};return this?.prefix&&(s.prefix=this.prefix),this?.system&&(s.system=this.system),this?.shots&&(s.shots=this.shots),this?.afterShot&&(s.afterShot=this.afterShot),this?.stop&&(s.stop=this.stop),this?.linebreaks&&(s.linebreaks=this.linebreaks),s}replaceSystem(s){return this.system?(this._replaceSystem=s,this):this}afterSystem(s){return this.system?(this._extraSystem=s,this):this}afterAssistant(s){return this._extraAssistant=s,this}replacePrompt(s){return this._replacePrompt=s,this}addShot(s,t){this?.shots||(this.shots=[]);let e=t;return this.shots.push({user:s,assistant:e}),this}render(s=!1){const t=new Array;this.prefix&&t.push(this.prefix);const e=this._buildSystemBlock(s);if(e.length>0&&(t.push(e),this?.linebreaks?.system&&t.push("\n".repeat(this.linebreaks.system))),this?.shots)for(const s of this.shots){t.push(this._buildUserBlock(s.user));let e=s.assistant;this.afterShot?e+=this.afterShot:e+="\n\n",t.push(this._buildAssistantBlock(e))}return t.push(this._buildUserBlock()),t.push(this._buildAssistantBlock()),t.join("")}prompt(s){return this.render().replace("{prompt}",s)}_buildSystemBlock(s=!1){let t="";return this?.system?(this._replaceSystem&&(this.system.message=this._replaceSystem),this.system?.message?(t=this.system.schema.replace("{system}",this.system.message),this._extraSystem&&(t+=this._extraSystem)):s||(t=this.system.schema),t):""}_buildUserBlock(s){let t=[],e=this.user;return this._replacePrompt.length>0&&(e=e.replace("{prompt}",this._replacePrompt)),t.push(e),this?.linebreaks?.user&&t.push("\n".repeat(this.linebreaks.user)),s&&(t[0]=e.replace("{prompt}",s)),t.join("")}_buildAssistantBlock(s){let t=[],e=this.assistant;return this._extraAssistant.length>0&&(e+=this._extraAssistant),t.push(e),this?.linebreaks?.assistant&&t.push("\n".repeat(this.linebreaks.assistant)),s&&t.push(s),t.join("")}_load(s){try{if(s in t)return t[s];throw new Error(`Template ${s} not found`)}catch(t){throw new Error(`Error loading template ${s}: ${t}`)}}}return s.PromptTemplate=e,s.templates=t,s}({});
|