@botpress/zai 2.4.0 → 2.4.2

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/src/zai.ts CHANGED
@@ -8,9 +8,27 @@ import { Adapter } from './adapters/adapter'
8
8
  import { TableAdapter } from './adapters/botpress-table'
9
9
  import { MemoryAdapter } from './adapters/memory'
10
10
 
11
+ /**
12
+ * Active learning configuration for improving AI operations over time.
13
+ *
14
+ * When enabled, Zai stores successful operation results in a table and uses them as examples
15
+ * for future operations, improving accuracy and consistency.
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * const activeLearning = {
20
+ * enable: true,
21
+ * tableName: 'MyAppLearningTable',
22
+ * taskId: 'sentiment-analysis'
23
+ * }
24
+ * ```
25
+ */
11
26
  type ActiveLearning = {
27
+ /** Whether to enable active learning for this Zai instance */
12
28
  enable: boolean
29
+ /** Name of the Botpress table to store learning examples (must end with 'Table') */
13
30
  tableName: string
31
+ /** Unique identifier for this learning task */
14
32
  taskId: string
15
33
  }
16
34
 
@@ -34,11 +52,39 @@ const _ActiveLearning = z.object({
34
52
  .default('default'),
35
53
  })
36
54
 
55
+ /**
56
+ * Configuration options for creating a Zai instance.
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * import { Client } from '@botpress/client'
61
+ * import { Zai } from '@botpress/zai'
62
+ *
63
+ * const client = new Client({ token: 'your-token' })
64
+ * const config: ZaiConfig = {
65
+ * client,
66
+ * modelId: 'best', // Use the best available model
67
+ * userId: 'user-123',
68
+ * namespace: 'my-app',
69
+ * activeLearning: {
70
+ * enable: true,
71
+ * tableName: 'MyLearningTable',
72
+ * taskId: 'extraction'
73
+ * }
74
+ * }
75
+ * const zai = new Zai(config)
76
+ * ```
77
+ */
37
78
  type ZaiConfig = {
79
+ /** Botpress client or Cognitive client instance */
38
80
  client: BotpressClientLike | Cognitive
81
+ /** Optional user ID for tracking and attribution */
39
82
  userId?: string
83
+ /** Model to use: 'best' (default), 'fast', or specific model like 'openai:gpt-4' */
40
84
  modelId?: Models
85
+ /** Active learning configuration to improve operations over time */
41
86
  activeLearning?: ActiveLearning
87
+ /** Namespace for organizing tasks (default: 'zai') */
42
88
  namespace?: string
43
89
  }
44
90
 
@@ -74,6 +120,68 @@ const _ZaiConfig = z.object({
74
120
  .default('zai'),
75
121
  })
76
122
 
