modprompt 0.0.6 → 0.0.8

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 CHANGED
@@ -1,5 +1,5 @@
1
1
  import { LmTemplate, PromptBlock, TurnBlock, SpacingSlots } from "./interfaces";
2
- declare class ModTemplate implements LmTemplate {
2
+ declare class PromptTemplate implements LmTemplate {
3
3
  name: string;
4
4
  user: string;
5
5
  assistant: string;
@@ -7,14 +7,15 @@ declare class ModTemplate implements LmTemplate {
7
7
  shots?: Array<TurnBlock>;
8
8
  stop?: Array<string>;
9
9
  linebreaks?: SpacingSlots;
10
- spacing?: SpacingSlots;
11
10
  private _systemBlock;
12
11
  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;
12
+ cloneTo(name: string): PromptTemplate;
13
+ toJson(): LmTemplate;
14
+ replaceSystem(msg: string): PromptTemplate;
15
+ afterSystem(msg: string): PromptTemplate;
16
+ afterAssistant(msg: string): PromptTemplate;
17
+ replacePrompt(msg: string): PromptTemplate;
18
+ addShot(user: string, assistant: string): PromptTemplate;
18
19
  render(): string;
19
20
  prompt(msg: string): string;
20
21
  private _buildSystemBlock;
@@ -22,4 +23,4 @@ declare class ModTemplate implements LmTemplate {
22
23
  private _buildAssistantBlock;
23
24
  private _load;
24
25
  }
25
- export { ModTemplate };
26
+ export { PromptTemplate };
package/dist/main.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { templates } from "./db.js";
2
- import { ModTemplate } from "./cls.js";
3
- import type { SpacingSlots, PromptBlock, TurnBlock, LmTemplate } from "@/interfaces.js";
4
- export { templates, ModTemplate };
5
- export type { SpacingSlots, PromptBlock, TurnBlock, LmTemplate };
2
+ import { PromptTemplate } from "./cls.js";
3
+ import { SpacingSlots, PromptBlock, TurnBlock, LmTemplate } from "./interfaces.js";
4
+ export { templates, PromptTemplate, SpacingSlots, PromptBlock, TurnBlock, LmTemplate };
@@ -19,7 +19,7 @@ const templates = {
19
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
20
  },
21
21
  "user": "{prompt}",
22
- "assistant": " [/INST]",
22
+ "assistant": " [/INST] ",
23
23
  "linebreaks": {
24
24
  "system": 2,
25
25
  "user": 0
@@ -134,10 +134,10 @@ const templates = {
134
134
  "user": 2,
135
135
  "system": 1,
136
136
  },
137
- }
137
+ },
138
138
  };
139
139
 
140
- class ModTemplate {
140
+ class PromptTemplate {
141
141
  name;
142
142
  user;
143
143
  assistant;
@@ -145,7 +145,6 @@ class ModTemplate {
145
145
  shots;
146
146
  stop;
147
147
  linebreaks;
148
- spacing;
149
148
  _systemBlock = "";
150
149
  constructor(template) {
151
150
  let tpl;
@@ -174,6 +173,45 @@ class ModTemplate {
174
173
  this._systemBlock = this._buildSystemBlock();
175
174
  }
176
175
  }
176
+ cloneTo(name) {
177
+ const tpl = new PromptTemplate(name);
178
+ if (this?.system) {
179
+ tpl.system = this.system;
180
+ }
181
+ if (this?.shots) {
182
+ tpl.shots = this.shots;
183
+ }
184
+ if (this?.stop) {
185
+ tpl.stop = this.stop;
186
+ }
187
+ if (this?.linebreaks) {
188
+ tpl.linebreaks = this.linebreaks;
189
+ }
190
+ if (tpl?.system) {
191
+ tpl.replaceSystem(this._buildSystemBlock());
192
+ }
193
+ return tpl;
194
+ }
195
+ toJson() {
196
+ const res = {
197
+ name: this.name,
198
+ user: this.user,
199
+ assistant: this.assistant,
200
+ };
201
+ if (this?.system) {
202
+ this.system = this.system;
203
+ }
204
+ if (this?.shots) {
205
+ this.shots = this.shots;
206
+ }
207
+ if (this?.stop) {
208
+ this.stop = this.stop;
209
+ }
210
+ if (this?.linebreaks) {
211
+ this.linebreaks = this.linebreaks;
212
+ }
213
+ return res;
214
+ }
177
215
  replaceSystem(msg) {
178
216
  this._systemBlock = this._buildSystemBlock(msg);
179
217
  return this;
@@ -270,7 +308,6 @@ class ModTemplate {
270
308
  _load(name) {
271
309
  try {
272
310
  if (name in templates) {
273
- console.log("Loading", name);
274
311
  return templates[name];
275
312
  }
276
313
  else {
@@ -278,9 +315,9 @@ class ModTemplate {
278
315
  }
279
316
  }
280
317
  catch (err) {
281
- throw new Error(`Error loading template ${name}`);
318
+ throw new Error(`Error loading template ${name}: ${err}`);
282
319
  }
283
320
  }
284
321
  }
285
322
 
286
- export { ModTemplate, templates };
323
+ 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 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}({});
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}}};class e{name;user;assistant;system;shots;stop;linebreaks;_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())}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={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.6",
3
+ "version": "0.0.8",
4
4
  "description": "Prompt templates for language models",
5
5
  "license": "MIT",
6
6
  "scripts": {
@@ -22,16 +22,15 @@
22
22
  "typedoc-plugin-rename-defaults": "^0.6.5",
23
23
  "typescript": "^5.2.2"
24
24
  },
25
+ "type": "module",
25
26
  "files": [
26
27
  "dist"
27
28
  ],
28
- "main": "./dist/mod.cjs.min.js",
29
- "module": "./dist/mod.es.js",
29
+ "module": "./dist/mod.es.mjs",
30
30
  "types": "./dist/main.d.ts",
31
31
  "exports": {
32
32
  ".": {
33
- "import": "./dist/mod.es.js",
34
- "require": "./dist/mod.cjs.min.js"
33
+ "import": "./dist/mod.es.mjs"
35
34
  }
36
35
  },
37
36
  "publishConfig": {
@@ -1 +0,0 @@
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:"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}}};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),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(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;