new ChronicleTextBuilder and custom Order Plugin

This commit is contained in:
Ralph Soika 2025-12-28 18:08:38 +01:00
parent 15116bf299
commit 9964d78b2f
2 changed files with 500 additions and 259 deletions

View file

@ -14,29 +14,37 @@
package ai.dynamixs;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.imixs.ai.rag.RAGService;
import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.WorkflowKernel;
import org.imixs.workflow.engine.WorkflowService;
import org.imixs.workflow.exceptions.AdapterException;
import org.imixs.workflow.exceptions.PluginException;
import org.imixs.workflow.exceptions.QueryException;
import jakarta.inject.Inject;
/**
* This RAGIndexPlugin extends the default imixs-ai RAGIndexPlugin.
* The OrderPlugin is a generic plugin for Order workflows.
* <p>
* The plugin collects a list of $uniqueids for the last 5 orders for the given
* customer.ref. The list is stored in the item `order.history.ref`
*
* In the mode INDEX this plugins generates a default prompt template if no
* prompt definition was found. This is to simplify the models.
* <p>
* The plugin extends the default imixs-ai RAGIndexPlugin. In the mode INDEX
* this plugins generates a default prompt template.This is to simplify the
* models.
* <p>
*
* <pre>
{@code
<imixs-ai name="INDEX">
<dynamixs-ai name="INDEX">
<debug>false</debug>
</imixs-ai>
</dynamixs-ai>
}
* </pre>
* <p>
@ -46,10 +54,11 @@ import jakarta.inject.Inject;
* @author rsoika
*
*/
public class RAGDefaultIndexPlugin extends org.imixs.ai.rag.RAGIndexPlugin {
private static final Logger logger = Logger.getLogger(RAGDefaultIndexPlugin.class.getName());
public class OrderPlugin extends org.imixs.ai.rag.RAGIndexPlugin {
private static final Logger logger = Logger.getLogger(OrderPlugin.class.getName());
public static final String LINK_PROPERTY = "$workitemref";
public static final int MAX_HISTORY_RESULT = 5;
@Inject
private WorkflowService workflowService;
@ -65,10 +74,13 @@ public class RAGDefaultIndexPlugin extends org.imixs.ai.rag.RAGIndexPlugin {
return workitem;
}
logger.info("├── running Dynamixs.AI RAGPlugin");
logger.info("├── running Dynamixs.AI Order Plugin");
// Update History....
updateOrderHistory(workitem);
// read optional configuration form the model or imixs.properties....
List<ItemCollection> ragIndexDefinitions = workflowService.evalWorkflowResultXML(event, "imixs-ai",
List<ItemCollection> ragIndexDefinitions = workflowService.evalWorkflowResultXML(event, "dynamixs-ai",
RAGService.RAG_INDEX, workitem, false);
try {
@ -85,16 +97,52 @@ public class RAGDefaultIndexPlugin extends org.imixs.ai.rag.RAGIndexPlugin {
workitem.setItemValue("ai.chronicle.summary", promptTemplate);
indexDefinition.setItemValue("promptTemplate", embedTemplate(promptTemplate));
ragService.createIndex(ragIndexDefinitions, workitem, event);
}
}
} catch (AdapterException e) {
throw new PluginException(e);
}
return workitem;
}
/**
* This method updates the item `order.history.ref` with the latest 5 orders for
* the current customer
*
* @param workitem
*/
private void updateOrderHistory(ItemCollection workitem) {
workitem.removeItem("order.history.ref");
String customerRef = workitem.getItemValueString("customer.ref");
if (!customerRef.isEmpty()) {
// append customer ref to $workitemref
workitem.appendItemValueUnique("$workitemref", customerRef);
// collect all history orders....
String sQuery = "(";
sQuery = " (type:\"workitem\" OR type:\"workitemarchive\") AND (" + LINK_PROPERTY + ":\"" + customerRef
+ "\") AND ($workflowgroup:\"" + workitem.getWorkflowGroup() + "\")";
List<ItemCollection> orderHistory = null;
try {
orderHistory = workflowService.getDocumentService().findStubs(sQuery, MAX_HISTORY_RESULT, 0,
WorkflowKernel.LASTEVENTDATE, true);
} catch (QueryException e) {
e.printStackTrace();
}
if (orderHistory != null) {
List<String> orderHistoryRefList = new ArrayList<>();
for (ItemCollection order : orderHistory) {
if (!workitem.getUniqueID().equals(order.getUniqueID())) { // skip current id!
orderHistoryRefList.add(order.getUniqueID());
}
}
workitem.setItemValue("order.history.ref", orderHistoryRefList);
}
// default behavior
return super.run(workitem, event);
} catch (AdapterException e) {
throw new PluginException(e);
}
}

File diff suppressed because it is too large Load diff