Zai class

The package exports one class: Zai. This class gives you access to the library’s methods. To create a new Zai instance using your Botpress client:
const client = new Client({ botId: 'YOUR_BOT_ID', token: 'YOUR_TOKEN' })
const zai = new Zai({ client })
Parameters
options
ZaiConfig
required

Methods

Here’s a reference for all methods available with the Zai object.
All of the methods below return a Response object. If you just await the method’s result, it will return simplest form of the result. However, the Response object also has its own methods for accessing the result, event handling, and request control.

check()

Checks whether a condition is true or false for the given input.
const result = await zai.check(input, condition, options)
Parameters
input
unknown
required
The input data to check the condition against.
condition
string
required
The condition to check against the input.
options
CheckOptions
Returns
Response
Response<CheckResult, boolean>

extract()

Extracts one or many elements from an arbitrary input using a schema.
const result = await zai.extract(input, schema, options)
Parameters
input
unknown
required
The input data to extract elements from.
schema
ZodSchema
required
The Zod schema defining the structure of the data to extract.
options
ExtractOptions
Returns
Response
Response<T>
The extracted data matching the provided schema.

filter()

Filters elements of an array against a condition.
const result = await zai.filter(input, condition, options)
Parameters
input
Array<T>
required
The array of elements to filter.
condition
string
required
The condition to filter elements against.
options
FilterOptions
Returns
Response
Response<Array<T>>
The filtered array containing only elements that match the condition.

label()

Tags the provided input with a list of predefined labels.
const result = await zai.label(input, labels, options)
Parameters
input
unknown
required
The input data to label.
labels
Record<string, string>
required
A mapping of label keys to their descriptions/questions.
options
LabelOptions
Returns
Response
Response<LabelResults, BooleanLabels>

rewrite()

Rewrites a string according to the provided prompt.
const result = await zai.rewrite(original, prompt, options)
Parameters
original
string
required
The original text to rewrite.
prompt
string
required
The prompt describing how to rewrite the text.
options
RewriteOptions
Returns
Response
Response<string>
The rewritten text according to the prompt.

summarize()

Summarizes a text of any length to a summary of the desired length.
const result = await zai.summarize(original, options)
Parameters
original
string
required
The original text to summarize.
options
SummarizeOptions
Returns
Response
Response<string>
The summarized text according to the specified options.

text()

Generates a text of the desired length according to the prompt.
const result = await zai.text(prompt, options)
Parameters
prompt
string
required
The prompt describing what text to generate.
options
TextOptions
Returns
Response
Response<string>
The generated text according to the prompt.

with()

Creates a new Zai instance with modified configuration options.
const newZai = zai.with(options)
Parameters
options
Partial<ZaiConfig>
required
Configuration options to override. Can include any of the ZaiConfig properties: client, userId, modelId, activeLearning, or namespace.
Returns
Zai
Zai
A new Zai instance with the updated configuration.

learn()

Creates a new Zai instance with active learning enabled for the specified task ID.
const learningZai = zai.learn(taskId)
Parameters
taskId
string
required
The ID of the task for active learning. This will be used to organize and retrieve examples for improving future responses.
Returns
Zai
Zai
A new Zai instance with active learning enabled for the specified task.

Response methods

All Zai operations return a Response object that implements promise-like behavior while providing additional functionality for event handling and request control. You can call any of the following methods on the Response object:

result()

Returns the complete result including output, usage statistics, and elapsed time.
const { output, usage, elapsed } = await response.result()
Returns
Promise
Promise<ResultData>

on()

Registers an event listener for the specified event type.
response.on('progress', (usage) => {
  console.log('Request progress:', usage)
})
Parameters
type
'progress' | 'complete' | 'error'
required
The event type to listen for:
  • progress: Emitted during request processing with usage statistics
  • complete: Emitted when the operation completes successfully
  • error: Emitted when an error occurs
listener
(event: EventData) => void
required
The callback function to execute when the event is emitted.
Returns
Response
Response
The same Response instance for method chaining.

off()

Removes an event listener for the specified event type.
const listener = (usage) => console.log(usage)
response.on('progress', listener)
response.off('progress', listener)
Parameters
type
'progress' | 'complete' | 'error'
required
The event type to remove the listener from.
listener
(event: EventData) => void
required
The specific listener function to remove.
Returns
Response
Response
The same Response instance for method chaining.

once()

Registers an event listener that will be called only once.
response.once('complete', (result) => {
  console.log('Operation completed:', result)
})
Parameters
type
'progress' | 'complete' | 'error'
required
The event type to listen for.
listener
(event: EventData) => void
required
The callback function to execute when the event is emitted.
Returns
Response
Response
The same Response instance for method chaining.

bindSignal()

Binds an AbortSignal to the response for cancellation control.
const controller = new AbortController()
response.bindSignal(controller.signal)

// Cancel the operation
controller.abort('User cancelled')
Parameters
signal
AbortSignal
required
The AbortSignal to bind to this response.
Returns
Response
Response
The same Response instance for method chaining.

abort()

Aborts the ongoing operation.
response.abort('Operation cancelled by user')
Parameters
reason
string | Error
Optional reason for the abortion.
Returns
void
void
No return value.

then()

Attaches callbacks for the resolution and/or rejection of the response.
response
  .then(result => console.log('Success:', result))
  .catch(error => console.error('Error:', error))
Parameters
onfulfilled
(value: T) => TResult1 | PromiseLike<TResult1>
Callback to execute when the response resolves successfully.
onrejected
(reason: any) => TResult2 | PromiseLike<TResult2>
Callback to execute when the response rejects.
Returns
PromiseLike
PromiseLike<TResult1 | TResult2>
A promise-like object for further chaining.

catch()

Attaches a callback for handling rejection of the response.
response.catch(error => {
  console.error('Operation failed:', error)
})
Parameters
onrejected
(reason: any) => TResult | PromiseLike<TResult>
Callback to execute when the response rejects.
Returns
PromiseLike
PromiseLike<T | TResult>
A promise-like object for further chaining.