Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Issue #610
  • Loading branch information
rsoika committed Oct 8, 2024
1 parent 75dcf76 commit 685daa2
Showing 1 changed file with 91 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand All @@ -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<Integer> years = chronicleController.getYears();
Collections.reverse(years);
for (int year : years) {
List<Integer> months = chronicleController.getMonths(year);
Collections.reverse(months);
for (int month : months) {
List<ChronicleEntity> chronicleEntries = chronicleController.getChroniclePerMonth(year, month);

// prompt += "\n" + year + "/" + month + "\n\n";
for (ChronicleEntity entry : chronicleEntries) {
String user = userController.getUserName(entry.getUser());

List<ItemCollection> 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]</s>\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
Expand Down

0 comments on commit 685daa2

Please sign in to comment.