Search code examples
python-3.xboto3botoamazon-bedrock

retrieveAndGenerate Syntax Error: Unknown parameter generationConfiguration or retrievalConfiguration (Claude-v3, Amazon Bedrock)


I am trying to retrieve and generate response from knowledge base use claude-v3 model. To do so I followed the boto3 documentation and Blog Post on Amazon and wrote this method:

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

But it is giving me this error:

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

I call the function as this:

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

The same error is raised with even one of aforementioned fields.

Full trace of the exception

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

I tried to put generationConfiguration and retrievalConfiguration out of knowledgeBaseConfiguration but those cases are also raising the same error.

It only works with minimum required fields like this:

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

What is my mistake and how do I solve it? Appreciate your responses.

UPDATE:

I was running the application inside a docker contiainer. Even though I updated boto3 version by using the shell inside the container and the boto3 version was showing the latest one, the update didn't take effect untill I rebuild the container. While rebuilding I insatalled the latest version of the boto3 and the problem was solved.


Solution

  • The issue I encountered was due to running my application inside a Docker container, where the updated version of the boto3 library was not being reflected despite showing the latest version installed.

    To solve this problem, I needed to rebuild the Docker container by installing the latest version of the boto3 library. Simply updating the library within the existing container was not sufficient.

    To update boto3 while rebuilding, I modified the of the library in requirement.txt file and rebuilt the docker container with --no-cache parameter:

    docker-compose build --no-cache
    

    After rebuilding the Docker container with the latest boto3 version installed, the retrieveAndGenerate function worked as expected.