Skip to content

How do I configure the boto3 retrieve_and_generate function to filter knowledge base retrieval for a date range?

0

How do I perform a date filter in my bedrock knowledgebase using boto? My guess is that there's a type mismatch between what boto3 is providing and what OpenSearch is expecting, but would like to confirm before trying database rebuild options. The examples that I've found in AWS documentation filter by year, which might not be a date type.

I have a knowledge base configured with AWS OpenSearch. In OpenSearch, I can run a date range query like the following with no issues:

GET bedrock-knowledge-base-default-index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "publication_date": {
              "gte": "2024-01-30",
              "lte": "2025-01-30",
              "format": "yyyy-MM-dd"
            }}}]}}}

I have tried passing dates as strings of yyyy-MM-dd and other formats (to include string of epoch millis) but this seems to present type mismatches. Converting my dates so epoch millis seems to get a step closer

However, when calling with the retrieve_and_generate function, I get errors like the following:

File "/var/lang/lib/python3.12/site-packages/botocore/client.py", line 1061, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the RetrieveAndGenerate operation: failed to parse date field [1.6015104E12] with format [strict_date_optional_time||epoch_millis]: [failed to parse date field [1.6015104E12] with format [strict_date_optional_time||epoch_millis]]

Some additional background:

My filter is set up as follows (which works well for booleans and string types):

import boto3
def retrieve_and_generate(input_text, chat_filter):
    knowledge_base_config = {
        "generationConfiguration": {
            "promptTemplate": {
                "textPromptTemplate": PROMPT #your prompt text here
            }
        },
        "knowledgeBaseId": KB_ID, #your knowledge base ID here
        "modelArn": GENERATIVE_MODEL_ARN,#your model ARN here
        "retrievalConfiguration": {
            "vectorSearchConfiguration": {
                "filter": chat_filter,
                "numberOfResults": 20
            }
        }
    }

    retrieve_and_generate_config = {
        "knowledgeBaseConfiguration": knowledge_base_config,
        "type": "KNOWLEDGE_BASE"
    }
    if session_id:
        response = bar_client.retrieve_and_generate(
            input={"text": input_text},
            retrieveAndGenerateConfiguration=retrieve_and_generate_config,
            sessionId=session_id
        )
    else:
        response = bar_client.retrieve_and_generate(
            input={"text": input_text},
            retrieveAndGenerateConfiguration=retrieve_and_generate_config
        )
    return response

How do I configure the boto3 retrieve_and_generate function to filter knowledge base retrieval for a date range?

I have a knowledge base configured with AWS OpenSearch. In OpenSearch, I can run a date range query like the following with no issues:

GET bedrock-knowledge-base-default-index/_search { "query": { "bool": { "must": [ { "range": { "publication_date": { "gte": "2024-01-30", "lte": "2025-01-30", "format": "yyyy-MM-dd" }}}]}}} I have tried passing dates as strings of yyyy-MM-dd and other formats (to include string of epoch millis) but this seems to present type mismatches. Converting my dates so epoch millis seems to get a step closer

However, when calling with the retrieve_and_generate function, I get errors like the following:

File "/var/lang/lib/python3.12/site-packages/botocore/client.py", line 1061, in _make_api_call raise error_class(parsed_response, operation_name) botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the RetrieveAndGenerate operation: failed to parse date field [1.6015104E12] with format [strict_date_optional_time||epoch_millis]: [failed to parse date field [1.6015104E12] with format [strict_date_optional_time||epoch_millis]] My filter is set up as follows (which works well for booleans and string types):

import boto3 def retrieve_and_generate(input_text, chat_filter): knowledge_base_config = { "generationConfiguration": { "promptTemplate": { "textPromptTemplate": PROMPT #your prompt text here } }, "knowledgeBaseId": KB_ID, #your knowledge base ID here "modelArn": GENERATIVE_MODEL_ARN,#your model ARN here "retrievalConfiguration": { "vectorSearchConfiguration": { "filter": chat_filter, "numberOfResults": 20 } } }

retrieve_and_generate_config = {
    "knowledgeBaseConfiguration": knowledge_base_config,
    "type": "KNOWLEDGE_BASE"
}
if session_id:
    response = bar_client.retrieve_and_generate(
        input={"text": input_text},
        retrieveAndGenerateConfiguration=retrieve_and_generate_config,
        sessionId=session_id
    )
else:
    response = bar_client.retrieve_and_generate(
        input={"text": input_text},
        retrieveAndGenerateConfiguration=retrieve_and_generate_config
    )
return response

I'm defining chat_filter in the main lambda call as follows:

def lambda_handler(event, context):
    # input type might vary if using AWS test event
    try:
        body = json.loads(event['body'])  # Parse if it's a string
    except Exception as e:
        body = event.get('body')  # Directly assign if it's a dict
    user_query = body.get('msg') 

    # Construct the filter dynamically based on the input
    filters = []

    # Date Range Filters (just a start date for minimum example)
    pub_date_gte = body.get('publication_date_gte')
    pub_date_gte = datetime.fromisoformat(pub_date_gte.replace('Z', '+00:00'))
    pub_date_gte = pub_date_gte.astimezone(timezone.utc)
    pub_date_gte = int(pub_date_gte.timestamp()*1000)
    pub_date_gte_type = type(pub_date_gte)
    if pub_date_gte:
        filters.append({
            "greaterThanOrEquals": {  # Fixed operator name
                "key": "effective_date",  
                "value":  pub_date_gte
            }
        })

    # Combine filters with AND logic
    # If no filters are added then filter needs to be {} instead of {"andAll": []}.
    # Bedrock will throw a fit if you send an empty list.
    if filters:
        chat_filter = {"andAll": filters}
    else:
        chat_filter = {} #likely need an if/else for the whole statement

    # Retrieve and generate api for chat
        result = retrieve_and_generate(user_query, session_id, chat_filter)
    return result
1 Answer
0

Based on your error message and code, it appears you're encountering issues with date formatting when filtering your Bedrock knowledge base. The error suggests that the epoch milliseconds format you're trying to use (1.6015104E12) isn't being properly parsed.

When working with date filters in Amazon Bedrock knowledge bases, you need to ensure the date format matches what the underlying system expects. For date range queries in Bedrock knowledge bases backed by OpenSearch, you should:

  1. Use string representations of dates rather than numeric timestamps
  2. Format the dates in a standard ISO format

Here's how you should modify your filter code:

# Date Range Filters
pub_date_gte = body.get('publication_date_gte')
if pub_date_gte:
# Convert to datetime object if it's a string
if isinstance(pub_date_gte, str):
pub_date_gte = datetime.fromisoformat(pub_date_gte.replace('Z', '+00:00'))

# Format as ISO string in YYYY-MM-DD format
formatted_date = pub_date_gte.strftime('%Y-%m-%d')

filters.append({
"greaterThanOrEquals": {
"key": "effective_date",  
"value": formatted_date
}
})

The key points are:

  1. Convert your date to a proper ISO format string (YYYY-MM-DD)
  2. Pass this string value directly in the filter
  3. Make sure the field name ("effective_date" in your example) matches exactly what's in your OpenSearch index

For the Bedrock RetrievalFilter API, date values should be passed as properly formatted strings rather than epoch milliseconds or numeric values. This aligns with how you're successfully querying OpenSearch directly with the format "yyyy-MM-dd".

If you continue to experience issues, verify that the field in your OpenSearch index is properly mapped as a date field and that the format you're using matches the expected format in your index mapping.
Sources
Query a knowledge base connected to an Amazon Neptune Analytics graph - Amazon Bedrock

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.