AI Interpolator

A framework module that enables automatic population of entity fields using AI-based processing from source text fields.

ai_interpolator
106 sites
43
drupal.org

Overview

AI Interpolator provides a powerful plugin-based framework for automatically populating Drupal entity fields using AI services. The module works by reading content from a source field (typically a text field) and using AI to generate appropriate values for target fields of various types.

The module itself does not include any AI providers - it serves as a framework that other modules can extend with specific AI implementations (like OpenAI, Claude, etc.). This architecture allows site builders to choose their preferred AI service while maintaining consistent field configuration interfaces.

Key architectural features include:

  • Plugin-based Field Rules: Extensible system for defining how different field types should be populated using AI
  • Multiple Processing Methods: Support for direct synchronous processing, JavaScript batch processing, or queue-based asynchronous processing via cron
  • Event System: Comprehensive events for intercepting and modifying interpolation behavior at various stages
  • Token Support: Advanced mode using Drupal tokens for dynamic prompt generation
  • Status Tracking: Automatic status field for tracking interpolation progress (pending, processing, failed, finished)

Features

  • Plugin-based architecture for defining AI field rules for different field types (text, taxonomy, boolean, numeric, entity reference, image, link, options, etc.)
  • Three processing modes: Direct (synchronous), Batch (JavaScript-based), and Queue/Cron (asynchronous background processing)
  • Automatic status field tracking for entities with interpolation enabled (pending, processing, failed, finished)
  • Base Mode: Uses a source field's content with customizable prompts using Twig-style placeholders
  • Advanced Token Mode: Integration with Drupal Token module for dynamic prompt generation based on entity data
  • Weight-based field processing order for handling dependencies between interpolated fields
  • Edit mode option to re-interpolate fields when source content changes
  • 20 pre-built base classes for common field type rules including SimpleTextChat, ComplexTextChat, Taxonomy, Boolean, Numeric, Options, Lists, Link, EntityReference, TextToImage, ImageToImage, and more
  • Event-driven architecture with 5 events for customizing configuration, field processing, rule visibility, value changes, and verbose information
  • Helper services for common operations: JSON parsing, LLM parameters, file handling, image style preprocessing, text joining
  • Example plugin implementation included for creating custom non-AI rules

Use Cases

Automatic Taxonomy Tagging

Configure a taxonomy term reference field to be automatically populated based on article body content. The AI analyzes the text and selects or creates appropriate tags from the configured vocabulary. Supports auto-creation of new terms when enabled, text manipulation (lowercase, uppercase, first character uppercase), and similar tag matching to avoid duplicates.

Content Summarization

Set up a summary text field that automatically generates an executive summary from a long-form body field. Use the SimpleTextChat base class with a prompt like 'Summarize the following content in 2-3 sentences: {{ context }}'. The summary is generated when content is saved.

Sentiment Analysis

Add a list field with sentiment options (positive, negative, neutral) and configure AI Interpolator with the Options base class to automatically analyze and categorize content sentiment based on the body text.

AI-Generated Featured Images

Use the TextToImage base class (requires AI image generation sub-module) to automatically generate featured images based on article titles or descriptions. The AI creates images matching the content theme.

Automated SEO Meta Fields

Configure string fields for meta titles and descriptions to be auto-generated from the main content. Use prompts that instruct the AI to create SEO-optimized text within character limits.

Entity Reference Population

Automatically create and link related entities based on source content. The EntityReference base class can parse content and generate child entities with multiple fields populated by AI.

Boolean Field Classification

Add boolean fields that automatically classify content based on AI analysis. For example, 'Contains Personal Data' or 'Is Time-Sensitive' flags that the AI determines from content analysis.

Numeric Scoring

Use the Numeric base class to have AI assign numeric scores or ratings to content. Respects min/max field settings and validates numeric output.

Link Extraction

Automatically extract and store URLs mentioned in content using the Link base class. The AI identifies links and their context to populate link fields with URI and title.

Asynchronous Bulk Processing

Use Queue/Cron processing mode for importing large amounts of content where AI processing time would cause timeouts. Content is saved immediately with 'pending' status, and AI fields are populated in the background via cron.

Tips

  • Always install at least one AI provider sub-module - the base AI Interpolator module provides the framework but no actual AI functionality
  • Use Queue/Cron processing mode for production sites with slow AI APIs to avoid HTTP timeouts during content editing
  • Set appropriate weights when multiple fields depend on each other's interpolated values - lower weights process first
  • Enable 'Edit when changed' carefully as it will re-process fields on every save when the source field changes
  • Use Advanced Token Mode when you need to include data from referenced entities or complex entity relationships in prompts
  • The status field (ai_interpolator_status) is automatically added to entity bundles when any field has interpolation enabled - use it to filter or display processing status
  • Test prompts thoroughly with representative content before enabling on production - AI responses can vary significantly based on prompt wording
  • Consider creating custom rules without AI for simple transformations (like the StripTags example) to avoid unnecessary API calls
  • Use the GeneralHelper service's parseJson() method when creating rules that expect JSON responses from AI
  • Subscribe to the ai_interpolator.change_value event to post-process AI responses before storage, such as additional validation or formatting

Technical Details

Admin Pages 1
Field Configuration /admin/structure/types/manage/{bundle}/fields/{field_config}

AI Interpolator adds settings to field configuration forms when applicable rules exist for the field type. The settings appear as an 'Enable AI Interpolator' checkbox that reveals additional configuration options when checked.

Hooks 2
hook_ai_interpolator_field_rule_alter

Alter the discovered field rule plugin definitions

hook_ai_interpolator_process_alter

Alter the discovered process rule plugin definitions