Keywords: generative AI, Amazon AWS, Bedrock, API, wrapper, LLM, inference, NLP.
Bedrock Inference is a simple python package to handle 2FA and on-demand calls to AWS Bedrock foundation models.
To invoke Amazon Bedrock models, you need to authenticate with your AWS IAM account and establish a connection through MFA. This requires to retrieve some codes
-
An
Access key ID
(es: AKIAIOSFODNN7EXAMPLE) and its correspondentsecret access key
(es: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY) for API requests-
These credentials can be found created at Home -> Security credentials -> Access Keys.
- If you don't have an access key yet: Access Keys -> Create access key
-
If you have AWS CLI/SDK installed and set up on your machine, chances are your credentials are already stored at
~/.aws/credentials
and/or in theAWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
environment variables. In this case, the package will retrieve them automatically.- Note: AWS CLI and AWS SDks are not required to use bedrock-inference.
-
-
A
Multi-factor authentication device ARN
(es: arn:aws:iam::012345678910:mfa/example_username)- The ARN of your MFA device can be located at Home -> Security credentials -> Multi-factor authentication
-
The 6 digits
temp token
generated by the MDA device (es: 366 712)
Even if you don't have any AWS service set up on your machine, you can still create the ~/.aws/credential
file to store your access key credentials for later reuse.
The credential file should look like this:
[bedrock_profile]
aws_access_key_id = AKIAIOSFODNN7EXAMPLE
aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
The profile
name you choose can be used later in aws_login_mfa
in place of aws_access_key_id
and aws_secret_access_key
arguments.
- Make sure you have the latest version of pip installed
pip install --upgrade pip
- Install araucanaxai through pip
pip install bedrock_inference
Here is a minimal example of how the AWS Bedrock models can be invoked with bedrock-inference
.
from bedrock_inference import bedrock
# Authenticates through MFA and establish a session
session = bedrock.aws_login_mfa(arn="MFA_device_arn", token="MFA_token", aws_access_key_id="your_ID", aws_secret_access_key="your_key")
#session = bedrock.aws_login_mfa(arn="MFA_device_arn", token="MFA_token") #use this if you already have AWS credentials set up for API requests on your machine
# Instantiate youre Bedrock caller
caller = bedrock.Bedrock(session=session, region="your_aws_region")
#Check available models in your region
print(caller.list_models())
#Invoke the target AWS model and retrieve the generated answer
answer = caller.invoke_model(prompt="What's the conceptual opposite of 'Hello World'?", model_id="target_aws_model_id")
print(answer)
A more extensive example, including advanced usage, can be found in this notebook.
-
Project Link: https://github.com/detsutut/bedrock-inference
-
Package Link: https://pypi.org/project/bedrock-inference/
Distributed under MIT License. See LICENSE
for more information.