123
+ /**
124
+ * Zai - A type-safe LLM utility library for production-ready AI operations.
125
+ *
126
+ * Zai provides high-level abstractions for common AI tasks with built-in features like:
127
+ * - Active learning (learns from successful operations)
128
+ * - Automatic chunking for large inputs
129
+ * - Retry logic with error recovery
130
+ * - Usage tracking (tokens, cost, latency)
131
+ * - Type-safe schema validation with Zod
132
+ *
133
+ * @example Basic usage
134
+ * ```typescript
135
+ * import { Client } from '@botpress/client'
136
+ * import { Zai } from '@botpress/zai'
137
+ * import { z } from '@bpinternal/zui'
138
+ *
139
+ * const client = new Client({ token: process.env.BOTPRESS_TOKEN })
140
+ * const zai = new Zai({ client })
141
+ *
142
+ * // Extract structured data
143
+ * const schema = z.object({
144
+ * name: z.string(),
145
+ * age: z.number()
146
+ * })
147
+ * const person = await zai.extract('John is 30 years old', schema)
148
+ * // Output: { name: 'John', age: 30 }
149
+ *
150
+ * // Check conditions
151
+ * const isPositive = await zai.check('I love this product!', 'Is the sentiment positive?')
152
+ * // Output: true
153
+ *
154
+ * // Summarize text
155
+ * const summary = await zai.summarize(longDocument, { length: 100 })
156
+ * ```
157
+ *
158
+ * @example With active learning
159
+ * ```typescript
160
+ * const zai = new Zai({
161
+ * client,
162
+ * activeLearning: {
163
+ * enable: true,
164
+ * tableName: 'SentimentTable',
165
+ * taskId: 'product-reviews'
166
+ * }
167
+ * })
168
+ *
169
+ * // Enable learning for specific task
170
+ * const result = await zai.learn('sentiment').check(review, 'Is this positive?')
171
+ * // Future calls will use approved examples for better accuracy
172
+ * ```
173
+ *
174
+ * @example Chaining configuration
175
+ * ```typescript
176
+ * // Use fast model for quick operations
177
+ * const fastZai = zai.with({ modelId: 'fast' })
178
+ * await fastZai.check(text, 'Is this spam?')
179
+ *
180
+ * // Use specific model
181
+ * const gpt4Zai = zai.with({ modelId: 'openai:gpt-4' })
182
+ * await gpt4Zai.extract(document, complexSchema)
183
+ * ```
184
+ */
77
185
  export class Zai {
78
186
  protected static tokenizer: TextTokenizer = null!
79
187
  protected client: Cognitive
@@ -88,6 +196,27 @@ export class Zai {
88
196
  protected adapter: Adapter
89
197
  protected activeLearning: ActiveLearning
90
198
 
199
+ /**
200
+ * Creates a new Zai instance with the specified configuration.
201
+ *
202
+ * @param config - Configuration object containing client, model, and learning settings
203
+ *
204
+ * @example
205
+ * ```typescript
206
+ * import { Client } from '@botpress/client'
207
+ * import { Zai } from '@botpress/zai'
208
+ *
209
+ * const client = new Client({ token: 'your-token' })
210
+ * const zai = new Zai({
211
+ * client,
212
+ * modelId: 'best',
213
+ * namespace: 'my-app',
214
+ * userId: 'user-123'
215
+ * })
216
+ * ```
217
+ *
218
+ * @throws {Error} If the configuration is invalid (e.g., invalid modelId format)
219
+ */
91
220
  public constructor(config: ZaiConfig) {
92
221
  this._originalConfig = config
93
222
  const parsed = _ZaiConfig.parse(config) as ZaiConfig
@@ -146,6 +275,41 @@ export class Zai {
146
275
  return `${this.namespace}/${this.activeLearning.taskId}`.replace(/\/+/g, '/')
147
276
  }
148
277
 
278
+ /**
279
+ * Creates a new Zai instance with merged configuration options.
280
+ *
281
+ * This method allows you to create variations of your Zai instance with different
282
+ * settings without modifying the original. Useful for switching models, namespaces,
283
+ * or other configuration on a per-operation basis.
284
+ *
285
+ * @param options - Partial configuration to override the current settings
286
+ * @returns A new Zai instance with the merged configuration
287
+ *
288
+ * @example Switch to a faster model
289
+ * ```typescript
290
+ * const zai = new Zai({ client })
291
+ *
292
+ * // Use fast model for simple operations
293
+ * const fastZai = zai.with({ modelId: 'fast' })
294
+ * await fastZai.check(text, 'Is this spam?')
295
+ *
296
+ * // Use best model for complex operations
297
+ * const bestZai = zai.with({ modelId: 'best' })
298
+ * await bestZai.extract(document, complexSchema)
299
+ * ```
300
+ *
301
+ * @example Change namespace
302
+ * ```typescript
303
+ * const customerZai = zai.with({ namespace: 'customer-support' })
304
+ * const salesZai = zai.with({ namespace: 'sales' })
305
+ * ```
306
+ *
307
+ * @example Use specific model
308
+ * ```typescript
309
+ * const gpt4 = zai.with({ modelId: 'openai:gpt-4' })
310
+ * const claude = zai.with({ modelId: 'anthropic:claude-3-5-sonnet-20241022' })
311
+ * ```
312
+ */
149
313
  public with(options: Partial<ZaiConfig>): Zai {
150
314
  return new Zai({
151
315
  ...this._originalConfig,
@@ -153,6 +317,56 @@ export class Zai {
153
317
  })
154
318
  }
155
319
 
320
+ /**
321
+ * Creates a new Zai instance with active learning enabled for a specific task.
322
+ *
323
+ * Active learning stores successful operation results and uses them as examples for
324
+ * future operations, improving accuracy and consistency over time. Each task ID
325
+ * maintains its own set of learned examples.
326
+ *
327
+ * @param taskId - Unique identifier for the learning task (alphanumeric, hyphens, underscores, slashes)
328
+ * @returns A new Zai instance with active learning enabled for the specified task
329
+ *
330
+ * @example Sentiment analysis with learning
331
+ * ```typescript
332
+ * const zai = new Zai({
333
+ * client,
334
+ * activeLearning: {
335
+ * enable: false,
336
+ * tableName: 'AppLearningTable',
337
+ * taskId: 'default'
338
+ * }
339
+ * })
340
+ *
341
+ * // Enable learning for sentiment analysis
342
+ * const sentimentZai = zai.learn('sentiment-analysis')
343
+ * const result = await sentimentZai.check(review, 'Is this review positive?')
344
+ *
345
+ * // Each successful call is stored and used to improve future calls
346
+ * ```
347
+ *
348
+ * @example Different tasks for different purposes
349
+ * ```typescript
350
+ * // Extract user info with learning
351
+ * const userExtractor = zai.learn('user-extraction')
352
+ * await userExtractor.extract(text, userSchema)
353
+ *
354
+ * // Extract product info with separate learning
355
+ * const productExtractor = zai.learn('product-extraction')
356
+ * await productExtractor.extract(text, productSchema)
357
+ *
358
+ * // Each task learns independently
359
+ * ```
360
+ *
361
+ * @example Combining with other configuration
362
+ * ```typescript
363
+ * // Use fast model + learning
364
+ * const fastLearner = zai.with({ modelId: 'fast' }).learn('quick-checks')
365
+ * await fastLearner.check(email, 'Is this spam?')
366
+ * ```
367
+ *
368
+ * @see {@link ZaiConfig.activeLearning} for configuration options
369
+ */
156
370
  public learn(taskId: string) {
157
371
  return new Zai({
158
372
  ...this._originalConfig,