Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ public class OpenAiChatAction {
private OpenAiChatAction() {
}

public static Object perform(
Parameters inputParameters, Parameters connectionParameters, ActionContext actionContext) {

return CHAT_MODEL.getResponse(inputParameters, connectionParameters, actionContext);
public static Object perform(Parameters inputParameters, Parameters connectionParameters, ActionContext context) {
return CHAT_MODEL.getResponse(inputParameters, connectionParameters, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,24 @@ enum Format {
org.springframework.ai.chat.model.ChatModel createChatModel(
Parameters inputParameters, Parameters connectionParameters, boolean responseFormatRequired);

@Nullable
default Object getResponse(Parameters inputParameters, Parameters connectionParameters, ActionContext context) {
return getResponse(inputParameters, connectionParameters, context, true, true);
}

@Nullable
default Object getResponse(
Parameters inputParameters, Parameters connectionParameters, ActionContext actionContext) {
Parameters inputParameters, Parameters connectionParameters, ActionContext context,
boolean messageFormatRequired, boolean responseFormatRequired) {

org.springframework.ai.chat.model.ChatModel chatModel = createChatModel(
inputParameters, connectionParameters, true);
inputParameters, connectionParameters, responseFormatRequired);

List<org.springframework.ai.chat.messages.Message> messages = ModelUtils.getMessages(
inputParameters, actionContext);
inputParameters, context, messageFormatRequired);

ChatClient.ChatClientRequestSpec chatClientRequestSpec = createPrompt(
inputParameters, actionContext, chatModel);
chatModel, inputParameters, responseFormatRequired, context);

ChatClient.CallResponseSpec callResponseSpec = chatClientRequestSpec
.messages(messages)
Expand All @@ -73,23 +79,26 @@ default Object getResponse(
.build())
.call();

return ModelUtils.getChatResponse(callResponseSpec, inputParameters, actionContext);
return ModelUtils.getChatResponse(callResponseSpec, inputParameters, responseFormatRequired, context);
}

private ChatClient.ChatClientRequestSpec createPrompt(
Parameters inputParameters, ActionContext actionContext,
org.springframework.ai.chat.model.ChatModel chatModel) {
org.springframework.ai.chat.model.ChatModel chatModel, Parameters inputParameters,
boolean responseFormatRequired, ActionContext context) {

ChatClient chatClient = ChatClient.create(chatModel);
ResponseFormat responseFormat = ResponseFormat.TEXT;

ResponseFormat responseFormat = inputParameters.getRequiredFromPath(
RESPONSE + "." + RESPONSE_FORMAT, ResponseFormat.class);
if (responseFormatRequired) {
responseFormat = inputParameters.getRequiredFromPath(
RESPONSE + "." + RESPONSE_FORMAT, ResponseFormat.class);
}

if (responseFormat.equals(ResponseFormat.TEXT)) {
return chatClient.prompt();
} else {
JsonSchemaStructuredOutputConverter converter = new JsonSchemaStructuredOutputConverter(
inputParameters.getFromPath(RESPONSE + "." + RESPONSE_SCHEMA, String.class), actionContext);
inputParameters.getFromPath(RESPONSE + "." + RESPONSE_SCHEMA, String.class), context);

return chatClient.prompt(converter.getFormat());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.ChatClient.CallResponseSpec;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
Expand Down Expand Up @@ -77,11 +77,22 @@ private ModelUtils() {
@SuppressFBWarnings("NP")
@Nullable
public static Object getChatResponse(
ChatClient.CallResponseSpec callResponseSpec, Parameters parameters, Context context) {
CallResponseSpec callResponseSpec, Parameters parameters, Context context) {

return getChatResponse(callResponseSpec, parameters, true, context);
}

@SuppressFBWarnings("NP")
@Nullable
public static Object getChatResponse(
CallResponseSpec callResponseSpec, Parameters parameters, boolean responseFormatRequired, Context context) {

Object response = null;
ResponseFormat responseFormat = parameters.getRequiredFromPath(
RESPONSE + "." + RESPONSE_FORMAT, ResponseFormat.class);
ResponseFormat responseFormat = TEXT;

if (responseFormatRequired) {
responseFormat = parameters.getRequiredFromPath(RESPONSE + "." + RESPONSE_FORMAT, ResponseFormat.class);
}

if (responseFormat == TEXT) {
try {
Expand Down Expand Up @@ -121,24 +132,34 @@ public static <R> List<Option<R>> getEnumOptions(Map<String, R> map) {
}

public static List<Message> getMessages(Parameters inputParameters, ActionContext actionContext) {
return getMessages(inputParameters, actionContext, true);
}

public static List<Message> getMessages(
Parameters inputParameters, ActionContext actionContext, boolean messageFormatRequired) {

List<ChatModel.Message> chatModelMessages = new ArrayList<>();

String format = inputParameters.getRequiredString(FORMAT);
if (messageFormatRequired) {
String format = inputParameters.getRequiredString(FORMAT);

if (format.equals(Format.SIMPLE.name())) {
String userPrompt = inputParameters.getRequiredString(USER_PROMPT);
if (format.equals(Format.SIMPLE.name())) {
String userPrompt = inputParameters.getRequiredString(USER_PROMPT);

ChatModel.Message userMessage = new ChatModel.Message(
userPrompt, inputParameters.getList(ATTACHMENTS, FileEntry.class), Role.USER);
ChatModel.Message userMessage = new ChatModel.Message(
userPrompt, inputParameters.getList(ATTACHMENTS, FileEntry.class), Role.USER);

chatModelMessages.add(userMessage);
chatModelMessages.add(userMessage);

String systemPrompt = inputParameters.getString(SYSTEM_PROMPT);
String systemPrompt = inputParameters.getString(SYSTEM_PROMPT);

if (systemPrompt != null && !systemPrompt.isEmpty()) {
ChatModel.Message systeMMessage = new ChatModel.Message(systemPrompt, null, Role.SYSTEM);
if (systemPrompt != null && !systemPrompt.isEmpty()) {
ChatModel.Message systeMMessage = new ChatModel.Message(systemPrompt, null, Role.SYSTEM);

chatModelMessages.add(systeMMessage);
chatModelMessages.add(systeMMessage);
}
} else {
chatModelMessages = inputParameters.getList(MESSAGES, new TypeReference<>() {});
}
} else {
chatModelMessages = inputParameters.getList(MESSAGES, new TypeReference<>() {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ public class VertexGeminiChatAction {
private VertexGeminiChatAction() {
}

public static Object perform(
Parameters inputParameters, Parameters connectionParameters, ActionContext context) {

public static Object perform(Parameters inputParameters, Parameters connectionParameters, ActionContext context) {
return CHAT_MODEL.getResponse(inputParameters, connectionParameters, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import static com.bytechef.component.definition.ComponentDsl.action;
import static com.bytechef.component.definition.ComponentDsl.array;
import static com.bytechef.component.definition.ComponentDsl.object;
import static com.bytechef.component.definition.ComponentDsl.outputSchema;
import static com.bytechef.component.definition.ComponentDsl.sampleOutput;
import static com.bytechef.component.definition.ComponentDsl.string;

import com.bytechef.component.ai.universal.text.action.definition.AiTextActionDefinition;
Expand Down Expand Up @@ -84,7 +86,11 @@ private AiTextActionDefinition getActionDefinition(
.additionalProperties(string()),
MAX_TOKENS_PROPERTY,
TEMPERATURE_PROPERTY)
.output(),
.output(
outputSchema(
string()
.description("The chosen category.")),
sampleOutput("sample category")),
provider, this, propertyService);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ public GenerateTextAction(ApplicationProperties.Ai.Provider provider, PropertySe
.required(true),
MAX_TOKENS_PROPERTY,
TEMPERATURE_PROPERTY)
.output(outputSchema(string().description("Generated text.")), sampleOutput("Generated text.")),
.output(
outputSchema(
string()
.description("Generated text.")),
sampleOutput("sample generated text.")),
provider, this, propertyService);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@
import static com.bytechef.component.definition.ComponentDsl.object;
import static com.bytechef.component.definition.ComponentDsl.string;

import com.bytechef.component.ai.llm.ChatModel;
import com.bytechef.component.ai.universal.text.action.definition.AiTextActionDefinition;
import com.bytechef.component.ai.universal.text.constant.AiTextConstants;
import com.bytechef.component.ai.universal.text.util.AiTextUtils;
import com.bytechef.component.definition.Parameters;
import com.bytechef.config.ApplicationProperties;
import com.bytechef.definition.BaseOutputDefinition.OutputResponse;
import com.bytechef.platform.component.definition.ParametersFactory;
import com.bytechef.platform.configuration.service.PropertyService;
import java.util.HashMap;
Expand All @@ -57,6 +59,36 @@ public class ScoreAction implements AiTextAction {

public final AiTextActionDefinition actionDefinition;

private static final String RESPONSE_SCHEMA = """
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "CriteriaList",
"type": "object",
"properties": {
"criteria": {
"type": "array",
"items": {
"type": "object",
"properties": {
"criteriaName": {
"type": "string",
"description": "Name of the evaluation criteria"
},
"criteriaValue": {
"type": "number",
"description": "Numeric value or score for the criteria"
}
},
"required": ["criteriaName", "criteriaValue"],
"additionalProperties": false
}
}
},
"required": ["criteria"],
"additionalProperties": false
}
""";

public ScoreAction(ApplicationProperties.Ai.Provider provider, PropertyService propertyService) {
this.actionDefinition = getActionDefinition(provider, propertyService);
}
Expand Down Expand Up @@ -103,7 +135,9 @@ private AiTextActionDefinition getActionDefinition(
.required(true),
MAX_TOKENS_PROPERTY,
TEMPERATURE_PROPERTY)
.output(),
.output(
(inputParameters, connectionParameters, context) -> OutputResponse.of(
context.outputSchema(outputSchema -> outputSchema.getOutputSchema(RESPONSE_SCHEMA)))),
provider, this, propertyService);
}

Expand Down Expand Up @@ -150,6 +184,11 @@ public Parameters createParameters(Parameters inputParameters) {
Map.of("content", systemPrompt, ROLE, SYSTEM.name()),
Map.of("content", userBuilder.toString(), ROLE, USER.name())));
modelInputParametersMap.put("model", inputParameters.getString(MODEL));
modelInputParametersMap.put(
"response",
Map.of(
"responseFormat", ChatModel.ResponseFormat.JSON,
"responseSchema", RESPONSE_SCHEMA));

return ParametersFactory.createParameters(modelInputParametersMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import static com.bytechef.component.ai.universal.text.constant.AiTextConstants.PROVIDER_PROPERTY;
import static com.bytechef.component.ai.universal.text.constant.AiTextConstants.TEXT;
import static com.bytechef.component.definition.ComponentDsl.action;
import static com.bytechef.component.definition.ComponentDsl.outputSchema;
import static com.bytechef.component.definition.ComponentDsl.sampleOutput;
import static com.bytechef.component.definition.ComponentDsl.string;

import com.bytechef.component.ai.universal.text.action.definition.AiTextActionDefinition;
Expand Down Expand Up @@ -69,7 +71,11 @@ private AiTextActionDefinition getActionDefinition(
.required(true),
MAX_TOKENS_PROPERTY,
TEMPERATURE_PROPERTY)
.output(),
.output(
outputSchema(
string()
.description("The chosen category.")),
sampleOutput("sample category")),
provider, this, propertyService);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@
import static com.bytechef.component.definition.ComponentDsl.integer;
import static com.bytechef.component.definition.ComponentDsl.string;

import com.bytechef.component.ai.llm.ChatModel;
import com.bytechef.component.ai.universal.text.action.definition.AiTextActionDefinition;
import com.bytechef.component.ai.universal.text.constant.AiTextConstants;
import com.bytechef.component.definition.Parameters;
import com.bytechef.config.ApplicationProperties;
import com.bytechef.definition.BaseOutputDefinition;
import com.bytechef.platform.component.definition.ParametersFactory;
import com.bytechef.platform.configuration.service.PropertyService;
import java.util.HashMap;
Expand All @@ -51,6 +53,36 @@ public class SimilaritySearchAction implements AiTextAction {

public final AiTextActionDefinition actionDefinition;

private static final String RESPONSE_SCHEMA = """
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "ResultObject",
"type": "object",
"properties": {
"result": {
"type": "array",
"items": {
"type": "object",
"properties": {
"chunk": {
"type": "string",
"description": "Text content of the chunk"
},
"match_score": {
"type": "number",
"description": "Similarity or relevance score of the chunk"
}
},
"required": ["chunk", "match_score"],
"additionalProperties": false
}
}
},
"required": ["result"],
"additionalProperties": false
}
""";

public SimilaritySearchAction(
ApplicationProperties.Ai.Provider provider, PropertyService propertyService) {

Expand Down Expand Up @@ -85,7 +117,9 @@ public SimilaritySearchAction(
.defaultValue(5),
MAX_TOKENS_PROPERTY,
TEMPERATURE_PROPERTY)
.output(),
.output(
(inputParameters, connectionParameters, context) -> BaseOutputDefinition.OutputResponse.of(
context.outputSchema(outputSchema -> outputSchema.getOutputSchema(RESPONSE_SCHEMA)))),
provider, this, propertyService);
}

Expand All @@ -100,11 +134,17 @@ public Parameters createParameters(Parameters inputParameters) {
+ "N: " + inputParameters.getInteger(NUM_RESULTS) + "\n"
+ "C: " + inputParameters.getInteger(CHUNK_SIZE);

modelInputParametersMap.put("messages",
modelInputParametersMap.put(
"messages",
List.of(
Map.of("content", systemPrompt, ROLE, SYSTEM.name()),
Map.of("content", userBuilder, ROLE, USER.name())));
modelInputParametersMap.put("model", inputParameters.getString(MODEL));
modelInputParametersMap.put(
"response",
Map.of(
"responseFormat", ChatModel.ResponseFormat.JSON,
"responseSchema", RESPONSE_SCHEMA));

return ParametersFactory.createParameters(modelInputParametersMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import static com.bytechef.component.ai.universal.text.constant.AiTextConstants.TEXT;
import static com.bytechef.component.definition.ComponentDsl.action;
import static com.bytechef.component.definition.ComponentDsl.option;
import static com.bytechef.component.definition.ComponentDsl.outputSchema;
import static com.bytechef.component.definition.ComponentDsl.sampleOutput;
import static com.bytechef.component.definition.ComponentDsl.string;

import com.bytechef.component.ai.universal.text.action.definition.AiTextActionDefinition;
Expand Down Expand Up @@ -97,7 +99,11 @@ public SummarizeTextAction(
.required(true),
MAX_TOKENS_PROPERTY,
TEMPERATURE_PROPERTY)
.output(),
.output(
outputSchema(
string()
.description("The summarized text.")),
sampleOutput("sample summarized text")),
provider, this, propertyService);
}

Expand Down
Loading