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:

  1. Built-in Enhancers: Out-of-the-box solutions for common enrichment tasks.
  2. 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 TypeEnhancer NameFunctionality
TextAutoSummarySummarizes text into concise outputs.
TextAutoKeywordsExtracts key terms from textual data.
ImageAutoVisionCaptions images, performs OCR, and tags objects within images.
VideoAutoChaptersGenerates video chapters based on subtitles or transcriptions.
AudioAutoTranscribeTranscribes speech from audio files into text.

Using Built-in Enhancers

To activate built-in enhancers:

  1. Navigate to the Dashboard and enable them under the Enhancers section.
  2. 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:

  1. Define a Pipeline
  2. Add Steps to the Pipeline
  3. 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

PropertyTypeRequiredDescription
namestringYesUnique identifier for the step.
actionActionYesThe action to perform.
outputObjectNoMaps results to database fields.
ifFunction(ctx):boolNoConditional 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:

  1. Takes input parameters (e.g., prompt, model).
  2. Executes and produces results.
  3. Outputs the results to the pipeline or database.

Available Actions

1. TextGenerator

Generates text using a language model.

ParameterTypeDescription
modelstringModel to use (e.g., openai-gpt-4).
promptFunction(ctx):stringDynamic prompt for the model.
temperaturenumberCreativity 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.

ParameterTypeDescription
modelstringModel to use (e.g., openai-gpt-4).
promptFunction(ctx):stringDynamic prompt for the model.
schemaFunction(ctx):SchemaSchema 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

  1. Reusability:
    • Use ctx.vars to avoid redundant prompts or configurations.
  2. Error Handling:
    • Use onFailure: "continue" for non-critical steps.
  3. 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!

©2024 Unbody