@botpress/zai

中文文档
3.1.3发布于 2026/11/05ISCTypeScriptESM + CJS构建来源已验证近 30 天下载 3.2万

Zui AI (zai) – An LLM utility library written on top of Zui and the Botpress API

原始使用文档
文档目录29 节
  1. ✨ Key Features
  2. 📦 Installation
  3. 🚀 Quick Start
  4. 📚 Core Operations
  5. 1. Extract - Get structured data from text
  6. 2. Check - Verify boolean conditions
  7. 3. Label - Apply multiple labels
  8. 4. Rewrite - Transform text
  9. 5. Filter - Filter arrays with natural language
  10. 6. Group - Organize items into categories
  11. 7. Rate - Score items on a 1-5 scale
  12. 8. Sort - Order items with natural language
  13. 9. Text - Generate content
  14. 10. Summarize - Create summaries
  15. 🧠 Active Learning
  16. ⚙️ Configuration
  17. Model Selection
  18. Progress Tracking
  19. Usage Monitoring
  20. 🎯 Benefits
  21. 📖 Advanced Usage
  22. Chaining Operations
  23. Handling Large Documents
  24. Custom Abort Signals
  25. 🛠️ API Reference
  26. Zai Class
  27. Operations
  28. Response Methods
  29. 📝 License

Zai - AI Operations Made Simple

Zai is a powerful LLM utility library that provides a clean, type-safe API for common AI operations. Built on Zod schemas and the Botpress API, it makes AI operations simple, intuitive, and production-ready.

✨ Key Features

  • 🎯 Simple API - One-liner operations for common AI tasks
  • 🔒 Type Safety - Full TypeScript support with Zod schema validation
  • 🧠 Active Learning - Learn from examples and improve over time
  • ⚡ Performance - Built-in retries, caching, and error handling
  • ♾️ Infinite Documents - Handle any document size with automatic chunking
  • 📊 Usage Tracking - Monitor tokens, costs, and performance

📦 Installation

npm install @botpress/zai @botpress/client @bpinternal/zui

🚀 Quick Start

import { Client } from '@botpress/client'
import { Zai } from '@botpress/zai'
import { z } from '@bpinternal/zui'

// Initialize
const client = new Client({ botId: 'YOUR_BOT_ID', token: 'YOUR_TOKEN' })
const zai = new Zai({ client })

// Extract structured data
const person = await zai.extract(
  'John Doe is 30 years old and lives in New York',
  z.object({
    name: z.string(),
    age: z.number(),
    location: z.string(),
  })
)
// Result: { name: 'John Doe', age: 30, location: 'New York' }

// Check content
const isPositive = await zai.check('This product is amazing!', 'expresses positive sentiment')
// Result: true

// Generate text
const story = await zai.text('Write a short story about AI', { length: 200 })

// Summarize documents
const summary = await zai.summarize(longDocument, { length: 500 })

📚 Core Operations

1. Extract - Get structured data from text

// Extract single object
const product = await zai.extract(
  text,
  z.object({
    name: z.string(),
    price: z.number(),
    inStock: z.boolean(),
  })
)

// Extract array of items
const products = await zai.extract(text, z.array(productSchema))

2. Check - Verify boolean conditions

const result = await zai.check(email, 'is spam')
const { value, explanation } = await result.full()

3. Label - Apply multiple labels

const labels = await zai.label(review, {
  positive: 'expresses positive sentiment',
  technical: 'mentions technical details',
  verified: 'from verified purchaser',
})
// Result: { positive: true, technical: false, verified: true }

4. Rewrite - Transform text

// Translate
const french = await zai.rewrite(text, 'translate to French')

// Change tone
const formal = await zai.rewrite('Hey! What's up?', 'make it professional')

5. Filter - Filter arrays with natural language

const techCompanies = await zai.filter(companies, 'are technology companies')
const recentPosts = await zai.filter(posts, 'were published this week')

6. Group - Organize items into categories

// Group items automatically
const grouped = await zai.group(tasks, {
  instructions: 'Group by priority level',
})
// Result: { 'High Priority': [...], 'Medium Priority': [...], 'Low Priority': [...] }

// Group with initial categories
const categorized = await zai.group(emails, {
  instructions: 'Group by topic',
  initialGroups: [
    { id: 'work', label: 'Work' },
    { id: 'personal', label: 'Personal' },
  ],
})

// Group large datasets efficiently
const organized = await zai.group(largeArray, {
  instructions: 'Group by date',
  chunkLength: 8000, // Process in chunks for better performance
})

7. Rate - Score items on a 1-5 scale

// Auto-generate criteria (returns total score)
const scores = await zai.rate(products, 'is it a good value product?')
// Result: [12, 8, 15] (total scores for each item)

// Get detailed ratings
const { output } = await zai.rate(products, 'is it a good value product?').result()
// Result: [
//   { affordability: 4, quality: 5, features: 3, total: 12 },
//   { affordability: 3, quality: 2, features: 3, total: 8 },
//   ...
// ]

// Use fixed criteria
const ratings = await zai.rate(passwords, {
  length: 'password length (12+ chars = very_good, 8-11 = good, 6-7 = average, 4-5 = bad, <4 = very_bad)',
  complexity: 'character variety (all types = very_good, 3 types = good, 2 types = average, 1 type = bad)',
  strength: 'overall password strength',
})
// Result: [
//   { length: 5, complexity: 5, strength: 5, total: 15 },
//   { length: 1, complexity: 1, strength: 1, total: 3 },
// ]

// Rate large datasets efficiently (parallelized)
const allRatings = await zai.rate(Array(500).fill(item), 'how complete is this?')
// Processes ~500 items in ~120ms with automatic chunking

8. Sort - Order items with natural language

// Sort by natural criteria
const sorted = await zai.sort(emails, 'sort by urgency')
// LLM determines criteria and orders items accordingly

// Sort with detailed results
const { output } = await zai.sort(tasks, 'sort by priority').result()
// output includes scoring breakdown for each item

// Complex multi-criteria sorting
const prioritized = await zai.sort(tickets, 'sort by customer importance and issue severity')

// Sort large datasets efficiently (parallelized with chunking)
const orderedItems = await zai.sort(Array(500).fill(item), 'sort by relevance')

9. Text - Generate content

const blogPost = await zai.text('Write about the future of AI', {
  length: 1000,
  temperature: 0.7,
})

10. Summarize - Create summaries

// Simple summary
const summary = await zai.summarize(article)

// With custom prompt
const technicalSummary = await zai.summarize(paper, {
  length: 500,
  prompt: 'Focus on technical implementation details',
})

🧠 Active Learning

Enable active learning to improve accuracy over time:

const zai = new Zai({
  client,
  activeLearning: {
    enable: true,
    tableName: 'ai_learning_data',
    taskId: 'sentiment-analysis',
  },
})

// Use with task ID for learning
const result = await zai.learn('sentiment-analysis').check(text, 'is positive')

⚙️ Configuration

Model Selection

// Use the best model (default)
const zai = new Zai({ client, model: 'best' })

// Use fast model for speed
const fastZai = new Zai({ client, model: 'fast' })

// Use specific model
const customZai = new Zai({ client, model: 'gpt-4-turbo' })

Progress Tracking

const response = zai.summarize(veryLongDocument)

// Track progress
response.on('progress', (progress) => {
  console.log(`${progress.percent}% complete`)
})

const summary = await response

Usage Monitoring

const result = await zai.extract(text, schema)
const usage = await result.usage()

console.log({
  tokens: usage.totalTokens,
  cost: usage.totalCost,
  latency: usage.totalLatency,
})

🎯 Benefits

  1. Production Ready - Built-in error handling, retries, and rate limiting
  2. Type Safe - Full TypeScript support with runtime validation
  3. Scalable - Handle documents of any size with automatic chunking
  4. Cost Effective - Track usage and optimize with active learning
  5. Developer Friendly - Clean API with method chaining and events

📖 Advanced Usage

Chaining Operations

const processedData = await zai.with({ temperature: 0.3 }).learn('data-extraction').extract(document, complexSchema)

Handling Large Documents

// Automatically chunks and processes in parallel
const extractedData = await zai.extract(
  hugeDocument, // 100k+ tokens
  z.array(recordSchema),
  { chunkSize: 4000 }
)

Custom Abort Signals

const controller = new AbortController()
const response = zai.summarize(document, { signal: controller.signal })

// Cancel if needed
setTimeout(() => controller.abort(), 5000)

🛠️ API Reference

Zai Class

  • new Zai(options) - Create instance with client and configuration
  • .with(config) - Create new instance with merged configuration
  • .learn(taskId) - Enable active learning for specific task

Operations

  • .extract(content, schema, options?) - Extract structured data
  • .check(content, condition, options?) - Verify boolean condition
  • .label(content, criteria, options?) - Apply multiple labels
  • .rewrite(content, instruction, options?) - Transform text
  • .filter(items, condition, options?) - Filter array items
  • .group(items, options?) - Organize items into categories
  • .rate(items, instructions, options?) - Rate items on 1-5 scale
  • .sort(items, instructions, options?) - Order items with natural language
  • .text(prompt, options?) - Generate text
  • .summarize(content, options?) - Create summary

Response Methods

  • await response - Get simple result
  • await response.full() - Get detailed result with metadata
  • await response.usage() - Get usage statistics
  • response.on('progress', handler) - Track progress
  • response.abort() - Cancel operation

📝 License

MIT - See LICENSE file for details

版本与发布记录

@botpress/zai 共发布 124 个版本,首次发布于 2024/12/12;过去 12 个月发布了 62 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.1.3latest2026/11/053 天前
3.1.22026/10/2415 天前
3.1.12026/10/2415 天前
3.1.02026/10/2415 天前
3.0.52026/09/171 个月前
3.0.42026/09/052 个月前

关于 @botpress/zai 的常见问题

@botpress/zai 是什么?

Zui AI (zai) – An LLM utility library written on top of Zui and the Botpress API

如何安装 @botpress/zai?

在项目目录运行 npm install @botpress/zai,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@bpinternal/zui(^2.4.1)、@bpinternal/thicktoken(^3.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@botpress/zai 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@botpress/zai 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@botpress/zai 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@botpress/zai 可以商用吗?

@botpress/zai 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@botpress/zai 的下载量有多少?

根据 npm 官方下载统计,@botpress/zai 在 2026/10/09 – 2026/11/07 这 30 天内被下载 32,337 次,日均约 1,078 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@botpress/zai 还在维护吗?

最新版本 3.1.3 发布于 2026/11/05(3 天前);至今共发布 124 个版本,其中过去 12 个月发布了 62 个;首次发布于 2024/12/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。