|
| 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.analysis.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.analysis.constant.AiTextAnalysisConstants.CRITERIA; |
| 23 | +import static com.bytechef.component.ai.text.analysis.constant.AiTextAnalysisConstants.CRITERION; |
| 24 | +import static com.bytechef.component.ai.text.analysis.constant.AiTextAnalysisConstants.HIGHEST_SCORE; |
| 25 | +import static com.bytechef.component.ai.text.analysis.constant.AiTextAnalysisConstants.IS_DECIMAL; |
| 26 | +import static com.bytechef.component.ai.text.analysis.constant.AiTextAnalysisConstants.LOWEST_SCORE; |
| 27 | +import static com.bytechef.component.ai.text.analysis.constant.AiTextAnalysisConstants.MODEL_NO_OPTIONS_PROPERTY; |
| 28 | +import static com.bytechef.component.ai.text.analysis.constant.AiTextAnalysisConstants.MODEL_OPTIONS_PROPERTY; |
| 29 | +import static com.bytechef.component.ai.text.analysis.constant.AiTextAnalysisConstants.MODEL_PROVIDER_PROPERTY; |
| 30 | +import static com.bytechef.component.ai.text.analysis.constant.AiTextAnalysisConstants.MODEL_URL_PROPERTY; |
| 31 | +import static com.bytechef.component.ai.text.analysis.constant.AiTextAnalysisConstants.TEXT; |
| 32 | +import static com.bytechef.component.definition.ComponentDsl.action; |
| 33 | +import static com.bytechef.component.definition.ComponentDsl.array; |
| 34 | +import static com.bytechef.component.definition.ComponentDsl.bool; |
| 35 | +import static com.bytechef.component.definition.ComponentDsl.number; |
| 36 | +import static com.bytechef.component.definition.ComponentDsl.object; |
| 37 | +import static com.bytechef.component.definition.ComponentDsl.string; |
| 38 | + |
| 39 | +import com.bytechef.component.ai.text.analysis.action.definition.AiTextAnalysisActionDefinition; |
| 40 | +import com.bytechef.component.ai.text.analysis.constant.AiTextAnalysisConstants; |
| 41 | +import com.bytechef.component.ai.text.analysis.util.AiTextAnalysisUtil; |
| 42 | +import com.bytechef.component.definition.Parameters; |
| 43 | +import com.bytechef.config.ApplicationProperties; |
| 44 | +import com.bytechef.platform.component.definition.ParametersFactory; |
| 45 | +import java.util.HashMap; |
| 46 | +import java.util.List; |
| 47 | +import java.util.Map; |
| 48 | + |
| 49 | +public class ScoreAction implements AITextAnalysisAction { |
| 50 | + public final AiTextAnalysisActionDefinition actionDefinition; |
| 51 | + |
| 52 | + public ScoreAction(ApplicationProperties.Ai.Component component) { |
| 53 | + this.actionDefinition = getActionDefinition(component); |
| 54 | + } |
| 55 | + |
| 56 | + private AiTextAnalysisActionDefinition getActionDefinition(ApplicationProperties.Ai.Component component) { |
| 57 | + return new AiTextAnalysisActionDefinition( |
| 58 | + action(AiTextAnalysisConstants.SCORE) |
| 59 | + .title("Score") |
| 60 | + .description("Scores the text based on several criteria") |
| 61 | + .properties( |
| 62 | + MODEL_PROVIDER_PROPERTY, |
| 63 | + MODEL_OPTIONS_PROPERTY, |
| 64 | + MODEL_NO_OPTIONS_PROPERTY, |
| 65 | + MODEL_URL_PROPERTY, |
| 66 | + string(TEXT) |
| 67 | + .label("Text") |
| 68 | + .description("The text that is to be scored.") |
| 69 | + .required(true), |
| 70 | + array(CRITERIA) |
| 71 | + .label("Criteria") |
| 72 | + .items( |
| 73 | + object() |
| 74 | + .properties( |
| 75 | + string(CRITERION) |
| 76 | + .label("Criterion") |
| 77 | + .description("What is the criterion of the text that is to be scored.") |
| 78 | + .exampleValue("Grammar") |
| 79 | + .required(true), |
| 80 | + number(LOWEST_SCORE) |
| 81 | + .label("Lowest Score") |
| 82 | + .description("The lowest possible score the text can achieve in this category.") |
| 83 | + .defaultValue(1), |
| 84 | + number(HIGHEST_SCORE) |
| 85 | + .label("Highest Score") |
| 86 | + .description( |
| 87 | + "The highest possible score the text can achieve in this category.") |
| 88 | + .defaultValue(10), |
| 89 | + bool(IS_DECIMAL) |
| 90 | + .label("Decimal Numbers") |
| 91 | + .description("Whether the score should use decimal numbers or not.") |
| 92 | + .defaultValue(false))) |
| 93 | + .required(true), |
| 94 | + MAX_TOKENS_PROPERTY, |
| 95 | + TEMPERATURE_PROPERTY) |
| 96 | + .output(), |
| 97 | + component, this); |
| 98 | + } |
| 99 | + |
| 100 | + public Parameters createParameters(Parameters inputParameters) { |
| 101 | + Map<String, Object> modelInputParametersMap = new HashMap<>(); |
| 102 | + |
| 103 | + String systemPrompt = |
| 104 | + "You are an objective text scoring judge. You will receive a text and list of criteria that you will score the text on. Within the list of criteria you will also receive `Lowest Score` which indicates the lowest possible score you can give, `Highest Score` which indicates the highest possible score you can give and `Decimal` which tells you that you will be using decimal numbers if true or only integers if false. Your response will be a JSON array of objects for each criteria containing your score and a short explanation."; |
| 105 | + |
| 106 | + StringBuilder userBuilder = new StringBuilder(); |
| 107 | + userBuilder.append("Text: ") |
| 108 | + .append(inputParameters.getString(TEXT)) |
| 109 | + .append("\n"); |
| 110 | + |
| 111 | + List<AiTextAnalysisUtil.Criteria> criteria = |
| 112 | + inputParameters.getList(CRITERIA, AiTextAnalysisUtil.Criteria.class, List.of()); |
| 113 | + |
| 114 | + userBuilder.append("Criteria: {") |
| 115 | + .append("\n"); |
| 116 | + |
| 117 | + criteria.forEach(critrion -> userBuilder.append("{") |
| 118 | + .append("\n") |
| 119 | + .append("Criterion: ") |
| 120 | + .append(critrion.criterion()) |
| 121 | + .append("\n") |
| 122 | + .append("Lowest Score: ") |
| 123 | + .append(critrion.lowestScore()) |
| 124 | + .append("\n") |
| 125 | + .append("Highest Score: ") |
| 126 | + .append(critrion.highestScore()) |
| 127 | + .append("\n") |
| 128 | + .append("Decimal: ") |
| 129 | + .append(critrion.isDecimal()) |
| 130 | + .append("\n") |
| 131 | + .append("},") |
| 132 | + .append("\n")); |
| 133 | + |
| 134 | + userBuilder.append("}\n"); |
| 135 | + |
| 136 | + modelInputParametersMap.put("messages", |
| 137 | + List.of( |
| 138 | + Map.of("content", systemPrompt, "role", "system"), |
| 139 | + Map.of("content", userBuilder.toString(), "role", "user"))); |
| 140 | + modelInputParametersMap.put("model", inputParameters.getString(MODEL)); |
| 141 | + |
| 142 | + return ParametersFactory.createParameters(modelInputParametersMap); |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments