user3829743
user3829743

Reputation: 1

AWS Bedrock Agent - Unable to get InvokeAgent to stream response even after setting streamFinalResponse to true

I am using AWS Lambda to call the InvokeAgentCommand function; while it's returning the response fine, but when setting the streamFinalResponse to true, it's not sending the response in multiple chunks.

I know that my streamifyResponse implementation works, as I was able stream traces and actually see them when invoking via function URL

I suspect the flag for streamFinalResponse is not getting picked up for whatever reason. Funny enough I did the Python version and was able to get the agent to stream responses. Unfortunately there is no streamifyResponse equivalent in Python runtime so had to go with Node.

Wondering if anyone has run into similar issue and is able to provide some advice. I included the code below for reference. Thanks

import util from 'util';
import stream from 'stream';
const { Readable, Transform } = stream;
const pipeline = util.promisify(stream.pipeline);   
import {
  BedrockAgentRuntimeClient,
  InvokeAgentCommand,
} from "@aws-sdk/client-bedrock-agent-runtime";

export const handler = awslambda.streamifyResponse(
  
  async (event, responseStream, context) => {

    console.log(`event: ${JSON.stringify(event)}`)
    const client = new BedrockAgentRuntimeClient({ region: "us-east-1" });
    const agentId = "xxxxxxxx";
    const agentAliasId = "yyyyyyyy";  
    const sessionId = event['queryStringParameters']['sessionId'];
    const prompt = event['queryStringParameters']['prompt'];

    const command = new InvokeAgentCommand({

      streamingConfigurations: {
        streamFinalResponse: true
      },
      agentId,
      agentAliasId,
      sessionId,
      inputText: prompt,
      enableTrace: true,
      endSession: false
    });
    
    try {

      const response = await client.send(command);
      console.log(`response: ${JSON.stringify(response)}`)
      
      const processChunk = new Transform({

        objectMode: true,
        transform(chunk, encoding, callback) {

          const chunk_string = JSON.stringify(chunk)
          console.log(`transform chunk ${chunk_string}`);

          if (chunk.chunk) {
            const decodedResponse = new TextDecoder("utf-8").decode(chunk.chunk.bytes);
            console.log(decodedResponse);
            this.push(decodedResponse);       
          } else if (chunk.internalServerException) {
            this.push(JSON.stringify({ error: 'Internal Server Exception' }) + '\n');
          }
          callback();
        }          

      });

      await pipeline(
        response.completion,
        processChunk,
        responseStream
      )
    } catch (err) {
      console.error(err);
    }
  }
);

I originally thought it was a permission issue so I give it AmazonBedrockFullAccess but that didn't make a difference

I have gone through the documentation: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/bedrock-agent-runtime/command/InvokeAgentCommand/ to make sure I didn't miss any configuration that is required for streaming

Upvotes: 0

Views: 23

Answers (0)

Related Questions