modprompt 0.0.9 → 0.0.11
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/dist/cls.d.ts +5 -2
- package/dist/mod.es.mjs +63 -42
- package/dist/mod.min.js +1 -1
- package/package.json +1 -1
package/dist/cls.d.ts
CHANGED
|
@@ -8,9 +8,12 @@ declare class PromptTemplate {
|
|
|
8
8
|
shots?: Array<TurnBlock>;
|
|
9
9
|
stop?: Array<string>;
|
|
10
10
|
linebreaks?: SpacingSlots;
|
|
11
|
-
|
|
11
|
+
_extraSystem: string;
|
|
12
|
+
_extraAssistant: string;
|
|
13
|
+
_replacePrompt: string;
|
|
14
|
+
_replaceSystem: string;
|
|
12
15
|
constructor(template: string | LmTemplate);
|
|
13
|
-
cloneTo(
|
|
16
|
+
cloneTo(template: string | LmTemplate, keepShots?: boolean): PromptTemplate;
|
|
14
17
|
toJson(): LmTemplate;
|
|
15
18
|
replaceSystem(msg: string): PromptTemplate;
|
|
16
19
|
afterSystem(msg: string): PromptTemplate;
|
package/dist/mod.es.mjs
CHANGED
|
@@ -109,7 +109,8 @@ const templates = {
|
|
|
109
109
|
"system": 1,
|
|
110
110
|
"user": 1,
|
|
111
111
|
"assistant": 1,
|
|
112
|
-
}
|
|
112
|
+
},
|
|
113
|
+
"stop": ["<|im_end|>"]
|
|
113
114
|
},
|
|
114
115
|
"mamba": {
|
|
115
116
|
"id": "mamba",
|
|
@@ -166,7 +167,10 @@ class PromptTemplate {
|
|
|
166
167
|
shots;
|
|
167
168
|
stop;
|
|
168
169
|
linebreaks;
|
|
169
|
-
|
|
170
|
+
_extraSystem = "";
|
|
171
|
+
_extraAssistant = "";
|
|
172
|
+
_replacePrompt = "";
|
|
173
|
+
_replaceSystem = "";
|
|
170
174
|
constructor(template) {
|
|
171
175
|
let tpl;
|
|
172
176
|
if (typeof template == "string") {
|
|
@@ -191,26 +195,27 @@ class PromptTemplate {
|
|
|
191
195
|
if (tpl?.linebreaks) {
|
|
192
196
|
this.linebreaks = tpl.linebreaks;
|
|
193
197
|
}
|
|
194
|
-
if (tpl?.system) {
|
|
195
|
-
this._systemBlock = this._buildSystemBlock();
|
|
196
|
-
}
|
|
197
198
|
}
|
|
198
|
-
cloneTo(
|
|
199
|
-
const tpl = new PromptTemplate(
|
|
200
|
-
if (
|
|
201
|
-
|
|
199
|
+
cloneTo(template, keepShots = true) {
|
|
200
|
+
const tpl = new PromptTemplate(template);
|
|
201
|
+
if (keepShots) {
|
|
202
|
+
if (this?.shots) {
|
|
203
|
+
this.shots.forEach((s) => {
|
|
204
|
+
tpl.addShot(s.user, s.assistant);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
202
207
|
}
|
|
203
|
-
if (this
|
|
204
|
-
tpl.
|
|
208
|
+
if (this._extraSystem.length > 0) {
|
|
209
|
+
tpl.afterSystem(this._extraSystem);
|
|
205
210
|
}
|
|
206
|
-
if (this
|
|
207
|
-
tpl.
|
|
211
|
+
if (this._replaceSystem.length > 0) {
|
|
212
|
+
tpl.replaceSystem(this._replaceSystem);
|
|
208
213
|
}
|
|
209
|
-
if (this
|
|
210
|
-
tpl.
|
|
214
|
+
if (this._extraAssistant.length > 0) {
|
|
215
|
+
tpl.afterAssistant(this._extraAssistant);
|
|
211
216
|
}
|
|
212
|
-
if (
|
|
213
|
-
tpl.
|
|
217
|
+
if (this._replacePrompt.length > 0) {
|
|
218
|
+
tpl.replacePrompt(this._replacePrompt);
|
|
214
219
|
}
|
|
215
220
|
return tpl;
|
|
216
221
|
}
|
|
@@ -236,41 +241,48 @@ class PromptTemplate {
|
|
|
236
241
|
return res;
|
|
237
242
|
}
|
|
238
243
|
replaceSystem(msg) {
|
|
239
|
-
|
|
244
|
+
if (!this.system) {
|
|
245
|
+
return this;
|
|
246
|
+
}
|
|
247
|
+
this._replaceSystem = msg;
|
|
240
248
|
return this;
|
|
241
249
|
}
|
|
242
250
|
afterSystem(msg) {
|
|
243
251
|
if (!this.system) {
|
|
244
|
-
|
|
245
|
-
}
|
|
246
|
-
if (!this.system?.message) {
|
|
247
|
-
this.system.message = "";
|
|
252
|
+
return this;
|
|
248
253
|
}
|
|
249
|
-
this.
|
|
254
|
+
this._extraSystem = msg;
|
|
250
255
|
return this;
|
|
251
256
|
}
|
|
252
257
|
afterAssistant(msg) {
|
|
253
|
-
this.
|
|
258
|
+
this._extraAssistant = msg;
|
|
254
259
|
return this;
|
|
255
260
|
}
|
|
256
261
|
replacePrompt(msg) {
|
|
257
|
-
this.
|
|
262
|
+
this._replacePrompt = msg;
|
|
258
263
|
return this;
|
|
259
264
|
}
|
|
260
265
|
addShot(user, assistant) {
|
|
261
266
|
if (!this?.shots) {
|
|
262
267
|
this.shots = [];
|
|
263
268
|
}
|
|
269
|
+
let _assistantMsg = assistant;
|
|
270
|
+
if (this.stop) {
|
|
271
|
+
if (this.stop.length > 0) {
|
|
272
|
+
_assistantMsg += this.stop[0];
|
|
273
|
+
}
|
|
274
|
+
}
|
|
264
275
|
this.shots.push({
|
|
265
276
|
user: user,
|
|
266
|
-
assistant:
|
|
277
|
+
assistant: _assistantMsg,
|
|
267
278
|
});
|
|
268
279
|
return this;
|
|
269
280
|
}
|
|
270
281
|
render() {
|
|
271
282
|
const buf = new Array();
|
|
272
|
-
|
|
273
|
-
|
|
283
|
+
const _systemBlock = this._buildSystemBlock();
|
|
284
|
+
if (_systemBlock.length > 0) {
|
|
285
|
+
buf.push(_systemBlock);
|
|
274
286
|
if (this?.linebreaks?.system) {
|
|
275
287
|
buf.push("\n".repeat(this.linebreaks.system));
|
|
276
288
|
}
|
|
@@ -288,43 +300,52 @@ class PromptTemplate {
|
|
|
288
300
|
prompt(msg) {
|
|
289
301
|
return this.render().replace("{prompt}", msg);
|
|
290
302
|
}
|
|
291
|
-
_buildSystemBlock(
|
|
303
|
+
_buildSystemBlock() {
|
|
292
304
|
let res = "";
|
|
293
305
|
if (!this?.system) {
|
|
294
|
-
|
|
306
|
+
return "";
|
|
307
|
+
}
|
|
308
|
+
if (this._replaceSystem) {
|
|
309
|
+
this.system.message = this._replaceSystem;
|
|
295
310
|
}
|
|
296
|
-
if (
|
|
297
|
-
res = this.system.schema.replace("{system}",
|
|
311
|
+
if (this.system?.message) {
|
|
312
|
+
res = this.system.schema.replace("{system}", this.system.message);
|
|
298
313
|
}
|
|
299
314
|
else {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
res = this.system.schema;
|
|
305
|
-
}
|
|
315
|
+
res = this.system.schema.replace("{system}", "");
|
|
316
|
+
}
|
|
317
|
+
if (this._extraSystem) {
|
|
318
|
+
res = res + this._extraSystem;
|
|
306
319
|
}
|
|
307
320
|
return res;
|
|
308
321
|
}
|
|
309
322
|
_buildUserBlock(msg) {
|
|
310
323
|
let buf = [];
|
|
311
|
-
|
|
324
|
+
let _userBlock = this.user;
|
|
325
|
+
if (this._replacePrompt.length > 0) {
|
|
326
|
+
_userBlock = _userBlock.replace("{prompt}", this._replacePrompt);
|
|
327
|
+
}
|
|
328
|
+
buf.push(_userBlock);
|
|
312
329
|
if (this?.linebreaks?.user) {
|
|
313
330
|
buf.push("\n".repeat(this.linebreaks.user));
|
|
314
331
|
}
|
|
315
332
|
if (msg) {
|
|
316
|
-
buf[0] =
|
|
333
|
+
buf[0] = _userBlock.replace("{prompt}", msg);
|
|
317
334
|
}
|
|
318
335
|
return buf.join("");
|
|
319
336
|
}
|
|
320
337
|
_buildAssistantBlock(msg) {
|
|
321
338
|
let buf = [];
|
|
322
|
-
|
|
339
|
+
let amsg = this.assistant;
|
|
340
|
+
if (this._extraAssistant.length > 0) {
|
|
341
|
+
amsg += this._extraAssistant;
|
|
342
|
+
}
|
|
343
|
+
buf.push(amsg);
|
|
323
344
|
if (this?.linebreaks?.assistant) {
|
|
324
345
|
buf.push("\n".repeat(this.linebreaks.assistant));
|
|
325
346
|
}
|
|
326
347
|
if (msg) {
|
|
327
|
-
buf[0] = buf[0] + msg
|
|
348
|
+
buf[0] = buf[0] + msg;
|
|
328
349
|
}
|
|
329
350
|
return buf.join("");
|
|
330
351
|
}
|
package/dist/mod.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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;
|
|
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},stop:["<|im_end|>"]},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="";_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,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,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?.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._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.stop&&this.stop.length>0&&(e+=this.stop[0]),this.shots.push({user:s,assistant:e}),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?(this._replaceSystem&&(this.system.message=this._replaceSystem),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=[],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[0]=t[0]+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}({});
|