Resource

Resource Basics

Resource Name AI Prompt
Resource Version 1.0
Category Artificial Intelligence
Downloads 225
Release Date 3/3/2026 10:47:44 PM
 

Resource Details

Description This script allows you to enter a prompt which is sent to the AI service for every selected image. The answer is displayed in a text box
Installation Instructions na
Usage Instructions na
Changes in this version na
Special Requirements
 

Author Information

Author Name IDimager Support
 

Script

{
  This script will use AI to judge an image and you can enter the prompt for it.

  Initial date: 20 feb 2025
  Initial Author: IDimager Support

  Version History:
    Version: 1.0
    Author: IDimager Support
    Date: 20 feb 2025
}

  procedure HandleItem(ACatItem: TCatalogItem; APrompt: WideString);
  var
    AAI: TidBaseOpenAI;
    AAIWorker: TOpenAI;
    AText: WideString;
    APreview: TMemoryStream;
  begin
    AAI := Publiccatalog.CreateOpenAIObject;
    AAIWorker := AAI.Worker;
    // by default, the model from Preferences is used. Optionally change it
    // AAIWorker.Model := oaimGPT4oMini;

    // get the preview
    APreview := TMemoryStream.Create;
    Thumbnailer.LoadRawPreviewStream(ACatItem.GUID, APreview);

    AText := AAIWorker.CompletionsForImageStream(
      APreview,
      'image/jpeg',
      APrompt,
      '',
      nil,
      0
    );
    Say(ACatItem.FileName + CrLf2W + AText + CrLf2W);
    APreview.Free;

    AAI.Free;
  end;

  procedure HandleImageItem(AImg: TImageItem; APrompt: WideString);
  var
    ACatItem: TCatalogItem;
  begin
    // find the image in the catalog
    ACatItem := TCatalogItem.Create(nil);

    if PublicCatalog.FindItemCombinedByGUID(AImg.GUID, ACatItem) then
      HandleItem(ACatItem, APrompt);

    ACatItem.Free;
  end;

var
  i: Integer;
  APro: TxomProgress;
  APrompt: WideString;
begin
  if Selected.Count = 0 then
  begin
    Say('Select at least one thumbnail.');
    exit;
  end;

  APrompt := '';
  if not AskInput('Enter a prompt', 'Enter a prompt', '', APrompt, False, nil) then
    exit;

  if APrompt = '' then
  begin
    Say('no prompt entered.');
    exit;
  end;

  APro := TxomProgress.Create(nil);
  APro.Max := Selected.Count;
  APro.Pos := 0;
  APro.Caption := 'Generate AI results';

  for i := 0 to Selected.Count - 1 do
  begin
    APro.Pos := i + 1;
    if APro.Cancel then
      break;

    HandleImageItem(Selected.Items[i], APrompt);
  end;

  APro.Free;
end;