Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -240,6 +240,9 @@ public static String addToolsToFunctionCalling(
toolParams.put(NAME, StringEscapeUtils.escapeJson(tool.getName()));
toolParams.put(DESCRIPTION, StringEscapeUtils.escapeJson(tool.getDescription()));
Map<String, ?> attributes = tool.getAttributes();
if (attributes == null || !attributes.containsKey("input_schema")) {
toolParams.put("attributes.input_schema", "{\"type\":\"object\",\"properties\":{}}");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we create constants for both key and value

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — extracted both into INPUT_SCHEMA and DEFAULT_INPUT_SCHEMA constants, and replaced all 5 occurrences of the "input_schema" string literal in the file.

}
if (attributes != null) {
for (String key : attributes.keySet()) {
toolParams.put("attributes." + key, attributes.get(key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,50 @@ public void testAddToolsToFunctionCalling() {
assertEquals(expectedTools, parameters.get(TOOLS));
}

@Test
public void testAddToolsToFunctionCalling_ToolWithNoInputSchema() {
Tool toolNoSchema = mock(Tool.class);
when(toolNoSchema.getName()).thenReturn("VectorDBTool");
when(toolNoSchema.getDescription()).thenReturn("knn dense retrieval tool");
when(toolNoSchema.getAttributes()).thenReturn(null);

Map<String, Tool> tools = new HashMap<>();
tools.put("VectorDBTool", toolNoSchema);

Map<String, String> parameters = new HashMap<>();
String toolTemplate =
"{\"toolSpec\":{\"name\":\"${tool.name}\",\"description\":\"${tool.description}\",\"inputSchema\":{\"json\":${tool.attributes.input_schema}}}}";
parameters.put(TOOL_TEMPLATE, toolTemplate);

AgentUtils.addToolsToFunctionCalling(tools, parameters, List.of("VectorDBTool"), "prompt");

String result = parameters.get(TOOLS);
Assert.assertFalse(result.contains("${tool.attributes.input_schema}"));
Assert.assertTrue(result.contains("\"inputSchema\":{\"json\":{\"type\":\"object\",\"properties\":{}}}"));
}

@Test
public void testAddToolsToFunctionCalling_ToolWithAttributesButNoInputSchema() {
Tool toolNoSchema = mock(Tool.class);
when(toolNoSchema.getName()).thenReturn("VectorDBTool");
when(toolNoSchema.getDescription()).thenReturn("knn dense retrieval tool");
when(toolNoSchema.getAttributes()).thenReturn(Map.of("some_other_key", "value"));

Map<String, Tool> tools = new HashMap<>();
tools.put("VectorDBTool", toolNoSchema);

Map<String, String> parameters = new HashMap<>();
String toolTemplate =
"{\"toolSpec\":{\"name\":\"${tool.name}\",\"description\":\"${tool.description}\",\"inputSchema\":{\"json\":${tool.attributes.input_schema}}}}";
parameters.put(TOOL_TEMPLATE, toolTemplate);

AgentUtils.addToolsToFunctionCalling(tools, parameters, List.of("VectorDBTool"), "prompt");

String result = parameters.get(TOOLS);
Assert.assertFalse(result.contains("${tool.attributes.input_schema}"));
Assert.assertTrue(result.contains("\"inputSchema\":{\"json\":{\"type\":\"object\",\"properties\":{}}}"));
}

@Test
public void testAddToolsToFunctionCalling_ToolNotRegistered() {
Map<String, Tool> tools = new HashMap<>();
Expand Down
Loading