I’m trying to use the foundational models in Amazon Bedrock to build a simple chatbot using Python. I create Boto3 client and set the model ID, accept and content types as:
bedrock = boto3.client(
service_name='bedrock-runtime',
region_name="us-east-1"
)
modelId = 'cohere.command-text-v14'
accept = 'application/json'
contentType = 'application/json'
body = json.dumps({
"prompt": "Hello World",
"temperature": 0.75,
"p": 0.01,
"k": 0,
})
I then try to generate text by using invoke_model:
response = bedrock.invoke_model(body=body, modelId=modelId, accept=accept, contentType=contentType)
response_body = json.loads(response.get('body').read())
print(response_body['generations'][0]['text'])
I get the following error:
---------------------------------------------------------------------------
ResourceNotFoundException Traceback (most recent call last)
3 contentType = 'application/json'
4
----> 5 response = bedrock.invoke_model(body=body, modelId=modelId, accept=accept, contentType=contentType)
6
7 response_body = json.loads(response.get('body').read())
~/Library/Python/3.8/lib/python/site-packages/botocore/client.py in _api_call(self, *args, **kwargs)
551 )
552 # The "self" in this scope is referring to the BaseClient.
--> 553 return self._make_api_call(operation_name, kwargs)
554
555 _api_call.__name__ = str(py_operation_name)
~/Library/Python/3.8/lib/python/site-packages/botocore/client.py in _make_api_call(self, operation_name, api_params)
1007 )
1008 error_class = self.exceptions.from_code(error_code)
-> 1009 raise error_class(parsed_response, operation_name)
1010 else:
1011 return parsed_response
ResourceNotFoundException: An error occurred (ResourceNotFoundException) when calling the InvokeModel operation: Could not resolve the foundation model from the provided model identifier.
I read about this error in the Amazon Bedrock documentation, and it seems to be a problem with the service ARN (unsure what to do about this).
2
Answers
This error occurred for me because I hadn’t requested access to the model in the AWS Console.
Go to your console, search for bedrock and select model access, then request access to the desired models.
I received this error because I did not specify the region of the Bedrock resource when initiating a new client. The lambda was in
us-east-2
, and so the client was defaulting to looking in that region. I had to specify the region as shown below and then it was working.Some places online state that you cannot invoke cross-regionally from lambda to Bedrock in different regions but I was able to.
In the lambda:
const client = new BedrockRuntimeClient({ region: "us-east-1" });