我正在尝试使用 claude-v3 模型从知识库检索并生成响应。为此,我遵循了boto3文档和亚马逊上的博客文章并编写了这个方法:
def retrieveAndGenerate(input, kbId, modelArn=None):
response = boto_runtime.retrieve_and_generate(
input={
'text': input
},
retrieveAndGenerateConfiguration={
'knowledgeBaseConfiguration': {
'generationConfiguration': {
'promptTemplate': {
'textPromptTemplate': promptTemplate
}
},
'knowledgeBaseId': kbId,
'modelArn': modelArn,
"retrievalConfiguration": {
'vectorSearchConfiguration': {
'numberOfResults': 5
}
}
},
'type': 'KNOWLEDGE_BASE'
}
)
return response
但是它给了我这个错误:
ParamValidationError: Parameter validation failed: Unknown parameter in retrieveAndGenerateConfiguration.knowledgeBaseConfiguration: "generationConfiguration", must be one of: knowledgeBaseId, modelArn
Unknown parameter in retrieveAndGenerateConfiguration.knowledgeBaseConfiguration: "retrievalConfiguration", must be of one: knowledgeBaseId, modelArn
我将该函数称为:
anthropicModelArns = [arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-sonnet-20240229-v1:0']
response = retrieveAndGenerate(input='Felsefe nedir?', kbId='VPY6GXXXXX', modelArn=anthropicModelArns[0])
即使上述字段之一也会引发相同的错误。
异常的完整跟踪
ParamValidationError Traceback (most recent call last)
Cell In[45], line 1
----> 1 response = retrieveAndGenerate(input='Felsefe nedir?', kbId='VPY6GXXXX', modelArn=anthropicModelArns[0])
Cell In[44], line 2
1 def retrieveAndGenerate(input, kbId, modelArn=None):
----> 2 response = boto_runtime.retrieve_and_generate(
3 input={
4 'text': input
5 },
6 retrieveAndGenerateConfiguration={
7 'knowledgeBaseConfiguration': {
8 'generationConfiguration': {
9 'promptTemplate': {
10 'textPromptTemplate': promptTemplate
11 }
12 },
13 'knowledgeBaseId': kbId,
14 'modelArn': modelArn,
15 "retrievalConfiguration": {
16 'vectorSearchConfiguration': {
17 'numberOfResults': 5
18 }
19 }
20 },
21 'type': 'KNOWLEDGE_BASE'
22 }
23 )
25 return response
File /usr/local/lib/python3.12/site-packages/botocore/client.py:553, in ClientCreator._create_api_method.<locals>._api_call(self, *args, **kwargs)
549 raise TypeError(
550 f"{py_operation_name}() only accepts keyword arguments."
551 )
552 # The "self" in this scope is referring to the BaseClient.
--> 553 return self._make_api_call(operation_name, kwargs)
File /usr/local/lib/python3.12/site-packages/botocore/client.py:962, in BaseClient._make_api_call(self, operation_name, api_params)
958 if properties:
959 # Pass arbitrary endpoint info with the Request
960 # for use during construction.
961 request_context['endpoint_properties'] = properties
--> 962 request_dict = self._convert_to_request_dict(
963 api_params=api_params,
964 operation_model=operation_model,
965 endpoint_url=endpoint_url,
966 context=request_context,
967 headers=additional_headers,
968 )
969 resolve_checksum_context(request_dict, operation_model, api_params)
971 service_id = self._service_model.service_id.hyphenize()
File /usr/local/lib/python3.12/site-packages/botocore/client.py:1036, in BaseClient._convert_to_request_dict(self, api_params, operation_model, endpoint_url, context, headers, set_user_agent_header)
1027 def _convert_to_request_dict(
1028 self,
1029 api_params,
(...)
1034 set_user_agent_header=True,
1035 ):
-> 1036 request_dict = self._serializer.serialize_to_request(
1037 api_params, operation_model
1038 )
1039 if not self._client_config.inject_host_prefix:
1040 request_dict.pop('host_prefix', None)
File /usr/local/lib/python3.12/site-packages/botocore/validate.py:381, in ParamValidationDecorator.serialize_to_request(self, parameters, operation_model)
377 report = self._param_validator.validate(
378 parameters, operation_model.input_shape
379 )
380 if report.has_errors():
--> 381 raise ParamValidationError(report=report.generate_report())
382 return self._serializer.serialize_to_request(
383 parameters, operation_model
384 )
ParamValidationError: Parameter validation failed:
Unknown parameter in retrieveAndGenerateConfiguration.knowledgeBaseConfiguration: "generationConfiguration", must be one of: knowledgeBaseId, modelArn
Unknown parameter in retrieveAndGenerateConfiguration.knowledgeBaseConfiguration: "retrievalConfiguration", must be one of: knowledgeBaseId, modelArn
我尝试将
generationConfiguration
和 retrievalConfiguration
排除在 knowledgeBaseConfiguration
之外,但这些情况也引发了相同的错误。
它仅适用于最少的必填字段,如下所示:
def retrieveAndGenerate(input, kbId, modelArn=None):
response = boto_runtime.retrieve_and_generate(
input={
'text': input
},
retrieveAndGenerateConfiguration={
'knowledgeBaseConfiguration': {
'knowledgeBaseId': kbId,
'modelArn': modelArn
},
'type': 'KNOWLEDGE_BASE'
}
)
return response
我的错误是什么以及如何解决?感谢您的回复。
我遇到了同样的问题,但在 lambda 上更新 boto3 版本后解决了。
您可以直接在 Lambda 代码中或使用 Lambda 层更新 boto3。有关在 AWS Lambda 中更新 boto3 的指南,您可以参考此 Stack Overflow 讨论:AWS Lambda 控制台 - 升级 boto3 版本。