Skip to content

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

2 minute read
7

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

Resolution

A knowledge base session lasts 24 hours. You can't extend the session, and you can't reactivate a session after it expires. However, within the 24-hour window, you can provide the same session ID in the next API call input to reuse the session. If you don't include a session ID, then you automatically create a new session.

To reuse the same knowledge base session, call the RetrieveAndGenerate API and note the sessionId from the API call's response.

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"  
}

To assign the specified session ID to the current knowledge base session, call the RetrieveAndGenerate API to pass on the sessionId within 24 hours.

Note: The knowledge base remembers your previous conversation and provides the answers.

Example API call:

{
   "input": { 
      "text": "string"
   },
   "retrieveAndGenerateConfiguration": { 
      "knowledgeBaseConfiguration": { 
         "knowledgeBaseId": "string",
         "modelArn": "string"
      },
      "type": "KNOWLEDGE_BASE"
   },
   "sessionConfiguration": { 
      "kmsKeyArn": "string"
   },
   "sessionId": "string"
}
5 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 a year ago

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

AWS
MODERATOR
replied a year 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 year ago

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

AWS
MODERATOR
replied a year ago

Any update or document related to this will be helpful.

replied 7 months ago