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:
Methods
Here’s a reference for all methods available with theZai
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.
The input data to check the condition against.
The condition to check against the input.
extract()
Extracts one or many elements from an arbitrary input using a schema.
The input data to extract elements from.
The Zod schema defining the structure of the data to extract.
The extracted data matching the provided schema.
filter()
Filters elements of an array against a condition.
The array of elements to filter.
The condition to filter elements against.
The filtered array containing only elements that match the condition.
label()
Tags the provided input with a list of predefined labels.
The input data to label.
A mapping of label keys to their descriptions/questions.
rewrite()
Rewrites a string according to the provided prompt.
The original text to rewrite.
The prompt describing how to rewrite the text.
The rewritten text according to the prompt.
summarize()
Summarizes a text of any length to a summary of the desired length.
The original text to summarize.
The summarized text according to the specified options.
text()
Generates a text of the desired length according to the prompt.
The prompt describing what text to generate.
The generated text according to the prompt.
with()
Creates a new Zai instance with modified configuration options.
Configuration options to override. Can include any of the
ZaiConfig
properties: client
, userId
, modelId
, activeLearning
, or namespace
.A new Zai instance with the updated configuration.
learn()
Creates a new Zai instance with active learning enabled for the specified task ID.
The ID of the task for active learning. This will be used to organize and retrieve examples for improving future responses.
A new Zai instance with active learning enabled for the specified task.
Response methods
All Zai operations return aResponse
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.
on()
Registers an event listener for the specified event type.
The event type to listen for:
progress
: Emitted during request processing with usage statisticscomplete
: Emitted when the operation completes successfullyerror
: Emitted when an error occurs
The callback function to execute when the event is emitted.
The same Response instance for method chaining.
off()
Removes an event listener for the specified event type.
The event type to remove the listener from.
The specific listener function to remove.
The same Response instance for method chaining.
once()
Registers an event listener that will be called only once.
The event type to listen for.
The callback function to execute when the event is emitted.
The same Response instance for method chaining.
bindSignal()
Binds an AbortSignal to the response for cancellation control.
The AbortSignal to bind to this response.
The same Response instance for method chaining.
abort()
Aborts the ongoing operation.
Optional reason for the abortion.
No return value.
then()
Attaches callbacks for the resolution and/or rejection of the response.
Callback to execute when the response resolves successfully.
Callback to execute when the response rejects.
A promise-like object for further chaining.
catch()
Attaches a callback for handling rejection of the response.
Callback to execute when the response rejects.
A promise-like object for further chaining.