Skip to content

Commit

Permalink
Migrate to the JsonSchemaElement API
Browse files Browse the repository at this point in the history
Closes #1054
  • Loading branch information
edeandrea committed Nov 22, 2024
1 parent 05e3d8c commit 0c407f7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,7 @@ static Tool toTool(ToolSpecification toolSpecification) {
.name(toolSpecification.name())
.description(toolSpecification.description());

if (toolSpecification.toolParameters() != null) {
for (Map.Entry<String, Map<String, Object>> p : toolSpecification.toolParameters().properties().entrySet()) {
builder.addParameter(p.getKey(), p.getValue(),
toolSpecification.toolParameters().required().contains(p.getKey()));
}
} else if (toolSpecification.parameters() != null) {
if (toolSpecification.parameters() != null) {
for (Map.Entry<String, JsonSchemaElement> p : toolSpecification.parameters().properties().entrySet()) {
builder.addParameter(p.getKey(), JsonSchemaElementHelper.toMap(p.getValue()),
toolSpecification.parameters().required().contains(p.getKey()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.fasterxml.jackson.core.type.TypeReference;

import dev.langchain4j.agent.tool.ToolExecutionRequest;
import dev.langchain4j.agent.tool.ToolParameters;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
Expand Down Expand Up @@ -142,23 +141,12 @@ static List<Tool> toTools(Collection<ToolSpecification> toolSpecifications) {
}

private static Tool toTool(ToolSpecification toolSpecification) {
Tool.Function.Parameters functionParameters;
if (toolSpecification.toolParameters() != null) {
functionParameters = toFunctionParameters(toolSpecification.toolParameters());
} else {
functionParameters = toFunctionParameters(toolSpecification.parameters());
}
Tool.Function.Parameters functionParameters = toFunctionParameters(toolSpecification.parameters());

return new Tool(Tool.Type.FUNCTION, new Tool.Function(toolSpecification.name(), toolSpecification.description(),
functionParameters));
}

private static Tool.Function.Parameters toFunctionParameters(ToolParameters toolParameters) {
if (toolParameters == null) {
return Tool.Function.Parameters.empty();
}
return Tool.Function.Parameters.objectType(toolParameters.properties(), toolParameters.required());
}

private static Tool.Function.Parameters toFunctionParameters(JsonObjectSchema parameters) {
if (parameters == null) {
return Tool.Function.Parameters.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.ToolExecutionResultMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.request.json.JsonSchemaElementHelper;
import io.quarkiverse.langchain4j.watsonx.bean.TextChatMessage.TextChatMessageAssistant;
import io.quarkiverse.langchain4j.watsonx.bean.TextChatMessage.TextChatMessageSystem;
import io.quarkiverse.langchain4j.watsonx.bean.TextChatMessage.TextChatMessageTool;
Expand Down Expand Up @@ -174,7 +175,7 @@ public static TextChatMessageTool of(ToolExecutionResultMessage toolExecutionRes
/**
* Creates a {@link TextChatMessageTool}.
*
* @param message the content of the message tool.
* @param content the content of the message tool.
* @param toolCallId the unique identifier of the message tool.
* @return the created {@link TextChatMessageTool}.
*/
Expand Down Expand Up @@ -219,15 +220,16 @@ public record TextChatParameterFunction(String name, String description, Map<Str
/**
* Creates a {@link TextChatParameterTool} from a {@link ToolSpecification}.
*
* @param toolExecutionRequest the tool specification to convert
* @param toolSpecification the tool specification to convert
* @return the created {@link TextChatParameterTool}
*/
public static TextChatParameterTool of(ToolSpecification toolSpecification) {
// FIXME: toolSpecification.toolParameters() is deprecated, we might receive a value in parameters() instead
var toolParams = JsonSchemaElementHelper.toMap(toolSpecification.parameters());

var parameters = new TextChatParameterFunction(toolSpecification.name(), toolSpecification.description(), Map.of(
"type", toolSpecification.toolParameters().type(),
"properties", toolSpecification.toolParameters().properties(),
"required", toolSpecification.toolParameters().required()));
"type", toolParams.get("type"),
"properties", toolParams.get("properties"),
"required", toolParams.get("required")));
return new TextChatParameterTool("function", parameters);
}
}
Expand Down

0 comments on commit 0c407f7

Please sign in to comment.