Skip to content

Services and value objects

ai_finetuner.runner (FinetuneRunner)

Builds datasets and drives trainings. Autowire it by type (Drupal\ai_finetuner\FinetuneRunner) or fetch ai_finetuner.runner from the container.

Method What it does
getPlugin(FinetuneJobInterface $job): AiFinetuneInterface Instantiates the job's plugin.
buildDataset(FinetuneJobInterface $job): FinetuneDataset Builds the dataset from the job's source. Throws \RuntimeException when it cannot.
buildFromEntityDisplay(array $config, string $type): FinetuneDataset The entity display source, callable without a job. $config keys: entity_type, bundle, view_mode, ids, limit.
buildFromView(array $config, string $type): FinetuneDataset The Views source. $config keys: view_id, display_id.
start(FinetuneJobInterface $job): FinetuneStatus Validates, starts the training through the plugin, applies and saves the status, logs.
refresh(FinetuneJobInterface $job): FinetuneStatus Asks the plugin for the status, applies and saves it.
cancel(FinetuneJobInterface $job): FinetuneStatus Cancels through the plugin, applies and saves.
refreshActive(): int Refreshes every pending or running job; returns how many. Cron calls this.
filesToImages(array $files): ImageFile[], fileToImage(FileInterface $file): ImageFile Convert managed files to the AI module's ImageFile.
htmlToText(string $html): string Strips scripts and styles, then converts through the AI module's HTML to Markdown converter when available, otherwise strips tags.

Creating and starting a job from code:

use Drupal\ai_finetuner\Entity\FinetuneJob;

$job = FinetuneJob::create([
  'label' => 'Product photos',
  'plugin' => 'krea_lora',
  'dataset_type' => 'image',
  'base_model' => 'flux_dev',
  'training_type' => 'Object',
  'source_type' => FinetuneJob::SOURCE_ENTITY_DISPLAY,
  'source_config' => [
    'entity_type' => 'media',
    'bundle' => 'image',
    'view_mode' => 'ai_finetuner',
    'limit' => 50,
  ],
]);
$job->save();

$status = \Drupal::service('ai_finetuner.runner')->start($job);

plugin.manager.ai_finetuner (AiFinetunePluginManager)

A standard DefaultPluginManager plus:

  • getOptions(bool $usable_only = TRUE, ?string $dataset_type = NULL): array returns id => label, filtered to usable plugins and, optionally, to plugins that accept a dataset type.

FinetuneDataset

Immutable value object describing the training material.

  • FinetuneDataset::images(ImageFile[] $images) and FinetuneDataset::texts(array $records) build one.
  • $dataset->type is FinetuneDataset::TYPE_IMAGE or TYPE_TEXT.
  • $dataset->images, $dataset->texts hold the items; count() returns the number for the active type.
  • toJsonl() encodes the text records as one JSON object per line.
  • withImage(ImageFile $image) returns a copy with one more image.

FinetuneStatus

Immutable value object returned by every plugin call and applied to the job.

new FinetuneStatus(
  status: FinetuneStatus::RUNNING,   // draft, pending, running, completed, failed, cancelled
  remoteJobId: 'job_123',            // stored when set
  resultId: NULL,                    // stored when set; the trained model or style id
  message: 'Training, step 200/500', // stored as the job message
  raw: $response,                    // stored on the job's result field
);

FinetuneStatus::ACTIVE lists the two active statuses; isActive() checks against it.

FinetuneJobInterface

The ai_finetuner entity. Getters: getPluginId(), getDatasetType(), getBaseModel(), getTrainingType(), getTriggerWord(), getSettings(), getImageFiles(), getSourceType(), getSourceConfig(), getStatus(), isActive(), getRemoteJobId(), getResultId(), getMessage(). applyStatus(FinetuneStatus $status) copies a status onto the entity, updates last_checked and sets completed the first time a job completes; it does not save.

Source type constants: FinetuneJob::SOURCE_UPLOAD, SOURCE_ENTITY_DISPLAY, SOURCE_VIEWS.

Base fields: label, plugin, dataset_type, base_model, training_type, trigger_word, settings (map), images (image, unlimited), source_type, source_config (map), status, remote_job_id, result_id, result (map), message, last_checked, completed, created, changed, uid.

Access is a single check on administer ai finetuning for every operation.

Views plugins

  • Display plugin ai_finetuner (FinetuneDatasetDisplay): no route, no pager, no exposed filters, no areas. It forces the style below and the fields row plugin.
  • Style plugin ai_finetuner_dataset (FinetuneDatasetStyle): options title_field, text_field, image_field. buildDataset(?FinetuneRunner $runner = NULL): array returns records with id, title, text and images (ImageFile[]) from the executed view; render() shows a preview table in the Views UI.

The runner uses buildDataset() on the style plugin after executing the view, so any other code that executes a view with this display can do the same.