office-dynamixs-ai/_OLD/cognition/CognitoPlugin.java
2026-02-09 11:14:10 +01:00

139 lines
4.8 KiB
Java

/****************************************************************************
* 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
* <p>
* The plugin can be activated by the following workflow result tag
*
* <pre>
{@code
<dynamixs-ai name="COGNITO" />
}
* </pre>
* <p>
* The embedded tag 'debug' is optional
*
* <p>
* 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.workflow.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<ItemCollection> cognitoAIDefinitions = workflowService.evalWorkflowResultXML(event, "dynamixs-ai",
"COGNITO", workitem, false);
// COGNITO Mode?
if (cognitoAIDefinitions != null && cognitoAIDefinitions.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("│ ├── loaded custom cognito prompt template from event");
} else {
promptTemplate = buildDefaultPromptTemplate(workitem);
logger.info("│ ├── created default cognito prompt template");
}
// we create a data object that holds the template
ItemCollection promptItemCol = new ItemCollection();
promptItemCol.setItemValue("category", "cognition");
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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + //
"<PromptDefinition>\n" + //
"\t<prompt_options>{\"n_predict\": 2048, \"temperature\": 0.2, \"top_p\": 0.9}</prompt_options>\n" + //
"\n" + //
" <prompt role=\"system\"><![CDATA[\n" + //
"You are a process analyst. A workflow has just been completed.\n" + //
"\n" + //
"Create a brief project insight:\n" + //
"- What specifically happened? (Include relevant names from people, companies, products when mentioned.)\n"
+ //
"- Is there anything unusual or important to remember for the future? (Pay attention to user comments - they often contain important warnings or decisions.)\n"
+ //
"\n" + //
"No general advice. Only project-specific facts and insights.]]>\n" + //
" </prompt>\n" + //
" <prompt role=\"user\"><![CDATA[\n" + promptTemplate + "]]>\n" + //
" </prompt>\n" + //
"\n" + //
"</PromptDefinition> ";
return result;
}
}