Skip to content

Is there a convenient way to provide a Tool to Bedrock?

0

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?

asked a year ago190 views

1 Answer
0

Hi,

You can include the tool description in the prompt itself. Look at https://docs.aws.amazon.com/bedrock/latest/userguide/tool-use-inference-call.html#tool-use-send-tool-info for all details and some samples.

Best,

Didier

EXPERT

answered a year ago

  • 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.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.