Function
Call AI like a function
Everything starts with ai. Call it, chain it, destructure it.
import { ai } from 'ai-functions'
// Template literal
const explanation = await ai`Explain ${topic} to a 5 year old`
// Dynamic functions
const code = await ai.generateAuthMiddleware(requirements)
const analysis = await ai.analyzeMarketTrends(data)
// Destructuring
const { title, tags, excerpt } = ai`metadata for: ${blogPost}`Promise Pipelining
Chain operations without await—dependencies resolve automatically:
const { score, qualified, reason } = ai`qualify ${lead}`
// Chain naturally
const email = ai`follow-up email based on: ${reason}`
const subject = ai`subject line for: ${email}`
// Only await when you need the value
if (await qualified) {
await send({ subject: await subject, body: await email })
}Batch Processing
Process arrays in a single call:
const leads = list`leads from ${campaign}`
const qualified = await leads.map(lead => ({
lead,
score: ai`score 1-100: ${lead}`,
fit: ai`${lead} matches ${icp}?`,
}))All Functions
Core
| Export | Description |
|---|---|
AI() | Create typed AI instances with predefined schemas |
ai | The simplest way to call AI |
define | The foundation for creating AI functions |
generate | The core generation primitive with schema support |
Function Types
Every ai.doSomething() becomes one of four function types:
| Type | What It Does | Returns |
|---|---|---|
| Code | Generates executable code | Code string |
| Generative | Creates content/data | Structured data |
| Agentic | Takes autonomous action | Action results |
| Human | Requires human input | Human response |
Generative
| Function | Description |
|---|---|
list | Generate a list of items |
lists | Generate multiple named lists |
write | Generate text content |
summarize | Summarize content |
extract | Extract structured data |
diagram | Generate diagrams |
slides | Generate presentations |
image | Generate images |
video | Generate video |
Logic
Action
| Function | Description |
|---|---|
do | Execute actions |
browse | Browse the web |
read | Read and parse content |
research | Research a topic |
ask | Ask a question (agent or human) |
approve | Request approval (agent or human) |
review | Request review (agent or human) |
Examples
Sales Pipeline
const leads = list`leads from ${campaign}`
const qualified = await leads.map(lead => ({
lead,
score: ai`score 1-100: ${lead}`,
fit: ai`${lead} matches ${icp}?`,
}))
const approved = qualified.filter(l => l.score > 80)
const emails = await approved.map(l => write`follow-up for ${l.lead}`)Content Generation
const ai = AI({
blogPost: {
title: 'Post title',
summary: 'Brief summary',
sections: [{
heading: 'Section heading',
content: 'Section content',
}],
},
})
const post = await ai.blogPost('TypeScript tips')
const summary = await summarize(post)
const tags = await list`tags for: ${post.title}`Code Generation
const middleware = await ai.generateAuthMiddleware({
provider: 'jwt',
roles: ['admin', 'user', 'guest'],
})
const migration = await ai.createMigration({
action: 'add users table',
columns: ['id', 'email', 'name', 'created_at'],
})Human-in-the-Loop
const proposal = await ai.generateProposal(requirements)
const approved = await approve`${proposal} for ${client}`
if (approved) {
const feedback = await review`implementation plan for ${proposal}`
const final = await ai.refineProposal(proposal, feedback)
}Installation
npm install ai-functionsPackage
The Function primitive is implemented in the ai-functions package.
See the package documentation for full API reference and additional examples.
Was this page helpful?