@mikugg/guidance 0.13.0 → 0.14.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/lib/__test__/_trie.test.js +7 -7
- package/dist/lib/__test__/template.test.js +58 -44
- package/dist/lib/_mistral-tokenizer.d.ts +19 -0
- package/dist/lib/_mistral-tokenizer.d.ts.map +1 -0
- package/dist/lib/_mistral-tokenizer.js +452 -0
- package/dist/lib/template.d.ts.map +1 -1
- package/dist/lib/template.js +28 -3
- package/dist/lib/token-generator.d.ts +2 -2
- package/dist/lib/token-generator.d.ts.map +1 -1
- package/dist/lib/token-generator.js +10 -24
- package/dist/lib/tokenizer.d.ts +5 -0
- package/dist/lib/tokenizer.d.ts.map +1 -1
- package/dist/lib/tokenizer.js +26 -1
- package/package.json +2 -2
|
@@ -4,39 +4,39 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const _trie_1 = __importDefault(require("../_trie")); // replace './Trie' with the actual path of your Trie class
|
|
7
|
-
describe(
|
|
7
|
+
describe("Trie", () => {
|
|
8
8
|
let trie;
|
|
9
9
|
beforeEach(() => {
|
|
10
10
|
trie = new _trie_1.default();
|
|
11
11
|
});
|
|
12
|
-
test(
|
|
12
|
+
test("addPrefix should add numbers to the trie", () => {
|
|
13
13
|
var _a, _b, _c;
|
|
14
14
|
trie.addPrefix([1, 2, 3]);
|
|
15
15
|
expect(trie.root.children.has(1)).toBeTruthy();
|
|
16
16
|
expect((_a = trie.root.children.get(1)) === null || _a === void 0 ? void 0 : _a.children.has(2)).toBeTruthy();
|
|
17
17
|
expect((_c = (_b = trie.root.children.get(1)) === null || _b === void 0 ? void 0 : _b.children.get(2)) === null || _c === void 0 ? void 0 : _c.children.has(3)).toBeTruthy();
|
|
18
18
|
});
|
|
19
|
-
test(
|
|
19
|
+
test("getNextChildren should return correct next children", () => {
|
|
20
20
|
trie.addPrefix([1, 2, 3]);
|
|
21
21
|
trie.addPrefix([1, 2, 4]);
|
|
22
22
|
expect(trie.getNextChildren([1, 2])).toEqual([3, 4]);
|
|
23
23
|
});
|
|
24
|
-
test(
|
|
24
|
+
test("getNextPrefix should return correct next prefix", () => {
|
|
25
25
|
trie.addPrefix([1, 2, 3]);
|
|
26
26
|
trie.addPrefix([1, 2, 3, 4]);
|
|
27
27
|
expect(trie.getNextPrefix([1, 2])).toEqual([1, 2, 3]);
|
|
28
28
|
});
|
|
29
|
-
test(
|
|
29
|
+
test("getNextPrefix should return correct next prefix", () => {
|
|
30
30
|
trie.addPrefix([1, 2, 3, 4, 5]);
|
|
31
31
|
trie.addPrefix([1, 2, 3, 4, 7]);
|
|
32
32
|
expect(trie.getNextPrefix([1, 2])).toEqual([1, 2, 3, 4]);
|
|
33
33
|
});
|
|
34
|
-
test(
|
|
34
|
+
test("getWord should return correct word until the end of the prefix", () => {
|
|
35
35
|
trie.addPrefix([1, 2, 3]);
|
|
36
36
|
trie.addPrefix([1, 2, 3, 4]);
|
|
37
37
|
expect(trie.getWord([1, 2])).toEqual([1, 2, 3]);
|
|
38
38
|
});
|
|
39
|
-
test(
|
|
39
|
+
test("getWord should return empty array if prefix not found", () => {
|
|
40
40
|
trie.addPrefix([1, 2, 3]);
|
|
41
41
|
expect(trie.getWord([4])).toEqual([]);
|
|
42
42
|
});
|
|
@@ -25,20 +25,28 @@ const template_1 = require("../template");
|
|
|
25
25
|
const tokenizer_1 = require("../tokenizer"); // import paths as required
|
|
26
26
|
const token_generator_1 = require("../token-generator");
|
|
27
27
|
class MockTokenGenerator extends token_generator_1.AbstractTokenGenerator {
|
|
28
|
+
generateTokenLogProgs(
|
|
28
29
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
29
|
-
|
|
30
|
+
prompt,
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
32
|
+
logit_bias) {
|
|
30
33
|
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
-
return
|
|
34
|
+
return {
|
|
35
|
+
"<TOK>": 100,
|
|
36
|
+
};
|
|
32
37
|
});
|
|
33
38
|
}
|
|
39
|
+
generateString(
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
41
|
+
prompt,
|
|
34
42
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
35
|
-
|
|
43
|
+
options) {
|
|
36
44
|
return __asyncGenerator(this, arguments, function* generateString_1() {
|
|
37
|
-
return yield __await(
|
|
45
|
+
return yield __await("generated");
|
|
38
46
|
});
|
|
39
47
|
}
|
|
40
48
|
}
|
|
41
|
-
describe(
|
|
49
|
+
describe("TemplateProcessor", () => {
|
|
42
50
|
let tokenizer;
|
|
43
51
|
let generator;
|
|
44
52
|
let templateProcessor;
|
|
@@ -47,67 +55,73 @@ describe('TemplateProcessor', () => {
|
|
|
47
55
|
generator = new MockTokenGenerator();
|
|
48
56
|
templateProcessor = new template_1.TemplateProcessor(tokenizer, generator);
|
|
49
57
|
});
|
|
50
|
-
describe(
|
|
51
|
-
it(
|
|
52
|
-
const spyGenerateString = jest.spyOn(generator,
|
|
53
|
-
const result = yield templateProcessor.processTemplate(
|
|
54
|
-
expect(spyGenerateString).toHaveBeenCalledWith(
|
|
55
|
-
expect(result.get(
|
|
58
|
+
describe("processTemplate", () => {
|
|
59
|
+
it("should process template with GEN method correctly", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
60
|
+
const spyGenerateString = jest.spyOn(generator, "generateString");
|
|
61
|
+
const result = yield templateProcessor.processTemplate("Hello, {{user}}. The weather is {{GEN weather}}", new Map([["user", "Hina"]]));
|
|
62
|
+
expect(spyGenerateString).toHaveBeenCalledWith("Hello, Hina. The weather is ", {});
|
|
63
|
+
expect(result.get("weather")).toEqual("generated");
|
|
56
64
|
}));
|
|
57
|
-
it(
|
|
58
|
-
const spyGenerateString = jest.spyOn(generator,
|
|
59
|
-
const result = yield templateProcessor.processTemplate(
|
|
60
|
-
expect(spyGenerateString).toHaveBeenCalledWith(
|
|
61
|
-
expect(result.get(
|
|
65
|
+
it("should process template with GEN method and stop correctly", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
66
|
+
const spyGenerateString = jest.spyOn(generator, "generateString");
|
|
67
|
+
const result = yield templateProcessor.processTemplate("Hello, {{user}}. The weather is {{GEN weather stop=.}}", new Map([["user", "Hina"]]));
|
|
68
|
+
expect(spyGenerateString).toHaveBeenCalledWith("Hello, Hina. The weather is ", { stop: "." });
|
|
69
|
+
expect(result.get("weather")).toEqual("generated");
|
|
62
70
|
}));
|
|
63
|
-
it(
|
|
64
|
-
const spyGenerateString = jest.spyOn(generator,
|
|
65
|
-
const result = yield templateProcessor.processTemplate(
|
|
66
|
-
expect(spyGenerateString).toHaveBeenCalledWith(
|
|
67
|
-
expect(result.get(
|
|
71
|
+
it("should process template with GEN method and temperature and repetition_penalty and stop correctly", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
72
|
+
const spyGenerateString = jest.spyOn(generator, "generateString");
|
|
73
|
+
const result = yield templateProcessor.processTemplate("Hello, {{user}}. The weather is {{GEN weather temperature=0.5 repetition_penalty=1 stop=.}}", new Map([["user", "Hina"]]));
|
|
74
|
+
expect(spyGenerateString).toHaveBeenCalledWith("Hello, Hina. The weather is ", { temperature: "0.5", repetition_penalty: "1", stop: "." });
|
|
75
|
+
expect(result.get("weather")).toEqual("generated");
|
|
68
76
|
}));
|
|
69
|
-
it(
|
|
77
|
+
it("should process template with SEL method correctly", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
70
78
|
// 1153 = " ra"
|
|
71
|
-
const spyGenerateToken = jest
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
79
|
+
const spyGenerateToken = jest
|
|
80
|
+
.spyOn(generator, "generateTokenLogProgs")
|
|
81
|
+
.mockReturnValue(new Promise((resolve) => resolve({ " ra": 0 })));
|
|
82
|
+
const result = yield templateProcessor.processTemplate("Hello, {{user}}. The weather is{{SEL weather options=weatherOptions}}", new Map([
|
|
83
|
+
["user", "Hina"],
|
|
84
|
+
["weatherOptions", [" sunny", " rainy", " cloudy"]],
|
|
75
85
|
]));
|
|
76
|
-
expect(spyGenerateToken).toHaveBeenCalledWith(
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
86
|
+
expect(spyGenerateToken).toHaveBeenCalledWith("Hello, Hina. The weather is", {
|
|
87
|
+
"6575": 100,
|
|
88
|
+
"1153": 100,
|
|
89
|
+
"9570": 100,
|
|
80
90
|
});
|
|
81
|
-
expect(result.get(
|
|
91
|
+
expect(result.get("weather")).toEqual(" rainy");
|
|
82
92
|
}));
|
|
83
|
-
it(
|
|
93
|
+
it("should process template with SEL method in a JSON correctly", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
84
94
|
const mockGenerateString = function () {
|
|
85
95
|
return __asyncGenerator(this, arguments, function* () {
|
|
86
|
-
yield yield __await(
|
|
96
|
+
yield yield __await("wizard");
|
|
87
97
|
});
|
|
88
98
|
};
|
|
89
99
|
// 29879 = "s"
|
|
90
|
-
const spyGenerateToken = jest
|
|
91
|
-
|
|
100
|
+
const spyGenerateToken = jest
|
|
101
|
+
.spyOn(generator, "generateTokenLogProgs")
|
|
102
|
+
.mockReturnValue(new Promise((resolve) => resolve({ s: 0 })));
|
|
103
|
+
const spyGenerateString = jest
|
|
104
|
+
.spyOn(generator, "generateString")
|
|
105
|
+
.mockImplementation(mockGenerateString);
|
|
92
106
|
const result = yield templateProcessor.processTemplate(`RPG Game Character specification
|
|
93
107
|
{
|
|
94
108
|
"name": "{{name}}",
|
|
95
109
|
"job": "{{GEN job stop=",}}",
|
|
96
110
|
"weapon": "{{SEL weapon options=valid_weapons}}",
|
|
97
111
|
}`, new Map([
|
|
98
|
-
[
|
|
99
|
-
[
|
|
112
|
+
["name", "Rudeus"],
|
|
113
|
+
["valid_weapons", ["axe", "mace", "sword", "bow", "crossbow"]],
|
|
100
114
|
]));
|
|
101
115
|
expect(spyGenerateString).toHaveBeenCalledWith('RPG Game Character specification\n {\n "name": "Rudeus",\n "job": "', { stop: '",' });
|
|
102
116
|
expect(spyGenerateToken).toHaveBeenCalledWith('RPG Game Character specification\n {\n "name": "Rudeus",\n "job": "wizard",\n "weapon": "', {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
117
|
+
"29879": 100,
|
|
118
|
+
"1165": 100,
|
|
119
|
+
"655": 100,
|
|
120
|
+
"17729": 100,
|
|
121
|
+
"19128": 100,
|
|
108
122
|
});
|
|
109
|
-
expect(result.get(
|
|
110
|
-
expect(result.get(
|
|
123
|
+
expect(result.get("weapon")).toEqual("sword");
|
|
124
|
+
expect(result.get("job")).toEqual("wizard");
|
|
111
125
|
}));
|
|
112
126
|
});
|
|
113
127
|
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MIT LICENSE
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2023 belladore.ai
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
7
|
+
*
|
|
8
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
9
|
+
*
|
|
10
|
+
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
13
|
+
export interface MistralTokenizer {
|
|
14
|
+
encode: (text: string, add_bos_token?: boolean, add_preceding_space?: boolean, log_performance?: boolean) => number[];
|
|
15
|
+
decode: (tokenIds: number[], add_bos_token?: boolean, add_preceding_space?: boolean) => string;
|
|
16
|
+
}
|
|
17
|
+
declare const mistralTokenizer: MistralTokenizer;
|
|
18
|
+
export default mistralTokenizer;
|
|
19
|
+
//# sourceMappingURL=_mistral-tokenizer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_mistral-tokenizer.d.ts","sourceRoot":"","sources":["../../src/lib/_mistral-tokenizer.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AAEH,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,CACN,IAAI,EAAE,MAAM,EACZ,aAAa,CAAC,EAAE,OAAO,EACvB,mBAAmB,CAAC,EAAE,OAAO,EAC7B,eAAe,CAAC,EAAE,OAAO,KACtB,MAAM,EAAE,CAAC;IACd,MAAM,EAAE,CACN,QAAQ,EAAE,MAAM,EAAE,EAClB,aAAa,CAAC,EAAE,OAAO,EACvB,mBAAmB,CAAC,EAAE,OAAO,KAC1B,MAAM,CAAC;CACb;AAED,QAAA,MAAM,gBAAgB,EAAE,gBAAqB,CAAC;AAwgB9C,eAAe,gBAAgB,CAAC"}
|