Search code examples
pythonamazon-web-servicesamazon-bedrock

How to send a request to AWS Bedrock properly?


I have the following Python code talking to the Bedrock API:

Setting up the session:

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",
)

Trying to get a simple answer:

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)
)

And it throws this:

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.

Not sure why.


Solution

  • The correct endpoint url is this:

    endpoint_url="https://bedrock-runtime.us-east-1.amazonaws.com",
    

    Change your session setup and the call will be made. You can see that in the boto implementation here