|
| 1 | +/* |
| 2 | + * Copyright 2023-present ByteChef Inc. |
| 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.text.action; |
| 18 | + |
| 19 | +import static com.bytechef.component.ai.llm.constant.LLMConstants.MAX_TOKENS_PROPERTY; |
| 20 | +import static com.bytechef.component.ai.llm.constant.LLMConstants.MODEL; |
| 21 | +import static com.bytechef.component.ai.llm.constant.LLMConstants.TEMPERATURE_PROPERTY; |
| 22 | +import static com.bytechef.component.ai.text.constant.AiTextConstants.MODEL_NO_OPTIONS_PROPERTY; |
| 23 | +import static com.bytechef.component.ai.text.constant.AiTextConstants.MODEL_OPTIONS_PROPERTY; |
| 24 | +import static com.bytechef.component.ai.text.constant.AiTextConstants.MODEL_PROVIDER_PROPERTY; |
| 25 | +import static com.bytechef.component.ai.text.constant.AiTextConstants.MODEL_URL_PROPERTY; |
| 26 | +import static com.bytechef.component.ai.text.constant.AiTextConstants.PROMPT; |
| 27 | +import static com.bytechef.component.ai.text.constant.AiTextConstants.TEXT; |
| 28 | +import static com.bytechef.component.definition.ComponentDsl.action; |
| 29 | +import static com.bytechef.component.definition.ComponentDsl.string; |
| 30 | + |
| 31 | +import com.bytechef.component.ai.text.action.definition.AiTextActionDefinition; |
| 32 | +import com.bytechef.component.ai.text.constant.AiTextConstants; |
| 33 | +import com.bytechef.component.definition.Parameters; |
| 34 | +import com.bytechef.config.ApplicationProperties; |
| 35 | +import com.bytechef.platform.component.definition.ParametersFactory; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author Marko Kriskovic |
| 42 | + */ |
| 43 | +public class TextGenerationAction implements AiTextAction { |
| 44 | + |
| 45 | + public final AiTextActionDefinition actionDefinition; |
| 46 | + |
| 47 | + public TextGenerationAction(ApplicationProperties.Ai.Component component) { |
| 48 | + this.actionDefinition = new AiTextActionDefinition( |
| 49 | + action(AiTextConstants.TEXT_GENERATION) |
| 50 | + .title("Text Generation") |
| 51 | + .description("AI generates text based on the given prompt.") |
| 52 | + .properties( |
| 53 | + MODEL_PROVIDER_PROPERTY, |
| 54 | + MODEL_OPTIONS_PROPERTY, |
| 55 | + MODEL_NO_OPTIONS_PROPERTY, |
| 56 | + MODEL_URL_PROPERTY, |
| 57 | + string(PROMPT) |
| 58 | + .label("Prompt") |
| 59 | + .description("Write your prompt for generating text.") |
| 60 | + .required(true), |
| 61 | + MAX_TOKENS_PROPERTY, |
| 62 | + TEMPERATURE_PROPERTY) |
| 63 | + .output(), |
| 64 | + component, this); |
| 65 | + } |
| 66 | + |
| 67 | + public Parameters createParameters(Parameters inputParameters) { |
| 68 | + Map<String, Object> modelInputParametersMap = new HashMap<>(); |
| 69 | + |
| 70 | + modelInputParametersMap.put("messages", |
| 71 | + List.of( |
| 72 | + Map.of("content", inputParameters.getString(TEXT), "role", "user"))); |
| 73 | + modelInputParametersMap.put("model", inputParameters.getString(MODEL)); |
| 74 | + |
| 75 | + return ParametersFactory.createParameters(modelInputParametersMap); |
| 76 | + } |
| 77 | +} |
0 commit comments