Skip to content

Commit

Permalink
checking for duplicate System Prompt node name substitutions
Browse files Browse the repository at this point in the history
  • Loading branch information
Clay-Ferguson committed Dec 13, 2024
1 parent c912bf7 commit fb4a558
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/main/java/quanta/util/AIUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.sql.Timestamp;
import java.time.Instant;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -55,7 +56,7 @@ public boolean parseAIConfig(SubNode node, SystemConfig system) {
system.setAgentNodeId(node.getIdStr());

String prompt = node.getStr(NodeProp.AI_PROMPT.s());
prompt = composePrompt(prompt);
prompt = composePrompt(prompt, null);
system.setSystemPrompt(prompt);
system.setFoldersToInclude(node.getStr(NodeProp.AI_FOLDERS_TO_INCLUDE.s()));
system.setFoldersToExclude(node.getStr(NodeProp.AI_FOLDERS_TO_EXCLUDE.s()));
Expand Down Expand Up @@ -94,8 +95,12 @@ public boolean parseAIConfig(SubNode node, SystemConfig system) {
* Allows the 'prompt' to have lines formatted like "::nodeName::" which will be replaced with the
* content of the node with that name. This allows for embedding prompts within prompts. The system
* is fully composable and can have multiple levels of embedding.
*
* We pass nodeNames, to allow detection of dupliates.
*/
public String composePrompt(String prompt) {
public String composePrompt(String prompt, HashSet<String> nodeNames) {
if (nodeNames == null)
nodeNames = new HashSet<>();
// split prompt into multiple lines, by tokenizing on newline
String[] lines = prompt.split("\n");
StringBuilder sb = new StringBuilder();
Expand All @@ -107,6 +112,12 @@ public String composePrompt(String prompt) {

// now put the nodeName into the expected format for a user-specific lookup
nodeName = TL.getSC().getUserName() + ":" + nodeName;

// add nodeName to nodeNames to detect duplicates and throw exception if it's a duplicate
if (nodeNames.contains(nodeName)) {
throw new MessageException("Duplicate node name in prompt substitutions: [" + nodeName + "]");
}
nodeNames.add(nodeName);
String content = buildSystemPromptFromNode(nodeName);
composed = true;
sb.append(content + "\n\n");
Expand All @@ -119,7 +130,7 @@ public String composePrompt(String prompt) {
return sb.toString();
}
// call again to allow embedding of prompts within prompts
String ret = composePrompt(sb.toString());
String ret = composePrompt(sb.toString(), nodeNames);
return ret;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/public/src/JavaIntf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export interface SearchDefinition {

export interface SystemConfig {
agentNodeId: string;
prompt: string;
systemPrompt: string;
foldersToInclude: string;
foldersToExclude: string;
template: string;
Expand Down

0 comments on commit fb4a558

Please sign in to comment.