|
| 1 | +/* |
| 2 | + * Copyright 2025 ByteChef |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.bytechef.component.ai.universal.text.action; |
| 18 | + |
| 19 | +import static com.bytechef.component.ai.llm.ChatModel.Role.SYSTEM; |
| 20 | +import static com.bytechef.component.ai.llm.ChatModel.Role.USER; |
| 21 | +import static com.bytechef.component.ai.llm.constant.LLMConstants.MAX_TOKENS_PROPERTY; |
| 22 | +import static com.bytechef.component.ai.llm.constant.LLMConstants.MODEL; |
| 23 | +import static com.bytechef.component.ai.llm.constant.LLMConstants.ROLE; |
| 24 | +import static com.bytechef.component.ai.llm.constant.LLMConstants.TEMPERATURE_PROPERTY; |
| 25 | +import static com.bytechef.component.ai.universal.text.constant.AiTextConstants.MASK_MAP; |
| 26 | +import static com.bytechef.component.ai.universal.text.constant.AiTextConstants.MODEL_NO_OPTIONS_PROPERTY; |
| 27 | +import static com.bytechef.component.ai.universal.text.constant.AiTextConstants.MODEL_OPTIONS_PROPERTY; |
| 28 | +import static com.bytechef.component.ai.universal.text.constant.AiTextConstants.MODEL_URL_PROPERTY; |
| 29 | +import static com.bytechef.component.ai.universal.text.constant.AiTextConstants.PROVIDER_PROPERTY; |
| 30 | +import static com.bytechef.component.ai.universal.text.constant.AiTextConstants.TEXT; |
| 31 | +import static com.bytechef.component.definition.ComponentDsl.action; |
| 32 | +import static com.bytechef.component.definition.ComponentDsl.object; |
| 33 | +import static com.bytechef.component.definition.ComponentDsl.outputSchema; |
| 34 | +import static com.bytechef.component.definition.ComponentDsl.sampleOutput; |
| 35 | +import static com.bytechef.component.definition.ComponentDsl.string; |
| 36 | + |
| 37 | +import com.bytechef.component.ai.universal.text.action.definition.AiTextActionDefinition; |
| 38 | +import com.bytechef.component.ai.universal.text.constant.AiTextConstants; |
| 39 | +import com.bytechef.component.definition.Parameters; |
| 40 | +import com.bytechef.config.ApplicationProperties; |
| 41 | +import com.bytechef.platform.component.definition.ParametersFactory; |
| 42 | +import com.bytechef.platform.configuration.service.PropertyService; |
| 43 | +import java.util.HashMap; |
| 44 | +import java.util.List; |
| 45 | +import java.util.Map; |
| 46 | + |
| 47 | +/** |
| 48 | + * @author Marko Kriskovic |
| 49 | + */ |
| 50 | +public class UnmaskAction implements AiTextAction { |
| 51 | + |
| 52 | + public static AiTextActionDefinition of( |
| 53 | + ApplicationProperties.Ai.Provider provider, PropertyService propertyService) { |
| 54 | + |
| 55 | + return new AiTextActionDefinition( |
| 56 | + action(AiTextConstants.UNMASK) |
| 57 | + .title("Unmask") |
| 58 | + .description("Uses AI and a map of masking entities to unmask the text.") |
| 59 | + .properties( |
| 60 | + PROVIDER_PROPERTY.apply(provider, propertyService), |
| 61 | + MODEL_OPTIONS_PROPERTY, |
| 62 | + MODEL_NO_OPTIONS_PROPERTY, |
| 63 | + MODEL_URL_PROPERTY, |
| 64 | + string(TEXT) |
| 65 | + .label("Text") |
| 66 | + .description("The text to process.") |
| 67 | + .required(true), |
| 68 | + object(MASK_MAP) |
| 69 | + .label("Masked map") |
| 70 | + .description("Map of masked entities to replace with values.") |
| 71 | + .additionalProperties(string()), |
| 72 | + MAX_TOKENS_PROPERTY, |
| 73 | + TEMPERATURE_PROPERTY) |
| 74 | + .output( |
| 75 | + outputSchema(string().description("The text with sensitive content redacted.")), |
| 76 | + sampleOutput("Hello, my name is [REDACTED] and my email is [EMAIL].")), |
| 77 | + provider, new UnmaskAction(), propertyService); |
| 78 | + } |
| 79 | + |
| 80 | + private UnmaskAction() { |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public Parameters createParameters(Parameters inputParameters) { |
| 85 | + Map<String, Object> modelInputParametersMap = new HashMap<>(); |
| 86 | + |
| 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 | + |
| 92 | + userBuilder.append("Text: ") |
| 93 | + .append(inputParameters.getString(TEXT)) |
| 94 | + .append("\n\nInstructions:\n"); |
| 95 | + |
| 96 | + Map<String, String> maskMap = inputParameters.getMap(MASK_MAP, String.class, Map.of()); |
| 97 | + |
| 98 | + if (!maskMap.isEmpty()) { |
| 99 | + userBuilder.append("Mask Map: ") |
| 100 | + .append("\n"); |
| 101 | + maskMap.forEach((key, value) -> userBuilder.append("- ") |
| 102 | + .append(key) |
| 103 | + .append(": ") |
| 104 | + .append(value) |
| 105 | + .append("\n")); |
| 106 | + } |
| 107 | + |
| 108 | + modelInputParametersMap.put( |
| 109 | + "messages", |
| 110 | + List.of( |
| 111 | + Map.of("content", systemPrompt, ROLE, SYSTEM.name()), |
| 112 | + Map.of("content", userBuilder.toString(), ROLE, USER.name()))); |
| 113 | + modelInputParametersMap.put("model", inputParameters.getString(MODEL)); |
| 114 | + |
| 115 | + return ParametersFactory.create(modelInputParametersMap); |
| 116 | + } |
| 117 | +} |
0 commit comments