Skip to content

Commit de640d2

Browse files
committed
1345 Reorganize
1 parent 6c42a32 commit de640d2

2 files changed

Lines changed: 45 additions & 43 deletions

File tree

server/libs/modules/components/ai/universal/universal-text/src/main/java/com/bytechef/component/ai/universal/text/action/MaskAction.java

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,29 @@
5656
*/
5757
public class MaskAction implements AiTextAction {
5858

59+
private static final String RESPONSE_SCHEMA = """
60+
{
61+
"type": "object",
62+
"properties": {
63+
"text": {
64+
"type": "string"
65+
},
66+
"maskMap": {
67+
"type": "object",
68+
"additionalProperties": {
69+
"type": "string"
70+
}
71+
}
72+
},
73+
"required": ["text", "maskMap"]
74+
}
75+
""";
76+
private static final String SYSTEM_PROMPT =
77+
"You are a content redaction specialist. Detect and replace sensitive information in the given text with " +
78+
"mask tokens. Increment the number suffix (_1, _2, ...) for each unique occurrence of the same type " +
79+
"so every masking is unique. Respond with a JSON object with two fields: \"text\" (the redacted " +
80+
"text) and \"maskMap\" (an object mapping each mask token to the original value it replaced).";
81+
5982
public static AiTextActionDefinition of(
6083
ApplicationProperties.Ai.Provider provider, PropertyService propertyService) {
6184

@@ -120,29 +143,24 @@ public static List<Option<String>> getPiiDetectionOptions() {
120143
public Parameters createParameters(Parameters inputParameters) {
121144
Map<String, Object> modelInputParametersMap = new HashMap<>();
122145

123-
String systemPrompt =
124-
"You are a content redaction specialist. Detect and replace sensitive information in the given text with mask tokens. "
125-
+ "Increment the number suffix (_1, _2, ...) for each unique occurrence of the same type so every masking is unique. "
126-
+ "Respond with a JSON object with two fields: \"text\" (the redacted text) and \"maskMap\" (an object mapping each mask token to the original value it replaced).";
146+
StringBuilder userPrompt = new StringBuilder();
127147

128-
StringBuilder userBuilder = new StringBuilder();
129-
130-
userBuilder.append("Text: ")
148+
userPrompt.append("Text: ")
131149
.append(inputParameters.getString(TEXT))
132150
.append("\n\nInstructions:\n");
133151

134-
List<String> keywords = inputParameters.getList(SENSITIVE_KEYWORDS, String.class, List.of());
152+
List<String> sensitiveKeywords = inputParameters.getList(SENSITIVE_KEYWORDS, String.class, List.of());
135153

136-
if (!keywords.isEmpty()) {
137-
userBuilder.append("- Replace each of the following sensitive keywords with [REDACTED_N]: ")
138-
.append(String.join(", ", keywords))
154+
if (!sensitiveKeywords.isEmpty()) {
155+
userPrompt.append("- Replace each of the following sensitive keywords with [REDACTED_N]: ")
156+
.append(String.join(", ", sensitiveKeywords))
139157
.append("\n");
140158
}
141159

142160
List<String> selectedPiiTypes = inputParameters.getList(PII_DETECTION, String.class, List.of());
143161

144162
for (String piiType : selectedPiiTypes) {
145-
userBuilder.append("- Replace all ")
163+
userPrompt.append("- Replace all ")
146164
.append(piiType.toLowerCase()
147165
.replace("_", " "))
148166
.append(" values with [")
@@ -153,40 +171,22 @@ public Parameters createParameters(Parameters inputParameters) {
153171
List<String> customPatterns = inputParameters.getList(CUSTOM_PATTERNS, String.class, List.of());
154172

155173
if (!customPatterns.isEmpty()) {
156-
userBuilder.append("- Replace all matches of the following patterns with [CUSTOM_N]: ")
174+
userPrompt.append("- Replace all matches of the following patterns with [CUSTOM_N]: ")
157175
.append(String.join(", ", customPatterns))
158176
.append("\n");
159177
}
160178

161-
String responseSchema = """
162-
{
163-
"type": "object",
164-
"properties": {
165-
"text": {
166-
"type": "string"
167-
},
168-
"maskMap": {
169-
"type": "object",
170-
"additionalProperties": {
171-
"type": "string"
172-
}
173-
}
174-
},
175-
"required": ["text", "maskMap"]
176-
}
177-
""";
178-
179179
modelInputParametersMap.put(
180180
"messages",
181181
List.of(
182-
Map.of("content", systemPrompt, ROLE, SYSTEM.name()),
183-
Map.of("content", userBuilder.toString(), ROLE, USER.name())));
182+
Map.of("content", SYSTEM_PROMPT, ROLE, SYSTEM.name()),
183+
Map.of("content", userPrompt.toString(), ROLE, USER.name())));
184184
modelInputParametersMap.put("model", inputParameters.getString(MODEL));
185185
modelInputParametersMap.put(
186186
"response",
187187
Map.of(
188188
"responseFormat", ChatModel.ResponseFormat.JSON,
189-
"responseSchema", responseSchema));
189+
"responseSchema", RESPONSE_SCHEMA));
190190

191191
return ParametersFactory.create(modelInputParametersMap);
192192
}

server/libs/modules/components/ai/universal/universal-text/src/main/java/com/bytechef/component/ai/universal/text/action/UnmaskAction.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
*/
5050
public class UnmaskAction implements AiTextAction {
5151

52+
private static final String SYSTEM_PROMPT =
53+
"Replace the redacted content with values in the map. Return only the unredacted text with no additional " +
54+
"commentary.";
55+
5256
public static AiTextActionDefinition of(
5357
ApplicationProperties.Ai.Provider provider, PropertyService propertyService) {
5458

@@ -84,21 +88,19 @@ private UnmaskAction() {
8488
public Parameters createParameters(Parameters inputParameters) {
8589
Map<String, Object> modelInputParametersMap = new HashMap<>();
8690

87-
String systemPrompt =
88-
"Replace the redacted content with values in the map. Return only the unredacted text with no additional commentary.";
89-
90-
StringBuilder userBuilder = new StringBuilder();
91+
StringBuilder userPrompt = new StringBuilder();
9192

92-
userBuilder.append("Text: ")
93+
userPrompt.append("Text: ")
9394
.append(inputParameters.getString(TEXT))
9495
.append("\n\nInstructions:\n");
9596

9697
Map<String, String> maskMap = inputParameters.getMap(MASK_MAP, String.class, Map.of());
9798

9899
if (!maskMap.isEmpty()) {
99-
userBuilder.append("Mask Map: ")
100+
userPrompt.append("Mask Map: ")
100101
.append("\n");
101-
maskMap.forEach((key, value) -> userBuilder.append("- ")
102+
103+
maskMap.forEach((key, value) -> userPrompt.append("- ")
102104
.append(key)
103105
.append(": ")
104106
.append(value)
@@ -108,8 +110,8 @@ public Parameters createParameters(Parameters inputParameters) {
108110
modelInputParametersMap.put(
109111
"messages",
110112
List.of(
111-
Map.of("content", systemPrompt, ROLE, SYSTEM.name()),
112-
Map.of("content", userBuilder.toString(), ROLE, USER.name())));
113+
Map.of("content", SYSTEM_PROMPT, ROLE, SYSTEM.name()),
114+
Map.of("content", userPrompt.toString(), ROLE, USER.name())));
113115
modelInputParametersMap.put("model", inputParameters.getString(MODEL));
114116

115117
return ParametersFactory.create(modelInputParametersMap);

0 commit comments

Comments
 (0)