Skip to main content
AG2 allows you to use Amazon’s generative AI Bedrock service to run inference with a number of open-weight models and as well as their own models. Amazon Bedrock supports models from providers such as Meta, Anthropic, Cohere, and Mistral. In this notebook, we demonstrate how to use Anthropic’s Sonnet model for AgentChat in AG2.

Model features / support

Amazon Bedrock supports a wide range of models, not only for text generation but also for image classification and generation. Not all features are supported by AG2 or by the Converse API used. Please see Amazon’s documentation on the features supported by the Converse API. At this point in time AG2 supports text generation and image classification (passing images to the LLM).

Requirements

To use Amazon Bedrock with AG2, first you need to install the ag2[bedrock] package.

Pricing

When we combine the number of models supported and costs being on a per-region basis, it’s not feasible to maintain the costs for each model+region combination within the AG2 implementation. Therefore, it’s recommended that you add the following to your config with cost per 1,000 input and output tokens, respectively:
Amazon Bedrock pricing is available here.

Set the config for Amazon Bedrock

Amazon’s Bedrock does not use the api_key as per other cloud inference providers for authentication, instead it uses a number of access, token, and profile values. These fields will need to be added to your client configuration. Please check the Amazon Bedrock documentation to determine which ones you will need to add. The available parameters are:
  • aws_region (mandatory)
  • aws_access_key (or environment variable: AWS_ACCESS_KEY)
  • aws_secret_key (or environment variable: AWS_SECRET_KEY)
  • aws_session_token (or environment variable: AWS_SESSION_TOKEN)
  • aws_profile_name
Beyond the authentication credentials, the only mandatory parameters are api_type and model. The following parameters are common across all models used:
  • temperature
  • topP
  • maxTokens
You can also include parameters specific to the model you are using (see the model detail within Amazon’s documentation for more information), the four supported additional parameters are:
  • top_p
  • top_k
  • k
  • seed
An additional parameter can be added that denotes whether the model supports a system prompt (which is where the system messages are not included in the message list, but in a separate parameter). This defaults to True, so set it to False if your model (for example Mistral’s Instruct models) doesn’t support this feature:
  • supports_system_prompts
It is important to add the api_type field and set it to a string that corresponds to the client type used: bedrock. Example:

Using within an AWS Lambda function

If you are using your AG2 code within an AWS Lambda function, you can utilise the attached role to access the Bedrock service and do not need to provide access, token, or profile values.

Two-agent Coding Example

Configuration

Start with our configuration - we’ll use Anthropic’s Sonnet model and put in recent pricing. Additionally, we’ll reduce the temperature to 0.1 so its responses are less varied. Then we’ll construct a simple conversation between a User proxy and an ConversableAgent, which uses the Sonnet model.

Tool Call Example

In this example, instead of writing code, we will show how we can perform multiple tool calling with Meta’s Llama 3.1 70B model, where it recommends calling more than one tool at a time. We’ll use a simple travel agent assistant program where we have a couple of tools for weather and currency conversion.

Group Chat Example with Anthropic’s Claude 3 Sonnet, Mistral’s Large 2, and Meta’s Llama 3.1 70B

The flexibility of using LLMs from the industry’s leading providers, particularly larger models, with Amazon Bedrock allows you to use multiple of them in a single workflow. Here we have a conversation that has two models (Anthropic’s Claude 3 Sonnet and Mistral’s Large 2) debate each other with another as the judge (Meta’s Llama 3.1 70B). Additionally, a tool call is made to pull through some mock news that they will debate on.
And there we have it, a number of different LLMs all collaborating together on a single cloud platform.

Image classification with Anthropic’s Claude 3 Sonnet

AG2’s Amazon Bedrock client class supports inputting images for the LLM to respond to. In this simple example, we’ll use an image on the Internet and send it to Anthropic’s Claude 3 Sonnet model to describe. Here’s the image we’ll use: I -heart- AutoGen