Accessing Amazon Bedrock Claude-3-Haiku and Claude-3-Sonnet models via Lambda Functions

0

Hi!

Can Claude-3-Haiku and Claude-3-Sonnet models be accessed using a Lambda function?

I gave it a try using model id anthropic.claude-3-haiku-20240307-v1:0 and got the following error (the same error was received for model id anthropic.claude-3-sonnet-20240229-v1:0):

[ERROR] ValidationException: An error occurred (ValidationException) when calling the InvokeModel operation: "claude-3-haiku-20240307" is not supported on this API. Please use the Messages API instead.

The error comes from the following code (the code works for all other LLMs on Bedrock including Claude 2.1): brt = boto3.client('bedrock-runtime') response = brt.invoke_model(body=body, modelId=modelId, accept=accept, contentType=contentType)

Please advise, thanks!

2 回答
1

Hello.

I'm not sure since I haven't seen the full code, but are you using the Text Completions API?
Amazon Bedrock's Text Completions API cannot use Claude-3-Sonnet etc. as of March 2024.
Only the following models are compatible.
https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-text-completion.html

  • Anthropic Claude Instant v1.2
  • Anthropic Claude v2
  • Anthropic Claude v2.1

For Messages API, you can use Claude-3-Sonnet etc. as shown below.
https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages.html

import boto3
import json

def lambda_handler(event, context):

    bedrock = boto3.client('bedrock-runtime', region_name = "us-east-1")
    body = json.dumps(
        {
            "anthropic_version": "bedrock-2023-05-31",
            "max_tokens": 1000,
            "messages": [
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "text",
                            "text":"Hello My name is Riku"
                        }
                    ]
                }
            ]
        }
    )
    modelId = 'anthropic.claude-3-sonnet-20240229-v1:0'
    accept = 'application/json'
    contentType = 'application/json'
    response = bedrock.invoke_model(body=body, modelId=modelId, accept=accept, contentType=contentType)
    response_body = json.loads(response.get('body').read())
    answer = response_body["content"][0]["text"]
    print(answer)
profile picture
专家
已回答 3 个月前
profile picture
专家
已审核 2 个月前
0

Hi Here a response using AWS Bedrock in 2024.

Basically I had the same error message, what I did was:

  1. Follow the next documentation [1], here are some simple examples of what you can do using different program lenguages.
  2. Double check the Anthopic_version in the body request, I was using a different one that the supported [2]
  3. Double check if you are using a supported model id. You can see the available ones in this link [3]
  4. Double check if you have access to the model in the AWS Bedrock console. Here the steps to configure the model access [4]

Additional, a good way to try this [1] examples really quick is using Amazon Lambda, just modify the AWS IAM role of the lambda and add the less privilege in order to use Amazon bedrock.

Hope this information is usefull. Best regards

Edgar Hernández ITera's AWS Data and Analytics Manger (MX)

[1] Invoke Anthropic Claude models on Amazon Bedrock using the Invoke Model API. https://docs.aws.amazon.com/bedrock/latest/userguide/bedrock-runtime_example_bedrock-runtime_InvokeModel_AnthropicClaude_section.html

[2] Anthropic_version used "anthropic_version": "bedrock-2023-05-31"

[3] Amazon Bedrock base model IDs (on-demand throughput) https://docs.aws.amazon.com/bedrock/latest/userguide/model-ids.html#model-ids-arns

[4] Add model access https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html

profile picture
已回答 几秒前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则