Skip to content

Commit 8131bbf

Browse files
1886 - implemented
1 parent 14888a5 commit 8131bbf

File tree

5 files changed

+1316
-2
lines changed

5 files changed

+1316
-2
lines changed

server/libs/modules/components/ai/ai-text-analysis/src/main/java/com/bytechef/component/ai/text/analysis/AiTextAnalysisComponentHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.bytechef.component.ComponentHandler;
2323
import com.bytechef.component.ai.text.analysis.action.ClassifyTextAction;
24+
import com.bytechef.component.ai.text.analysis.action.ScoreAction;
2425
import com.bytechef.component.ai.text.analysis.action.SentimentAnalysisAction;
2526
import com.bytechef.component.ai.text.analysis.action.SummarizeTextAction;
2627
import com.bytechef.component.definition.ComponentCategory;
@@ -62,7 +63,8 @@ private AiTextAnalysisComponentDefinitionImpl(ApplicationProperties.Ai.Component
6263
.actions(
6364
new SummarizeTextAction(component).actionDefinition,
6465
new ClassifyTextAction(component).actionDefinition,
65-
new SentimentAnalysisAction(component).actionDefinition));
66+
new SentimentAnalysisAction(component).actionDefinition,
67+
new ScoreAction(component).actionDefinition));
6668
}
6769
}
6870
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
}

server/libs/modules/components/ai/ai-text-analysis/src/main/java/com/bytechef/component/ai/text/analysis/constant/AiTextAnalysisConstants.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,19 @@ public class AiTextAnalysisConstants {
3535
public static final String FORMAT = "format";
3636
public static final String PROMPT = "prompt";
3737
public static final String CATEGORIES = "categories";
38+
public static final String CRITERIA = "criteria";
3839
public static final String EXAMPLES = "examples";
3940

41+
public static final String CRITERION = "criterion";
42+
public static final String LOWEST_SCORE = "lowestScore";
43+
public static final String HIGHEST_SCORE = "highestScore";
44+
public static final String IS_DECIMAL = "isDecimal";
45+
4046
public static final String MODEL_PROVIDER = "modelProvider";
4147
public static final String SUMMARIZE_TEXT = "summarizeText";
4248
public static final String CLASSIFY_TEXT = "classifyText";
4349
public static final String SENTIMENT_ANALYSIS = "sentimentAnalysis";
50+
public static final String SCORE = "score";
4451

4552
public static final ModifiableIntegerProperty MODEL_PROVIDER_PROPERTY = integer(MODEL_PROVIDER)
4653
.label("Model provider")

server/libs/modules/components/ai/ai-text-analysis/src/main/java/com/bytechef/component/ai/text/analysis/util/AiTextAnalysisUtil.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,7 @@ public static List<? extends Option<String>> createModelProperties(
5353
default -> throw new IllegalStateException("Unexpected value: " + modelProvider);
5454
};
5555
}
56+
57+
public record Criteria(String criterion, double lowestScore, double highestScore, boolean isDecimal) {
58+
}
5659
}

0 commit comments

Comments
 (0)