Function calling using Amazon Bedrock with Anthropic Claude 3
Amazon Bedrock is a fully managed service that offers a choice of high-performing foundation models from leading AI companies and a set of capabilities to build generative AI applications.
This sample repo provide an example for using function calling using the Converse API with Anthropic Claude 3 Sonnet, using multiple tools. This repo is a sample only code, that demonstrate how to use function calling as tools for a model to use to fetch results using plain function code.
The Converse API provides a consistent interface that works with all models that support messages. This allows you to write code once and use it with different models. Should a model have unique inference parameters, you can also pass those unique parameters to the model.
Overview
Function calling (also known as tools), is a way to provide the model, with a descriptive guidance for a function that is available to the model to use for answering the user input.
In this sample, we will ask a Claude 3 to answer, what is a stock ticker value, and with option to convert the default ticker currency to any currency that was provided in the user input.
Tools
There are 2 tools available to the models to use:
- get_stock_price - given a ticker string, the open source library yfinance will get the current stock value, and currency that its being traded.
- convert_currency - given an amount, source and target currency, the open source library CurrencyConverter will convert the amount given from source currency to target currency.
The model each turn will review the prompt given, will decide if it can answer properly the question provided in the user input, each turn according to the response from Bedrock end_turn or tool_use. end_turn means that the final answer was provided, and will parse the appropriate data per the tool description that will be used to execute the tool function, and build a proper result back to the model.