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!

1 Answer
0

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
EXPERT
answered 2 months ago
profile picture
EXPERT
reviewed a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions