Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added anthropic bedrock #3103

Merged
merged 13 commits into from
Jul 19, 2024
75 changes: 71 additions & 4 deletions autogen/oai/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,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
Expand Down Expand Up @@ -64,14 +85,44 @@ 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)
makkzone marked this conversation as resolved.
Show resolved Hide resolved

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.")
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.")
makkzone marked this conversation as resolved.
Show resolved Hide resolved

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 = Anthropic(api_key=self._api_key)
self._last_tooluse_status = {}

def load_config(self, params: Dict[str, Any]):
Expand Down Expand Up @@ -107,6 +158,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"])
Expand Down