aws / aws-sdk

Landing page for the AWS SDKs on GitHub
https://aws.amazon.com/tools/
Other
68 stars 12 forks source link

session-id not working in aws bedrock-agent-runtime retrieve-and-generate #763

Closed leandro-loos closed 1 week ago

leandro-loos commented 1 week ago

No matter what input I give when calling with a --session-id parameter, the following error pops up: An error occurred (ValidationException) when calling the RetrieveAndGenerate operation: Session with Id <WHATEVER> is not valid. Please check and try again.

sample call: aws bedrock-agent-runtime retrieve-and-generate --retrieve-and-generate-configuration type=KNOWLEDGE_BASE,knowledgeBaseConfiguration="{knowledgeBaseId=<EXISTING_ID>,modelArn=arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-sonnet-20240229-v1:0}" --input text="oso test" --session-id="<WHATEVER>"

Tested the same session ids for the bedrock-runtime api with invoke-model and over there the session ids work just fine.

Also, it would be nice if the sessionID would show up on the cloudwatch logs

tim-finnigan commented 1 week ago

Can you share your debug logs (with any sensitive info redacted) by adding --debug to your command? Also here is the CLI documentation for reference: https://awscli.amazonaws.com/v2/documentation/api/latest/reference/bedrock-agent-runtime/retrieve-and-generate.html. Running that command involves a call to the RetrieveAndGenerate API, so we may need to reach out to the Bedrock team if the API is not behaving as expected.

moellr commented 1 week ago

I ran into the same issue and did some research. Unlike invoke-model in the bedrock-runtimeAPI, you cannot define the session id yourself. Instead, the session id will be created by Amazon Bedrock during the first API call as uuidv4-string and provided in the response object. You use that session id as parameter for subsequent calls within the same sessions.

Referring to your example:

Initial invocation: aws bedrock-agent-runtime retrieve-and-generate --retrieve-and-generate-configuration type=KNOWLEDGE_BASE,knowledgeBaseConfiguration="{knowledgeBaseId=<EXISTING_ID>,modelArn=arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-sonnet-20240229-v1:0}" --input text="oso test"

Response:

{
     "citations": ...
     "output" : ...
     "sessionId": "a1b2c3d4-a1b2-a1b2-a1b2-a1b2c3d4e5f6"
}

Second invocation: aws bedrock-agent-runtime retrieve-and-generate --retrieve-and-generate-configuration type=KNOWLEDGE_BASE,knowledgeBaseConfiguration="{knowledgeBaseId=<EXISTING_ID>,modelArn=arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-sonnet-20240229-v1:0}" --input text="oso test follow-up question" --session-id="1b2c3d4-a1b2-a1b2-a1b2-a1b2c3d4e5f6"

leandro-loos commented 1 week ago

--debug

Heres the debug data, not much there:

2024-06-21 09:26:44,008 - MainThread - botocore.auth - DEBUG - CanonicalRequest: POST /retrieveAndGenerate

content-type:application/json host:bedrock-agent-runtime.us-east-1.amazonaws.com x-amz-date:20240621T072644Z x-amz-security-token:

