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

Add support for Amazon Bedrock models #35

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open

Conversation

Cerrix
Copy link

@Cerrix Cerrix commented Jun 27, 2024

Overview

This PR adds support for Amazon Bedrock, allowing TextGrad users to leverage a variety of foundation models from AI21 Labs, Anthropic, Cohere, Meta, Mistral AI, Stability AI, and Amazon through a single API.

Key Changes

  • Implemented support for Amazon Bedrock in the get_engine function
  • Added compatibility for Meta, Anthropic, Cohere, Mistral, and Amazon models
  • Introduced a new naming convention for Bedrock models: bedrock-<model_name_id> (here the model id list)

Usage

To use a Bedrock model, call the get_engine function with the Bedrock model name ID prefixed by "bedrock-". For example:

engine = get_engine("bedrock-anthropic.claude-3-sonnet-20240229-v1:0", max_tokens=2048)

Authentication

The PR supports two authentication methods:

  1. AWS environment using AWS Roles
  2. Environment variables:
  • AWS_DEFAULT_REGION (optional)
  • AWS_SECRET_ACCESS_KEY
  • AWS_ACCESS_KEY_ID
  • AWS_SESSION_TOKEN (optional)

Supported Models

This PR adds support for the following model families:

  • Meta
  • Anthropic
  • Cohere
  • Mistral
  • Amazon

A complete list of supported model IDs will be added to the documentation.

Testing

  • Tested with various Bedrock models
  • Verified authentication methods
  • Ensured compatibility with existing TextGrad functionality

Feedback

I welcome any feedback or suggestions for improvement. Please let me know if any additional changes or clarifications are needed.

Copy link
Member

@mertyg mertyg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much @Cerrix !!! I just added a couple minor comments. Bare minimum let's change the requirements.txt file and improve the import error handling adding the environment variable warnings -- if you have time, would love to have multiple messages functionality as I added the comment (not required though).

Thanks again for your contribution!

requirements.txt Outdated
gdown>=5.2.0
boto3>=1.34.133
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make this optional? i.e., without adding this to requirements.txt, can we just leave it to the import error handler (like we have for e.g., anthropic.py / openai.py etc., saying people who want to use bedrock models should install boto?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure we can, I'm already managing the import error :) let me change that

from botocore.config import Config

except ImportError:
raise ImportError("If you'd like to use Amazon Bedrock models, please install the boto3 package by running `pip install boto3`")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add the related environment variables that would be needed here? i.e. AWS_ACCESS_KEY_ID etc,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 63/68 handles the variables needed. But if you prefer I can move them also here


return response

def generate(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is great! Do you want to add support for multiple inputs to this engine? Like here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I can do! Can you provide me a simple example to test the implementation out?

Regarding to images only Anthropic models in Bedrock support vision so I'll enable that only for them!

@Cerrix
Copy link
Author

Cerrix commented Jul 10, 2024

Hi @mertyg thank you so much for the comments! I modified the requirements and commented out the other two comments (for env variables please check if you need them also at line 6, they are already specified at line 63/68).

I'm happy to implement multiple inputs support but can you please provide me an example to test that out? I'll enable the vision capabilities only for Anthropic models (currently they are the only one to support vision in Amazon bedrock).

thank you!

@alecl
Copy link

alecl commented Aug 10, 2024

It would be excellent to see this pushed through opening up quite a lot of models today and in the future.

@t3ch-3nthus
Copy link

Hi, I would definitely make use of the bedrock version, do you have an estimated date for this branch ? thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants