|
| 1 | +.. _integration-aws-bedrock: |
| 2 | + |
| 3 | +AWS Bedrock API Client |
| 4 | +======================= |
| 5 | + |
| 6 | +.. admonition:: Author |
| 7 | + :class: highlight |
| 8 | + |
| 9 | + `Ajith Kumar <https://github.com/ajithvcoder>`_ |
| 10 | + |
| 11 | +Getting Credentials |
| 12 | +------------------- |
| 13 | + |
| 14 | +You need to have an AWS account and an access key and secret key to use AWS Bedrock services. Moreover, the account associated with the access key must have |
| 15 | +the necessary permissions to access Bedrock services. Refer to the `AWS documentation <https://docs.aws.amazon.com/singlesignon/latest/userguide/howtogetcredentials.html>`_ for more information on obtaining credentials. |
| 16 | + |
| 17 | +Enabling Foundation Models |
| 18 | +-------------------------- |
| 19 | + |
| 20 | +AWS Bedrock offers several foundation models from providers like "Meta," "Amazon," "Cohere," "Anthropic," and "Microsoft." To access these models, you need to enable them first. Note that each AWS region supports a specific set of models. Not all foundation models are available in every region, and pricing varies by region. |
| 21 | + |
| 22 | +Pricing information: `AWS Bedrock Pricing <https://aws.amazon.com/bedrock/pricing/>`_ |
| 23 | + |
| 24 | +Steps for enabling model access: |
| 25 | + |
| 26 | +1. Select the desired region in the AWS Console (e.g., `us-east-1 (N. Virginia)`). |
| 27 | +2. Navigate to the `Bedrock services home page <https://console.aws.amazon.com/bedrock/home>`_. |
| 28 | +3. On the left sidebar, under "Bedrock Configuration," click "Model Access." |
| 29 | + |
| 30 | + You will be redirected to a page where you can select the models to enable. |
| 31 | + |
| 32 | +Note: |
| 33 | + |
| 34 | +1. Avoid enabling high-cost models to prevent accidental high charges due to incorrect usage. |
| 35 | +2. As of Nov 2024, a cost-effective option is the Llama-3.2 1B model, with model ID: ``meta.llama3-2-1b-instruct-v1:0`` in the ``us-east-1`` region. |
| 36 | +3. AWS tags certain models with `inferenceTypesSupported` = `INFERENCE_PROFILE` and in UI it might appear with a tooltip as `This model can only be used through an inference profile.` In such cases you may need to use the Model ARN: ``arn:aws:bedrock:us-east-1:306093656765:inference-profile/us.meta.llama3-2-1b-instruct-v1:0`` in the model ID field when using Adalflow. |
| 37 | +4. Ensure (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION_NAME) or AWS_DEFAULT_PROFILE is set in the ``.env`` file. Mention exact key names in ``.env`` file for example access key id is ``AWS_ACCESS_KEY_ID`` |
| 38 | + |
| 39 | +.. code-block:: python |
| 40 | +
|
| 41 | + import adalflow as adal |
| 42 | + import os |
| 43 | +
|
| 44 | + # Ensure (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION_NAME) or AWS_DEFAULT_PROFILE is set in the .env file |
| 45 | + adal.setup_env() |
| 46 | + model_client = adal.BedrockAPIClient() |
| 47 | + model_client.list_models() |
| 48 | +
|
| 49 | +Which ever profile is tagged with ``INFERENCE_PROFILE`` you might need to provide ``Model ARN`` in ``model`` filed of ``model_kwargs`` |
| 50 | + |
| 51 | +References |
| 52 | +---------- |
| 53 | + |
| 54 | +1. You can refer to Model IDs or Model ARNs `here <https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/models>`_. Clicking on a model card provides additional information. |
| 55 | +2. Internally, Adalflow's AWS client uses the `Converse API <https://boto3.amazonaws.com/v1/documentation/api/1.35.8/reference/services/bedrock-runtime/client/converse.html>`_ for each conversation. |
0 commit comments