Skip to content

Commit

Permalink
integrated dynamic user-defined prompts into workflow / context menus…
Browse files Browse the repository at this point in the history
…. issue #2395
  • Loading branch information
j-dimension committed Aug 8, 2024
1 parent 28e7d95 commit 3687442
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,10 @@ You should also get your employer (if you work as a programmer) or school,
import com.jdimension.jlawyer.ai.AiRequestStatus;
import com.jdimension.jlawyer.ai.Input;
import com.jdimension.jlawyer.ai.ParameterData;
import com.jdimension.jlawyer.ai.Prompt;
import com.jdimension.jlawyer.client.editors.EditorsRegistry;
import com.jdimension.jlawyer.client.utils.FrameUtils;
import com.jdimension.jlawyer.persistence.AssistantPrompt;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -693,6 +695,9 @@ public class AssistantAccess {
private static AssistantAccess instance = null;
private Map<AssistantConfig, List<com.jdimension.jlawyer.ai.AiCapability>> capabilities = null;

// request type to list of custom prompts
private Map<String, List<AssistantPrompt>> customPrompts = new HashMap<>();

private AssistantAccess() {

}
Expand All @@ -704,6 +709,34 @@ public static synchronized AssistantAccess getInstance() {
return instance;
}

public void flushCustomPrompts() {
synchronized(customPrompts) {
this.customPrompts.clear();
}
}

private Map<String, List<AssistantPrompt>> getCustomPrompts() throws Exception {
synchronized (customPrompts) {
if (customPrompts.isEmpty()) {
ClientSettings settings = ClientSettings.getInstance();
try {
JLawyerServiceLocator locator = JLawyerServiceLocator.getInstance(settings.getLookupProperties());
List<AssistantPrompt> allPrompts = locator.lookupIntegrationServiceRemote().getAllAssistantPrompts();
for(AssistantPrompt p: allPrompts) {
if(!customPrompts.containsKey(p.getRequestType()))
customPrompts.put(p.getRequestType(), new ArrayList<>());
customPrompts.get(p.getRequestType()).add(p);
}
} catch (Exception ex) {
log.error("Error getting AI capabilities", ex);
throw new Exception("Assistenten-Funktionen können nicht ermittelt werden: " + ex.getMessage());
}
}
return customPrompts;
}

}

public Map<AssistantConfig, List<AiCapability>> getCapabilities() throws Exception {
if (this.capabilities == null) {
ClientSettings settings = ClientSettings.getInstance();
Expand All @@ -729,6 +762,9 @@ public Map<AssistantConfig, List<AiCapability>> getCapabilities() throws Excepti
* @throws Exception
*/
public Map<AssistantConfig, List<AiCapability>> filterCapabilities(String requestType, String inputType) throws Exception {

Map<String, List<AssistantPrompt>> prompts=this.getCustomPrompts();

Map<AssistantConfig, List<AiCapability>> all = this.getCapabilities();
Map<AssistantConfig, List<AiCapability>> filtered = new HashMap<>();
for (AssistantConfig config : all.keySet()) {
Expand All @@ -753,6 +789,21 @@ public Map<AssistantConfig, List<AiCapability>> filterCapabilities(String reques
filtered.put(config, new ArrayList<>());
}
filtered.get(config).add(c);

// if capability allows use of custom prompts, add the capability with the custom prompt to the filtered list
if(prompts.containsKey(requestType) && c.isCustomPrompts()) {
for(AssistantPrompt p: prompts.get(requestType)) {
AiCapability clone=(AiCapability)c.clone();
clone.setName(p.getName());
Prompt cp=new Prompt();
if(c.getDefaultPrompt()!=null) {
cp.setMaxTokens(c.getDefaultPrompt().getMaxTokens());
}
cp.setDefaultPrompt(p.getPrompt());
clone.setDefaultPrompt(cp);
filtered.get(config).add(clone);
}
}
}
}
return filtered;
Expand Down Expand Up @@ -818,7 +869,7 @@ public void populateMenu(JPopupMenu menu, Map<AssistantConfig, List<AiCapability
}

}

public void populateMenu(JMenu menu, Map<AssistantConfig, List<AiCapability>> capabilities, AssistantInputAdapter adapter) {

for (AssistantConfig config : capabilities.keySet()) {
Expand All @@ -836,5 +887,5 @@ public void populateMenu(JMenu menu, Map<AssistantConfig, List<AiCapability>> ca
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,7 @@ private void cmdAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:
ap.setPrompt(this.taPrompt.getText());

AssistantPrompt savedPrompt = locator.lookupIntegrationServiceRemote().addAssistantPrompt(ap);
AssistantAccess.getInstance().flushCustomPrompts();

((DefaultTableModel) this.tblPrompts.getModel()).addRow(new Object[]{savedPrompt, savedPrompt.getRequestType()});
this.tblPrompts.getSelectionModel().setSelectionInterval(this.tblPrompts.getRowCount()-1, this.tblPrompts.getRowCount()-1);
Expand Down Expand Up @@ -998,6 +999,7 @@ private void cmdSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST
JLawyerServiceLocator locator = JLawyerServiceLocator.getInstance(settings.getLookupProperties());

AssistantPrompt savedPrompt = locator.lookupIntegrationServiceRemote().updateAssistantPrompt(ap);
AssistantAccess.getInstance().flushCustomPrompts();
row = this.tblPrompts.convertRowIndexToModel(row);
((DefaultTableModel) this.tblPrompts.getModel()).setValueAt(savedPrompt, row, 0);
((DefaultTableModel) this.tblPrompts.getModel()).setValueAt(savedPrompt.getRequestType(), row, 1);
Expand All @@ -1023,6 +1025,7 @@ private void cmdRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIR
JLawyerServiceLocator locator = JLawyerServiceLocator.getInstance(settings.getLookupProperties());

locator.lookupIntegrationServiceRemote().removeAssistantPrompt(ap);
AssistantAccess.getInstance().flushCustomPrompts();
row = this.tblPrompts.convertRowIndexToModel(row);
((DefaultTableModel) this.tblPrompts.getModel()).removeRow(row);

Expand Down
33 changes: 32 additions & 1 deletion j-lawyer-fax/src/com/jdimension/jlawyer/ai/AiCapability.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class AiCapability implements Serializable {
private String requestType;
private String modelType;
private boolean async=false;
private boolean customPrompts=false;

private Prompt defaultPrompt=null;

Expand All @@ -38,7 +39,7 @@ public class AiCapability implements Serializable {

private List<Output> output=new ArrayList<>();

private static List<String> capabilities=new ArrayList<>();
private static final List<String> capabilities=new ArrayList<>();

static {
capabilities.add(REQUESTTYPE_CHAT);
Expand All @@ -52,6 +53,22 @@ public class AiCapability implements Serializable {
public static List<String> capabilities() {
return capabilities;
}

@Override
public Object clone() throws CloneNotSupportedException {
AiCapability clone=new AiCapability();
clone.async=async;
clone.customPrompts=customPrompts;
clone.defaultPrompt=defaultPrompt;
clone.description=description;
clone.input=input;
clone.modelType=modelType;
clone.name=name;
clone.output=output;
clone.parameters=parameters;
clone.requestType=requestType;
return clone;
}

/**
* @return the name
Expand Down Expand Up @@ -182,6 +199,20 @@ public List<Output> getOutput() {
public void setOutput(List<Output> output) {
this.output = output;
}

/**
* @return the customPrompts
*/
public boolean isCustomPrompts() {
return customPrompts;
}

/**
* @param customPrompts the customPrompts to set
*/
public void setCustomPrompts(boolean customPrompts) {
this.customPrompts = customPrompts;
}


}
3 changes: 3 additions & 0 deletions j-lawyer-fax/src/com/jdimension/jlawyer/ai/AssistantAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,9 @@ public List<AiCapability> getCapabilities() throws AssistantException {

stringKey = Jsoner.mintJsonKey("async", null);
capability.setAsync(c.getBoolean(stringKey));

stringKey = Jsoner.mintJsonKey("customPrompts", null);
capability.setCustomPrompts(c.getBoolean(stringKey));

stringKey = Jsoner.mintJsonKey("description", null);
capability.setDescription(c.getString(stringKey));
Expand Down

0 comments on commit 3687442

Please sign in to comment.