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 +21 -0
- package/README.md +20 -6
- package/dist/cls.d.ts +25 -0
- package/dist/interfaces.d.ts +0 -1
- package/dist/main.d.ts +2 -2
- package/dist/mod.cjs.min.js +1 -1
- package/dist/mod.es.js +136 -95
- package/dist/mod.min.js +1 -1
- package/package.json +1 -1
- package/dist/template.d.ts +0 -10
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,
|
|
33
|
+
import { templates, ModTemplate } from "modprompt";
|
|
34
34
|
|
|
35
|
-
const tpl =
|
|
35
|
+
const tpl = new ModTemplate(templates.alpaca)
|
|
36
36
|
// or
|
|
37
|
-
const tpl =
|
|
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.
|
|
112
|
+
tpl.afterAssistant(" (answer in valid json)")
|
|
113
113
|
// modify the prompt message
|
|
114
|
-
tpl.
|
|
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 };
|
package/dist/interfaces.d.ts
CHANGED
package/dist/main.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { templates } from "./db.js";
|
|
2
|
-
import {
|
|
2
|
+
import { ModTemplate } from "./cls.js";
|
|
3
3
|
import type { SpacingSlots, PromptBlock, TurnBlock, LmTemplate } from "@/interfaces.js";
|
|
4
|
-
export { templates,
|
|
4
|
+
export { templates, ModTemplate };
|
|
5
5
|
export type { SpacingSlots, PromptBlock, TurnBlock, LmTemplate };
|
package/dist/mod.cjs.min.js
CHANGED
|
@@ -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.
|
|
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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
77
|
-
|
|
124
|
+
if (!this.system?.message) {
|
|
125
|
+
this.system.message = "";
|
|
78
126
|
}
|
|
79
|
-
|
|
80
|
-
|
|
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
|
-
|
|
166
|
+
prompt(msg) {
|
|
167
|
+
return this.render().replace("{prompt}", msg);
|
|
168
|
+
}
|
|
169
|
+
_buildSystemBlock(systemMsg) {
|
|
84
170
|
let res = "";
|
|
85
|
-
if (!
|
|
86
|
-
throw new Error(`The template ${
|
|
171
|
+
if (!this?.system) {
|
|
172
|
+
throw new Error(`The template ${this.name} has no system var`);
|
|
87
173
|
}
|
|
88
174
|
if (systemMsg) {
|
|
89
|
-
res =
|
|
175
|
+
res = this.system.schema.replace("{system}", systemMsg);
|
|
90
176
|
}
|
|
91
177
|
else {
|
|
92
|
-
if (
|
|
93
|
-
res =
|
|
178
|
+
if (this.system?.message) {
|
|
179
|
+
res = this.system.schema.replace("{system}", this.system.message);
|
|
94
180
|
}
|
|
95
181
|
else {
|
|
96
|
-
res =
|
|
182
|
+
res = this.system.schema;
|
|
97
183
|
}
|
|
98
184
|
}
|
|
99
185
|
return res;
|
|
100
186
|
}
|
|
101
|
-
|
|
187
|
+
_buildUserBlock(msg) {
|
|
102
188
|
let buf = [];
|
|
103
|
-
buf.push(
|
|
104
|
-
if (
|
|
105
|
-
buf.push("
|
|
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
|
-
|
|
198
|
+
_buildAssistantBlock(msg) {
|
|
116
199
|
let buf = [];
|
|
117
|
-
buf.push(
|
|
118
|
-
if (
|
|
119
|
-
buf.push("
|
|
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
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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
|
-
|
|
158
|
-
|
|
215
|
+
else {
|
|
216
|
+
throw new Error(`Template ${name} not found`);
|
|
159
217
|
}
|
|
160
218
|
}
|
|
161
|
-
|
|
162
|
-
|
|
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
|
-
|
|
168
|
-
|
|
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 {
|
|
225
|
+
export { ModTemplate, templates };
|
package/dist/mod.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var $tpl=function(s){"use strict";const
|
|
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
package/dist/template.d.ts
DELETED
|
@@ -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 };
|