If I'm using the OpenAI API, and I create a tool like this:
@JsonClassDescription("List directory contents on the local computer")
public static class DirectoryLister {
@JsonPropertyDescription("An absolute path to a local folder")
public String path;
public List<String> listDirectory() {
try {
//noinspection resource
return Files.list( Path.of( path ) ).filter( path1 -> ! path1.getFileName().toString().startsWith( "." ) ).map( Path::toString ).toList();
} catch( IOException e ) {
throw new RuntimeException( e ); //An IOException here would likely indicate a programming error
}
}
}
All I need to do to provide this tool in my conversation is this one line of code. The SDK generates the JSON Schema automatically from my Java class definition and Jackson annotations.
ChatCompletionCreateParams.Builder convo = ChatCompletionCreateParams.builder()
.addTool( DirectoryLister.class ) // <-- advertise our tool
//etc
With Bedrock, it seems like I need to manually do all this in a Document class:
try {
Document schemaDoc2 = Document.mapBuilder()
.putString("type", "object")
.putMap("properties", props -> {
props.putMap("path", path -> {
path.putString("type", "string");
path.putString("description", "An absolute path to a local folder");
});
})
.build();
ToolSpecification spec = ToolSpecification.builder()
.name( toolClass.getSimpleName() )
.description( "List directory contents on the local computer" )
.inputSchema( b->b.json( schemaDoc2 ) )
.build();
return ToolConfiguration.builder()
.tools( b -> b.toolSpec( spec ).build() )
.build();
} catch( JsonProcessingException e ) {
throw new RuntimeException( e );
}
Is there a more convenient way to do this, like in the OpenAI approach?
Are you proposing not using the SDK at all, just send JSON to the HTTP endpoint? I don't see any way in the SDK to include raw JSON for tools.