How do I use the RetrieveAndGenerate API to reuse a knowledge base session in Amazon Bedrock?

2 minute read
2

I want to know how to use my knowledge base session more than once in Amazon Bedrock.

Resolution

Note: A knowledge base session lasts for only 24 hours, and you can't extend the session. 

To reuse a knowledge base session within the 24 hours, get a session ID for the knowledge base session. Then, specify the session ID in the next API call input.

Complete the following steps:

  1. Use the request syntax to call the RetrieveAndGenerate API that assigns the session ID to the current knowledge base session.

  2. Get the sessionId from the API call's response syntax.
    Example output:

    {  
       "citations": [   
          {   
             "generatedResponsePart": {   
                "textResponsePart": {   
                   "span": {   
                      "end": number,  
                      "start": number  
                   },  
                   "text": "string"  
                }  
             },  
             "retrievedReferences": [   
                {   
                   "content": {   
                      "text": "string"  
                   },  
                   "location": {   
                      "s3Location": {   
                         "uri": "string"  
                      },  
                      "type": "string"  
                   },  
                   "metadata": {   
                      "string" : JSON value   
                   }  
                }  
             ]  
          }  
       ],  
       "output": {   
          "text": "string"  
       },  
       "sessionId": "string"  
    }
  3. Specify the session ID in the next API call input within 24 hours to continue to use the same session.
    Note: The knowledge base remembers your previous conversation and provides the answers.

If you don't want to reuse a knowledge base session, then remove the session ID from the API call input to automatically create a new session.

AWS OFFICIAL
AWS OFFICIALUpdated 5 months ago
4 Comments

What does KB session reuse mean exactly? Does it mean the KB caches all retrieved chunks for 24 hours? And if my next question is relevant and comes within 24 hours then I get the chunks from the KB cache? Please clarify. Thanks!

replied 4 months ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied 4 months ago

Hello there,

This part of the solution is confuse:

Note: A knowledge base session lasts for only 24 hours, and you can't extend the session.

To reuse a knowledge base session after the 24 hours, get a session ID for the knowledge base session. Then, specify the session ID in the next API call input.

So you're saying that the session lasts 24 hours and can't be extended but then you said that we just need to send the session ID to use the same session AFTER 24h. I can only use the same session within 24h then? I that is the case, there is a way we can use the conversation as a knowledge base?

replied a month ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied a month ago