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.Documentation Index
Fetch the complete documentation index at: https://private-04b27de1.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
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 theag2[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:Set the config for Amazon Bedrock
Amazon’s Bedrock does not use theapi_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
api_type and model.
The following parameters are common across all models used:
- temperature
- topP
- maxTokens
- top_p
- top_k
- k
- seed
True, so set it to False if your model (for example Mistral’s Instruct models) doesn’t support this feature:
- supports_system_prompts
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.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: