@prompty/core 0.1.4 → 2.0.0-alpha.1
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 +105 -2
- package/dist/index.cjs +5125 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2327 -0
- package/dist/index.d.ts +2324 -82
- package/dist/index.js +4964 -374
- package/dist/index.js.map +1 -0
- package/package.json +61 -51
- package/dist/index.d.mts +0 -85
- package/dist/index.mjs +0 -402
package/package.json
CHANGED
|
@@ -1,51 +1,61 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@prompty/core",
|
|
3
|
-
"version": "0.1
|
|
4
|
-
"description": "Prompty core
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
"
|
|
50
|
-
|
|
51
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@prompty/core",
|
|
3
|
+
"version": "2.0.0-alpha.1",
|
|
4
|
+
"description": "Prompty core runtime — load, render, parse, and trace .prompty files",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsup",
|
|
21
|
+
"dev": "tsup --watch",
|
|
22
|
+
"test": "vitest run",
|
|
23
|
+
"test:watch": "vitest",
|
|
24
|
+
"lint": "tsc --noEmit",
|
|
25
|
+
"clean": "rimraf dist"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"prompty",
|
|
29
|
+
"llm",
|
|
30
|
+
"prompt-engineering",
|
|
31
|
+
"ai"
|
|
32
|
+
],
|
|
33
|
+
"author": "Microsoft",
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "git+https://github.com/microsoft/prompty.git",
|
|
41
|
+
"directory": "runtime/typescript/packages/core"
|
|
42
|
+
},
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=18.0.0"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"dotenv": "^16.4.0",
|
|
48
|
+
"gray-matter": "^4.0.3",
|
|
49
|
+
"mustache": "^4.2.0",
|
|
50
|
+
"nunjucks": "^3.2.4",
|
|
51
|
+
"yaml": "^2.6.0"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@types/mustache": "^4.2.5",
|
|
55
|
+
"@types/node": "^20.11.0",
|
|
56
|
+
"@types/nunjucks": "^3.2.6",
|
|
57
|
+
"tsup": "^8.4.0",
|
|
58
|
+
"typescript": "^5.7.0",
|
|
59
|
+
"vitest": "^3.0.0"
|
|
60
|
+
}
|
|
61
|
+
}
|
package/dist/index.d.mts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
declare class PropertySettings {
|
|
2
|
-
type: string;
|
|
3
|
-
default: any;
|
|
4
|
-
description: string;
|
|
5
|
-
}
|
|
6
|
-
declare class ModelConfiguration {
|
|
7
|
-
type: string;
|
|
8
|
-
[key: string]: any;
|
|
9
|
-
}
|
|
10
|
-
declare class ModelSettings {
|
|
11
|
-
api: string;
|
|
12
|
-
configuration: ModelConfiguration;
|
|
13
|
-
parameters: any;
|
|
14
|
-
response: any;
|
|
15
|
-
}
|
|
16
|
-
declare class TemplateSettings {
|
|
17
|
-
type: string;
|
|
18
|
-
parser: string;
|
|
19
|
-
}
|
|
20
|
-
interface ExecutionOptions {
|
|
21
|
-
configuration?: any;
|
|
22
|
-
parameters?: any;
|
|
23
|
-
raw?: boolean;
|
|
24
|
-
connection?: any;
|
|
25
|
-
}
|
|
26
|
-
declare class Prompty {
|
|
27
|
-
name: string;
|
|
28
|
-
description: string;
|
|
29
|
-
authors: string[];
|
|
30
|
-
tags: string[];
|
|
31
|
-
version: string;
|
|
32
|
-
base: string;
|
|
33
|
-
basePrompty?: Prompty;
|
|
34
|
-
model: ModelSettings;
|
|
35
|
-
sample: any;
|
|
36
|
-
input: {
|
|
37
|
-
[key: string]: PropertySettings;
|
|
38
|
-
};
|
|
39
|
-
output: {
|
|
40
|
-
[key: string]: PropertySettings;
|
|
41
|
-
};
|
|
42
|
-
template: TemplateSettings;
|
|
43
|
-
file: string;
|
|
44
|
-
content: string;
|
|
45
|
-
constructor(content: string);
|
|
46
|
-
private static _findGlobalConfig;
|
|
47
|
-
static load(filePath: string, configuration?: string): Promise<any>;
|
|
48
|
-
static prepare(prompt: Prompty, inputs?: any): Promise<any>;
|
|
49
|
-
static prepareSync(prompt: Prompty, inputs?: any): any;
|
|
50
|
-
static run(prompt: Prompty, inputs?: any, options?: ExecutionOptions): Promise<any>;
|
|
51
|
-
static export(prompt: Prompty): string;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
declare abstract class Invoker {
|
|
55
|
-
prompty: Prompty;
|
|
56
|
-
constructor(prompty: Prompty);
|
|
57
|
-
abstract invoke(data: any): Promise<any>;
|
|
58
|
-
abstract invokeSync(data: any): any;
|
|
59
|
-
call(data: any): Promise<any>;
|
|
60
|
-
callSync(data: any): any;
|
|
61
|
-
}
|
|
62
|
-
type InvokerConstructor = new (prompty: Prompty) => Invoker;
|
|
63
|
-
type InvokerType = "renderer" | "parser" | "executor" | "processor";
|
|
64
|
-
declare class InvokerFactory {
|
|
65
|
-
private static _instance;
|
|
66
|
-
private _invokers;
|
|
67
|
-
register(type: InvokerType, name: string, invokerClass: InvokerConstructor): void;
|
|
68
|
-
create(type: InvokerType, name: string, prompty: Prompty): Invoker;
|
|
69
|
-
private static getName;
|
|
70
|
-
static getInstance(): InvokerFactory;
|
|
71
|
-
call(type: InvokerType, prompty: Prompty, data: any, alt?: any): Promise<any>;
|
|
72
|
-
callRenderer(prompty: Prompty, data: any, alt?: any): Promise<any>;
|
|
73
|
-
callParser(prompty: Prompty, data: any, alt?: any): Promise<any>;
|
|
74
|
-
callExecutor(prompty: Prompty, data: any, alt?: any): Promise<any>;
|
|
75
|
-
callProcessor(prompty: Prompty, data: any, alt?: any): Promise<any>;
|
|
76
|
-
callSync(type: InvokerType, prompty: Prompty, data: any, alt?: any): any;
|
|
77
|
-
callRendererSync(prompty: Prompty, data: any, alt?: any): any;
|
|
78
|
-
callParserSync(prompty: Prompty, data: any, alt?: any): any;
|
|
79
|
-
callExecutorSync(prompty: Prompty, data: any, alt?: any): any;
|
|
80
|
-
callProcessorSync(prompty: Prompty, data: any, alt?: any): any;
|
|
81
|
-
toDict(): Record<string, Record<string, string>>;
|
|
82
|
-
toJson(): string;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export { Invoker, InvokerFactory, ModelConfiguration, Prompty };
|
package/dist/index.mjs
DELETED
|
@@ -1,402 +0,0 @@
|
|
|
1
|
-
// src/core.ts
|
|
2
|
-
import matter from "gray-matter";
|
|
3
|
-
|
|
4
|
-
// src/utils.ts
|
|
5
|
-
import * as fs from "fs/promises";
|
|
6
|
-
var utils = class _utils {
|
|
7
|
-
static async importModuleSync(moduleName) {
|
|
8
|
-
const importedModule = import(moduleName);
|
|
9
|
-
return importedModule;
|
|
10
|
-
}
|
|
11
|
-
static isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
|
|
12
|
-
static async readFileSafe(filePath, encoding = "utf-8") {
|
|
13
|
-
if (_utils.isNode) {
|
|
14
|
-
const data = await fs.readFile(filePath, encoding);
|
|
15
|
-
return data;
|
|
16
|
-
} else {
|
|
17
|
-
throw new Error("Load from file not supported in browser");
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
static paramHoisting(top, bottom, topKey = null) {
|
|
21
|
-
let newDict = {};
|
|
22
|
-
if (topKey) {
|
|
23
|
-
newDict = topKey in top ? { ...top[topKey] } : {};
|
|
24
|
-
} else {
|
|
25
|
-
newDict = { ...top };
|
|
26
|
-
}
|
|
27
|
-
for (const key in bottom) {
|
|
28
|
-
if (!(key in newDict)) {
|
|
29
|
-
newDict[key] = bottom[key];
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return newDict;
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
// src/invokerFactory.ts
|
|
37
|
-
var Invoker = class {
|
|
38
|
-
prompty;
|
|
39
|
-
constructor(prompty) {
|
|
40
|
-
this.prompty = prompty;
|
|
41
|
-
}
|
|
42
|
-
async call(data) {
|
|
43
|
-
return await this.invoke(data);
|
|
44
|
-
}
|
|
45
|
-
callSync(data) {
|
|
46
|
-
return this.invokeSync(data);
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
var NoOpInvoker = class extends Invoker {
|
|
50
|
-
async invoke(data) {
|
|
51
|
-
return Promise.resolve(data);
|
|
52
|
-
}
|
|
53
|
-
invokeSync(data) {
|
|
54
|
-
return data;
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
var InvokerFactory = class _InvokerFactory {
|
|
58
|
-
static _instance;
|
|
59
|
-
_invokers = {
|
|
60
|
-
renderer: { ["NOOP"]: NoOpInvoker },
|
|
61
|
-
parser: { ["NOOP"]: NoOpInvoker },
|
|
62
|
-
executor: { ["NOOP"]: NoOpInvoker },
|
|
63
|
-
processor: { ["NOOP"]: NoOpInvoker }
|
|
64
|
-
};
|
|
65
|
-
register(type, name, invokerClass) {
|
|
66
|
-
if (!this._invokers[type]) {
|
|
67
|
-
throw new Error(`Invalid invoker type: ${type}`);
|
|
68
|
-
}
|
|
69
|
-
this._invokers[type][name] = invokerClass;
|
|
70
|
-
}
|
|
71
|
-
create(type, name, prompty) {
|
|
72
|
-
const invokerClass = this._invokers[type][name];
|
|
73
|
-
if (!invokerClass) {
|
|
74
|
-
throw new Error(`No registered ${type} for name: ${name}`);
|
|
75
|
-
}
|
|
76
|
-
return new invokerClass(prompty);
|
|
77
|
-
}
|
|
78
|
-
static getName(type, prompty) {
|
|
79
|
-
switch (type) {
|
|
80
|
-
case "renderer":
|
|
81
|
-
return prompty.template.type;
|
|
82
|
-
case "parser":
|
|
83
|
-
return `${prompty.template.parser}.${prompty.model.api}`;
|
|
84
|
-
case "executor":
|
|
85
|
-
return prompty.model.configuration.type;
|
|
86
|
-
case "processor":
|
|
87
|
-
return prompty.model.configuration.type;
|
|
88
|
-
default:
|
|
89
|
-
throw new Error(`Invalid invoker type: ${type}`);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
static getInstance() {
|
|
93
|
-
if (!this._instance) {
|
|
94
|
-
this._instance = new _InvokerFactory();
|
|
95
|
-
}
|
|
96
|
-
return this._instance;
|
|
97
|
-
}
|
|
98
|
-
async call(type, prompty, data, alt) {
|
|
99
|
-
const name = _InvokerFactory.getName(type, prompty);
|
|
100
|
-
if (name.startsWith("NOOP") && alt) {
|
|
101
|
-
return alt;
|
|
102
|
-
} else if (name.startsWith("NOOP")) {
|
|
103
|
-
return data;
|
|
104
|
-
}
|
|
105
|
-
const invoker = this.create(type, name, prompty);
|
|
106
|
-
return await invoker.call(data);
|
|
107
|
-
}
|
|
108
|
-
async callRenderer(prompty, data, alt) {
|
|
109
|
-
return await this.call("renderer", prompty, data, alt);
|
|
110
|
-
}
|
|
111
|
-
async callParser(prompty, data, alt) {
|
|
112
|
-
return await this.call("parser", prompty, data, alt);
|
|
113
|
-
}
|
|
114
|
-
async callExecutor(prompty, data, alt) {
|
|
115
|
-
return await this.call("executor", prompty, data, alt);
|
|
116
|
-
}
|
|
117
|
-
async callProcessor(prompty, data, alt) {
|
|
118
|
-
return await this.call("processor", prompty, data, alt);
|
|
119
|
-
}
|
|
120
|
-
callSync(type, prompty, data, alt) {
|
|
121
|
-
const name = _InvokerFactory.getName(type, prompty);
|
|
122
|
-
if (name.startsWith("NOOP") && alt) {
|
|
123
|
-
return alt;
|
|
124
|
-
} else if (name.startsWith("NOOP")) {
|
|
125
|
-
return data;
|
|
126
|
-
}
|
|
127
|
-
const invoker = this.create(type, name, prompty);
|
|
128
|
-
return invoker.callSync(data);
|
|
129
|
-
}
|
|
130
|
-
callRendererSync(prompty, data, alt) {
|
|
131
|
-
return this.callSync("renderer", prompty, data, alt);
|
|
132
|
-
}
|
|
133
|
-
callParserSync(prompty, data, alt) {
|
|
134
|
-
return this.callSync("parser", prompty, data, alt);
|
|
135
|
-
}
|
|
136
|
-
callExecutorSync(prompty, data, alt) {
|
|
137
|
-
return this.callSync("executor", prompty, data, alt);
|
|
138
|
-
}
|
|
139
|
-
callProcessorSync(prompty, data, alt) {
|
|
140
|
-
return this.callSync("processor", prompty, data, alt);
|
|
141
|
-
}
|
|
142
|
-
toDict() {
|
|
143
|
-
const dict = {};
|
|
144
|
-
for (const [type, invokers] of Object.entries(this._invokers)) {
|
|
145
|
-
dict[type] = Object.fromEntries(
|
|
146
|
-
Object.entries(invokers).map(([key, value]) => [key, value.name])
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
return dict;
|
|
150
|
-
}
|
|
151
|
-
toJson() {
|
|
152
|
-
return JSON.stringify(this.toDict());
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
// src/parsers.ts
|
|
157
|
-
import * as path from "path";
|
|
158
|
-
var PromptyChatParser = class extends Invoker {
|
|
159
|
-
roles;
|
|
160
|
-
constructor(prompty) {
|
|
161
|
-
super(prompty);
|
|
162
|
-
this.roles = ["assistant", "function", "system", "user"];
|
|
163
|
-
}
|
|
164
|
-
async inlineImage(imageItem) {
|
|
165
|
-
if (imageItem.startsWith("http") || imageItem.startsWith("data")) {
|
|
166
|
-
return imageItem;
|
|
167
|
-
} else {
|
|
168
|
-
if (utils.isNode) {
|
|
169
|
-
const imagePath = path.join(path.dirname(this.prompty.file), imageItem);
|
|
170
|
-
const fileContent = await utils.readFileSafe(imagePath, "base64");
|
|
171
|
-
const extension = path.extname(imagePath).toLowerCase();
|
|
172
|
-
switch (extension) {
|
|
173
|
-
case ".png":
|
|
174
|
-
return `data:image/png;base64,${fileContent}`;
|
|
175
|
-
case ".jpg":
|
|
176
|
-
case ".jpeg":
|
|
177
|
-
return `data:image/jpeg;base64,${fileContent}`;
|
|
178
|
-
default:
|
|
179
|
-
throw new Error(`Invalid image format ${extension} - currently only .png and .jpg/.jpeg are supported.`);
|
|
180
|
-
}
|
|
181
|
-
} else {
|
|
182
|
-
throw new Error("Load from file not supported in browser");
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
async parseContent(content, role) {
|
|
187
|
-
const imageRegex = /!\[(.*?)\]\((.*?)\)/gm;
|
|
188
|
-
let matches;
|
|
189
|
-
let contentItems = [];
|
|
190
|
-
let contentChunks = content.split(imageRegex);
|
|
191
|
-
for (let index = 0; index < contentChunks.length; index++) {
|
|
192
|
-
const chunk = contentChunks[index];
|
|
193
|
-
if (index % 3 === 0 && chunk.trim()) {
|
|
194
|
-
contentItems.push({ type: "text", text: chunk.trim() });
|
|
195
|
-
} else if (index % 3 === 2) {
|
|
196
|
-
const base64Str = await this.inlineImage(chunk.split(" ")[0].trim());
|
|
197
|
-
let msg = {
|
|
198
|
-
type: "image_url",
|
|
199
|
-
image_url: { url: base64Str }
|
|
200
|
-
};
|
|
201
|
-
contentItems.push(msg);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
return contentItems;
|
|
205
|
-
}
|
|
206
|
-
invokeSync(data) {
|
|
207
|
-
throw new Error("Not Supported");
|
|
208
|
-
}
|
|
209
|
-
async invoke(data) {
|
|
210
|
-
let messages = [];
|
|
211
|
-
const separator = new RegExp(`\\s*#?\\s*(${this.roles.join("|")})\\s*:\\s*\\n`, "im");
|
|
212
|
-
let chunks = data.replaceAll("\r\n", "\n").split(separator).filter((chunk) => chunk.trim());
|
|
213
|
-
if (!this.roles.includes(chunks[0].trim().toLowerCase())) {
|
|
214
|
-
chunks.unshift("system");
|
|
215
|
-
}
|
|
216
|
-
if (this.roles.includes(chunks[chunks.length - 1].trim().toLowerCase())) {
|
|
217
|
-
chunks.pop();
|
|
218
|
-
}
|
|
219
|
-
if (chunks.length % 2 !== 0) {
|
|
220
|
-
throw new Error("Invalid prompt format");
|
|
221
|
-
}
|
|
222
|
-
for (let i = 0; i < chunks.length; i += 2) {
|
|
223
|
-
const role = chunks[i].trim().toLowerCase();
|
|
224
|
-
const content = chunks[i + 1].trim();
|
|
225
|
-
const parsedContent = await this.parseContent(content, role);
|
|
226
|
-
if (parsedContent.length == 1 && parsedContent[0].type == "text") {
|
|
227
|
-
messages.push({ role, content: parsedContent[0].text });
|
|
228
|
-
} else {
|
|
229
|
-
messages.push({ role, content: parsedContent });
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
return messages;
|
|
233
|
-
}
|
|
234
|
-
};
|
|
235
|
-
var factory = InvokerFactory.getInstance();
|
|
236
|
-
factory.register("parser", "prompty.chat", PromptyChatParser);
|
|
237
|
-
factory.register("parser", "prompty.embedding", NoOpInvoker);
|
|
238
|
-
factory.register("parser", "prompty.image", NoOpInvoker);
|
|
239
|
-
factory.register("parser", "prompty.completion", NoOpInvoker);
|
|
240
|
-
|
|
241
|
-
// src/renderers.ts
|
|
242
|
-
import * as nunjucks from "nunjucks";
|
|
243
|
-
import * as mustache from "mustache";
|
|
244
|
-
var NunjucksRenderer = class extends Invoker {
|
|
245
|
-
templates = {};
|
|
246
|
-
//private name: string;
|
|
247
|
-
async invoke(data) {
|
|
248
|
-
return Promise.resolve(this.invokeSync(data));
|
|
249
|
-
}
|
|
250
|
-
invokeSync(data) {
|
|
251
|
-
return nunjucks.renderString(this.prompty.content, data);
|
|
252
|
-
}
|
|
253
|
-
};
|
|
254
|
-
var MustacheRenderer = class extends Invoker {
|
|
255
|
-
templates = {};
|
|
256
|
-
//private name: string;
|
|
257
|
-
async invoke(data) {
|
|
258
|
-
return Promise.resolve(this.invokeSync(data));
|
|
259
|
-
}
|
|
260
|
-
invokeSync(data) {
|
|
261
|
-
return mustache.render(this.prompty.content, data);
|
|
262
|
-
}
|
|
263
|
-
};
|
|
264
|
-
var factory2 = InvokerFactory.getInstance();
|
|
265
|
-
factory2.register("renderer", "jinja2", NunjucksRenderer);
|
|
266
|
-
factory2.register("renderer", "mustache", MustacheRenderer);
|
|
267
|
-
|
|
268
|
-
// src/core.ts
|
|
269
|
-
import * as yaml from "js-yaml";
|
|
270
|
-
import path2 from "path";
|
|
271
|
-
import { glob } from "glob";
|
|
272
|
-
var ModelConfiguration = class {
|
|
273
|
-
type = "unknown";
|
|
274
|
-
};
|
|
275
|
-
var ModelSettings = class {
|
|
276
|
-
api = "chat";
|
|
277
|
-
configuration = { type: "unknown" };
|
|
278
|
-
parameters = {};
|
|
279
|
-
response = {};
|
|
280
|
-
};
|
|
281
|
-
var TemplateSettings = class {
|
|
282
|
-
type = "jinja2";
|
|
283
|
-
parser = "prompty";
|
|
284
|
-
};
|
|
285
|
-
var Prompty = class _Prompty {
|
|
286
|
-
// metadata
|
|
287
|
-
name = "";
|
|
288
|
-
description = "";
|
|
289
|
-
authors = [];
|
|
290
|
-
tags = [];
|
|
291
|
-
version = "";
|
|
292
|
-
base = "";
|
|
293
|
-
basePrompty;
|
|
294
|
-
// model
|
|
295
|
-
model = new ModelSettings();
|
|
296
|
-
// sample
|
|
297
|
-
sample = {};
|
|
298
|
-
// input / output
|
|
299
|
-
input = {};
|
|
300
|
-
output = {};
|
|
301
|
-
// template
|
|
302
|
-
template = new TemplateSettings();
|
|
303
|
-
// misc
|
|
304
|
-
file = "";
|
|
305
|
-
content = "";
|
|
306
|
-
constructor(content) {
|
|
307
|
-
const items = matter(content, {
|
|
308
|
-
engines: {
|
|
309
|
-
yaml: {
|
|
310
|
-
parse: (input) => yaml.load(input)
|
|
311
|
-
},
|
|
312
|
-
js: {
|
|
313
|
-
parse: (input) => {
|
|
314
|
-
console.log("JS execution disabled");
|
|
315
|
-
return {};
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
});
|
|
320
|
-
this.name = items.data.name || "";
|
|
321
|
-
this.description = items.data.description || "";
|
|
322
|
-
this.authors = items.data.authors || [];
|
|
323
|
-
this.tags = items.data.tags || [];
|
|
324
|
-
this.version = items.data.version || "";
|
|
325
|
-
this.base = items.data.base || "";
|
|
326
|
-
this.model.api = items.data.model?.api || "chat";
|
|
327
|
-
this.model.configuration = items.data.model?.configuration || {};
|
|
328
|
-
this.model.parameters = items.data.model?.parameters || {};
|
|
329
|
-
this.model.response = items.data.model?.response || {};
|
|
330
|
-
this.sample = items.data.sample || {};
|
|
331
|
-
this.input = items.data.input || {};
|
|
332
|
-
this.output = items.data.output || {};
|
|
333
|
-
if (items.data.template && typeof items.data.template === "string") {
|
|
334
|
-
this.template.type = items.data.template || this.template.type;
|
|
335
|
-
} else {
|
|
336
|
-
this.template.type = items.data.template?.type || this.template.type;
|
|
337
|
-
this.template.parser = items.data.template?.parser || this.template.parser;
|
|
338
|
-
}
|
|
339
|
-
this.file = content;
|
|
340
|
-
this.content = items.content;
|
|
341
|
-
}
|
|
342
|
-
static async _findGlobalConfig(promptyPath) {
|
|
343
|
-
const configs = await glob("**/prompty.json", {
|
|
344
|
-
cwd: process.cwd()
|
|
345
|
-
});
|
|
346
|
-
const filtered = configs.map((c) => path2.resolve(c)).filter((config) => config.length <= promptyPath.length).sort((a, b) => a.length - b.length);
|
|
347
|
-
if (filtered.length > 0) {
|
|
348
|
-
return filtered[filtered.length - 1];
|
|
349
|
-
} else {
|
|
350
|
-
return void 0;
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
static async load(filePath, configuration = "default") {
|
|
354
|
-
filePath = path2.resolve(filePath);
|
|
355
|
-
const p = new _Prompty(await utils.readFileSafe(filePath));
|
|
356
|
-
const c = await _Prompty._findGlobalConfig(filePath);
|
|
357
|
-
p.file = filePath;
|
|
358
|
-
return p;
|
|
359
|
-
}
|
|
360
|
-
static async prepare(prompt, inputs = {}) {
|
|
361
|
-
const invoker = InvokerFactory.getInstance();
|
|
362
|
-
inputs = utils.paramHoisting(inputs, prompt.sample);
|
|
363
|
-
const render2 = await invoker.callRenderer(prompt, inputs, prompt.content);
|
|
364
|
-
const result = await invoker.callParser(prompt, render2);
|
|
365
|
-
return result;
|
|
366
|
-
}
|
|
367
|
-
static prepareSync(prompt, inputs = {}) {
|
|
368
|
-
const invoker = InvokerFactory.getInstance();
|
|
369
|
-
inputs = utils.paramHoisting(inputs, prompt.sample);
|
|
370
|
-
const render2 = invoker.callRendererSync(prompt, inputs, prompt.content);
|
|
371
|
-
const result = invoker.callParserSync(prompt, render2);
|
|
372
|
-
return result;
|
|
373
|
-
}
|
|
374
|
-
static async run(prompt, inputs = {}, options = {}) {
|
|
375
|
-
const invoker = InvokerFactory.getInstance();
|
|
376
|
-
inputs = utils.paramHoisting(inputs, prompt.sample);
|
|
377
|
-
return {};
|
|
378
|
-
}
|
|
379
|
-
static export(prompt) {
|
|
380
|
-
const front_matter = {
|
|
381
|
-
name: prompt.name,
|
|
382
|
-
description: prompt.description,
|
|
383
|
-
authors: prompt.authors,
|
|
384
|
-
tags: prompt.tags,
|
|
385
|
-
version: prompt.version,
|
|
386
|
-
base: prompt.base,
|
|
387
|
-
model: prompt.model,
|
|
388
|
-
sample: prompt.sample,
|
|
389
|
-
input: prompt.input,
|
|
390
|
-
output: prompt.output,
|
|
391
|
-
template: prompt.template
|
|
392
|
-
};
|
|
393
|
-
const yaml_str = "---\r\n" + yaml.dump(front_matter) + "---\r\n" + prompt.content;
|
|
394
|
-
return yaml_str;
|
|
395
|
-
}
|
|
396
|
-
};
|
|
397
|
-
export {
|
|
398
|
-
Invoker,
|
|
399
|
-
InvokerFactory,
|
|
400
|
-
ModelConfiguration,
|
|
401
|
-
Prompty
|
|
402
|
-
};
|