Skip to content

How to access responseStream from InvokeFlowCommand from Bedrock?

0

Hi! I made a fully working prompt flow in Bedrock in the prompt flow builder console. I want to connect it to and interact with it from my local index.ts. I use the method provided here: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/bedrock-agent-runtime/command/InvokeFlowCommand/ Printing the result show a httpStatus code 200 ok. But how do I access the actual response output from the promt flow? I managed to do it for a bedrock agent instead of a prompt flow, but the response from the prompt flow dont have the same/so many properties. I have not found a successful way to read the stream. Or should i use a totally different approach to connect to it?

1 Answer
0

Hi,

To access the responseStream from InvokeFlowCommand in Amazon Bedrock using TypeScript, you need to properly handle the stream data. Here's how to do it: When working with prompt flows in Bedrock, the response comes as a stream that you need to process differently than with agents. Here's the correct approach:

typescript
import { BedrockAgentRuntimeClient, InvokeFlowCommand } from "@aws-sdk/client-bedrock-agent-runtime";
import { Readable } from "stream";

async function invokeBedrockFlow() {
  const client = new BedrockAgentRuntimeClient({ region: "us-east-1" }); // Use your region
  
  const command = new InvokeFlowCommand({
    flowId: "your-flow-id",
    inputs: [
      {
        inputText: "Your input text here"
      }
    ]
  });
  
  try {
    const response = await client.send(command);
    
    // Check if responseStream exists
    if (response.responseStream instanceof Readable) {
      // Process the stream
      let responseData = '';
      
      // Convert stream to string
      for await (const chunk of response.responseStream) {
        // If chunk is a Uint8Array, convert to string
        if (chunk instanceof Uint8Array) {
          responseData += new TextDecoder().decode(chunk);
        } else {
          responseData += chunk;
        }
      }
      
      // Parse the response if it's JSON
      try {
        const parsedResponse = JSON.parse(responseData);
        console.log("Parsed response:", parsedResponse);
        return parsedResponse;
      } catch (e) {
        // If not JSON, return as is
        console.log("Response data:", responseData);
        return responseData;
      }
    } else {
      console.log("No response stream found in the response:", response);
      return response;
    }
  } catch (error) {
    console.error("Error invoking flow:", error);
    throw error;
  }
}

// Call the function
invokeBedrockFlow().catch(console.error);

Important points about working with Bedrock prompt flow responses:

  1. The response comes as a stream in response.responseStream
  2. You need to process this stream by reading chunks and converting them to text
  3. Depending on your flow configuration, the response might be JSON or plain text
  4. Make sure you're using the correct region and flow ID

Let me know if this works.

AWS
answered a year ago
AWS
EXPERT
reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.