/**************************************************************************** * Copyright (c) 2022-2025 Imixs Software Solutions GmbH and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at * https://www.eclipse.org/legal/epl-2.0 * * This Source Code may also be made available under the terms of the * GNU General Public License, version 2 or later (GPL-2.0-or-later), * which is available at https://www.gnu.org/licenses/old-licenses/gpl-2.0.html * * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later ****************************************************************************/ package ai.dynamixs.cognition; import java.util.List; import java.util.logging.Logger; import org.imixs.ai.workflow.ImixsAIPromptService; import org.imixs.workflow.ItemCollection; import org.imixs.workflow.engine.EventLogService; import org.imixs.workflow.engine.WorkflowService; import org.imixs.workflow.exceptions.PluginException; import ai.dynamixs.ChronicleTextBuilder; import jakarta.inject.Inject; /** * The CognitoPlugin is a generic plugin to generate an cognito.create.event. * This event is processed by the `CognitoEventService` to generate an AI based * workflow abstract *

* The plugin can be activated by the following workflow result tag * *

  {@code
   
    }
 * 
*

* The embedded tag 'debug' is optional * *

* The prompt definition is either loaded from the corresponding BPMN event * (DataObject) or generated as generic default template in case that no * PromptDefinition is assigned to the current event. * * * @author rsoika * */ public class CognitoPlugin extends org.imixs.ai.rag.RAGIndexPlugin { private static final Logger logger = Logger.getLogger(CognitoPlugin.class.getName()); @Inject private WorkflowService workflowService; @Inject private EventLogService eventLogService; @Inject private ImixsAIPromptService imixsAIPromptService; @Override public ItemCollection run(ItemCollection workitem, ItemCollection event) throws PluginException { // do only run on type = workitem! if (!workitem.getType().equals("workitem")) { // no op! return workitem; } // read optional configuration form the model or imixs.properties.... List abstarctAIDefinitions = workflowService.evalWorkflowResultXML(event, "dynamixs-ai", "COGNITO", workitem, false); // INDEX Mode? if (abstarctAIDefinitions != null && abstarctAIDefinitions.size() > 0) { logger.info("├── running Dynamixs.AI Cognito Plugin"); // test if the event provides a Prompt template! String promptTemplate = imixsAIPromptService.loadPromptTemplateByModelElement(event); if (promptTemplate != null && !promptTemplate.isBlank()) { logger.info("│ ├── load custom cognito prompt template from event"); } else { promptTemplate = buildDefaultPromptTemplate(workitem); } // we create a data object that holds the template ItemCollection promptItemCol = new ItemCollection(); promptItemCol.setItemValue(CognitoEventService.ITEM_PROMPT_DEFINITION, promptTemplate); eventLogService.createEvent(CognitoEventService.EVENTLOG_TOPIC_COGNITO_PROMPT, workitem.getUniqueID(), promptItemCol); } return workitem; } /** * This method generates a generic prompt definition including a prompt template * including the workflow chronicle data. * * @param workitem * @return */ private String buildDefaultPromptTemplate(ItemCollection workitem) { // generate default template ChronicleTextBuilder chronicleTextBuilder = new ChronicleTextBuilder(workitem); String promptTemplate = chronicleTextBuilder.build(); String result = "\n" + // "\n" + // "\t{\"n_predict\": 4096, \"temperature\": 0.0}\n" + // "\n" + // " \n" + // " \n" + // " \n" + // " \n" + // "\n" + // " "; return result; } }