Enhancers
Enhancers transform raw data into enriched, actionable knowledge. They are essential for turning unstructured or semi-structured data into a usable format for AI-native applications.
Enhancers are data-type specific, meaning they cater to specific data formats like text, images, or videos. They are also modular and designed to integrate seamlessly with your data pipeline.
Unbody supports two types of enhancers:
- Built-in Enhancers: Out-of-the-box solutions for common enrichment tasks.
- Custom Enhancers: Flexible pipelines tailored to your unique workflows.
Built-in Enhancers
Built-in enhancers are categorized by data type, offering seamless functionality right out of the box.
Available Built-in Enhancers
Data Type | Enhancer Name | Functionality |
---|---|---|
Text | AutoSummary | Summarizes text into concise outputs. |
Text | AutoKeywords | Extracts key terms from textual data. |
Image | AutoVision | Captions images, performs OCR, and tags objects within images. |
Video | AutoChapters | Generates video chapters based on subtitles or transcriptions. |
Audio | AutoTranscribe | Transcribes speech from audio files into text. |
Using Built-in Enhancers
To activate built-in enhancers:
- Navigate to the Dashboard and enable them under the Enhancers section.
- Alternatively, configure them using the Admin API.
import { UnbodyAdmin, ProjectSettings, AutoSummary, AutoKeywords } from 'unbody/admin'
// Initialize the Admin API
const admin = new UnbodyAdmin({
auth: {
username: '[admin-key-id]',
password: '[admin-key-secret]',
},
})
// Fetch an existing project
const project = await admin.projects.get({
id: '[project-id]',
})
// Create a project settings object
const settings = new ProjectSettings()
// Enable AutoSummary and AutoKeywords
settings.set(new AutoSummary(AutoSummary.OpenAI.GPT4o))
settings.set(new AutoKeywords(AutoKeywords.OpenAI.GPT4o))
// Apply settings to the project
await project.updateSettings(settings)
console.log(`Updated enhancers for project: ${project.name}`)
Custom Enhancers
Custom enhancers offer unparalleled flexibility, allowing you to design complex enrichment workflows tailored to your application’s needs.
Why Use Custom Enhancers?
- Specificity: When prebuilt options don’t meet your requirements.
- Complexity: For chaining multiple tasks into a single, cohesive workflow.
- Adaptability: To work with unique or non-standard data types and formats.
Architecture of Custom Enhancers
Custom enhancers follow a structured architecture:
- Pipeline: The overarching container that defines the workflow.
- Steps: Discrete tasks within the pipeline.
- Actions: Core operations performed in each step (e.g., text generation, tagging).
Workflow for Custom Enhancers
Creating a custom enhancer involves three main steps:
- Define a Pipeline
- Add Steps to the Pipeline
- Attach the Pipeline to Project Settings
1. Pipelines
What is a Pipeline?
A pipeline is a container for the entire enhancement workflow. It:
- Targets a specific collection (e.g.,
TextDocument
,VideoFile
). - Contains one or more steps executed in sequence.
- Supports conditional logic to enable or skip steps dynamically.
Defining a Pipeline
import { Enhancement } from 'unbody/admin'
const pipeline = new Enhancement.Pipeline(
'custom_pipeline', // Unique name
'TextDocument', // Target collection
)
2. Steps
What is a Step?
A step represents a single task in the pipeline. Each step:
- Has a name for identification.
- Performs an action.
- Specifies output mappings for storing results.
- Can include conditional execution logic and failure handling.
Anatomy of a Step
Property | Type | Required | Description |
---|---|---|---|
name | string | Yes | Unique identifier for the step. |
action | Action | Yes | The action to perform. |
output | Object | No | Maps results to database fields. |
if | Function(ctx):bool | No | Conditional logic for execution. |
onFailure | `“continue" | "stop”` | No |
Example: Defining a Step
const step = new Enhancement.Step(
'summarize_step',
new Enhancement.Action.TextGenerator({
model: 'openai-gpt-4',
prompt: (ctx) => `Summarize the following: ${ctx.record.content}`,
}),
{
output: {
summary: (ctx) => ctx.result.content,
},
if: (ctx) => ctx.record.content.length > 100, // Execute only if content > 100 chars
onFailure: 'continue', // Continue even if this step fails
},
)
pipeline.add(step)
3. Actions
What is an Action?
Actions are the core operations within a step. Unbody supports various built-in actions, each tailored for specific tasks. All actions share:
- Initialization Parameters: Define what the action does.
- Dynamic Behavior: Use
ctx
to adjust actions based on runtime context.
How Actions Work
Actions are initialized and attached to steps. Each action:
- Takes input parameters (e.g., prompt, model).
- Executes and produces results.
- Outputs the results to the pipeline or database.
Available Actions
1. TextGenerator
Generates text using a language model.
Parameter | Type | Description |
---|---|---|
model | string | Model to use (e.g., openai-gpt-4 ). |
prompt | Function(ctx):string | Dynamic prompt for the model. |
temperature | number | Creativity level (higher = more creative). |
Example:
new Enhancement.Action.TextGenerator({
model: 'openai-gpt-4',
prompt: (ctx) => `Summarize this: ${ctx.record.content}`,
temperature: 0.7,
})
2. StructuredGenerator
Generates structured JSON output based on a schema.
Parameter | Type | Description |
---|---|---|
model | string | Model to use (e.g., openai-gpt-4 ). |
prompt | Function(ctx):string | Dynamic prompt for the model. |
schema | Function(ctx):Schema | Schema for structured output (uses Zod). |
Example:
new Enhancement.Action.StructuredGenerator({
model: 'openai-gpt-4',
prompt: (ctx) => `Extract tags: ${ctx.record.content}`,
schema: (ctx, { z }) => z.object({
tags: z.array(z.string()).describe('Tags for the content'),
}),
})
Best Practices for Custom Enhancers
- Reusability:
- Use
ctx.vars
to avoid redundant prompts or configurations.
- Use
- Error Handling:
- Use
onFailure: "continue"
for non-critical steps.
- Use
- Testing:
- Test pipelines on a small dataset before deployment.
With this structure, every detail is covered comprehensively. Let me know if more areas need elaboration!