From 8043bdf4bfac48713e84ea0b39d5a85e1c125c39 Mon Sep 17 00:00:00 2001 From: Manojkumar Kotakonda <44414430+makkzone@users.noreply.github.com> Date: Wed, 10 Jul 2024 07:03:43 -0300 Subject: [PATCH 1/4] Added anthropic bedrock --- autogen/oai/anthropic.py | 72 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 6 deletions(-) diff --git a/autogen/oai/anthropic.py b/autogen/oai/anthropic.py index e2448929e618..ba9adbdc1e30 100644 --- a/autogen/oai/anthropic.py +++ b/autogen/oai/anthropic.py @@ -16,6 +16,27 @@ ] assistant = autogen.AssistantAgent("assistant", llm_config={"config_list": config_list}) + +Example usage for Anthropic Bedrock: + +Install the `anthropic` package by running `pip install --upgrade anthropic`. +- https://docs.anthropic.com/en/docs/quickstart-guide + +import autogen + +config_list = [ + { + "model": "anthropic.claude-3-5-sonnet-20240620-v1:0", + "aws_access_key":accessKey, + "aws_secret_key":secretKey, + "aws_session_token":sessionTok, + "aws_region":"us-east-1", + "api_type": "anthropic", + } +] + +assistant = autogen.AssistantAgent("assistant", llm_config={"config_list": config_list}) + """ from __future__ import annotations @@ -64,14 +85,37 @@ def __init__(self, **kwargs: Any): api_key (str): The API key for the Anthropic API or set the `ANTHROPIC_API_KEY` environment variable. """ self._api_key = kwargs.get("api_key", None) - + self._aws_access_key = kwargs.get("aws_access_key", None) + self._aws_secret_key = kwargs.get("aws_secret_key", None) + self._aws_session_token = kwargs.get("aws_session_token", None) + self._aws_region = kwargs.get("aws_region", None) + if not self._api_key: self._api_key = os.getenv("ANTHROPIC_API_KEY") - - if self._api_key is None: - raise ValueError("API key is required to use the Anthropic API.") - - self._client = Anthropic(api_key=self._api_key) + + if not self._aws_access_key: + self._aws_access_key = os.getenv("AWS_ACCESS_KEY") + + 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): + raise ValueError("API key or AWS credentials are required to use the Anthropic API.") + + if self._api_key is not None: + self._client = Anthropic(api_key=self._api_key) + else: + self._client = AnthropicBedrock(aws_access_key=self._aws_access_key, + aws_secret_key=self._aws_secret_key, + aws_session_token=self._aws_session_token, + aws_region=self._aws_region) + self._last_tooluse_status = {} def load_config(self, params: Dict[str, Any]): @@ -107,6 +151,22 @@ def cost(self, response) -> float: def api_key(self): return self._api_key + @property + def aws_access_key(self): + return self._aws_access_key + + @property + def aws_secret_key(self): + return self._aws_secret_key + + @property + def aws_session_token(self): + return self._aws_session_token + + @property + def aws_region(self): + return self._aws_region + def create(self, params: Dict[str, Any]) -> Completion: if "tools" in params: converted_functions = self.convert_tools_to_functions(params["tools"]) From 7fb11cd1a6dcc9fa7b7054b112196f97bbb969ff Mon Sep 17 00:00:00 2001 From: Manojkumar Kotakonda <44414430+makkzone@users.noreply.github.com> Date: Fri, 12 Jul 2024 06:44:11 -0300 Subject: [PATCH 2/4] Code format and fixed import --- autogen/oai/anthropic.py | 49 +++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/autogen/oai/anthropic.py b/autogen/oai/anthropic.py index ba9adbdc1e30..62078d42631d 100644 --- a/autogen/oai/anthropic.py +++ b/autogen/oai/anthropic.py @@ -27,9 +27,9 @@ config_list = [ { "model": "anthropic.claude-3-5-sonnet-20240620-v1:0", - "aws_access_key":accessKey, - "aws_secret_key":secretKey, - "aws_session_token":sessionTok, + "aws_access_key":, + "aws_secret_key":, + "aws_session_token":, "aws_region":"us-east-1", "api_type": "anthropic", } @@ -49,7 +49,7 @@ import warnings from typing import Any, Dict, List, Tuple, Union -from anthropic import Anthropic +from anthropic import Anthropic, AnthropicBedrock from anthropic import __version__ as anthropic_version from anthropic.types import Completion, Message, TextBlock, ToolUseBlock from openai.types.chat import ChatCompletion, ChatCompletionMessageToolCall @@ -89,33 +89,40 @@ def __init__(self, **kwargs: Any): self._aws_secret_key = kwargs.get("aws_secret_key", None) self._aws_session_token = kwargs.get("aws_session_token", None) self._aws_region = kwargs.get("aws_region", None) - + if not self._api_key: self._api_key = os.getenv("ANTHROPIC_API_KEY") - + if not self._aws_access_key: self._aws_access_key = os.getenv("AWS_ACCESS_KEY") - + 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): + + 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 + ): raise ValueError("API key or AWS credentials are required to use the Anthropic API.") - + if self._api_key is not None: self._client = Anthropic(api_key=self._api_key) else: - self._client = AnthropicBedrock(aws_access_key=self._aws_access_key, - aws_secret_key=self._aws_secret_key, - aws_session_token=self._aws_session_token, - aws_region=self._aws_region) - + self._client = AnthropicBedrock( + aws_access_key=self._aws_access_key, + aws_secret_key=self._aws_secret_key, + aws_session_token=self._aws_session_token, + aws_region=self._aws_region, + ) + self._last_tooluse_status = {} def load_config(self, params: Dict[str, Any]): @@ -154,19 +161,19 @@ def api_key(self): @property def aws_access_key(self): return self._aws_access_key - + @property def aws_secret_key(self): return self._aws_secret_key - + @property def aws_session_token(self): return self._aws_session_token - + @property def aws_region(self): return self._aws_region - + def create(self, params: Dict[str, Any]) -> Completion: if "tools" in params: converted_functions = self.convert_tools_to_functions(params["tools"]) From f8002871bdebea04e8fd8bff624e0323d758c3b3 Mon Sep 17 00:00:00 2001 From: Manojkumar Kotakonda <44414430+makkzone@users.noreply.github.com> Date: Mon, 15 Jul 2024 06:47:26 -0300 Subject: [PATCH 3/4] Added tests for anthropic bedrock --- test/oai/test_anthropic.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/oai/test_anthropic.py b/test/oai/test_anthropic.py index 379ab47f6756..ed9b2d17844a 100644 --- a/test/oai/test_anthropic.py +++ b/test/oai/test_anthropic.py @@ -50,7 +50,11 @@ def anthropic_client(): @pytest.mark.skipif(skip, reason=reason) def test_initialization_missing_api_key(): os.environ.pop("ANTHROPIC_API_KEY", None) - with pytest.raises(ValueError, match="API key is required to use the Anthropic API."): + os.environ.pop("AWS_ACCESS_KEY", None) + os.environ.pop("AWS_SECRET_KEY", None) + os.environ.pop("AWS_SESSION_TOKEN", None) + os.environ.pop("AWS_REGION", None) + with pytest.raises(ValueError, match="API key or AWS credentials are required to use the Anthropic API."): AnthropicClient() AnthropicClient(api_key="dummy_api_key") From 50b6e03e7e241860950599f1a7c4d7a7da3be39b Mon Sep 17 00:00:00 2001 From: HRUSHIKESH DOKALA <96101829+Hk669@users.noreply.github.com> Date: Fri, 19 Jul 2024 16:48:09 +0000 Subject: [PATCH 4/4] tests update --- test/oai/test_anthropic.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/oai/test_anthropic.py b/test/oai/test_anthropic.py index ed9b2d17844a..53926dbd18d6 100644 --- a/test/oai/test_anthropic.py +++ b/test/oai/test_anthropic.py @@ -60,11 +60,37 @@ def test_initialization_missing_api_key(): AnthropicClient(api_key="dummy_api_key") +@pytest.fixture() +def anthropic_client_with_aws_credentials(): + return AnthropicClient( + aws_access_key="dummy_access_key", + aws_secret_key="dummy_secret_key", + aws_session_token="dummy_session_token", + aws_region="us-west-2", + ) + + @pytest.mark.skipif(skip, reason=reason) def test_intialization(anthropic_client): assert anthropic_client.api_key == "dummy_api_key", "`api_key` should be correctly set in the config" +@pytest.mark.skipif(skip, reason=reason) +def test_intialization_with_aws_credentials(anthropic_client_with_aws_credentials): + assert ( + anthropic_client_with_aws_credentials.aws_access_key == "dummy_access_key" + ), "`aws_access_key` should be correctly set in the config" + assert ( + anthropic_client_with_aws_credentials.aws_secret_key == "dummy_secret_key" + ), "`aws_secret_key` should be correctly set in the config" + assert ( + anthropic_client_with_aws_credentials.aws_session_token == "dummy_session_token" + ), "`aws_session_token` should be correctly set in the config" + assert ( + anthropic_client_with_aws_credentials.aws_region == "us-west-2" + ), "`aws_region` should be correctly set in the config" + + # Test cost calculation @pytest.mark.skipif(skip, reason=reason) def test_cost_calculation(mock_completion):