content-type;host;x-amz-date;x-amz-security-token cb8e8bea2324e9d1d6d5ae87eb9d423786c0cfe856444603f40d4689a9f2fd86 2024-06-21 09:26:44,008 - MainThread - botocore.auth - DEBUG - StringToSign: AWS4-HMAC-SHA256 20240621T072644Z 20240621/us-east-1/bedrock/aws4_request 9597558d3920f8224b29e59cd651b41d4e38c840d84ad0124fd84453a2a0c54e 2024-06-21 09:26:44,008 - MainThread - botocore.auth - DEBUG - Signature: 198d57aff31a00bf8db2f37e7ab2ca57dc4fc4c104c9b5ffacc0ea178c032f1c 2024-06-21 09:26:44,008 - MainThread - botocore.endpoint - DEBUG - Sending http request: <AWSPreparedRequest stream_output=False, method=POST, url=https://bedrock-agent-runtime.us-east-1.amazonaws.com/retrieveAndGenerate, headers={'Content-Type': b'application/json', 'User-Agent': b'aws-cli/2.15.44 Python/3.11.8 Darwin/23.5.0 exe/x86_64 prompt/off command/bedrock-agent-runtime.retrieve-and-generate', 'X-Amz-Date': b'20240621T072644Z', 'X-Amz-Security-Token': , SignedHeaders=content-type;host;x-amz-date;x-amz-security-token, Signature=198d57aff31a00bf8db2f37e7ab2ca57dc4fc4c104c9b5ffacc0ea178c032f1c', 'Content-Length': '284'}> 2024-06-21 09:26:44,009 - MainThread - botocore.httpsession - DEBUG - Certificate path: /usr/local/aws-cli/awscli/botocore/cacert.pem 2024-06-21 09:26:44,009 - MainThread - urllib3.connectionpool - DEBUG - Starting new HTTPS connection (1): bedrock-agent-runtime.us-east-1.amazonaws.com:443 2024-06-21 09:26:44,682 - MainThread - urllib3.connectionpool - DEBUG - https://bedrock-agent-runtime.us-east-1.amazonaws.com:443 "POST /retrieveAndGenerate HTTP/1.1" 400 80 2024-06-21 09:26:44,682 - MainThread - botocore.parsers - DEBUG - Response headers: {'Date': 'Fri, 21 Jun 2024 07:26:44 GMT', 'Content-Type': 'application/json', 'Content-Length': '80', 'Connection': 'keep-alive', 'x-amzn-RequestId': '9348e691-4d22-47be-a651-c957cffb562e', 'x-amzn-ErrorType': 'ValidationException:http://internal.amazon.com/coral/com.amazon.bedrock.agent.runtimeservice/'} 2024-06-21 09:26:44,682 - MainThread - botocore.parsers - DEBUG - Response body: b'{"message":"Session with Id TEST1234 is not valid. Please check and try again."}' 2024-06-21 09:26:44,683 - MainThread - botocore.parsers - DEBUG - Response headers: {'Date': 'Fri, 21 Jun 2024 07:26:44 GMT', 'Content-Type': 'application/json', 'Content-Length': '80', 'Connection': 'keep-alive', 'x-amzn-RequestId': '9348e691-4d22-47be-a651-c957cffb562e', 'x-amzn-ErrorType': 'ValidationException:http://internal.amazon.com/coral/com.amazon.bedrock.agent.runtimeservice/'} 2024-06-21 09:26:44,683 - MainThread - botocore.parsers - DEBUG - Response body: b'{"message":"Session with Id TEST1234 is not valid. Please check and try again."}' 2024-06-21 09:26:44,683 - MainThread - botocore.hooks - DEBUG - Event needs-retry.bedrock-agent-runtime.RetrieveAndGenerate: calling handler <bound method RetryHandler.needs_retry of <botocore.retries.standard.RetryHandler object at 0x10d9473d0>> 2024-06-21 09:26:44,683 - MainThread - botocore.retries.standard - DEBUG - Not retrying request. 2024-06-21 09:26:44,683 - MainThread - botocore.hooks - DEBUG - Event after-call.bedrock-agent-runtime.RetrieveAndGenerate: calling handler <bound method RetryQuotaChecker.release_retry_quota of <botocore.retries.standard.RetryQuotaChecker object at 0x10d43ec10>> 2024-06-21 09:26:44,684 - MainThread - awscli.clidriver - DEBUG - Exception caught in main() Traceback (most recent call last): File "awscli/clidriver.py", line 460, in main File "awscli/clidriver.py", line 595, in call File "awscli/clidriver.py", line 798, in call File "awscli/clidriver.py", line 929, in invoke File "awscli/clidriver.py", line 941, in _make_client_call File "awscli/botocore/client.py", line 357, in _api_call File "awscli/botocore/client.py", line 724, in _make_api_call botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the RetrieveAndGenerate operation: Session with Id TEST1234 is not valid. Please check and try again.

An error occurred (ValidationException) when calling the RetrieveAndGenerate operation: Session with Id TEST1234 is not valid. Please check and try again.

leandro-loos commented 1 week ago

I ran into the same issue and did some research. Unlike invoke-model in the bedrock-runtimeAPI, you cannot define the session id yourself. Instead, the session id will be created by Amazon Bedrock during the first API call as uuidv4-string and provided in the response object. You use that session id as parameter for subsequent calls within the same sessions.

Referring to your example:

Initial invocation: aws bedrock-agent-runtime retrieve-and-generate --retrieve-and-generate-configuration type=KNOWLEDGE_BASE,knowledgeBaseConfiguration="{knowledgeBaseId=<EXISTING_ID>,modelArn=arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-sonnet-20240229-v1:0}" --input text="oso test"

Response:

{
     "citations": ...
     "output" : ...
     "sessionId": "a1b2c3d4-a1b2-a1b2-a1b2-a1b2c3d4e5f6"
}

Second invocation: aws bedrock-agent-runtime retrieve-and-generate --retrieve-and-generate-configuration type=KNOWLEDGE_BASE,knowledgeBaseConfiguration="{knowledgeBaseId=<EXISTING_ID>,modelArn=arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-sonnet-20240229-v1:0}" --input text="oso test follow-up question" --session-id="1b2c3d4-a1b2-a1b2-a1b2-a1b2c3d4e5f6"

Yes @moellr ! Seems like this is how it works, thanks for the explanation... Quite likely is a matter of explaining it correctly on the docs @tim-finnigan : https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html

tim-finnigan commented 1 week ago

Thanks for following up and identifying the issue! I do see in the API documentation that it says the following for sessionId:

The unique identifier of the session. Reuse the same value to continue the same session with the knowledge base.

So it does mention that the value needs to be reused. But the wording could probably be improved to clarify the behavior a bit better. I reached out to the Bedrock team for feedback on this (ref: P136727715) and will see what they. In the future you can also use the Provide feedback link at the bottom of API documentation pages to send feedback directly to the appropriate team.

tim-finnigan commented 1 week ago

We heard back from the team and they went ahead and made the update! Thanks again for bringing this to our attention:

The unique identifier of the session. When you first make a RetrieveAndGenerate request, Amazon Bedrock automatically generates this value. You must reuse this value for all subsequent requests in the same conversational session. This value allows Amazon Bedrock to maintain context and knowledge from previous interactions. You can't explicitly set the sessionId yourself.

https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html

github-actions[bot] commented 1 week ago

This issue is now closed.

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.