Execute AI prompt

Prerequisites

You need to create the model definition as described here: https://docs.magnolia-cms.com/ai-accelerator/developers/model-configuration/

Please ensure you have the the newest version of the ai accelerator installed.

After that you can copy this groovy script and you need to change following:

1. Change the prompt text
2. Change the model name

import info.magnolia.ai.task.AiExecutionContext;
import info.magnolia.ai.registry.AiModelRegistry;
import info.magnolia.ai.registry.AiTaskRegistry;
import info.magnolia.ai.model.AiModelExecutor;
import info.magnolia.ai.model.Prompt;
import info.magnolia.objectfactory.Components;
import info.magnolia.rest.definition.dto.Property;
import info.magnolia.rest.definition.dto.PropertyType;
import info.magnolia.rest.definition.dto.Schema;
import info.magnolia.ai.task.ExecutePromptTask;
import com.google.common.collect.ImmutableMap;
import java.util.LinkedHashMap;

/* Setup for AI Task Usage */
AiModelRegistry modelRegistry = Components.getComponent(AiModelRegistry.class);
AiTaskRegistry taskRegistry = Components.getComponent(AiTaskRegistry.class);
AiModelExecutor modelExecutor = Components.getComponent(AiModelExecutor.class);
Schema DEFAULT_TEXT_SCHEMA = Schema.builder()
        .property("text", Property.builder().type(PropertyType.STRING).build())
        .build();
AiExecutionContext aiExecutionContext = new AiExecutionContext();
aiExecutionContext.setSchema(DEFAULT_TEXT_SCHEMA)

/*
 * Add your prompt here
 */
Prompt prompt = Prompt.builder()
        .message(Prompt.Message.builder().role(Prompt.Role.user).content("Tell me a joke").build())
        .build();
aiExecutionContext.setPrompt(prompt);

/*
 * Choose your aiModels yaml name
 */
aiExecutionContext.setModelDefinition(modelRegistry.getByModelId("GPT4o"));

/*
 * Choose your aiTask in our case we only want to do text based prompting. We choose ExecutePromptTask
 */
aiExecutionContext.setTaskDefinition(taskRegistry.getByTaskType(ExecutePromptTask.class).iterator().next());

// Execute Prompt
Map<String, Object> execute = modelExecutor.execute(aiExecutionContext);
Map<String, Object> response = ImmutableMap.<String, Object>builder().putAll(execute).put("prompt", prompt).build();

if (response.get("error") == true) {
    println response.toString()
} else {
    LinkedHashMap responseData = ((LinkedHashMap) response.get("data"));
	
	// Receive your response from your model here
    println responseData.get("text").toString()
}