@mikugg/guidance 0.8.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/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Guidance-like library for Node.js
2
+ This library is a port of the [Guidance](https://github.com/guidance-ai/guidance) library for Node.js. It allows you to generate text from templates using a Language Model (LLM) and an endpoint that can generate text from a prompt.
3
+
4
+ ## Installation
5
+ ```bash
6
+ npm install @mikugg/guidance
7
+ ```
8
+
9
+ ## Usage
10
+ You can use end OpenAI endpoint to generate text from templates. The template processor will generate text from the template and then use the generated text as a prompt to the OpenAI endpoint. The generated text will be appended to the template and the process will repeat until the stop condition is met.
11
+
12
+ For serving LLaMA or Mistral models, I recommend using [Aphrodite Engine](https://github.com/PygmalionAI/aphrodite-engine), since it has `logit_bias` support.
13
+
14
+ ```javascript
15
+ import * as Guidance from '@mikugg/guidance';
16
+
17
+ const tokenizer = new Guidance.Tokenizer.LLaMATokenizer();
18
+ const generator = new Guidance.TokenGenerator.OpenAITokenGenerator({
19
+ apiKey: 'sk-EMPTY',
20
+ baseURL: 'http://localhost:2242/v1',
21
+ model: 'mistralai/Mistral-7B-v0.1',
22
+ });
23
+ const templateProcessor = new Guidance.Template.TemplateProcessor(tokenizer, generator);
24
+
25
+ let result = await templateProcessor.processTemplate(
26
+ `Common sense question and answer, with short answers
27
+ Question: What is your favorite food?
28
+ Answer: "Sushi."
29
+ Question: What is your favorite color?
30
+ Answer: "Blue."
31
+ Question: What is your favorite animal?
32
+ Answer: "{{GEN response stop=" }}"`,
33
+ new Map([])
34
+ ).then((result) => {
35
+ console.log(result.entries()); // {"response": "Elephant."}
36
+ });
37
+ ```
38
+
39
+ ### Select from a list of options
40
+ You can provide an array of options to select the next text. The TemplateProcessor will increase the probability of one of those options being selected by the LLM.
41
+
42
+ ```javascript
43
+ import * as Guidance from '@mikugg/guidance';
44
+
45
+ const tokenizer = new Guidance.Tokenizer.LLaMATokenizer();
46
+ const generator = new Guidance.TokenGenerator.OpenAITokenGenerator({
47
+ apiKey: 'sk-EMPTY',
48
+ baseURL: 'http://localhost:2242/v1',
49
+ model: 'mistralai/Mistral-7B-v0.1',
50
+ });
51
+ const templateProcessor = new Guidance.Template.TemplateProcessor(tokenizer, generator);
52
+
53
+ let result = await templateProcessor.processTemplate(
54
+ `RPG Game Character specification
55
+ {
56
+ "name": "{{name}}",
57
+ "job": "{{GEN job stop=",}}",
58
+ "armor": "{{SEL armor options=valid_armors}}",
59
+ "weapon": "{{SEL weapon options=valid_weapons}}",
60
+ "pants": "{{SEL pants options=valid_pants}}"
61
+ }`,
62
+ new Map<string, string[] | string>([
63
+ ['name', 'Rudeus'],
64
+ ['valid_armors', ['plate', 'leather']],
65
+ ['valid_weapons', ["axe", "mace", "spear", "sword", "bow", "crossbow"]],
66
+ ['valid_pants', ['leather_jacket', 'leather_shorts', 'hat']], // Should select leather_shorts
67
+ ])
68
+ ).then((result) => {
69
+ console.log(result.entries());
70
+ });
71
+ ```
72
+
73
+ You can check more examples in the [this file](./demo/index.ts).
@@ -0,0 +1,5 @@
1
+ import * as Tokenizer from './lib/tokenizer';
2
+ import * as TokenGenerator from './lib/token-generator';
3
+ import * as Template from './lib/template';
4
+ export { Tokenizer, TokenGenerator, Template };
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAM,SAAS,MAAM,iBAAiB,CAAC;AAC9C,OAAO,KAAK,cAAc,MAAM,uBAAuB,CAAC;AACxD,OAAO,KAAK,QAAQ,MAAM,gBAAgB,CAAC;AAE3C,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,QAAQ,EAAE,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.Template = exports.TokenGenerator = exports.Tokenizer = void 0;
27
+ const Tokenizer = __importStar(require("./lib/tokenizer"));
28
+ exports.Tokenizer = Tokenizer;
29
+ const TokenGenerator = __importStar(require("./lib/token-generator"));
30
+ exports.TokenGenerator = TokenGenerator;
31
+ const Template = __importStar(require("./lib/template"));
32
+ exports.Template = Template;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=_trie.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_trie.test.d.ts","sourceRoot":"","sources":["../../../src/lib/__test__/_trie.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const _trie_1 = __importDefault(require("../_trie")); // replace './Trie' with the actual path of your Trie class
7
+ describe('Trie', () => {
8
+ let trie;
9
+ beforeEach(() => {
10
+ trie = new _trie_1.default();
11
+ });
12
+ test('addPrefix should add numbers to the trie', () => {
13
+ var _a, _b, _c;
14
+ trie.addPrefix([1, 2, 3]);
15
+ expect(trie.root.children.has(1)).toBeTruthy();
16
+ expect((_a = trie.root.children.get(1)) === null || _a === void 0 ? void 0 : _a.children.has(2)).toBeTruthy();
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
+ });
19
+ test('getNextChildren should return correct next children', () => {
20
+ trie.addPrefix([1, 2, 3]);
21
+ trie.addPrefix([1, 2, 4]);
22
+ expect(trie.getNextChildren([1, 2])).toEqual([3, 4]);
23
+ });
24
+ test('getNextPrefix should return correct next prefix', () => {
25
+ trie.addPrefix([1, 2, 3]);
26
+ trie.addPrefix([1, 2, 3, 4]);
27
+ expect(trie.getNextPrefix([1, 2])).toEqual([1, 2, 3]);
28
+ });
29
+ test('getNextPrefix should return correct next prefix', () => {
30
+ trie.addPrefix([1, 2, 3, 4, 5]);
31
+ trie.addPrefix([1, 2, 3, 4, 7]);
32
+ expect(trie.getNextPrefix([1, 2])).toEqual([1, 2, 3, 4]);
33
+ });
34
+ test('getWord should return correct word until the end of the prefix', () => {
35
+ trie.addPrefix([1, 2, 3]);
36
+ trie.addPrefix([1, 2, 3, 4]);
37
+ expect(trie.getWord([1, 2])).toEqual([1, 2, 3]);
38
+ });
39
+ test('getWord should return empty array if prefix not found', () => {
40
+ trie.addPrefix([1, 2, 3]);
41
+ expect(trie.getWord([4])).toEqual([]);
42
+ });
43
+ });
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=template.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"template.test.d.ts","sourceRoot":"","sources":["../../../src/lib/__test__/template.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const template_1 = require("../template");
13
+ const tokenizer_1 = require("../tokenizer"); // import paths as required
14
+ const token_generator_1 = require("../token-generator");
15
+ class MockTokenGenerator extends token_generator_1.AbstractTokenGenerator {
16
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
17
+ generateToken(prompt, logit_bias) {
18
+ return __awaiter(this, void 0, void 0, function* () {
19
+ return '<TOK>';
20
+ });
21
+ }
22
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
23
+ generateString(prompt, options) {
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ return 'generated';
26
+ });
27
+ }
28
+ }
29
+ describe('TemplateProcessor', () => {
30
+ let tokenizer;
31
+ let generator;
32
+ let templateProcessor;
33
+ beforeEach(() => {
34
+ tokenizer = new tokenizer_1.LLaMATokenizer();
35
+ generator = new MockTokenGenerator();
36
+ templateProcessor = new template_1.TemplateProcessor(tokenizer, generator);
37
+ });
38
+ describe('processTemplate', () => {
39
+ it('should process template with GEN method correctly', () => __awaiter(void 0, void 0, void 0, function* () {
40
+ const spyGenerateString = jest.spyOn(generator, 'generateString');
41
+ const result = yield templateProcessor.processTemplate('Hello, {{user}}. The weather is {{GEN weather}}', new Map([['user', 'Hina']]));
42
+ expect(spyGenerateString).toHaveBeenCalledWith('Hello, Hina. The weather is ', {});
43
+ expect(result.get('weather')).toEqual('generated');
44
+ }));
45
+ it('should process template with GEN method and stop correctly', () => __awaiter(void 0, void 0, void 0, function* () {
46
+ const spyGenerateString = jest.spyOn(generator, 'generateString');
47
+ const result = yield templateProcessor.processTemplate('Hello, {{user}}. The weather is {{GEN weather stop=.}}', new Map([['user', 'Hina']]));
48
+ expect(spyGenerateString).toHaveBeenCalledWith('Hello, Hina. The weather is ', { stop: '.' });
49
+ expect(result.get('weather')).toEqual('generated');
50
+ }));
51
+ it('should process template with GEN method and temperature and repetition_penalty and stop correctly', () => __awaiter(void 0, void 0, void 0, function* () {
52
+ const spyGenerateString = jest.spyOn(generator, 'generateString');
53
+ const result = yield templateProcessor.processTemplate('Hello, {{user}}. The weather is {{GEN weather temperature=0.5 repetition_penalty=1 stop=.}}', new Map([['user', 'Hina']]));
54
+ expect(spyGenerateString).toHaveBeenCalledWith('Hello, Hina. The weather is ', { temperature: '0.5', repetition_penalty: '1', stop: '.' });
55
+ expect(result.get('weather')).toEqual('generated');
56
+ }));
57
+ it('should process template with SEL method correctly', () => __awaiter(void 0, void 0, void 0, function* () {
58
+ // 1153 = " ra"
59
+ const spyGenerateToken = jest.spyOn(generator, 'generateToken').mockReturnValue(new Promise((resolve) => resolve(" ra")));
60
+ const result = yield templateProcessor.processTemplate('Hello, {{user}}. The weather is{{SEL weather options=weatherOptions}}', new Map([
61
+ ['user', 'Hina'],
62
+ ['weatherOptions', [' sunny', ' rainy', ' cloudy']]
63
+ ]));
64
+ expect(spyGenerateToken).toHaveBeenCalledWith('Hello, Hina. The weather is', {
65
+ '6575': 100,
66
+ '1153': 100,
67
+ '9570': 100
68
+ });
69
+ expect(result.get('weather')).toEqual(' rainy');
70
+ }));
71
+ it('should process template with SEL method in a JSON correctly', () => __awaiter(void 0, void 0, void 0, function* () {
72
+ // 29879 = "s"
73
+ const spyGenerateToken = jest.spyOn(generator, 'generateToken').mockReturnValue(new Promise((resolve) => resolve("s")));
74
+ const spyGenerateString = jest.spyOn(generator, 'generateString').mockReturnValue(new Promise((resolve) => resolve("wizard")));
75
+ const result = yield templateProcessor.processTemplate(`RPG Game Character specification
76
+ {
77
+ "name": "{{name}}",
78
+ "job": "{{GEN job stop=",}}",
79
+ "weapon": "{{SEL weapon options=valid_weapons}}",
80
+ }`, new Map([
81
+ ['name', 'Rudeus'],
82
+ ['valid_weapons', ["axe", "mace", "sword", "bow", "crossbow"]],
83
+ ]));
84
+ expect(spyGenerateString).toHaveBeenCalledWith('RPG Game Character specification\n {\n "name": "Rudeus",\n "job": "', { stop: '",' });
85
+ expect(spyGenerateToken).toHaveBeenCalledWith('RPG Game Character specification\n {\n "name": "Rudeus",\n "job": "wizard",\n "weapon": "', {
86
+ '29879': 100,
87
+ '1165': 100,
88
+ '655': 100,
89
+ '17729': 100,
90
+ '19128': 100,
91
+ });
92
+ expect(result.get('weapon')).toEqual('sword');
93
+ expect(result.get('job')).toEqual('wizard');
94
+ }));
95
+ });
96
+ });
@@ -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 LLaMATokenizer {
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 llamaTokenizer: LLaMATokenizer;
18
+ export default llamaTokenizer;
19
+ //# sourceMappingURL=_llama-tokenizer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_llama-tokenizer.d.ts","sourceRoot":"","sources":["../../src/lib/_llama-tokenizer.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AAEH,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,CACJ,IAAI,EAAE,MAAM,EACZ,aAAa,CAAC,EAAE,OAAO,EACvB,mBAAmB,CAAC,EAAE,OAAO,EAC7B,eAAe,CAAC,EAAE,OAAO,KACxB,MAAM,EAAE,CAAC;IACd,MAAM,EAAE,CACJ,QAAQ,EAAE,MAAM,EAAE,EAClB,aAAa,CAAC,EAAE,OAAO,EACvB,mBAAmB,CAAC,EAAE,OAAO,KAC5B,MAAM,CAAA;CACZ;AAED,QAAA,MAAM,cAAc,EAAE,cAAmB,CAAA;AA6czC,eAAe,cAAc,CAAA"}