Skip to content

A programming framework for agentic AI

Fix Anthropic Bedrock support (#3210)

Joris van Raaij2b29274fcb8b
diff --git a/autogen/oai/anthropic.py b/autogen/oai/anthropic.py
index 62078d426..8ed6f909e 100644
--- a/autogen/oai/anthropic.py
+++ b/autogen/oai/anthropic.py
@@ -99,17 +99,11 @@ class AnthropicClient:
if not self._aws_secret_key:
self._aws_secret_key = os.getenv("AWS_SECRET_KEY")
- if not self._aws_session_token:
- self._aws_session_token = os.getenv("AWS_SESSION_TOKEN")
-
if not self._aws_region:
self._aws_region = os.getenv("AWS_REGION")
if self._api_key is None and (
- self._aws_access_key is None
- or self._aws_secret_key is None
- or self._aws_session_token is None
- or self._aws_region is None
+ self._aws_access_key is None or self._aws_secret_key is None or self._aws_region is None
):
raise ValueError("API key or AWS credentials are required to use the Anthropic API.")
diff --git a/autogen/oai/client.py b/autogen/oai/client.py
index 4e9d794a1..4cc7c697f 100644
--- a/autogen/oai/client.py
+++ b/autogen/oai/client.py
@@ -454,12 +454,20 @@ class OpenAIWrapper:
azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
+ def _configure_openai_config_for_bedrock(self, config: Dict[str, Any], openai_config: Dict[str, Any]) -> None:
+ """Update openai_config with AWS credentials from config."""
+ required_keys = ["aws_access_key", "aws_secret_key", "aws_region"]
+
+ for key in required_keys:
+ if key in config:
+ openai_config[key] = config[key]
+
def _register_default_client(self, config: Dict[str, Any], openai_config: Dict[str, Any]) -> None:
"""Create a client with the given config to override openai_config,
after removing extra kwargs.
For Azure models/deployment names there's a convenience modification of model removing dots in
- the it's value (Azure deploment names can't have dots). I.e. if you have Azure deployment name
+ the it's value (Azure deployment names can't have dots). I.e. if you have Azure deployment name
"gpt-35-turbo" and define model "gpt-3.5-turbo" in the config the function will remove the dot
from the name and create a client that connects to "gpt-35-turbo" Azure deployment.
"""
@@ -485,6 +493,8 @@ class OpenAIWrapper:
client = GeminiClient(**openai_config)
self._clients.append(client)
elif api_type is not None and api_type.startswith("anthropic"):
+ if "api_key" not in config:
+ self._configure_openai_config_for_bedrock(config, openai_config)
if anthropic_import_exception:
raise ImportError("Please install `anthropic` to use Anthropic API.")
client = AnthropicClient(**openai_config)