@botpress/zai
v2.5.7
Published
Zui AI (zai) – An LLM utility library written on top of Zui and the Botpress API
Keywords
Readme
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 chunking8. 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 responseUsage 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
- Production Ready - Built-in error handling, retries, and rate limiting
- Type Safe - Full TypeScript support with runtime validation
- Scalable - Handle documents of any size with automatic chunking
- Cost Effective - Track usage and optimize with active learning
- 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 resultawait response.full()- Get detailed result with metadataawait response.usage()- Get usage statisticsresponse.on('progress', handler)- Track progressresponse.abort()- Cancel operation
📝 License
MIT - See LICENSE file for details
