@mikugg/guidance 0.16.0 → 0.17.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/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -5
- package/dist/lib/Agent.d.ts +24 -0
- package/dist/lib/Agent.d.ts.map +1 -0
- package/dist/lib/Agent.js +45 -0
- package/dist/lib/template.d.ts.map +1 -1
- package/dist/lib/template.js +4 -4
- package/dist/lib/token-generator.js +1 -1
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import * as
|
|
2
|
-
import * as TokenGenerator from "./lib/token-generator";
|
|
1
|
+
import * as Agents from "./lib/Agent";
|
|
3
2
|
import * as Template from "./lib/template";
|
|
4
|
-
|
|
3
|
+
import * as TokenGenerator from "./lib/token-generator";
|
|
4
|
+
import * as Tokenizer from "./lib/tokenizer";
|
|
5
|
+
export { Agents, Template, TokenGenerator, Tokenizer };
|
|
5
6
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,QAAQ,MAAM,gBAAgB,CAAC;AAC3C,OAAO,KAAK,cAAc,MAAM,uBAAuB,CAAC;AACxD,OAAO,KAAK,SAAS,MAAM,iBAAiB,CAAC;AAE7C,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,cAAc,EAAE,SAAS,EAAE,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -23,10 +23,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.
|
|
27
|
-
const
|
|
28
|
-
exports.
|
|
29
|
-
const TokenGenerator = __importStar(require("./lib/token-generator"));
|
|
30
|
-
exports.TokenGenerator = TokenGenerator;
|
|
26
|
+
exports.Tokenizer = exports.TokenGenerator = exports.Template = exports.Agents = void 0;
|
|
27
|
+
const Agents = __importStar(require("./lib/Agent"));
|
|
28
|
+
exports.Agents = Agents;
|
|
31
29
|
const Template = __importStar(require("./lib/template"));
|
|
32
30
|
exports.Template = Template;
|
|
31
|
+
const TokenGenerator = __importStar(require("./lib/token-generator"));
|
|
32
|
+
exports.TokenGenerator = TokenGenerator;
|
|
33
|
+
const Tokenizer = __importStar(require("./lib/tokenizer"));
|
|
34
|
+
exports.Tokenizer = Tokenizer;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface AgentPromptConfiguration<AgentInputs extends string[], AgentOutputs extends string[]> {
|
|
2
|
+
instruction: string;
|
|
3
|
+
description: string;
|
|
4
|
+
shotTemplate: {
|
|
5
|
+
input: string;
|
|
6
|
+
output: string;
|
|
7
|
+
};
|
|
8
|
+
shots: {
|
|
9
|
+
inputs: Record<AgentInputs[number], string>;
|
|
10
|
+
outputs: Record<AgentOutputs[number], string>;
|
|
11
|
+
}[];
|
|
12
|
+
instructSettings?: {
|
|
13
|
+
system?: string;
|
|
14
|
+
instruction?: string;
|
|
15
|
+
input?: string;
|
|
16
|
+
output?: string;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export declare class AgentPrompt<AgentInputs extends string[], AgentOutputs extends string[]> {
|
|
20
|
+
private configuration;
|
|
21
|
+
constructor(configuration: AgentPromptConfiguration<AgentInputs, AgentOutputs>);
|
|
22
|
+
generatePrompt(inputs: Record<AgentInputs[number], string>): string;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=Agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Agent.d.ts","sourceRoot":"","sources":["../../src/lib/Agent.ts"],"names":[],"mappings":"AAOA,MAAM,WAAW,wBAAwB,CAEvC,WAAW,SAAS,MAAM,EAAE,EAC5B,YAAY,SAAS,MAAM,EAAE;IAE7B,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE;QACZ,KAAK,EAAE,MAAM,CAAC;QACd,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;IACF,KAAK,EAAE;QACL,MAAM,EAAE,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC;QAC5C,OAAO,EAAE,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC;KAC/C,EAAE,CAAC;IACJ,gBAAgB,CAAC,EAAE;QACjB,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB,CAAC;CACH;AAED,qBAAa,WAAW,CACtB,WAAW,SAAS,MAAM,EAAE,EAC5B,YAAY,SAAS,MAAM,EAAE;IAE7B,OAAO,CAAC,aAAa,CAOnB;gBAGA,aAAa,EAAE,wBAAwB,CAAC,WAAW,EAAE,YAAY,CAAC;IAc7D,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,GAAG,MAAM;CA+B3E"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AgentPrompt = void 0;
|
|
4
|
+
function replaceTemplate(template, key, value) {
|
|
5
|
+
let regex = new RegExp(`{{GEN ${key}[^}]*}}`, "g");
|
|
6
|
+
template = template.replace(regex, value);
|
|
7
|
+
regex = new RegExp(`{{SEL ${key}[^}]*}}`, "g");
|
|
8
|
+
return template.replace(regex, value);
|
|
9
|
+
}
|
|
10
|
+
class AgentPrompt {
|
|
11
|
+
constructor(configuration) {
|
|
12
|
+
this.configuration = Object.assign(Object.assign({}, configuration), { instructSettings: Object.assign({ system: "", instruction: "\n### Instruction:\n", input: "\n### Input:\n", output: "\n### Response:\n" }, configuration.instructSettings) });
|
|
13
|
+
}
|
|
14
|
+
generatePrompt(inputs) {
|
|
15
|
+
let prompt = this.configuration.instructSettings.system;
|
|
16
|
+
prompt += this.configuration.description;
|
|
17
|
+
prompt += this.configuration.instructSettings.instruction;
|
|
18
|
+
prompt += this.configuration.instruction;
|
|
19
|
+
this.configuration.shots.forEach((shot) => {
|
|
20
|
+
prompt += this.configuration.instructSettings.input;
|
|
21
|
+
prompt += this.configuration.shotTemplate.input;
|
|
22
|
+
Object.keys(shot.inputs).forEach((key) => {
|
|
23
|
+
const value = shot.inputs[key];
|
|
24
|
+
prompt = prompt.replace(new RegExp(`{{${key}}}`, "g"), value);
|
|
25
|
+
});
|
|
26
|
+
prompt += this.configuration.instructSettings.output;
|
|
27
|
+
prompt += this.configuration.shotTemplate.output;
|
|
28
|
+
Object.keys(shot.outputs).forEach((key) => {
|
|
29
|
+
const value = shot.outputs[key];
|
|
30
|
+
prompt = prompt.replace(new RegExp(`{{${key}}}`, "g"), value);
|
|
31
|
+
prompt = replaceTemplate(prompt, key, value);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
prompt += this.configuration.instructSettings.input;
|
|
35
|
+
prompt += this.configuration.shotTemplate.input;
|
|
36
|
+
Object.keys(inputs).forEach((key) => {
|
|
37
|
+
const value = inputs[key];
|
|
38
|
+
prompt = prompt.replace(new RegExp(`{{${key}}}`, "g"), value);
|
|
39
|
+
});
|
|
40
|
+
prompt += this.configuration.instructSettings.output;
|
|
41
|
+
prompt += this.configuration.shotTemplate.output;
|
|
42
|
+
return prompt;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.AgentPrompt = AgentPrompt;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"template.d.ts","sourceRoot":"","sources":["../../src/lib/template.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAG3D,oBAAY,gBAAgB;IAC1B,GAAG,QAAQ;IACX,GAAG,QAAQ;CACZ;AAED,qBAAa,iBAAiB,CAAC,eAAe,GAAG,SAAS;IACxD,OAAO,CAAC,SAAS,CAAoB;IACrC,OAAO,CAAC,SAAS,CAA0C;gBAGzD,SAAS,EAAE,iBAAiB,EAC5B,SAAS,EAAE,sBAAsB,CAAC,eAAe,CAAC;IAM7C,YAAY,CAAC,SAAS,EAAE,iBAAiB;IAIzC,YAAY,CAAC,SAAS,EAAE,sBAAsB,CAAC,eAAe,CAAC;IAIzD,eAAe,CAC1B,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EACzC,UAAU,CAAC,EAAE,eAAe,GAC3B,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAajB,qBAAqB,CACjC,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EACzC,UAAU,CAAC,EAAE,eAAe,GAC3B,cAAc,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"template.d.ts","sourceRoot":"","sources":["../../src/lib/template.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAG3D,oBAAY,gBAAgB;IAC1B,GAAG,QAAQ;IACX,GAAG,QAAQ;CACZ;AAED,qBAAa,iBAAiB,CAAC,eAAe,GAAG,SAAS;IACxD,OAAO,CAAC,SAAS,CAAoB;IACrC,OAAO,CAAC,SAAS,CAA0C;gBAGzD,SAAS,EAAE,iBAAiB,EAC5B,SAAS,EAAE,sBAAsB,CAAC,eAAe,CAAC;IAM7C,YAAY,CAAC,SAAS,EAAE,iBAAiB;IAIzC,YAAY,CAAC,SAAS,EAAE,sBAAsB,CAAC,eAAe,CAAC;IAIzD,eAAe,CAC1B,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EACzC,UAAU,CAAC,EAAE,eAAe,GAC3B,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAajB,qBAAqB,CACjC,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EACzC,UAAU,CAAC,EAAE,eAAe,GAC3B,cAAc,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,IAAI,CAAC;IA0I5C,OAAO,CAAC,cAAc;CAQvB"}
|
package/dist/lib/template.js
CHANGED
|
@@ -122,9 +122,10 @@ class TemplateProcessor {
|
|
|
122
122
|
if (!options) {
|
|
123
123
|
throw new Error(`${methodArgs["options"]} variable not found`);
|
|
124
124
|
}
|
|
125
|
+
prompt = this.tokenizer.decodeString(this.tokenizer.encodeString(prompt));
|
|
125
126
|
// Add all options to trie
|
|
126
127
|
options.forEach((option) => {
|
|
127
|
-
const prefix = this.tokenizer.encodeString(prompt + option
|
|
128
|
+
const prefix = this.tokenizer.encodeString(prompt + option);
|
|
128
129
|
trie.addPrefix(prefix);
|
|
129
130
|
});
|
|
130
131
|
let currentPrefixPrompt = prompt;
|
|
@@ -136,8 +137,7 @@ class TemplateProcessor {
|
|
|
136
137
|
// If there is only one child, we complete
|
|
137
138
|
completion = this.tokenizer
|
|
138
139
|
.decodeString(trie.getWord(currentPrefix))
|
|
139
|
-
.substring(prompt.length)
|
|
140
|
-
.replace(this.tokenizer.getEOS(), "");
|
|
140
|
+
.substring(prompt.length);
|
|
141
141
|
break;
|
|
142
142
|
}
|
|
143
143
|
else {
|
|
@@ -146,7 +146,7 @@ class TemplateProcessor {
|
|
|
146
146
|
acc[child.toString()] = 100;
|
|
147
147
|
return acc;
|
|
148
148
|
}, {});
|
|
149
|
-
const top_logprobs = yield __await(this.generator.generateTokenLogProgs(
|
|
149
|
+
const top_logprobs = yield __await(this.generator.generateTokenLogProgs(currentPrefixPrompt, logit_bias, reqOptions));
|
|
150
150
|
// get max top_logpobs that is in logit_bias
|
|
151
151
|
let max = -Infinity;
|
|
152
152
|
let max_key = "";
|
|
@@ -55,7 +55,7 @@ class OpenAITokenGenerator extends AbstractTokenGenerator {
|
|
|
55
55
|
model: this.model,
|
|
56
56
|
prompt,
|
|
57
57
|
logit_bias,
|
|
58
|
-
logprobs:
|
|
58
|
+
logprobs: 5,
|
|
59
59
|
max_tokens: 1,
|
|
60
60
|
}, reqOptions);
|
|
61
61
|
const logprobsResult = ((_a = result.choices[0].logprobs) === null || _a === void 0 ? void 0 : _a.top_logprobs) || [];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikugg/guidance",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"scripts": {
|
|
11
11
|
"build": "tsc",
|
|
12
12
|
"demo": "ts-node demo/index.ts",
|
|
13
|
+
"demo-agent": "ts-node demo/agent.ts",
|
|
13
14
|
"lint": "eslint . --ext .ts",
|
|
14
15
|
"prepare": "npm run build",
|
|
15
16
|
"test": "jest"
|
|
@@ -39,5 +40,5 @@
|
|
|
39
40
|
"ts-node": "^10.9.1",
|
|
40
41
|
"typescript": "^5.2.2"
|
|
41
42
|
},
|
|
42
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "bc2da106abe2ac0473544691c85f5f1731b6edc1"
|
|
43
44
|
}
|