Skip to content

Commit edecd32

Browse files
committed
feat: add usage tutorial for bedrock client, docs: add documentation for aws bedrock integration
1 parent cc1887f commit edecd32

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import os
2+
3+
from adalflow.components.model_client import BedrockAPIClient
4+
from adalflow.core.types import ModelType
5+
from adalflow.utils import setup_env
6+
7+
8+
def list_models():
9+
# For list of models
10+
model_client = BedrockAPIClient()
11+
model_client.list_models(byProvider="meta")
12+
13+
14+
def bedrock_chat_conversation():
15+
# Initialize the Bedrock client for API interactions
16+
awsbedrock_client = BedrockAPIClient()
17+
query = "What is the capital of France?"
18+
19+
# Embed the prompt in Llama 3's instruction format.
20+
formatted_prompt = f"""
21+
<|begin_of_text|><|start_header_id|>user<|end_header_id|>
22+
{query}
23+
<|eot_id|>
24+
<|start_header_id|>assistant<|end_header_id|>
25+
"""
26+
27+
# Set the model type to Large Language Model (LLM)
28+
model_type = ModelType.LLM
29+
30+
# Configure model parameters:
31+
# - model: Specifies Llama-3-2 1B as the model to use
32+
# - temperature: Controls randomness (0.5 = balanced between deterministic and creative)
33+
# - max_tokens: Limits the response length to 100 tokens
34+
35+
# Using Model ARN since its has inference_profile in us-east-1 region
36+
# https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=meta.llama3-2-1b-instruct-v1:0
37+
model_id = "arn:aws:bedrock:us-east-1:306093656765:inference-profile/us.meta.llama3-2-1b-instruct-v1:0"
38+
model_kwargs = {"model": model_id, "temperature": 0.5, "max_tokens": 100}
39+
40+
# Convert the inputs into the format required by BedRock's API
41+
api_kwargs = awsbedrock_client.convert_inputs_to_api_kwargs(
42+
input=formatted_prompt, model_kwargs=model_kwargs, model_type=model_type
43+
)
44+
print(f"api_kwargs: {api_kwargs}")
45+
46+
response = awsbedrock_client.call(api_kwargs=api_kwargs, model_type=model_type)
47+
48+
# Extract the text from the chat completion response
49+
response_text = awsbedrock_client.parse_chat_completion(response)
50+
print(f"response_text: {response_text}")
51+
52+
53+
if __name__ == "__main__":
54+
setup_env()
55+
list_models()
56+
bedrock_chat_conversation()

0 commit comments

Comments
 (0)