1 Answer
- Newest
- Most votes
- Most comments
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:
- The response comes as a stream in response.responseStream
- You need to process this stream by reading chunks and converting them to text
- Depending on your flow configuration, the response might be JSON or plain text
- Make sure you're using the correct region and flow ID
Let me know if this works.
Relevant content
- asked a year ago
- asked 2 years ago
- asked 2 years ago
- AWS OFFICIALUpdated 2 months ago
