The streaming responses feature enhances the responses from QnABot by returning real-time stream from Large Language Models (LLMs) to appear in the chat interface. Instead of waiting for the complete response to be generated, the chat users can see the answer being constructed in real-time, providing a more interactive and engaging experience. Currently, this feature leverages Amazon Bedrock ConverseStream and RetrieveAndGenerateStream APIs to establish a real-time connection between the LLM and the QnABot chat interface, ensuring efficient delivery of response as they're generated.
- Real-time streaming of LLM responses through Amazon Bedrock
- Progressive text generation visible in chat interface
- Seamless integration with custom Lambda hooks
- Optional deployment of streaming resources through nested stack with EnableStreaming flag
- Reduced perceived latency for RAG flows
- More natural conversation flow
- Quasi-immediate visibility of response generation
- Enhanced user engagement
- When a user submits a question, the chat client establishes connection to QnABot using websocket endpoint that QnABot creates.
- QnABot connects to the configured LLM through Amazon Bedrock
- As the LLM generates the response, each text chunk is immediately streamed to the chat client.
- The users see the response being built incrementally, similar to human typing. The streaming continues until the complete response is delivered
- The QnABot admin needs to enable streaming option in the cloudformation template using parameter
EnableStreaming
. - When using an external chat client such as Lex Web UI, the admin will need to setup in Lex Web UI the
StreamingWebSocketEndpoint
output from QnABot stack.
- User visits the chat client with streaming enabled
- The chat client establishes a WebSocket connection
- QnABot establishes connection with Websocket
- A bi-directional communication channel is created between the chat client and QnABot
- User sends a question
- Backend (LLMs) begins generating the response
- Each text segment is immediately streamed to the client as it's generated
- The streaming continues until the LLM completes the response
- Fulfillment lambda returns the final complete response
- The streamed content is replaced with the final formatted response
In below example, QnABot on AWS answers a question by streaming response from Bedrock Knowledge Base.
- Uses API Gateway V2 for WebSocket connection to supports bi-directional real-time communication.
- Uses encrypted WebSocket protocol specification
wss://
(WebSocket Secure) - Secure access to WebSocket API controlled with IAM authorization and signed requests
- Default API Gateway V2 quotas apply for configuring and running a WebSocket API.
- Configures ping route to support one-way pings. To prevent Idle Connection Timeout of 10 min while user session is active, the chat client will send ping every 9 mins until 2 hours limit is reached.
- Implements logging for API Gateway V2 and Streaming Lambda, which are accessible in Amazon CloudWatch.
- Uses encrypted WebSocket protocol specification
- Uses ConverseStream API for streaming from Bedrock LLM
- Uses RetrieveAndGenerateStream API for streaming from Bedrock Knowledge Base
To turn on streaming support for QnABot:
-
Set the
EnableStreaming
cloudformation parameter toTRUE
and deploy the solution. This will create a nested which will deploy the following resources:- Amazon API Gateway V2
- Amazon DynamoDB Table
- AWS Lambda
-
Once stack update is complete, go to Stack >
Outputs
and copy the value forStreamingWebSocketEndpoint
output.
To turn on streaming support for Lex Web UI:
- Set the
AllowStreamingResponses
cloudformation parameter totrue
and deploy the solution. - Copy the
StreamingWebSocketEndpoint
value from the QnABot stackOutputs
and enter it as theStreamingWebSocketEndpoint
parameter when deploying the AWS Lex Web UI chat client CloudFormation template, as shown in the screenshot below.