@botpress/zai 2.0.3 → 2.0.5

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.
@@ -17,8 +17,8 @@ const FACTOR = 30;
17
17
  const Props = z.object({
18
18
  client: z.custom(() => true),
19
19
  tableName: z.string().regex(
20
- /^[a-zA-Z0-9_]{1,45}Table$/,
21
- "Table name must be lowercase and contain only letters, numbers and underscores"
20
+ /^[a-zA-Z0-9_]{1,45}Table$/i,
21
+ 'Table name must be lowercase and contain only letters, numbers and underscores. It must also end with "Table". Example: "ActiveLearningTable"'
22
22
  )
23
23
  });
24
24
  const TableSchema = z.object({
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@ import { z, ZodObject } from '@bpinternal/zui';
3
3
  import { Cognitive, Model, BotpressClientLike } from '@botpress/cognitive';
4
4
  import { TextTokenizer } from '@bpinternal/thicktoken';
5
5
 
6
- type GenerationMetadata = z.input<typeof GenerationMetadata>;
6
+ type GenerationMetadata = (typeof GenerationMetadata)['_input'];
7
7
  declare const GenerationMetadata: _bpinternal_zui.ZodObject<{
8
8
  model: _bpinternal_zui.ZodString;
9
9
  cost: _bpinternal_zui.ZodObject<{
@@ -79,7 +79,7 @@ declare abstract class Adapter {
79
79
  }
80
80
 
81
81
  type ModelId = Required<Parameters<Cognitive['generateContent']>[0]['model']>;
82
- type ActiveLearning = z.input<typeof ActiveLearning>;
82
+ type ActiveLearning = (typeof ActiveLearning)['_input'];
83
83
  declare const ActiveLearning: _bpinternal_zui.ZodObject<{
84
84
  enable: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodBoolean>;
85
85
  tableName: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodString>;
@@ -93,7 +93,7 @@ declare const ActiveLearning: _bpinternal_zui.ZodObject<{
93
93
  taskId?: string;
94
94
  enable?: boolean;
95
95
  }>;
96
- type ZaiConfig = z.input<typeof ZaiConfig>;
96
+ type ZaiConfig = (typeof ZaiConfig)['_input'];
97
97
  declare const ZaiConfig: _bpinternal_zui.ZodObject<{
98
98
  client: z.Schema<Cognitive | BotpressClientLike, _bpinternal_zui.ZodTypeDef, Cognitive | BotpressClientLike>;
99
99
  userId: _bpinternal_zui.ZodOptional<_bpinternal_zui.ZodString>;
@@ -153,7 +153,7 @@ declare class Zai {
153
153
  learn(taskId: string): Zai;
154
154
  }
155
155
 
156
- type Options$6 = z.input<typeof Options$6>;
156
+ type Options$6 = (typeof Options$6)['_input'];
157
157
  declare const Options$6: _bpinternal_zui.ZodObject<{
158
158
  length: _bpinternal_zui.ZodOptional<_bpinternal_zui.ZodNumber>;
159
159
  }, "strip", _bpinternal_zui.ZodTypeAny, {
@@ -168,7 +168,7 @@ declare module '@botpress/zai' {
168
168
  }
169
169
  }
170
170
 
171
- type Options$5 = z.input<typeof Options$5>;
171
+ type Options$5 = (typeof Options$5)['_input'];
172
172
  declare const Options$5: _bpinternal_zui.ZodObject<{
173
173
  examples: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodArray<_bpinternal_zui.ZodObject<{
174
174
  input: _bpinternal_zui.ZodString;
@@ -201,7 +201,7 @@ declare module '@botpress/zai' {
201
201
  }
202
202
  }
203
203
 
204
- type Options$4 = z.input<typeof Options$4>;
204
+ type Options$4 = (typeof Options$4)['_input'];
205
205
  declare const Options$4: _bpinternal_zui.ZodObject<{
206
206
  prompt: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodString>;
207
207
  format: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodString>;
@@ -246,7 +246,7 @@ declare module '@botpress/zai' {
246
246
  }
247
247
  }
248
248
 
249
- type Options$3 = z.input<typeof Options$3>;
249
+ type Options$3 = (typeof Options$3)['_input'];
250
250
  declare const Options$3: _bpinternal_zui.ZodObject<{
251
251
  examples: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodArray<_bpinternal_zui.ZodObject<{
252
252
  input: _bpinternal_zui.ZodAny;
@@ -286,7 +286,7 @@ declare module '@botpress/zai' {
286
286
  }
287
287
  }
288
288
 
289
- type Options$2 = z.input<typeof Options$2>;
289
+ type Options$2 = (typeof Options$2)['_input'];
290
290
  declare const Options$2: _bpinternal_zui.ZodObject<{
291
291
  tokensPerItem: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodOptional<_bpinternal_zui.ZodNumber>>;
292
292
  examples: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodArray<_bpinternal_zui.ZodObject<{
@@ -324,7 +324,7 @@ declare module '@botpress/zai' {
324
324
  }
325
325
  }
326
326
 
327
- type Options$1 = z.input<typeof Options$1>;
327
+ type Options$1 = (typeof Options$1)['_input'];
328
328
  declare const Options$1: ZodObject<{
329
329
  instructions: _bpinternal_zui.ZodOptional<_bpinternal_zui.ZodString>;
330
330
  chunkLength: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodOptional<_bpinternal_zui.ZodNumber>>;
@@ -335,10 +335,15 @@ declare const Options$1: ZodObject<{
335
335
  instructions?: string;
336
336
  chunkLength?: number;
337
337
  }>;
338
+ type __Z<T extends any = any> = {
339
+ _output: T;
340
+ };
341
+ type OfType<O, T extends __Z = __Z<O>> = T extends __Z<O> ? T : never;
342
+ type AnyObjectOrArray = Record<string, unknown> | Array<unknown>;
338
343
  declare module '@botpress/zai' {
339
344
  interface Zai {
340
345
  /** Extracts one or many elements from an arbitrary input */
341
- extract<S extends z.AnyZodObject | z.ZodArray>(input: unknown, schema: S, options?: Options$1): Promise<z.TypeOf<S>>;
346
+ extract<S extends OfType<AnyObjectOrArray>>(input: unknown, schema: S, options?: Options$1): Promise<S['_output']>;
342
347
  }
343
348
  }
344
349
 
@@ -357,7 +362,7 @@ type Example<T extends string> = {
357
362
  explanation?: string;
358
363
  }>>;
359
364
  };
360
- type Options<T extends string> = Omit<z.input<typeof Options>, 'examples'> & {
365
+ type Options<T extends string> = Omit<(typeof Options)['_input'], 'examples'> & {
361
366
  examples?: Array<Partial<Example<T>>>;
362
367
  };
363
368
  declare const Options: _bpinternal_zui.ZodObject<{
@@ -13,7 +13,8 @@ const Options = z.object({
13
13
  const START = "\u25A0json_start\u25A0";
14
14
  const END = "\u25A0json_end\u25A0";
15
15
  const NO_MORE = "\u25A0NO_MORE_ELEMENT\u25A0";
16
- Zai.prototype.extract = async function(input, schema, _options) {
16
+ Zai.prototype.extract = async function(input, _schema, _options) {
17
+ let schema = _schema;
17
18
  const options = Options.parse(_options ?? {});
18
19
  const tokenizer = await this.getTokenizer();
19
20
  await this.fetchModelDetails();
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.3",
4
+ "version": "2.0.5",
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.18",
26
+ "@botpress/cognitive": "0.1.20",
27
27
  "json5": "^2.2.3",
28
28
  "jsonrepair": "^3.10.0",
29
29
  "lodash-es": "^4.17.21"
@@ -25,12 +25,12 @@ const Props = z.object({
25
25
  tableName: z
26
26
  .string()
27
27
  .regex(
28
- /^[a-zA-Z0-9_]{1,45}Table$/,
29
- 'Table name must be lowercase and contain only letters, numbers and underscores'
28
+ /^[a-zA-Z0-9_]{1,45}Table$/i,
29
+ 'Table name must be lowercase and contain only letters, numbers and underscores. It must also end with "Table". Example: "ActiveLearningTable"'
30
30
  ),
31
31
  })
32
32
 
33
- export type TableSchema = z.input<typeof TableSchema>
33
+ export type TableSchema = (typeof TableSchema)['_input']
34
34
  const TableSchema = z.object({
35
35
  taskType: z.string().describe('The type of the task (filter, extract, etc.)'),
36
36
  taskId: z.string(),
@@ -65,7 +65,7 @@ export class TableAdapter extends Adapter {
65
65
 
66
66
  private _status: 'initialized' | 'ready' | 'error'
67
67
 
68
- public constructor(props: z.input<typeof Props>) {
68
+ public constructor(props: (typeof Props)['_input']) {
69
69
  super()
70
70
  props = Props.parse(props)
71
71
  this._client = props.client
@@ -11,7 +11,7 @@ const Example = z.object({
11
11
  reason: z.string().optional(),
12
12
  })
13
13
 
14
- export type Options = z.input<typeof Options>
14
+ export type Options = (typeof Options)['_input']
15
15
  const Options = z.object({
16
16
  examples: z.array(Example).describe('Examples to check the condition against').default([]),
17
17
  })
@@ -10,7 +10,7 @@ import { Zai } from '../zai'
10
10
  import { PROMPT_INPUT_BUFFER } from './constants'
11
11
  import { JsonParsingError } from './errors'
12
12
 
13
- export type Options = z.input<typeof Options>
13
+ export type Options = (typeof Options)['_input']
14
14
  const Options = z.object({
15
15
  instructions: z.string().optional().describe('Instructions to guide the user on how to extract the data'),
16
16
  chunkLength: z
@@ -22,10 +22,14 @@ const Options = z.object({
22
22
  .default(16_000),
23
23
  })
24
24
 
25
+ type __Z<T extends any = any> = { _output: T }
26
+ type OfType<O, T extends __Z = __Z<O>> = T extends __Z<O> ? T : never
27
+ type AnyObjectOrArray = Record<string, unknown> | Array<unknown>
28
+
25
29
  declare module '@botpress/zai' {
26
30
  interface Zai {
27
31
  /** Extracts one or many elements from an arbitrary input */
28
- extract<S extends z.AnyZodObject | z.ZodArray>(input: unknown, schema: S, options?: Options): Promise<z.TypeOf<S>>
32
+ extract<S extends OfType<AnyObjectOrArray>>(input: unknown, schema: S, options?: Options): Promise<S['_output']>
29
33
  }
30
34
  }
31
35
 
@@ -33,7 +37,13 @@ const START = '■json_start■'
33
37
  const END = '■json_end■'
34
38
  const NO_MORE = '■NO_MORE_ELEMENT■'
35
39
 
36
- Zai.prototype.extract = async function (this: Zai, input, schema, _options) {
40
+ Zai.prototype.extract = async function <S extends OfType<AnyObjectOrArray>>(
41
+ this: Zai,
42
+ input: unknown,
43
+ _schema: S,
44
+ _options?: Options
45
+ ): Promise<S['_output']> {
46
+ let schema = _schema as any as z.ZodType
37
47
  const options = Options.parse(_options ?? {})
38
48
  const tokenizer = await this.getTokenizer()
39
49
  await this.fetchModelDetails()
@@ -83,9 +93,9 @@ Zai.prototype.extract = async function (this: Zai, input, schema, _options) {
83
93
  if (isArrayOfObjects) {
84
94
  const tokens = tokenizer.split(inputAsString)
85
95
  const chunks = chunk(tokens, options.chunkLength).map((x) => x.join(''))
86
- const all = await Promise.all(chunks.map((chunk) => this.extract(chunk, originalSchema as z.AnyZodObject)))
96
+ const all = await Promise.all(chunks.map((chunk) => this.extract(chunk, originalSchema)))
87
97
 
88
- return all.flat()
98
+ return all.flat() as any as S['_output']
89
99
  } else {
90
100
  // Truncate the input to fit the model's input size
91
101
  inputAsString = tokenizer.truncate(stringify(input), options.chunkLength)
@@ -137,7 +147,7 @@ Zai.prototype.extract = async function (this: Zai, input, schema, _options) {
137
147
 
138
148
  const exactMatch = examples.find((x) => x.key === Key)
139
149
  if (exactMatch) {
140
- return exactMatch.output
150
+ return exactMatch.output as any as S['_output']
141
151
  }
142
152
 
143
153
  const defaultExample = isArrayOfObjects
@@ -6,14 +6,14 @@ 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 = z.input<typeof Example>
9
+ type Example = (typeof Example)['_input']
10
10
  const Example = z.object({
11
11
  input: z.any(),
12
12
  filter: z.boolean(),
13
13
  reason: z.string().optional(),
14
14
  })
15
15
 
16
- export type Options = z.input<typeof Options>
16
+ export type Options = (typeof Options)['_input']
17
17
  const Options = z.object({
18
18
  tokensPerItem: z
19
19
  .number()
@@ -22,7 +22,7 @@ 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<z.input<typeof Options>, 'examples'> & {
25
+ export type Options<T extends string> = Omit<(typeof Options)['_input'], 'examples'> & {
26
26
  examples?: Array<Partial<Example<T>>>
27
27
  }
28
28
 
@@ -5,13 +5,13 @@ 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 = z.input<typeof Example> & { instructions?: string }
8
+ type Example = (typeof Example)['_input'] & { instructions?: string }
9
9
  const Example = z.object({
10
10
  input: z.string(),
11
11
  output: z.string(),
12
12
  })
13
13
 
14
- export type Options = z.input<typeof Options>
14
+ export type Options = (typeof Options)['_input']
15
15
  const Options = z.object({
16
16
  examples: z.array(Example).default([]),
17
17
  length: z.number().min(10).max(16_000).optional().describe('The maximum number of tokens to generate'),
@@ -5,7 +5,7 @@ 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 = z.input<typeof Options>
8
+ export type Options = (typeof Options)['_input']
9
9
  const Options = z.object({
10
10
  prompt: z
11
11
  .string()
@@ -5,7 +5,7 @@ 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 = z.input<typeof Options>
8
+ export type Options = (typeof Options)['_input']
9
9
  const Options = z.object({
10
10
  length: z.number().min(1).max(100_000).optional().describe('The maximum number of tokens to generate'),
11
11
  })
package/src/utils.ts CHANGED
@@ -33,7 +33,7 @@ export const takeUntilTokens = <T>(arr: T[], tokens: number, count: (el: T) => n
33
33
  return result
34
34
  }
35
35
 
36
- export type GenerationMetadata = z.input<typeof GenerationMetadata>
36
+ export type GenerationMetadata = (typeof GenerationMetadata)['_input']
37
37
  export const GenerationMetadata = z.object({
38
38
  model: z.string(),
39
39
  cost: z
package/src/zai.ts CHANGED
@@ -9,7 +9,7 @@ import { MemoryAdapter } from './adapters/memory'
9
9
 
10
10
  type ModelId = Required<Parameters<Cognitive['generateContent']>[0]['model']>
11
11
 
12
- type ActiveLearning = z.input<typeof ActiveLearning>
12
+ type ActiveLearning = (typeof ActiveLearning)['_input']
13
13
  const ActiveLearning = z.object({
14
14
  enable: z.boolean().describe('Whether to enable active learning').default(false),
15
15
  tableName: z
@@ -30,7 +30,7 @@ const ActiveLearning = z.object({
30
30
  .default('default'),
31
31
  })
32
32
 
33
- type ZaiConfig = z.input<typeof ZaiConfig>
33
+ type ZaiConfig = (typeof ZaiConfig)['_input']
34
34
  const ZaiConfig = z.object({
35
35
  client: z.custom<BotpressClientLike | Cognitive>(),
36
36
  userId: z.string().describe('The ID of the user consuming the API').optional(),