aws-doc-sdk-examples icon indicating copy to clipboard operation
aws-doc-sdk-examples copied to clipboard

[Enhancement]: add bedrock-agent-runtime example for InvokeFlow command

Open danielgolub opened this issue 1 year ago • 0 comments

Background story

confusing responseStream in JS sdk instead of the invocation result most commands return the actual body of the documented response, but this command returns a responseStream which is not a regular js stream (that you can not do .on('finish') at)

I know that prompt flow is a preview feature, but a code example would be very beneficial for onboarding to it in my opinion.

What does this example accomplish?

invoke a prompt flow

Which AWS service(s)?

BedRock

Which AWS SDKs or tools?

  • [ ] All languages
  • [ ] .NET
  • [ ] C++
  • [ ] Go (v2)
  • [ ] Java
  • [ ] Java (v2)
  • [ ] JavaScript
  • [X] JavaScript (v3)
  • [ ] Kotlin
  • [ ] PHP
  • [ ] Python
  • [ ] Ruby
  • [ ] Rust
  • [ ] Swift
  • [ ] Not applicable

Are there existing code examples to leverage?

no

Do you have any reference code?

const client = new BedrockAgentRuntimeClient();
  const command = new InvokeFlowCommand({
    flowIdentifier: '{ARN}',
    flowAliasIdentifier: '{ARN}',
    inputs: [
      {
        content: {
          document: "{PROMPT}",
        },
        nodeName: 'FlowInputNode',
        nodeOutputName: 'document',
      },
    ],
  });
  try {
    const response = await client.send(command);

    let responseJson;
    for await (const chunkEvent of response.responseStream) {
      const { document } = chunkEvent.flowOutputEvent.content;
      responseJson = JSON.parse(document);
      break;
    }

    console.info('extracted output from BedRock prompt flow', {
      prompt,
      responseJson,
    });

    return responseJson;
  } catch (error) {
    logger.error('error while querying bedrock', error);
  }

danielgolub avatar Aug 13 '24 17:08 danielgolub