@botpress/zai 2.0.6 → 2.0.8

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.
@@ -1,5 +1,4 @@
1
1
  import { z } from "@bpinternal/zui";
2
- import { GenerationMetadata } from "../utils";
3
2
  import { Adapter } from "./adapter";
4
3
  const CRITICAL_TAGS = {
5
4
  system: "true",
@@ -29,7 +28,7 @@ const TableSchema = z.object({
29
28
  input: z.object({}).passthrough().describe("The input to the task"),
30
29
  output: z.object({}).passthrough().describe("The expected output"),
31
30
  explanation: z.string().nullable(),
32
- metadata: GenerationMetadata,
31
+ metadata: z.object({}).passthrough(),
33
32
  status: z.enum(["pending", "rejected", "approved"]),
34
33
  feedback: z.object({
35
34
  rating: z.enum(["very-bad", "bad", "good", "very-good"]),
@@ -105,7 +104,9 @@ export class TableAdapter extends Adapter {
105
104
  output: { value: output },
106
105
  explanation: explanation ?? null,
107
106
  status,
108
- metadata
107
+ metadata,
108
+ feedback: null
109
+ // Feedback is not provided at this point
109
110
  }
110
111
  ]
111
112
  }).catch(() => {
package/dist/index.d.ts CHANGED
@@ -1,55 +1,18 @@
1
- import * as _bpinternal_zui from '@bpinternal/zui';
2
- import { z, ZodObject } from '@bpinternal/zui';
3
1
  import { Cognitive, Model, BotpressClientLike } from '@botpress/cognitive';
4
2
  import { TextTokenizer } from '@bpinternal/thicktoken';
5
3
 
6
- type GenerationMetadata = (typeof GenerationMetadata)['_input'];
7
- declare const GenerationMetadata: _bpinternal_zui.ZodObject<{
8
- model: _bpinternal_zui.ZodString;
9
- cost: _bpinternal_zui.ZodObject<{
10
- input: _bpinternal_zui.ZodNumber;
11
- output: _bpinternal_zui.ZodNumber;
12
- }, "strip", _bpinternal_zui.ZodTypeAny, {
13
- input?: number;
14
- output?: number;
15
- }, {
16
- input?: number;
17
- output?: number;
18
- }>;
19
- latency: _bpinternal_zui.ZodNumber;
20
- tokens: _bpinternal_zui.ZodObject<{
21
- input: _bpinternal_zui.ZodNumber;
22
- output: _bpinternal_zui.ZodNumber;
23
- }, "strip", _bpinternal_zui.ZodTypeAny, {
24
- input?: number;
25
- output?: number;
26
- }, {
27
- input?: number;
28
- output?: number;
29
- }>;
30
- }, "strip", _bpinternal_zui.ZodTypeAny, {
31
- model?: string;
32
- cost?: {
33
- input?: number;
34
- output?: number;
35
- };
36
- latency?: number;
37
- tokens?: {
38
- input?: number;
39
- output?: number;
40
- };
41
- }, {
42
- model?: string;
43
- cost?: {
44
- input?: number;
45
- output?: number;
4
+ type GenerationMetadata = {
5
+ model: string;
6
+ cost: {
7
+ input: number;
8
+ output: number;
46
9
  };
47
- latency?: number;
48
- tokens?: {
49
- input?: number;
50
- output?: number;
10
+ latency: number;
11
+ tokens: {
12
+ input: number;
13
+ output: number;
51
14
  };
52
- }>;
15
+ };
53
16
 
54
17
  type SaveExampleProps<TInput, TOutput> = {
55
18
  key: string;
@@ -79,60 +42,18 @@ declare abstract class Adapter {
79
42
  }
80
43
 
81
44
  type ModelId = Required<Parameters<Cognitive['generateContent']>[0]['model']>;
82
- type ActiveLearning = (typeof ActiveLearning)['_input'];
83
- declare const ActiveLearning: _bpinternal_zui.ZodObject<{
84
- enable: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodBoolean>;
85
- tableName: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodString>;
86
- taskId: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodString>;
87
- }, "strip", _bpinternal_zui.ZodTypeAny, {
88
- tableName?: string;
89
- taskId?: string;
90
- enable?: boolean;
91
- }, {
92
- tableName?: string;
93
- taskId?: string;
94
- enable?: boolean;
95
- }>;
96
- type ZaiConfig = (typeof ZaiConfig)['_input'];
97
- declare const ZaiConfig: _bpinternal_zui.ZodObject<{
98
- client: z.Schema<Cognitive | BotpressClientLike, _bpinternal_zui.ZodTypeDef, Cognitive | BotpressClientLike>;
99
- userId: _bpinternal_zui.ZodOptional<_bpinternal_zui.ZodString>;
100
- modelId: _bpinternal_zui.ZodDefault<z.Schema<string, _bpinternal_zui.ZodTypeDef, string>>;
101
- activeLearning: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodObject<{
102
- enable: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodBoolean>;
103
- tableName: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodString>;
104
- taskId: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodString>;
105
- }, "strip", _bpinternal_zui.ZodTypeAny, {
106
- tableName?: string;
107
- taskId?: string;
108
- enable?: boolean;
109
- }, {
110
- tableName?: string;
111
- taskId?: string;
112
- enable?: boolean;
113
- }>>;
114
- namespace: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodString>;
115
- }, "strip", _bpinternal_zui.ZodTypeAny, {
116
- client?: Cognitive | BotpressClientLike;
117
- userId?: string;
118
- modelId?: string;
119
- activeLearning?: {
120
- tableName?: string;
121
- taskId?: string;
122
- enable?: boolean;
123
- };
124
- namespace?: string;
125
- }, {
126
- client?: Cognitive | BotpressClientLike;
45
+ type ActiveLearning = {
46
+ enable: boolean;
47
+ tableName: string;
48
+ taskId: string;
49
+ };
50
+ type ZaiConfig = {
51
+ client: BotpressClientLike | Cognitive;
127
52
  userId?: string;
128
- modelId?: string;
129
- activeLearning?: {
130
- tableName?: string;
131
- taskId?: string;
132
- enable?: boolean;
133
- };
53
+ modelId?: ModelId | string;
54
+ activeLearning?: ActiveLearning;
134
55
  namespace?: string;
135
- }>;
56
+ };
136
57
  declare class Zai {
137
58
  protected static tokenizer: TextTokenizer;
138
59
  protected client: Cognitive;
@@ -153,14 +74,10 @@ declare class Zai {
153
74
  learn(taskId: string): Zai;
154
75
  }
155
76
 
156
- type Options$6 = (typeof Options$6)['_input'];
157
- declare const Options$6: _bpinternal_zui.ZodObject<{
158
- length: _bpinternal_zui.ZodOptional<_bpinternal_zui.ZodNumber>;
159
- }, "strip", _bpinternal_zui.ZodTypeAny, {
77
+ type Options$6 = {
78
+ /** The maximum number of tokens to generate */
160
79
  length?: number;
161
- }, {
162
- length?: number;
163
- }>;
80
+ };
164
81
  declare module '@botpress/zai' {
165
82
  interface Zai {
166
83
  /** Generates a text of the desired length according to the prompt */
@@ -168,32 +85,17 @@ declare module '@botpress/zai' {
168
85
  }
169
86
  }
170
87
 
171
- type Options$5 = (typeof Options$5)['_input'];
172
- declare const Options$5: _bpinternal_zui.ZodObject<{
173
- examples: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodArray<_bpinternal_zui.ZodObject<{
174
- input: _bpinternal_zui.ZodString;
175
- output: _bpinternal_zui.ZodString;
176
- }, "strip", _bpinternal_zui.ZodTypeAny, {
177
- input?: string;
178
- output?: string;
179
- }, {
180
- input?: string;
181
- output?: string;
182
- }>, "many">>;
183
- length: _bpinternal_zui.ZodOptional<_bpinternal_zui.ZodNumber>;
184
- }, "strip", _bpinternal_zui.ZodTypeAny, {
185
- length?: number;
186
- examples?: {
187
- input?: string;
188
- output?: string;
189
- }[];
190
- }, {
88
+ type Example$2 = {
89
+ input: string;
90
+ output: string;
91
+ instructions?: string;
92
+ };
93
+ type Options$5 = {
94
+ /** Examples to guide the rewriting */
95
+ examples?: Array<Example$2>;
96
+ /** The maximum number of tokens to generate */
191
97
  length?: number;
192
- examples?: {
193
- input?: string;
194
- output?: string;
195
- }[];
196
- }>;
98
+ };
197
99
  declare module '@botpress/zai' {
198
100
  interface Zai {
199
101
  /** Rewrites a string according to match the prompt */
@@ -201,44 +103,23 @@ declare module '@botpress/zai' {
201
103
  }
202
104
  }
203
105
 
204
- type Options$4 = (typeof Options$4)['_input'];
205
- declare const Options$4: _bpinternal_zui.ZodObject<{
206
- prompt: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodString>;
207
- format: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodString>;
208
- length: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodNumber>;
209
- intermediateFactor: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodNumber>;
210
- maxIterations: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodNumber>;
211
- sliding: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodObject<{
212
- window: _bpinternal_zui.ZodNumber;
213
- overlap: _bpinternal_zui.ZodNumber;
214
- }, "strip", _bpinternal_zui.ZodTypeAny, {
215
- window?: number;
216
- overlap?: number;
217
- }, {
218
- window?: number;
219
- overlap?: number;
220
- }>>;
221
- }, "strip", _bpinternal_zui.ZodTypeAny, {
222
- length?: number;
106
+ type Options$4 = {
107
+ /** What should the text be summarized to? */
223
108
  prompt?: string;
109
+ /** How to format the example text */
224
110
  format?: string;
225
- intermediateFactor?: number;
226
- maxIterations?: number;
227
- sliding?: {
228
- window?: number;
229
- overlap?: number;
230
- };
231
- }, {
111
+ /** The length of the summary in tokens */
232
112
  length?: number;
233
- prompt?: string;
234
- format?: string;
113
+ /** How many times longer (than final length) are the intermediate summaries generated */
235
114
  intermediateFactor?: number;
115
+ /** The maximum number of iterations to perform */
236
116
  maxIterations?: number;
117
+ /** Sliding window options */
237
118
  sliding?: {
238
- window?: number;
239
- overlap?: number;
119
+ window: number;
120
+ overlap: number;
240
121
  };
241
- }>;
122
+ };
242
123
  declare module '@botpress/zai' {
243
124
  interface Zai {
244
125
  /** Summarizes a text of any length to a summary of the desired length */
@@ -246,34 +127,14 @@ declare module '@botpress/zai' {
246
127
  }
247
128
  }
248
129
 
249
- type Options$3 = (typeof Options$3)['_input'];
250
- declare const Options$3: _bpinternal_zui.ZodObject<{
251
- examples: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodArray<_bpinternal_zui.ZodObject<{
252
- input: _bpinternal_zui.ZodAny;
253
- check: _bpinternal_zui.ZodBoolean;
254
- reason: _bpinternal_zui.ZodOptional<_bpinternal_zui.ZodString>;
255
- }, "strip", _bpinternal_zui.ZodTypeAny, {
256
- input?: any;
257
- check?: boolean;
258
- reason?: string;
259
- }, {
260
- input?: any;
261
- check?: boolean;
262
- reason?: string;
263
- }>, "many">>;
264
- }, "strip", _bpinternal_zui.ZodTypeAny, {
265
- examples?: {
266
- input?: any;
267
- check?: boolean;
130
+ type Options$3 = {
131
+ /** Examples to check the condition against */
132
+ examples?: Array<{
133
+ input: unknown;
134
+ check: boolean;
268
135
  reason?: string;
269
- }[];
270
- }, {
271
- examples?: {
272
- input?: any;
273
- check?: boolean;
274
- reason?: string;
275
- }[];
276
- }>;
136
+ }>;
137
+ };
277
138
  declare module '@botpress/zai' {
278
139
  interface Zai {
279
140
  /** Checks wether a condition is true or not */
@@ -286,37 +147,17 @@ declare module '@botpress/zai' {
286
147
  }
287
148
  }
288
149
 
289
- type Options$2 = (typeof Options$2)['_input'];
290
- declare const Options$2: _bpinternal_zui.ZodObject<{
291
- tokensPerItem: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodOptional<_bpinternal_zui.ZodNumber>>;
292
- examples: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodArray<_bpinternal_zui.ZodObject<{
293
- input: _bpinternal_zui.ZodAny;
294
- filter: _bpinternal_zui.ZodBoolean;
295
- reason: _bpinternal_zui.ZodOptional<_bpinternal_zui.ZodString>;
296
- }, "strip", _bpinternal_zui.ZodTypeAny, {
297
- input?: any;
298
- filter?: boolean;
299
- reason?: string;
300
- }, {
301
- input?: any;
302
- filter?: boolean;
303
- reason?: string;
304
- }>, "many">>;
305
- }, "strip", _bpinternal_zui.ZodTypeAny, {
306
- examples?: {
307
- input?: any;
308
- filter?: boolean;
309
- reason?: string;
310
- }[];
311
- tokensPerItem?: number;
312
- }, {
313
- examples?: {
314
- input?: any;
315
- filter?: boolean;
316
- reason?: string;
317
- }[];
150
+ type Example$1 = {
151
+ input: unknown;
152
+ filter: boolean;
153
+ reason?: string;
154
+ };
155
+ type Options$2 = {
156
+ /** The maximum number of tokens per item */
318
157
  tokensPerItem?: number;
319
- }>;
158
+ /** Examples to filter the condition against */
159
+ examples?: Array<Example$1>;
160
+ };
320
161
  declare module '@botpress/zai' {
321
162
  interface Zai {
322
163
  /** Filters elements of an array against a condition */
@@ -324,17 +165,12 @@ declare module '@botpress/zai' {
324
165
  }
325
166
  }
326
167
 
327
- type Options$1 = (typeof Options$1)['_input'];
328
- declare const Options$1: ZodObject<{
329
- instructions: _bpinternal_zui.ZodOptional<_bpinternal_zui.ZodString>;
330
- chunkLength: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodOptional<_bpinternal_zui.ZodNumber>>;
331
- }, "strip", _bpinternal_zui.ZodTypeAny, {
332
- instructions?: string;
333
- chunkLength?: number;
334
- }, {
168
+ type Options$1 = {
169
+ /** Instructions to guide the user on how to extract the data */
335
170
  instructions?: string;
171
+ /** The maximum number of tokens per chunk */
336
172
  chunkLength?: number;
337
- }>;
173
+ };
338
174
  type __Z<T extends any = any> = {
339
175
  _output: T;
340
176
  };
@@ -362,60 +198,15 @@ type Example<T extends string> = {
362
198
  explanation?: string;
363
199
  }>>;
364
200
  };
365
- type Options<T extends string> = Omit<(typeof Options)['_input'], 'examples'> & {
366
- examples?: Array<Partial<Example<T>>>;
367
- };
368
- declare const Options: _bpinternal_zui.ZodObject<{
369
- examples: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodArray<_bpinternal_zui.ZodObject<{
370
- input: _bpinternal_zui.ZodAny;
371
- labels: _bpinternal_zui.ZodRecord<_bpinternal_zui.ZodString, _bpinternal_zui.ZodObject<{
372
- label: _bpinternal_zui.ZodEnum<never>;
373
- explanation: _bpinternal_zui.ZodOptional<_bpinternal_zui.ZodString>;
374
- }, "strip", _bpinternal_zui.ZodTypeAny, {
375
- label: never;
376
- explanation?: string;
377
- }, {
378
- label: never;
379
- explanation?: string;
380
- }>>;
381
- }, "strip", _bpinternal_zui.ZodTypeAny, {
382
- input?: any;
383
- labels?: Record<string, {
384
- label: never;
385
- explanation?: string;
386
- }>;
387
- }, {
388
- input?: any;
389
- labels?: Record<string, {
390
- label: never;
391
- explanation?: string;
392
- }>;
393
- }>, "many">>;
394
- instructions: _bpinternal_zui.ZodOptional<_bpinternal_zui.ZodString>;
395
- chunkLength: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodOptional<_bpinternal_zui.ZodNumber>>;
396
- }, "strip", _bpinternal_zui.ZodTypeAny, {
397
- instructions?: string;
398
- examples?: {
399
- input?: any;
400
- labels?: Record<string, {
401
- label: never;
402
- explanation?: string;
403
- }>;
404
- }[];
405
- chunkLength?: number;
406
- }, {
201
+ type Options<T extends string> = {
202
+ /** Examples to help the user make a decision */
203
+ examples?: Array<Example<T>>;
204
+ /** Instructions to guide the user on how to extract the data */
407
205
  instructions?: string;
408
- examples?: {
409
- input?: any;
410
- labels?: Record<string, {
411
- label: never;
412
- explanation?: string;
413
- }>;
414
- }[];
206
+ /** The maximum number of tokens per chunk */
415
207
  chunkLength?: number;
416
- }>;
208
+ };
417
209
  type Labels<T extends string> = Record<T, string>;
418
- declare const Labels: z.ZodTransformer<_bpinternal_zui.ZodRecord<_bpinternal_zui.ZodString, _bpinternal_zui.ZodString>, Record<string, string>, Record<string, string>>;
419
210
  declare module '@botpress/zai' {
420
211
  interface Zai {
421
212
  /** Tags the provided input with a list of predefined labels */
@@ -2,19 +2,19 @@ import { z } from "@bpinternal/zui";
2
2
  import { fastHash, stringify, takeUntilTokens } from "../utils";
3
3
  import { Zai } from "../zai";
4
4
  import { PROMPT_INPUT_BUFFER } from "./constants";
5
- const Example = z.object({
5
+ const _Example = z.object({
6
6
  input: z.any(),
7
7
  check: z.boolean(),
8
8
  reason: z.string().optional()
9
9
  });
10
- const Options = z.object({
11
- examples: z.array(Example).describe("Examples to check the condition against").default([])
10
+ const _Options = z.object({
11
+ examples: z.array(_Example).describe("Examples to check the condition against").default([])
12
12
  });
13
13
  const TRUE = "\u25A0TRUE\u25A0";
14
14
  const FALSE = "\u25A0FALSE\u25A0";
15
15
  const END = "\u25A0END\u25A0";
16
16
  Zai.prototype.check = async function(input, condition, _options) {
17
- const options = Options.parse(_options ?? {});
17
+ const options = _Options.parse(_options ?? {});
18
18
  const tokenizer = await this.getTokenizer();
19
19
  await this.fetchModelDetails();
20
20
  const PROMPT_COMPONENT = Math.max(this.ModelDetails.input.maxTokens - PROMPT_INPUT_BUFFER, 100);
@@ -3,18 +3,18 @@ import { clamp } from "lodash-es";
3
3
  import { fastHash, stringify, takeUntilTokens } from "../utils";
4
4
  import { Zai } from "../zai";
5
5
  import { PROMPT_INPUT_BUFFER, PROMPT_OUTPUT_BUFFER } from "./constants";
6
- const Example = z.object({
6
+ const _Example = z.object({
7
7
  input: z.any(),
8
8
  filter: z.boolean(),
9
9
  reason: z.string().optional()
10
10
  });
11
- const Options = z.object({
11
+ const _Options = z.object({
12
12
  tokensPerItem: z.number().min(1).max(1e5).optional().describe("The maximum number of tokens per item").default(250),
13
- examples: z.array(Example).describe("Examples to filter the condition against").default([])
13
+ examples: z.array(_Example).describe("Examples to filter the condition against").default([])
14
14
  });
15
15
  const END = "\u25A0END\u25A0";
16
16
  Zai.prototype.filter = async function(input, condition, _options) {
17
- const options = Options.parse(_options ?? {});
17
+ const options = _Options.parse(_options ?? {});
18
18
  const tokenizer = await this.getTokenizer();
19
19
  await this.fetchModelDetails();
20
20
  const taskId = this.taskId;
@@ -11,7 +11,7 @@ const LABELS = {
11
11
  ABSOLUTELY_YES: "ABSOLUTELY_YES"
12
12
  };
13
13
  const ALL_LABELS = Object.values(LABELS).join(" | ");
14
- const Options = z.object({
14
+ const _Options = z.object({
15
15
  examples: z.array(
16
16
  z.object({
17
17
  input: z.any(),
@@ -21,7 +21,7 @@ const Options = z.object({
21
21
  instructions: z.string().optional().describe("Instructions to guide the user on how to extract the data"),
22
22
  chunkLength: z.number().min(100).max(1e5).optional().describe("The maximum number of tokens per chunk").default(16e3)
23
23
  });
24
- const Labels = z.record(z.string().min(1).max(250), z.string()).superRefine((labels, ctx) => {
24
+ const _Labels = z.record(z.string().min(1).max(250), z.string()).superRefine((labels, ctx) => {
25
25
  const keys = Object.keys(labels);
26
26
  for (const key of keys) {
27
27
  if (key.length < 1 || key.length > 250) {
@@ -68,8 +68,8 @@ const getConfidence = (label) => {
68
68
  }
69
69
  };
70
70
  Zai.prototype.label = async function(input, _labels, _options) {
71
- const options = Options.parse(_options ?? {});
72
- const labels = Labels.parse(_labels);
71
+ const options = _Options.parse(_options ?? {});
72
+ const labels = _Labels.parse(_labels);
73
73
  const tokenizer = await this.getTokenizer();
74
74
  await this.fetchModelDetails();
75
75
  const taskId = this.taskId;
@@ -126,7 +126,7 @@ Zai.prototype.label = async function(input, _labels, _options) {
126
126
  options.examples.forEach((example) => {
127
127
  examples.push({
128
128
  key: fastHash(JSON.stringify(example)),
129
- input: example.input,
129
+ input: stringify(example.input),
130
130
  similarity: 1,
131
131
  explanation: "",
132
132
  output: example.labels
@@ -2,12 +2,12 @@ import { z } from "@bpinternal/zui";
2
2
  import { fastHash, stringify, takeUntilTokens } from "../utils";
3
3
  import { Zai } from "../zai";
4
4
  import { PROMPT_INPUT_BUFFER } from "./constants";
5
- const Example = z.object({
5
+ const _Example = z.object({
6
6
  input: z.string(),
7
7
  output: z.string()
8
8
  });
9
9
  const Options = z.object({
10
- examples: z.array(Example).default([]),
10
+ examples: z.array(_Example).default([]),
11
11
  length: z.number().min(10).max(16e3).optional().describe("The maximum number of tokens to generate")
12
12
  });
13
13
  const START = "\u25A0START\u25A0";
package/dist/utils.js CHANGED
@@ -1,4 +1,3 @@
1
- import { z } from "@bpinternal/zui";
2
1
  export const stringify = (input, beautify = true) => {
3
2
  return typeof input === "string" && !!input.length ? input : input ? JSON.stringify(input, beautify ? null : void 0, beautify ? 2 : void 0) : "<input is null, false, undefined or empty>";
4
3
  };
@@ -23,15 +22,3 @@ export const takeUntilTokens = (arr, tokens, count) => {
23
22
  }
24
23
  return result;
25
24
  };
26
- export const GenerationMetadata = z.object({
27
- model: z.string(),
28
- cost: z.object({
29
- input: z.number(),
30
- output: z.number()
31
- }).describe("Cost in $USD"),
32
- latency: z.number().describe("Latency in milliseconds"),
33
- tokens: z.object({
34
- input: z.number(),
35
- output: z.number()
36
- }).describe("Number of tokens used")
37
- });
package/dist/zai.js CHANGED
@@ -3,7 +3,7 @@ import { getWasmTokenizer } from "@bpinternal/thicktoken";
3
3
  import { z } from "@bpinternal/zui";
4
4
  import { TableAdapter } from "./adapters/botpress-table";
5
5
  import { MemoryAdapter } from "./adapters/memory";
6
- const ActiveLearning = z.object({
6
+ const _ActiveLearning = z.object({
7
7
  enable: z.boolean().describe("Whether to enable active learning").default(false),
8
8
  tableName: z.string().regex(
9
9
  /^[A-Za-z0-9_/-]{1,100}Table$/,
@@ -14,7 +14,7 @@ const ActiveLearning = z.object({
14
14
  "Namespace must be alphanumeric and contain only letters, numbers, underscores, hyphens and slashes"
15
15
  ).describe("The ID of the task").default("default")
16
16
  });
17
- const ZaiConfig = z.object({
17
+ const _ZaiConfig = z.object({
18
18
  client: z.custom(),
19
19
  userId: z.string().describe("The ID of the user consuming the API").optional(),
20
20
  modelId: z.custom(
@@ -31,7 +31,7 @@ const ZaiConfig = z.object({
31
31
  message: "Invalid model ID"
32
32
  }
33
33
  ).describe("The ID of the model you want to use").default("best"),
34
- activeLearning: ActiveLearning.default({ enable: false }),
34
+ activeLearning: _ActiveLearning.default({ enable: false }),
35
35
  namespace: z.string().regex(
36
36
  /^[A-Za-z0-9_/-]{1,100}$/,
37
37
  "Namespace must be alphanumeric and contain only letters, numbers, underscores, hyphens and slashes"
@@ -49,13 +49,16 @@ export class Zai {
49
49
  activeLearning;
50
50
  constructor(config) {
51
51
  this._originalConfig = config;
52
- const parsed = ZaiConfig.parse(config);
52
+ const parsed = _ZaiConfig.parse(config);
53
53
  this.client = Cognitive.isCognitiveClient(parsed.client) ? parsed.client : new Cognitive({ client: parsed.client });
54
54
  this.namespace = parsed.namespace;
55
55
  this._userId = parsed.userId;
56
56
  this.Model = parsed.modelId;
57
57
  this.activeLearning = parsed.activeLearning;
58
- this.adapter = parsed.activeLearning?.enable ? new TableAdapter({ client: this.client.client, tableName: parsed.activeLearning.tableName }) : new MemoryAdapter([]);
58
+ this.adapter = parsed.activeLearning?.enable ? new TableAdapter({
59
+ client: this.client.client,
60
+ tableName: parsed.activeLearning.tableName
61
+ }) : new MemoryAdapter([]);
59
62
  }
60
63
  /** @internal */
61
64
  async callModel(props) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@botpress/zai",
3
3
  "description": "Zui AI (zai) – An LLM utility library written on top of Zui and the Botpress API",
4
- "version": "2.0.6",
4
+ "version": "2.0.8",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "exports": {
@@ -23,7 +23,7 @@
23
23
  "author": "",
24
24
  "license": "ISC",
25
25
  "dependencies": {
26
- "@botpress/cognitive": "0.1.21",
26
+ "@botpress/cognitive": "0.1.23",
27
27
  "json5": "^2.2.3",
28
28
  "jsonrepair": "^3.10.0",
29
29
  "lodash-es": "^4.17.21"
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "peerDependencies": {
43
43
  "@bpinternal/thicktoken": "^1.0.0",
44
- "@bpinternal/zui": "^0.17.1"
44
+ "@bpinternal/zui": "^0.22.5"
45
45
  },
46
46
  "engines": {
47
47
  "node": ">=18.0.0"
@@ -20,6 +20,11 @@ const OPTIONAL_TAGS = {
20
20
 
21
21
  const FACTOR = 30
22
22
 
23
+ type Props = {
24
+ client: Client
25
+ tableName: string
26
+ }
27
+
23
28
  const Props = z.object({
24
29
  client: z.custom(() => true),
25
30
  tableName: z
@@ -30,7 +35,22 @@ const Props = z.object({
30
35
  ),
31
36
  })
32
37
 
33
- export type TableSchema = (typeof TableSchema)['_input']
38
+ export type TableSchema = {
39
+ taskType: string
40
+ taskId: string
41
+ key: string
42
+ instructions: string
43
+ input: Record<string, unknown>
44
+ output: Record<string, unknown>
45
+ explanation: string | null
46
+ metadata: GenerationMetadata
47
+ status: 'pending' | 'rejected' | 'approved'
48
+ feedback: {
49
+ rating: 'very-bad' | 'bad' | 'good' | 'very-good'
50
+ comment: string | null
51
+ } | null
52
+ }
53
+
34
54
  const TableSchema = z.object({
35
55
  taskType: z.string().describe('The type of the task (filter, extract, etc.)'),
36
56
  taskId: z.string(),
@@ -39,7 +59,7 @@ const TableSchema = z.object({
39
59
  input: z.object({}).passthrough().describe('The input to the task'),
40
60
  output: z.object({}).passthrough().describe('The expected output'),
41
61
  explanation: z.string().nullable(),
42
- metadata: GenerationMetadata,
62
+ metadata: z.object({}).passthrough(),
43
63
  status: z.enum(['pending', 'rejected', 'approved']),
44
64
  feedback: z
45
65
  .object({
@@ -65,9 +85,9 @@ export class TableAdapter extends Adapter {
65
85
 
66
86
  private _status: 'initialized' | 'ready' | 'error'
67
87
 
68
- public constructor(props: (typeof Props)['_input']) {
88
+ public constructor(props: Props) {
69
89
  super()
70
- props = Props.parse(props)
90
+ props = Props.parse(props) as Props
71
91
  this._client = props.client
72
92
  this._tableName = props.tableName
73
93
  this._status = 'ready'
@@ -131,6 +151,7 @@ export class TableAdapter extends Adapter {
131
151
  explanation: explanation ?? null,
132
152
  status,
133
153
  metadata,
154
+ feedback: null, // Feedback is not provided at this point
134
155
  } satisfies TableSchema,
135
156
  ],
136
157
  })
@@ -5,15 +5,23 @@ import { fastHash, stringify, takeUntilTokens } from '../utils'
5
5
  import { Zai } from '../zai'
6
6
  import { PROMPT_INPUT_BUFFER } from './constants'
7
7
 
8
- const Example = z.object({
8
+ const _Example = z.object({
9
9
  input: z.any(),
10
10
  check: z.boolean(),
11
11
  reason: z.string().optional(),
12
12
  })
13
13
 
14
- export type Options = (typeof Options)['_input']
15
- const Options = z.object({
16
- examples: z.array(Example).describe('Examples to check the condition against').default([]),
14
+ export type Options = {
15
+ /** Examples to check the condition against */
16
+ examples?: Array<{
17
+ input: unknown
18
+ check: boolean
19
+ reason?: string
20
+ }>
21
+ }
22
+
23
+ const _Options = z.object({
24
+ examples: z.array(_Example).describe('Examples to check the condition against').default([]),
17
25
  })
18
26
 
19
27
  declare module '@botpress/zai' {
@@ -36,8 +44,8 @@ const TRUE = '■TRUE■'
36
44
  const FALSE = '■FALSE■'
37
45
  const END = '■END■'
38
46
 
39
- Zai.prototype.check = async function (this: Zai, input, condition, _options) {
40
- const options = Options.parse(_options ?? {})
47
+ Zai.prototype.check = async function (this: Zai, input: unknown, condition: string, _options: Options | undefined) {
48
+ const options = _Options.parse(_options ?? {}) as Options
41
49
  const tokenizer = await this.getTokenizer()
42
50
  await this.fetchModelDetails()
43
51
  const PROMPT_COMPONENT = Math.max(this.ModelDetails.input.maxTokens - PROMPT_INPUT_BUFFER, 100)
@@ -10,7 +10,13 @@ import { Zai } from '../zai'
10
10
  import { PROMPT_INPUT_BUFFER } from './constants'
11
11
  import { JsonParsingError } from './errors'
12
12
 
13
- export type Options = (typeof Options)['_input']
13
+ export type Options = {
14
+ /** Instructions to guide the user on how to extract the data */
15
+ instructions?: string
16
+ /** The maximum number of tokens per chunk */
17
+ chunkLength?: number
18
+ }
19
+
14
20
  const Options = z.object({
15
21
  instructions: z.string().optional().describe('Instructions to guide the user on how to extract the data'),
16
22
  chunkLength: z
@@ -6,15 +6,26 @@ import { fastHash, stringify, takeUntilTokens } from '../utils'
6
6
  import { Zai } from '../zai'
7
7
  import { PROMPT_INPUT_BUFFER, PROMPT_OUTPUT_BUFFER } from './constants'
8
8
 
9
- type Example = (typeof Example)['_input']
10
- const Example = z.object({
9
+ type Example = {
10
+ input: unknown
11
+ filter: boolean
12
+ reason?: string
13
+ }
14
+
15
+ const _Example = z.object({
11
16
  input: z.any(),
12
17
  filter: z.boolean(),
13
18
  reason: z.string().optional(),
14
19
  })
15
20
 
16
- export type Options = (typeof Options)['_input']
17
- const Options = z.object({
21
+ export type Options = {
22
+ /** The maximum number of tokens per item */
23
+ tokensPerItem?: number
24
+ /** Examples to filter the condition against */
25
+ examples?: Array<Example>
26
+ }
27
+
28
+ const _Options = z.object({
18
29
  tokensPerItem: z
19
30
  .number()
20
31
  .min(1)
@@ -22,7 +33,7 @@ const Options = z.object({
22
33
  .optional()
23
34
  .describe('The maximum number of tokens per item')
24
35
  .default(250),
25
- examples: z.array(Example).describe('Examples to filter the condition against').default([]),
36
+ examples: z.array(_Example).describe('Examples to filter the condition against').default([]),
26
37
  })
27
38
 
28
39
  declare module '@botpress/zai' {
@@ -35,7 +46,7 @@ declare module '@botpress/zai' {
35
46
  const END = '■END■'
36
47
 
37
48
  Zai.prototype.filter = async function (this: Zai, input, condition, _options) {
38
- const options = Options.parse(_options ?? {})
49
+ const options = _Options.parse(_options ?? {}) as Options
39
50
  const tokenizer = await this.getTokenizer()
40
51
  await this.fetchModelDetails()
41
52
 
@@ -22,11 +22,16 @@ type Example<T extends string> = {
22
22
  labels: Partial<Record<T, { label: Label; explanation?: string }>>
23
23
  }
24
24
 
25
- export type Options<T extends string> = Omit<(typeof Options)['_input'], 'examples'> & {
26
- examples?: Array<Partial<Example<T>>>
25
+ export type Options<T extends string> = {
26
+ /** Examples to help the user make a decision */
27
+ examples?: Array<Example<T>>
28
+ /** Instructions to guide the user on how to extract the data */
29
+ instructions?: string
30
+ /** The maximum number of tokens per chunk */
31
+ chunkLength?: number
27
32
  }
28
33
 
29
- const Options = z.object({
34
+ const _Options = z.object({
30
35
  examples: z
31
36
  .array(
32
37
  z.object({
@@ -48,7 +53,7 @@ const Options = z.object({
48
53
 
49
54
  type Labels<T extends string> = Record<T, string>
50
55
 
51
- const Labels = z.record(z.string().min(1).max(250), z.string()).superRefine((labels, ctx) => {
56
+ const _Labels = z.record(z.string().min(1).max(250), z.string()).superRefine((labels, ctx) => {
52
57
  const keys = Object.keys(labels)
53
58
 
54
59
  for (const key of keys) {
@@ -119,9 +124,14 @@ const getConfidence = (label: Label) => {
119
124
  }
120
125
  }
121
126
 
122
- Zai.prototype.label = async function <T extends string>(this: Zai, input, _labels, _options) {
123
- const options = Options.parse(_options ?? {})
124
- const labels = Labels.parse(_labels)
127
+ Zai.prototype.label = async function <T extends string>(
128
+ this: Zai,
129
+ input: unknown,
130
+ _labels: Labels<T>,
131
+ _options: Options<T> | undefined
132
+ ) {
133
+ const options = _Options.parse(_options ?? {}) as unknown as Options<T>
134
+ const labels = _Labels.parse(_labels) as Labels<T>
125
135
  const tokenizer = await this.getTokenizer()
126
136
  await this.fetchModelDetails()
127
137
 
@@ -211,7 +221,7 @@ Zai.prototype.label = async function <T extends string>(this: Zai, input, _label
211
221
  options.examples.forEach((example) => {
212
222
  examples.push({
213
223
  key: fastHash(JSON.stringify(example)),
214
- input: example.input,
224
+ input: stringify(example.input),
215
225
  similarity: 1,
216
226
  explanation: '',
217
227
  output: example.labels as unknown as {
@@ -5,15 +5,26 @@ import { fastHash, stringify, takeUntilTokens } from '../utils'
5
5
  import { Zai } from '../zai'
6
6
  import { PROMPT_INPUT_BUFFER } from './constants'
7
7
 
8
- type Example = (typeof Example)['_input'] & { instructions?: string }
9
- const Example = z.object({
8
+ type Example = {
9
+ input: string
10
+ output: string
11
+ instructions?: string
12
+ }
13
+
14
+ const _Example = z.object({
10
15
  input: z.string(),
11
16
  output: z.string(),
12
17
  })
13
18
 
14
- export type Options = (typeof Options)['_input']
19
+ export type Options = {
20
+ /** Examples to guide the rewriting */
21
+ examples?: Array<Example>
22
+ /** The maximum number of tokens to generate */
23
+ length?: number
24
+ }
25
+
15
26
  const Options = z.object({
16
- examples: z.array(Example).default([]),
27
+ examples: z.array(_Example).default([]),
17
28
  length: z.number().min(10).max(16_000).optional().describe('The maximum number of tokens to generate'),
18
29
  })
19
30
 
@@ -28,7 +39,7 @@ const START = '■START■'
28
39
  const END = '■END■'
29
40
 
30
41
  Zai.prototype.rewrite = async function (this: Zai, original, prompt, _options) {
31
- const options = Options.parse(_options ?? {})
42
+ const options = Options.parse(_options ?? {}) as Options
32
43
  const tokenizer = await this.getTokenizer()
33
44
  await this.fetchModelDetails()
34
45
 
@@ -101,7 +112,7 @@ ${END}
101
112
  }
102
113
 
103
114
  const savedExamples: Example[] = [
104
- ...tableExamples.map((x) => ({ input: x.input as string, output: x.output as string })),
115
+ ...tableExamples.map((x) => ({ input: x.input as string, output: x.output as string }) satisfies Example),
105
116
  ...options.examples,
106
117
  ]
107
118
 
@@ -5,7 +5,24 @@ import { chunk } from 'lodash-es'
5
5
  import { Zai } from '../zai'
6
6
  import { PROMPT_INPUT_BUFFER, PROMPT_OUTPUT_BUFFER } from './constants'
7
7
 
8
- export type Options = (typeof Options)['_input']
8
+ export type Options = {
9
+ /** What should the text be summarized to? */
10
+ prompt?: string
11
+ /** How to format the example text */
12
+ format?: string
13
+ /** The length of the summary in tokens */
14
+ length?: number
15
+ /** How many times longer (than final length) are the intermediate summaries generated */
16
+ intermediateFactor?: number
17
+ /** The maximum number of iterations to perform */
18
+ maxIterations?: number
19
+ /** Sliding window options */
20
+ sliding?: {
21
+ window: number
22
+ overlap: number
23
+ }
24
+ }
25
+
9
26
  const Options = z.object({
10
27
  prompt: z
11
28
  .string()
@@ -45,7 +62,7 @@ const START = '■START■'
45
62
  const END = '■END■'
46
63
 
47
64
  Zai.prototype.summarize = async function (this: Zai, original, _options) {
48
- const options = Options.parse(_options ?? {})
65
+ const options = Options.parse(_options ?? {}) as Options
49
66
  const tokenizer = await this.getTokenizer()
50
67
  await this.fetchModelDetails()
51
68
 
@@ -5,7 +5,11 @@ import { clamp } from 'lodash-es'
5
5
  import { Zai } from '../zai'
6
6
  import { PROMPT_INPUT_BUFFER, PROMPT_OUTPUT_BUFFER } from './constants'
7
7
 
8
- export type Options = (typeof Options)['_input']
8
+ export type Options = {
9
+ /** The maximum number of tokens to generate */
10
+ length?: number
11
+ }
12
+
9
13
  const Options = z.object({
10
14
  length: z.number().min(1).max(100_000).optional().describe('The maximum number of tokens to generate'),
11
15
  })
package/src/utils.ts CHANGED
@@ -1,5 +1,3 @@
1
- import { z } from '@bpinternal/zui'
2
-
3
1
  export const stringify = (input: unknown, beautify = true) => {
4
2
  return typeof input === 'string' && !!input.length
5
3
  ? input
@@ -33,20 +31,15 @@ export const takeUntilTokens = <T>(arr: T[], tokens: number, count: (el: T) => n
33
31
  return result
34
32
  }
35
33
 
36
- export type GenerationMetadata = (typeof GenerationMetadata)['_input']
37
- export const GenerationMetadata = z.object({
38
- model: z.string(),
39
- cost: z
40
- .object({
41
- input: z.number(),
42
- output: z.number(),
43
- })
44
- .describe('Cost in $USD'),
45
- latency: z.number().describe('Latency in milliseconds'),
46
- tokens: z
47
- .object({
48
- input: z.number(),
49
- output: z.number(),
50
- })
51
- .describe('Number of tokens used'),
52
- })
34
+ export type GenerationMetadata = {
35
+ model: string
36
+ cost: {
37
+ input: number
38
+ output: number
39
+ }
40
+ latency: number
41
+ tokens: {
42
+ input: number
43
+ output: number
44
+ }
45
+ }
package/src/zai.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Client } from '@botpress/client'
1
2
  import { BotpressClientLike, Cognitive, Model } from '@botpress/cognitive'
2
3
 
3
4
  import { type TextTokenizer, getWasmTokenizer } from '@bpinternal/thicktoken'
@@ -9,8 +10,13 @@ import { MemoryAdapter } from './adapters/memory'
9
10
 
10
11
  type ModelId = Required<Parameters<Cognitive['generateContent']>[0]['model']>
11
12
 
12
- type ActiveLearning = (typeof ActiveLearning)['_input']
13
- const ActiveLearning = z.object({
13
+ type ActiveLearning = {
14
+ enable: boolean
15
+ tableName: string
16
+ taskId: string
17
+ }
18
+
19
+ const _ActiveLearning = z.object({
14
20
  enable: z.boolean().describe('Whether to enable active learning').default(false),
15
21
  tableName: z
16
22
  .string()
@@ -30,8 +36,15 @@ const ActiveLearning = z.object({
30
36
  .default('default'),
31
37
  })
32
38
 
33
- type ZaiConfig = (typeof ZaiConfig)['_input']
34
- const ZaiConfig = z.object({
39
+ type ZaiConfig = {
40
+ client: BotpressClientLike | Cognitive
41
+ userId?: string
42
+ modelId?: ModelId | string
43
+ activeLearning?: ActiveLearning
44
+ namespace?: string
45
+ }
46
+
47
+ const _ZaiConfig = z.object({
35
48
  client: z.custom<BotpressClientLike | Cognitive>(),
36
49
  userId: z.string().describe('The ID of the user consuming the API').optional(),
37
50
  modelId: z
@@ -53,7 +66,7 @@ const ZaiConfig = z.object({
53
66
  )
54
67
  .describe('The ID of the model you want to use')
55
68
  .default('best' satisfies ModelId),
56
- activeLearning: ActiveLearning.default({ enable: false }),
69
+ activeLearning: _ActiveLearning.default({ enable: false }),
57
70
  namespace: z
58
71
  .string()
59
72
  .regex(
@@ -79,7 +92,7 @@ export class Zai {
79
92
 
80
93
  public constructor(config: ZaiConfig) {
81
94
  this._originalConfig = config
82
- const parsed = ZaiConfig.parse(config)
95
+ const parsed = _ZaiConfig.parse(config)
83
96
 
84
97
  this.client = Cognitive.isCognitiveClient(parsed.client)
85
98
  ? (parsed.client as unknown as Cognitive)
@@ -88,10 +101,13 @@ export class Zai {
88
101
  this.namespace = parsed.namespace
89
102
  this._userId = parsed.userId
90
103
  this.Model = parsed.modelId as ModelId
91
- this.activeLearning = parsed.activeLearning
104
+ this.activeLearning = parsed.activeLearning as ActiveLearning
92
105
 
93
106
  this.adapter = parsed.activeLearning?.enable
94
- ? new TableAdapter({ client: this.client.client, tableName: parsed.activeLearning.tableName })
107
+ ? new TableAdapter({
108
+ client: this.client.client as unknown as Client,
109
+ tableName: parsed.activeLearning.tableName,
110
+ })
95
111
  : new MemoryAdapter([])
96
112
  }
97
113