我正在尝试使用 Amazon Bedrock 中的基础模型来使用 Python 构建一个简单的聊天机器人。我创建 Boto3 客户端并将模型 ID、接受和内容类型设置为:
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,
})
然后我尝试使用 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'])
我收到以下错误:
---------------------------------------------------------------------------
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.
我在 Amazon Bedrock 文档中读到了此错误,这似乎是服务 ARN 的问题(不确定该如何处理)。
您是否请求访问模型?我遇到了类似的错误,这就是问题所在。
https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html