From 685daa25bd55fe31a79ea3f229e766cb4068cec9 Mon Sep 17 00:00:00 2001 From: Ralph Soika Date: Tue, 8 Oct 2024 15:32:32 +0200 Subject: [PATCH] update Issue #610 --- .../workflow/office/forms/AIController.java | 93 ++++++++++++++++++- 1 file changed, 91 insertions(+), 2 deletions(-) diff --git a/imixs-office-workflow-util/src/main/java/org/imixs/workflow/office/forms/AIController.java b/imixs-office-workflow-util/src/main/java/org/imixs/workflow/office/forms/AIController.java index def2ee6d..4ba7dd36 100644 --- a/imixs-office-workflow-util/src/main/java/org/imixs/workflow/office/forms/AIController.java +++ b/imixs-office-workflow-util/src/main/java/org/imixs/workflow/office/forms/AIController.java @@ -32,14 +32,20 @@ import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Base64; +import java.util.Collections; +import java.util.Date; import java.util.List; import java.util.Map; import java.util.logging.Logger; import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.imixs.marty.profile.UserController; +import org.imixs.workflow.FileData; import org.imixs.workflow.ItemCollection; +import org.imixs.workflow.WorkflowKernel; import org.imixs.workflow.exceptions.PluginException; import org.imixs.workflow.faces.data.WorkflowController; import org.imixs.workflow.faces.data.WorkflowEvent; @@ -80,6 +86,12 @@ public class AIController implements Serializable { @Inject protected WorkflowController workflowController; + @Inject + protected ChronicleController chronicleController; + + @Inject + protected UserController userController; + @Inject @ConfigProperty(name = LLM_SERVICE_ENDPOINT, defaultValue = "a") String serviceEndpoint; @@ -253,11 +265,14 @@ public String postPromptCompletion(String apiEndpoint, JsonObject jsonPromptObje * @param prompt_options * @return */ - public JsonObject buildJsonPromptObject(String prompt, String prompt_options) { + public JsonObject buildJsonPromptObject(String question, String prompt_options) { // Create a JsonObjectBuilder instance JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder(); - jsonObjectBuilder.add("prompt", prompt); + + String contextPrompt = buildContextPrompt(question); + System.out.println(contextPrompt); + jsonObjectBuilder.add("prompt", contextPrompt); // Do we have options? if (prompt_options != null && !prompt_options.isEmpty()) { @@ -280,6 +295,80 @@ public JsonObject buildJsonPromptObject(String prompt, String prompt_options) { return jsonObject; } + /** + * This helper method creates a complex prompt containing the chronical data + * + * The prompt finishes with the given question. + * + * @param question + * @return + */ + private String buildContextPrompt(String question) { + + ItemCollection workitem = workflowController.getWorkitem(); + SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); + + String prompt = "[INST]"; + + prompt = "Geschäftsprozess: " + workitem.getWorkflowGroup() + "\n"; + prompt += "Erstellt: " + dateFormat.format(workitem.getItemValueDate(WorkflowKernel.CREATED)) + " von " + + userController.getUserName(workitem.getItemValueString("$creator")) + " \n"; + prompt += "Aktueller Status: " + workitem.getItemValueString(WorkflowKernel.WORKFLOWSTATUS) + "\n"; + + // chronical + + List years = chronicleController.getYears(); + Collections.reverse(years); + for (int year : years) { + List months = chronicleController.getMonths(year); + Collections.reverse(months); + for (int month : months) { + List chronicleEntries = chronicleController.getChroniclePerMonth(year, month); + + // prompt += "\n" + year + "/" + month + "\n\n"; + for (ChronicleEntity entry : chronicleEntries) { + String user = userController.getUserName(entry.getUser()); + + List cronicleEvents = entry.entries; + Collections.reverse(cronicleEvents); + + for (ItemCollection event : cronicleEvents) { + + // Date / User + prompt += dateFormat.format(entry.getDate()) + " - " + user + ": "; + + String type = event.getItemValueString("type"); + + Date date = event.getItemValueDate("date"); + String message = event.getItemValueString("message"); + + if ("comment".equals(type)) { + prompt += "Kommentar: " + message + "\n"; + } + if ("history".equals(type)) { + prompt += message + "\n"; + } + if ("dms".equals(type)) { + String fileName = event.getItemValueString("name"); + FileData fileData = workitem.getFileData(fileName); + String fileContent = fileData.getAttribute("text").toString(); + if (fileContent != null && !fileContent.isEmpty()) { + prompt += "Neues Dokument hinzugefügt: " + fileName + "\n\n"; + prompt += fileContent + "\n\n"; + } + } + + } + + } + } + } + + prompt += "[/INST]\n[INST] " + question + "[/INST]"; + return prompt; + + } + /** * This method processes a OpenAI API prompt result in JSON format. The method * expects a workitem* including the item 'ai.result' providing the LLM result