modprompt 0.1.2 → 0.3.0

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,9 +1,10 @@
1
- import { LmTemplate, PromptBlock, TurnBlock, SpacingSlots } from "./interfaces";
1
+ import { LmTemplate, PromptBlock, TurnBlock, SpacingSlots, HistoryTurn } from "./interfaces";
2
2
  declare class PromptTemplate {
3
3
  id: string;
4
4
  name: string;
5
5
  user: string;
6
6
  assistant: string;
7
+ history: Array<HistoryTurn>;
7
8
  system?: PromptBlock;
8
9
  shots?: Array<TurnBlock>;
9
10
  stop?: Array<string>;
@@ -22,8 +23,10 @@ declare class PromptTemplate {
22
23
  afterAssistant(msg: string): PromptTemplate;
23
24
  replacePrompt(msg: string): PromptTemplate;
24
25
  addShot(user: string, assistant: string): PromptTemplate;
26
+ renderShot(shot: TurnBlock | HistoryTurn): string;
25
27
  render(skip_empty_system?: boolean): string;
26
28
  prompt(msg: string): string;
29
+ pushToHistory(turn: HistoryTurn): PromptTemplate;
27
30
  private _buildSystemBlock;
28
31
  private _buildUserBlock;
29
32
  private _buildAssistantBlock;
@@ -23,4 +23,12 @@ interface LmTemplate {
23
23
  afterShot?: string;
24
24
  prefix?: string;
25
25
  }
26
- export { SpacingSlots, PromptBlock, TurnBlock, LmTemplate };
26
+ interface HistoryTurn {
27
+ user: string;
28
+ assistant: string;
29
+ images?: Array<{
30
+ id: string;
31
+ data: ArrayBuffer;
32
+ }>;
33
+ }
34
+ export { SpacingSlots, PromptBlock, TurnBlock, LmTemplate, HistoryTurn };
package/dist/mod.es.mjs CHANGED
@@ -165,6 +165,13 @@ const templates = {
165
165
  "user": 1
166
166
  },
167
167
  },
168
+ "openchat": {
169
+ "id": "openchat",
170
+ "name": "OpenChat",
171
+ "user": "GPT4 User: {prompt}<|end_of_turn|>",
172
+ "assistant": "GPT4 Assistant:",
173
+ "stop": ["<|end_of_turn|>"]
174
+ },
168
175
  "human_response": {
169
176
  "id": "human_response",
170
177
  "name": "Human response",
@@ -182,6 +189,7 @@ class PromptTemplate {
182
189
  name;
183
190
  user;
184
191
  assistant;
192
+ history = [];
185
193
  system;
186
194
  shots;
187
195
  stop;
@@ -294,6 +302,19 @@ class PromptTemplate {
294
302
  });
295
303
  return this;
296
304
  }
305
+ renderShot(shot) {
306
+ const buf = [];
307
+ buf.push(this._buildUserBlock(shot.user));
308
+ let _assistantMsg = shot.assistant;
309
+ if (this.afterShot) {
310
+ _assistantMsg += this.afterShot;
311
+ }
312
+ else {
313
+ _assistantMsg += "\n\n";
314
+ }
315
+ buf.push(this._buildAssistantBlock(_assistantMsg));
316
+ return buf.join("");
317
+ }
297
318
  render(skip_empty_system = false) {
298
319
  const buf = new Array();
299
320
  if (this.prefix) {
@@ -308,17 +329,12 @@ class PromptTemplate {
308
329
  }
309
330
  if (this?.shots) {
310
331
  for (const shot of this.shots) {
311
- buf.push(this._buildUserBlock(shot.user));
312
- let _assistantMsg = shot.assistant;
313
- if (this.afterShot) {
314
- _assistantMsg += this.afterShot;
315
- }
316
- else {
317
- _assistantMsg += "\n\n";
318
- }
319
- buf.push(this._buildAssistantBlock(_assistantMsg));
332
+ buf.push(this.renderShot(shot));
320
333
  }
321
334
  }
335
+ for (const turn of this.history) {
336
+ buf.push(this.renderShot(turn));
337
+ }
322
338
  buf.push(this._buildUserBlock());
323
339
  buf.push(this._buildAssistantBlock());
324
340
  return buf.join("");
@@ -326,7 +342,11 @@ class PromptTemplate {
326
342
  prompt(msg) {
327
343
  return this.render().replace("{prompt}", msg);
328
344
  }
329
- _buildSystemBlock(skip_empty_system = false) {
345
+ pushToHistory(turn) {
346
+ this.history.push(turn);
347
+ return this;
348
+ }
349
+ _buildSystemBlock(skip_empty_system) {
330
350
  let res = "";
331
351
  if (!this?.system) {
332
352
  return "";
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>",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}({});
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}},openchat:{id:"openchat",name:"OpenChat",user:"GPT4 User: {prompt}<|end_of_turn|>",assistant:"GPT4 Assistant:",stop:["<|end_of_turn|>"]},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;history=[];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}renderShot(s){const t=[];t.push(this._buildUserBlock(s.user));let e=s.assistant;return this.afterShot?e+=this.afterShot:e+="\n\n",t.push(this._buildAssistantBlock(e)),t.join("")}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.renderShot(s));for(const s of this.history)t.push(this.renderShot(s));return t.push(this._buildUserBlock()),t.push(this._buildAssistantBlock()),t.join("")}prompt(s){return this.render().replace("{prompt}",s)}pushToHistory(s){return this.history.push(s),this}_buildSystemBlock(s){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}({});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "modprompt",
3
- "version": "0.1.2",
3
+ "version": "0.3.0",
4
4
  "description": "Prompt templates for language models",
5
5
  "license": "MIT",
6
6
  "scripts": {