How to send a request to AWS Bedrock properly?

0

I am trying to reproduce the simple example from the user guide:

https://docs.aws.amazon.com/bedrock/latest/userguide/api-setup.html

import boto3

dev = boto3.session.Session(profile_name="dev")
bedrock_runtime = dev.client(
    service_name="bedrock-runtime",
    region_name="us-east-1",
    endpoint_url="https://bedrock.us-east-1.amazonaws.com",
)

import json

model_id = "anthropic.claude-v2"
content_type = "application/json"
accept = "*/*"
body = {"prompt":"Human: Who was the 40th president of the united states?","max_tokens_to_sample":42,"temperature":0.5,"top_k":250,"top_p":1,"anthropic_version":"bedrock-2023-05-31"}

resp = bedrock_runtime.invoke_model(
    modelId=model_id,
    contentType = content_type,
    accept = accept,
    body = json.dumps(body)
)
File ~/venv/lib/python3.11/site-packages/botocore/client.py:535, in ClientCreator._create_api_method.<locals>._api_call(self, *args, **kwargs)
    531     raise TypeError(
    532         f"{py_operation_name}() only accepts keyword arguments."
    533     )
    534 # The "self" in this scope is referring to the BaseClient.
--> 535 return self._make_api_call(operation_name, kwargs)

File ~/venv/lib/python3.11/site-packages/botocore/client.py:980, in BaseClient._make_api_call(self, operation_name, api_params)
    978     error_code = parsed_response.get("Error", {}).get("Code")
    979     error_class = self.exceptions.from_code(error_code)
--> 980     raise error_class(parsed_response, operation_name)
    981 else:
    982     return parsed_response

ValidationException: An error occurred (ValidationException) when calling the InvokeModel operation: The requested operation is not recognized by the service.
  • This was the prompt that worked for me Human: Who is abraham lincoln? Assistant:

    syntax :

    Human: <Your Prompt> Assistant:

Istvan
asked 8 months ago1369 views
2 Answers
1
Accepted Answer

Thank you for providing the code details. When we try to InvokeModel, we should use the endpoint bedrock-runtime.us-east-1.amazonaws.com (missing -runtime here). Therefore, the InvokeModel request is recognized.

More information in this document: https://docs.aws.amazon.com/bedrock/latest/userguide/endpointsTable.html

AWS
weidi
answered 8 months ago
  • This is it, the runtime endpoint was incorrect. Thanks!

0

Hi,

You should make sure you have the latest version of boto3 installed on the environment you are running these tests on.

For e.g.

  1. I installed the boto3 1.28.58 on my EC2 instance in a python3 virtual environment [The 1.28.58 is the latest version as of 02-Oct-2023]
  2. Provided the instance role a permission to access the Bedrock API (similar to the SageMaker Role [https://docs.aws.amazon.com/bedrock/latest/userguide/api-setup.html])
  3. Executed the code snippet provided on the AWS documentation, adjusted the prompts/parameters as per needs [https://docs.aws.amazon.com/bedrock/latest/userguide/api-setup.html]

I received an output from Bedrock based on the above steps.

Also, it is best to check the boto3 version in your current runtime by using print(boto3.__version__) and compare if this version corresponds to the latest which includes Bedrock.

profile pictureAWS
Rama
answered 8 months 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