

# Build a flow app with Amazon Bedrock
Build a flow app

A flow app let you link prompts, supported foundational models, and other units of work, such as an Amazon Bedrock knowledge base, together and create generative AI workflows for end-to-end solutions. For example, you could create flow apps to do the following. 
+ **Create a playlist of music** – Create a flow connecting a prompt node and knowledge base node. Provide the following prompt to generate a playlist: **Create a playlist**. After processing the prompt, the flow queries a knowledge base to look up information about local bands, such as the length of songs and genre of music. The flow then generates a playlist based the information in the knowledge base.
+ **Troubleshoot using the error message and the ID of the resource that is causing the error** – The flow looks up the possible causes of the error from a documentation knowledge base, pulls system logs and other relevant information about the resource, and updates the faulty configurations and values for the resource.

In this section you create a flow app that generates a playlist of music from a knowledge base of songs by fictional local bands. 

To create a flow app, you use the *flow builder* which is a tool in Amazon Bedrock in SageMaker Unified Studio to build and edit flow apps through a visual interface. You use the visual interface to drag and drop nodes onto the interface and configure inputs and outputs for these nodes to define your flow. 

![\[Example Amazon Bedrock in SageMaker Unified Studio flow.\]](http://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/images/bedrock/create-flow-kb-prompt-out.png)


In your flow you can apply logical conditions to direct the output from a node to different destinations. You can then run the flow within Amazon Bedrock in SageMaker Unified Studio and view the output.

The following list introduces you to the basic elements of a flow.
+ **Flow** – A flow is a construct consisting of a name, description, permissions, a collection of nodes, and connections between nodes. When you run a flow, the input to the flow is sent through each node of the flow until the flow emits the final output from an output node.

  
+ **Node** – A node is a step inside a flow. For each node, you configure its name, description, input, output, and any additional configurations. The configuration of a node differs based on its type. 

  For information about the types of nodes that Amazon Bedrock in SageMaker Unified Studio supports, see [Flow nodes available in Amazon Bedrock](nodes.md).
+ **Connection** – There are two types of connections used in flow apps:
  + A **data connection** is drawn between the output of one node (the *source node*) and the input of another node (the *target node*) and sends data from an upstream node to a downstream node. In the flow builder, data connections are solid lines.
  + A **conditional connection** is drawn between a condition in a condition node and a downstream node and sends data from the node that precedes the condition node to a downstream node if the condition is fulfilled. In the flow builder, conditional connections are dotted lines.
+ **Expressions** – An expression defines how to extract an input from the whole input entering a node. To learn how to write expressions, see [Define inputs with expressions](flows-expressions.md).

If you want to use your flow app outside of Amazon SageMaker Unified Studio, you can export and deploy the app to an AWS account. For more information, see [Use your app outside of Amazon SageMaker Unified Studio](app-export.md).

**Warning**  
Generative AI may give inaccurate responses. Avoid sharing sensitive information. Chats may be visible to others in your organization.

**Topics**
+ [

# Create a flow app with Amazon Bedrock
](build-flow.md)
+ [

# Define inputs with expressions
](flows-expressions.md)
+ [

# Use logic nodes to control flow
](flows-logic-nodes.md)
+ [

# Use a chat agent app in a flow app
](flows-use-chat-agent.md)
+ [

# Flow nodes available in Amazon Bedrock
](nodes.md)

# Create a flow app with Amazon Bedrock


In this section you first build a flow app that generates a playlist of music from an Amazon Bedrock knowledge base of songs by fictional local bands. Next, you use [Reusable prompts](creating-a-prompt.md) to add a prompt that can customizes the playlist for different genres of music. 

**Topics**
+ [

## Step 1: Create an initial flow app
](#build-flow-empty)
+ [

## Step 2: Add a Knowledge Base to your flow app
](#build-flow-kb)
+ [

## Step 3: Add a prompt to your flow app
](#build-flow-prompt)
+ [

## Step 4: Add a condition to your flow app
](#build-flow-condition)

## Step 1: Create an initial flow app


In this procedure you create an initial flow app which has an [Flow input](nodes.md#flow-node-input) node and a [Flow output](nodes.md#flow-node-output) node. 

A flow contains only one flow input node which is where the flow begins. The flow input node takes your input and passes it to the next node in a data type of your choice (String, Number, Boolean, Object and Array). In these procedures, the input to the flow is a String. To learn more about using different data types in a flow, see [Define inputs with expressions](flows-expressions.md). 

A flow output node extracts the input data from the previous node, based on the defined expression, and outputs the data. A flow can have multiple flow output nodes if there are multiple branches in the flow.

After completing the procedure, the flow app is empty, other than the flow input and flow output nodes. In the next step you add Knowledge Base as a data source and run the flow app for the first time.

While you develop your app, you work on the current draft. You can save the current draft to the app history. Later you might want to restart work from a previous draft. For more information, see [Use app history to view and restore versions of an Amazon Bedrock app](app-history.md).

**To create an initial flow app**

1. Navigate to the Amazon SageMaker Unified Studio landing page by using the URL from your administrator.

1. Access Amazon SageMaker Unified Studio using your IAM or single sign-on (SSO) credentials. For more information, see [Access Amazon SageMaker Unified Studio](getting-started-access-the-portal.md).

1. On the Amazon SageMaker Unified Studio home page, navigate to the **Amazon Bedrock in SageMaker Unified Studio** tile.

   For the **Build chat agent app** button dropdown, select **Build flow**.  
![\[Create Amazon Bedrock in SageMaker Unified Studio flow.\]](http://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/images/bedrock/bedrock-ide-build-create-flow.png)

1. In the **Select or create a new project to continue** dialog box, do one of the following:
   + If you want to use a new project, follow the instructions at [Create a new project](create-new-project.md). For the **Project profile** in step 1, choose **Generative AI application development**.
   + If you want to use an existing project, select the project that you want to use and then choose **Continue**. 

1. In the flow builder, choose the flow name (**Untitled flow-nnnn**) and enter **Local bands** as the name for the flow. 

1. In the **flow app builder** pane, select the **Nodes** tab. The center pane displays a **Flow input** node and a **Flow output** node. These are the input and output nodes for your flow. The circles on the nodes are connection points. In the next procedure, you use the connection points to connect a Knowledge Base node to the Flow input node and the Flow output node.   
![\[Input and output nodes in an empty Amazon Bedrock in SageMaker Unified Studio flow app.\]](http://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/images/bedrock/bedrock-ide-empty-flow.png)

1. Next step: [Step 2: Add a Knowledge Base to your flow app](#build-flow-kb).

## Step 2: Add a Knowledge Base to your flow app
Add a Knowledge Base

In this procedure, you add a [Knowlege Base](nodes.md#flow-node-kb) node as a data source to the flow that you created in [Step 1: Create an initial flow app](#build-flow-empty). The Knowledge Base you add is Comma Seperated Values (CSV) file containing a list of ficticious songs and artists. The list includes the duration (seconds) and genre of each song. For more information about Knowledge Bases, see [Add a Knowledge Base to your Amazon Bedrock app](data-sources.md).

During the procedure, you make connections from the Flow input node to the Knowledge Base node and from the Knowledge Base node to the Flow output node. At some point, you might need to delete a node or remove a node connection. To delete a node, select the node that you want to delete and press the Delete button. To remove a connection, choose the connection that you want to delete and then press the delete button. 

When you run the flow with the input **Create a playlist**, the app creates a playlist using songs only from the Knowledge Base. 

**To create the flow with a Knowledge Base**

1. Create a CSV file name *songs.csv* and fill with the following ficticious CSV data. This is the data source for your Knowledge Base. Save the CSV file to your local computer.

   ```
   song,artist,genre,length-seconds
   "Celestial Odyssey","Starry Renegades","Cosmic Rock",240
   "Neon Rapture","Synthwave Siren","Synthwave Pop",300
   "Wordsmith Warriors","Lyrical Legions","Lyrical Flow",180
   "Nebula Shredders","Galactic Axemen","Cosmic Rock",270
   "Electro Euphoria","Neon Nomads","Synthwave Pop",210
   "Rhythm Renegades","Percussive Pioneers","Lyrical Flow",240
   "Stardust Rift","Cosmic Crusaders","Cosmic Rock",180
   "Synthwave Serenade","Electro Enchanters","Synthwave Pop",300
   "Lyrical Legends","Rhyme Royale","Lyrical Flow",240
   "Supernova Shredders","Amplified Ascension","Cosmic Rock",300
   "Celestial Chords","Ethereal Echoes","Cosmic Rock",240
   "Neon Nirvana","Synthwave Sirens","Synthwave Pop",270
   "Verbal Virtuoso","Lyrical Maestros","Lyrical Flow",210
   "Cosmic Collision","Stellar Insurgents","Cosmic Rock",180
   "Pop Paradox","Melodic Mavericks","Synthwave Pop",240
   "Flow Fusion","Verbal Virtuosos","Lyrical Flow",300
   "Shredding Shadows","Crimson Crusaders","Cosmic Rock",270
   "Synth Serenade","Electro Enchanters","Synthwave Pop",180
   "Wordsmith Warlords","Lyrical Legionnaires","Lyrical Flow",240
   "Sonic Supernova","Amplified Ascension","Cosmic Rock",210
   "Celestial Symphony","Ethereal Ensemble","Cosmic Rock",300
   "Electro Euphoria","Neon Nomads","Synthwave Pop",180
   "Lyrical Legends","Rhyme Royale","Lyrical Flow",270
   "Crimson Crescendo","Scarlet Serenaders","Cosmic Rock",240
   "Euphoric Tides","Melodic Mystics","Synthwave Pop",210
   "Rhythm Renegades","Percussive Pioneers","Lyrical Flow",180
   "Cosmic Collision","Stellar Insurgents","Cosmic Rock",300
   "Stardust Serenade","Celestial Crooners","Synthwave Pop",240
   "Wordsmith Warriors","Lyrical Legions","Lyrical Flow",270
   "Sonic Supernova III","Amplified Ascension","Cosmic Rock",180
   ```

1. Open the flow app that you created in [Step 1: Create an initial flow app](#build-flow-empty).

1. Add and configure a Knowledge Base node by doing the following:

   1. In the **flow app builder** pane, select the **Nodes** tab.

   1. From the **Data** section, drag a **Knowledge Base** node onto the flow builder canvas.

   1. The circles on the nodes are connection points. Using your mouse, click on the circle for the **Flow input** node and draw a line to the circle on **Input** section of the Knowledge Base node that you just added. 

   1. Connect the **Output** of the Knowledge Base node in your flow with the **Input** of the **Flow output** node.   
![\[Knowledge Base node in an Amazon Bedrock in SageMaker Unified Studio flow app.\]](http://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/images/bedrock/bedrock-ide-build-prompt-flow-kb.png)

   1. Select the Knowledge Base node that you just added. 

   1. In the **flow builder** pane, choose the **Configure** tab and do the following:

      1. In **Node name** enter **Local\$1bands\$1knowledge\$1base**.

      1. In **Knowledge Base Details**, choose **Create new Knowledge Base** to open the **Create Knowledge Base** pane.

      1. For **Knowledge Base name**, enter **Local-bands**.

      1. For **Knowledge Base description**, enter **Songs by local bands. Includes song, artist, genre, and song length (in seconds).**.

      1. In **Add data sources**, choose **Local file**.

      1. Choose **Click to upload** and upload the CSV file (songs.csv) that you created in step 1. Alternatively, add your source document by dragging and dropping the CSV from your computer.

      1. For **Parsing** leave as **Default parsing**.

      1. For **Embeddings model**, choose a model for converting your data into vector embeddings.

      1. For **Vector store**, choose **OpenSearch Serverless**.

      1. Choose **Create** to create the Knowledge Base. It might take a few minutes to create the Knowledge Base.

   1. Back in the **flow builder** pane, in **Select Knowledge Base**, select the Knowledge Base that you just created (Local-bands).

   1. In **Select response generation model**, select the model that you want the Knowledge Base to generate responses with.

   1. (Optional) In **Select guardrail** select an existing guardrail or create a new guardrail. For more information, see [Safeguard your Amazon Bedrock app with a guardrail](guardrails.md).

1. Choose **Save** to save the app.

1. Test your prompt by doing the following:

   1. On right side of the page, choose **<** to open the test pane.

   1. Enter **Create a playlist** in the prompt **Text** box.

   1. Press Enter on the keyboard or choose the run button to test the prompt.

   1. If necessary, make changes to your flow. If you are satisfied with the flow, choose **Save**. 

1. Next step: [Step 3: Add a prompt to your flow app](#build-flow-prompt).

## Step 3: Add a prompt to your flow app
Add a prompt

In this procedure you add a prompt to the flow by adding a [prompt node](nodes.md#flow-node-prompt). The prompt allows you to easily choose which genre of songs should be included in the playlist that the flow generates. For more information, see [Reuse and share Amazon Bedrock prompts](prompt-mgmt.md).

**To add a prompt to the flow**

1. In the **flow builder** pane, select **Nodes**.

1. From the **Orchestration** section, drag a **Prompt** node onto the flow builder canvas.

1. Select the node you just added. 

1. In the **Configurations** tab of the **flow builder** pane, do the following:

   1. In **Node name** enter **Playlist\$1generator\$1node**.

   1. In **Prompt details** choose **Create new prompt** to open the **Create prompt** pane.

   1. For **Prompt name** enter **Playlist\$1generator\$1prompt**.

   1. For **Model**, choose the model that you want the prompt to use. 

   1. For **Prompt message** enter **Create a playlist of songs in the genre \$1\$1genre\$1\$1.**. 

   1. (Optional) In **Model configs**, make changes to the inference parameters or provide system instruction prompts.

   1. Choose **Save draft and create version** to create the prompt. It might take a couple of minutes to finish creating the prompt.

1. In the flow builder, choose the prompt node that you just added.

1. In the **Configure** tab, do the following in the **Prompt details** section: 

   1. In **Prompt** select the prompt that you just created.

   1. In **Version** select the version (**1**) of the prompt to use.

1. (Optional) In **Select guardrail** select an existing guardrail. For more information, see [Safeguard your Amazon Bedrock app with a guardrail](guardrails.md).

1. Update the flow paths by doing the following:

   1. Delete the output from the **Knowledge Base** node that goes into the **Flow output**.

   1. Connect the output from the **Knowledge Base** node to the input of the **Prompts** node. 

   1. Connect the output from the **Prompts** node to the input of the **Flow output** node. 

1. Choose **Save** to save the flow. The flow should look similar to the following.  
![\[Knowledge Base and prompt node in an Amazon Bedrock in SageMaker Unified Studio flow app.\]](http://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/images/bedrock/create-flow-kb-prompt-out.png)

1. Test your prompt by doing the following:

   1. On right side of the app flow page, choose **<** to open the test pane.

   1. For the **Text** box, enter **Cosmic Rock**. 

   1. Press Enter on the keyboard or choose the run button to test the prompt. The response should be a playlist of songs in the Cosmic Rock genre.

   1. Change the prompt to **Synthwave Pop** and run the prompt again. The songs should now be from the Synthwave Pop genre.

   1. If necessary, make changes to your flow. If you are satisfied with the flow, choose **Save**. 

1. Next step: [Step 4: Add a condition to your flow app](#build-flow-condition).

## Step 4: Add a condition to your flow app
Add a condition

In this procedure, you add a [condition](nodes.md#flow-node-condition) node to the flow so that if you enter the prompt **Cosmic Rock**, the flow only generates a playlist from the local bands Knowledge Base. If you enter a different genre, the flows uses the playlist generator prompt to create a playlist of well known artists in that genre.

**To add a condition to the flow**

1. In the **flow app builder** pane, choose **Nodes**.

1. From the **Logic** section, drag a **Condition** node onto the flow builder canvas.

1. Select the **Condition** node that you just added. 

1. Add the flow that generates a playlist from local bands by doing the following:

   1. In the **Inputs** section of the **Configurations** tab, change the **Node Name** to **Local\$1cosmic\$1rock\$1node**. 

   1. In the **Inputs** section, change the **Name** to **genre**. 

   1. In the **Conditions** section, do the following:

      1. For **Name**, enter **Local\$1cosmic\$1rock**.

      1. For **Condition**, enter the condition **genre == "Cosmic Rock"**.

   1. In the flow builder, choose the condition node that you just added.

   1. Connect **Go to node** to the **Knowledge base** node.

   1. Connect the **Output** of the **Flow input** node to the **Input** of the **Condition** node. Leave the existing connection to the **Knowledge Base** node as this ensures the prompt is passed to the Knowledge Base.  
![\[Condition node in an Amazon Bedrock in SageMaker Unified Studio flow app.\]](http://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/images/bedrock/bedrock-ide-build-prompt-local-condition.png)

1. Choose **Save** to save your flow app.

1. Add the flow that generates a playlist by well known bands by doing the following:

   1. In the **flow app builder** pane, select **Nodes**.

   1. From the **Orchestration** section, drag a **Prompt** node onto the flow builder canvas.

   1. Select the node you just added. 

   1. Choose the **Configurations** tab of the **flow builder** pane and do the following:

      1. For **Node name**, enter **Well\$1known\$1artist\$1playlist\$1generator\$1node**.

      1. In **Prompt details** section, choose the **Playlist\$1generator\$1prompt** prompt that you previously created.

      1. For **Version**, select the version (**1**) of the prompt to use.

      1. Connect the **Output** from the **Flow input** node to the **Input** of the prompt that you just created.

      1. In the **Condition** node, connect the **If all conditions are false** go to node to the new prompt. 

      1. In the **flow app builder** pane, select **Nodes**.

      1. From the **Other** section, drag a **Flow output** node onto the flow builder canvas.

      1. Connect the **Output** of the new **Prompt** (Well\$1known\$1artist\$1playlist\$1generator\$1node) to the input of the new **Flow output** node.

1. Choose **Save** to save the flow. The flow should look similar to the following.  
![\[Knowledge Base, prompt, and condition node in an Amazon Bedrock in SageMaker Unified Studio flow app.\]](http://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/images/bedrock/create-flow-kb-prompt-condition-out.png)

1. Test your prompt by doing the following:

   1. On right side of the app flow page, choose **<** to open the test pane.

   1. In **Enter prompt**, enter **Cosmic Rock**.

   1. Press Enter on the keyboard or choose the run button to test the prompt. The response should be a playlist of songs in the Cosmic Rock genre with bands that are only from the Knoweledge Base.

   1. Change the prompt to **Classic Rock** and run the prompt again. The songs should now be well known bands from the classic rock genre.

# Define inputs with expressions


When you configure the inputs for a node, you must define it in relation to the whole input that will enter the node. The whole input can be a string, number, boolean, array, or object. To define an input in relation to the whole input, you use a subset of supported expressions based off [JsonPath](https://github.com/json-path/JsonPath). Every expression must begin with `$.data`, which refers to the whole input. Note the following for using expressions:
+ If the whole input is a string, number, or boolean, the only expression that you can use to define an individual input is `$.data`
+ If the whole input is an array or object, you can extract a part of it to define an individual input.

As an example to understand how to use expressions, let's say that the whole input is the following JSON object:

```
{
    "animals": {
        "mammals": ["cat", "dog"],
        "reptiles": ["snake", "turtle", "iguana"]
    },
    "organisms": {
        "mammals": ["rabbit", "horse", "mouse"],
        "flowers": ["lily", "daisy"]
    },
    "numbers": [1, 2, 3, 5, 8]
}
```

You can use the following expressions to extract a part of the input (the examples refer to what would be returned from the preceding JSON object):


****  

| Expression | Meaning | Example | Example result | 
| --- | --- | --- | --- | 
| \$1.data | The entire input. | \$1.data | The entire object | 
| .name | The value for a field called name in a JSON object. | \$1.data.numbers | [1, 2, 3, 5, 8] | 
| [int] | The member at the index specified by int in an array. | \$1.data.animals.reptiles[2] | turtle | 
| [int1, int2, ...] | The members at the indices specified by each int in an array. | \$1.data.numbers[0, 3] | [1, 5] | 
| [int1:int2] | An array consisting of the items at the indices between int1 (inclusive) and int2 (exclusive) in an array. Omitting int1 or int2 is equivalent to the marking the beginning or end of the array. | \$1.data.organisms.mammals[1:] | ["horse", "mouse"] | 
| \$1 | A wildcard that can be used in place of a name or int. If there are multiple results, the results are returned in an array. | \$1.data.\$1.mammals | [["cat", "dog"], ["rabbit", "horse", "mouse"]] | 

The following procedure shows how to use expressions to identify fields in a JSON object that you send to a prompt node. The prompt generates a playlist of songs. The JSON object you pass to the flow identifies the number of songs that you want in the playlist and the genre of music that you want the songs to represent. For example, enter the following JSON object to request a playlist of 3 songs in the pop genre.

**\$1 "genre": "Pop", "number": 3 \$1**

**To use an expression**

1. Create an empty flow app by doing [Step 1: Create an initial flow app](build-flow.md#build-flow-empty).

1. In the flow builder, choose the **Flow input** node.

1. In the **flow builder** pane choose the **Configure** tab.

1. In **Outputs** section, choose **Type** and then select **Object**.

1. In the **flow builder** pane, select **Nodes**.

1. From the **Orchestration** section, drag a **Prompt** node onto the flow builder canvas.

1. Select the node you just added. 

1. In the **Configurations** tab of the **flow builder** pane, do the following:

   1. For **Node name**, enter **playlist\$1songs\$1genre\$1node**.

   1. In **Prompt details** choose **Create new prompt** to open the **Create prompt** pane.

   1. For **Prompt name**, enter **playlist\$1songs\$1genre\$1prompt**.

   1. For **Model**, choose the model that you want the prompt to use. 

   1. For **Prompt message** enter **Create a playlist of \$1\$1number\$1\$1 songs that are in the \$1\$1genre\$1\$1 genre of music.**. 

   1. (Optional) In **Model configs**, make changes to the inference parameters.

   1. Choose **Save draft and create version** to create the prompt. It might take a couple of minutes to finish creating the prompt.

1. In the flow builder, choose the prompt node that you just added.

1. Choose the **Configure** tab and do the following in the **Prompt details** section: 

   1. For **Prompt**, select the prompt that you just created (**playlist\$1songs\$1genre\$1prompt**).

   1. For **Version**, select the version (**1**) of the prompt to use.

   1. For the **number** input in the **Inputs** section, do the following:

      1. Change the value of **Type** to **Number**.

      1. Change the value of **Expression** to **\$1.data.number**.

   1. For the **genre** input in the **Inputs** section, do the following:

      1. Make sure the value of **Type** is **String**. 

      1. Change the expression for the input to **\$1.data.genre**.  
![\[Input expressions for a JSON object passed to a prompt node in an Amazon Bedrock in SageMaker Unified Studio flow app.\]](http://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/images/bedrock/create-flow-json-expression-configure.png)

1. Connect the output from **Flow input** node to the input **number** of the Prompt node. 

1. Connect the output from **Flow input** node to the input **genre** of the Prompt node. 

1. Connect the output from the prompt node to the input of the **Flow output** node. 

1. Choose **Save** to save the flow. The flow should look similar to the following.  
![\[JSON input to a prompt node in an Amazon Bedrock in SageMaker Unified Studio flow app.\]](http://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/images/bedrock/create-flow-prompt-expression-json.png)

1. Test your prompt by doing the following:

   1. On the right side of the page, choose **<** to open the **Test** pane.

   1. Enter the following JSON in the **Enter prompt** text box.

      ```
      { 
          "genre": "Pop",
          "number": 3 
      }
      ```

   1. Press Enter on your keyboard or choose the run button to test the prompt. The response should be a playlist of 3 songs in the pop music genre.

# Use logic nodes to control flow


Within an Amazon Bedrock in SageMaker Unified Studio flow app you can use logic flows to control how the flow processes input. 

The [condition](nodes.md#flow-node-condition) node lets you change the flow of processing based on values passed to the node. For example, suppose you have a flow that creates music playlists. You can use a condition node to direct requests for local artists to a sub-flow that uses a knowledge base of local artist information. For national artists, the knowledge base wouldn't be needed and a different flow path can be used. For an example, see [Step 4: Add a condition to your flow app](build-flow.md#build-flow-condition).

 You can also use the [iterator](nodes.md#flow-node-iterator) and [collector](nodes.md#flow-node-collector) nodes to process arrays of information. For example, a radio station might want descriptions and song suggestions for a list of artists. With a flow, you can send a list (Array) of artists to an iterator node which then passes each artist to a prompt. The prompt processes the artists in the array, one at time, to get the required descriptions and song suggestions. The collector node collects the results of the prompt as an array which can then be sent to other nodes.

The following procedure shows how to use iterator and collector nodes to generate descriptions for each artist in a list. The flow also generates a suggested popular, and less popular, song for the artist. When you run the flow, you supply the list of artists as an array, such as the following. 

**["Stereophonics", "Manic Street Preachers"]**

**To get artist descriptions**

1. Create an empty flow app by doing [Step 1: Create an initial flow app](build-flow.md#build-flow-empty). In step 5, name the app **band info**.

1. On the flow canvas, select the **Flow input** node.

1. In the **flow builder** pane choose the **Configure** tab.

1. In **Outputs** section, choose **Type** and then select **Array**.

1. In the **flow app builder** pane, select **Nodes**.

1. From the **Logic** section, drag an **Iterator** node onto the builder canvas.

1. Select the **Iterator** node.

1. In the **Configure** tab of the **flow builder** pane, do the following:

   1. For **Node name**, enter **artist\$1list**.

   1. In the **Output** section, make sure the **Type** for **arrayItem** is **String**.

   1. In the **Output** section, make sure the **Type** for **arraySize** is **Number**.

1. On the canvas, connect **document** from the output of the Flow input node to the **array** input of the **Iterator** node.

1. In the **flow app builder** pane, select **Nodes**.

1. From the **Orchestration** section, drag a **Prompt** node onto the flow builder canvas.

1. Select the **Prompt** node.

1. In the **Configure** tab of the **flow app builder** pane, do the following:

   1. For **Node name**, enter **artist\$1description**.

   1. In **Prompt details** choose **Create new prompt** to open the **Create prompt** pane.

   1. For **Prompt name**, enter **get\$1artist\$1description\$1prompt**.

   1. For **Model**, choose the model that you want the prompt to use. 

   1. For **Prompt message** enter the following:

      ```
      Give a one sentence description about the music played by the artist {{artist}}. Format your response as follows: 
                          
      Artist : the artist name 
      Description :  the artist description
      Popular song : a popular song by the artist
      Deep cut_song :  a less well known song by the artist
      ```

   1. (Optional) In **Model configs**, make changes to the inference parameters.

   1. Choose **Save draft and create version** to create the prompt. It might take a couple of minutes to finish creating the prompt.

1. In the **flow app builder** pane, select **Nodes**.

1. From the **Logic** section, drag a **Collector** node onto the canvas.

1. Select the **Collector node** on the canvas.

1. In the **Configure** tab of the **flow builder** pane, do the following:

   1. For **Node name**, enter **artist\$1descriptions**.

1. Select the **Iterator** node on the canvas, and do the following: 

   1. Connect **arrayItem** to the **artist** input of the Prompt. 

   1. Connect **arraySize** to the **arraySize** input of the Collector node. 

1. In the **Prompt** node, connect **modelCompletion** to the **arrayItem** input of the **Collector** node.

1. In the **Collector** node, connect the **collectedArray** output to the **document** input of the **Flow output** node.

1. Select the **Output** node on the canvas., and do the following: 

1. In the **Configure** tab of the **flow app builder** pane, do the following:

   1. In the **Outputs** section, change the type of **collectedArray** to **Array**.

1. Choose **Save** to save the flow. The flow should look similar to the following.  
![\[Amazon Bedrock in SageMaker Unified Studio flow app with iterator and collector logic nodes.\]](http://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/images/bedrock/bedrock-ide-flow-logic.png)

1. Test your flow by doing the following:

   1. On the right side of the page, choose **<** to open the **Test** pane.

   1. Enter the following JSON in the **Enter prompt** text box.

      ```
      ["Stereophonics", "Manic Street Preachers"]
      ```

   1. Press Enter on your keyboard or choose the run button to test the prompt. The response should be an array of artists with a descriptions and suggested songs for each artist.

# Use a chat agent app in a flow app


You can use a chat agent app in a flow app by adding an [agent node](nodes.md#flow-node-agent) that references the chat agent app.

Before you can use an chat agent app in a flow, you must first [deploy](app-deploy.md) the chat agent app. Deployment creates an alias for the chat agent app and a new version of the chat agent app. In your flow, you add an agent node and configure the agent node to reference the alias of the chat agent app.

The following procedure shows how to integrate a chat agent app as an agent node in a flow app. After completing this procedure you can add other nodes to the flow, as necessary for your solution.

**To use a chat agent app in an agent node**

1. Create a chat agent app by following instructions at [Build a chat agent app with Amazon Bedrock](create-chat-app.md). For this procedure, you only need to do [Step 1: Create the initial chat agent app](create-chat-app-with-components.md#chat-app-create-app), but you can complete the other steps, as desired.

1. Deploy the chat agent app by following the instructions at [Deploy an Amazon Bedrock chat agent app](app-deploy.md).

1. Create an empty flow app by following the instructions at [Step 1: Create an initial flow app](build-flow.md#build-flow-empty). Don't do the subsequent steps on that page.

1. In the **flow app builder** pane, select the **Nodes** tab.

1. From the **Orchestration** section, drag an **Agent** node onto the flow builder canvas.

1. In the flow builder, select the **Agent** node, if it isn't already selected. 

1. In the **flow builder** pane choose the **Configure** tab.

1. For **Node name**, enter a name for the agent node.

1. For **Chat agent**, select the name of the chat agent app that you want to use.

1. For **Agent**, select the alias for the chat agent app that you want to use.

1. Connect the **Input** of the Agent node with the **output** of the **Flow input** node. 

1. Connect the **Output** of the Agent node with the **Input** of the **Flow output** node. 

1. Choose **Save** to save the flow. The flow should look similar to the following image.  
![\[Agent node in an Amazon Bedrock in SageMaker Unified Studio flow app.\]](http://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/images/bedrock/bedrock-ide-flow-agent-node.png)

1. Test your prompt by doing the following:

   1. On the right side of the page, choose **<** to open the **Test** pane.

   1. In the **Enter prompt** text box, enter **Create a playlist of pop music**.

   1. Press Enter on your keyboard or choose the run button to test the prompt. The response should be a playlist music in the pop music genre.

# Flow nodes available in Amazon Bedrock
Flow nodes

Amazon Bedrock in SageMaker Unified Studio provides the following node types to build your flow app. A node comprises of the following:
+ Name – The name for the node.
+ Type – the type of the node. For more information, see [Flow nodes available in Amazon Bedrock](#nodes).
+ Inputs – Provide a name and data type for each input. Some nodes have pre-defined names or types that you must use. In the expression field, define the part of the whole input to use as the individual input. For more information, see [Define inputs with expressions](flows-expressions.md).

   In the flow builder, an input appears as a circle on the left edge of a node. Connect each input to an output of an upstream node.
+ Outputs – Provide a name and data type for each output. Some nodes have pre-defined names or types that you must use. In the flow builder, an output appears as a circle on the right edge of a node. Connect each output to at least one input in a downstream node. If an output from a node is sent to more than one node, or if a condition node is included, the path of a flow will split into multiple branches. Each branch can potentially yield another output in the flow response.
+ Configuration – You define node-specific fields at the top of the node. 

**Note**  
Amazon Bedrock in SageMaker Unified Studio supports a subset of the nodes that are available in Amazon Bedrock. For more information, see [Node types in flow](https://docs.aws.amazon.com/bedrock/latest/userguide/flows-nodes.html). 

**Topics**
+ [

## Input node
](#flow-node-input)
+ [

## Output node
](#flow-node-output)
+ [

## Collector node
](#flow-node-collector)
+ [

## Condition node
](#flow-node-condition)
+ [

## Iterator node
](#flow-node-iterator)
+ [

## Iterator node outputs
](#flow-node-iterator-outputs)
+ [

## Prompt node
](#flow-node-prompt)
+ [

## Knowledge Base node
](#flow-node-kb)
+ [

## Agent node
](#flow-node-agent)
+ [

## S3 storage node
](#flow-node-s3-storage)
+ [

## S3 retrieval node
](#flow-node-s3-retrieval)
+ [

## Adding an Amazon S3 bucket
](#adding-s3-bucket)

## Input node


Every flow contains only one flow input node and must begin with it. When you run the flow, the input is fed into this node and the configured output is passed to the next step.

### Input node inputs



| Name | Type | Expression | 
| --- | --- | --- | 
|  N/A  |  N/A  |  N/A  | 

### Input node outputs



| Name | Type | 
| --- | --- | 
|  document  |  String, Number, Boolean, Object and Array.   | 

## Output node


A flow output node extracts the input data from the previous node, based on the defined expression, and returns it. A flow can have multiple flow output nodes if there are multiple branches in the flow.

### Output node inputs



| Name | Type | Expression | 
| --- | --- | --- | 
|  document  |  String, Number, Boolean, Object, and Array.   | Yes | 

### Input node outputs



| Name | Type | 
| --- | --- | 
|  N/A  |  N/A  | 

## Collector node


A collector node takes an iterated input, in addition to the size that the array will be, and returns them as an array. You can use a collector node downstream from an iterator node to collect the iterated items after sending them through some nodes.

### Collector inputs



| Name | Type | Expression | 
| --- | --- | --- | 
|  arrayItem  |  String \$1 Number \$1 Boolean \$1 Object \$1 Array  |  Yes  | 
|  arraySize  |  Number  |  Yes  | 

### Collector outputs



| Name | Type | 
| --- | --- | 
|  collectedArray  |  Array  | 

## Condition node


A condition node sends data from the previous node to different nodes, depending on the conditions that are defined. A condition node can take multiple inputs.
+ **Node name** – Any
+ **Input field name** – Any
+ **Input field types** – String, Number, Boolean, Object and Array. 
+ **Input expression** – Yes 
+ **Condition field name** – Any
+ **Output field types** – String, Number, Boolean, Object and Array. 
+ **Output expression** – Yes 

### Condition expressions


To define a condition, you refer to an input by its name and compare it to a value using any of the following relational operators:


****  

| Operator | Meaning | Supported data types | Example usage | Example meaning | 
| --- | --- | --- | --- | --- | 
| == | Equal to (the data type must also be equal) | String, Number, Boolean | A == B | If A is equal to B | 
| \$1= | Not equal to | String, Number, Boolean | A \$1= B | If A isn't equal to B | 
| > | Greater than | Number | A > B | If A is greater than B | 
| >= | Greater than or equal to | Number | A >= B | If A is greater than or equal to B | 
| < | Less than | Number | A < B | If A is less than B | 
| <= | Less than or equal to | Number | A <= B | If A is less than or equal to B | 

You can compare inputs to other inputs or to a constant in a conditional expression. For example, if you have a numerical input called `profit` and another one called `expenses`, both **profit > expenses** or **profit <= 1000** are valid expressions.

You can use the following logical operators to combine expressions for more complex conditions. We recommend that you use parentheses to resolve ambiguities in grouping of expressions:


****  

| Operator | Meaning | Example usage | Example meaning | 
| --- | --- | --- | --- | 
| and | Both expressions are true | (A < B) and (C == 1) | If both expressions are true: [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/nodes.html) | 
| or | At least one expression is true | (A \$1= 2) or (B > C) | If either expressions is true: [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/nodes.html) | 
| not | The expression isn't true | not (A > B) | If A isn't greater than B (equivalent to A <= B) | 

## Iterator node


An iterator node takes an array and iteratively returns its items as output to the downstream node. The inputs to the iterator node are processed one by one and not in parallel with each other. The flow output node returns the final result for each input in a different response. You can use also use a collector node downstream from the iterator node to collect the iterated responses and return them as an array, in addition to the size of the array.

### Iterator node inputs



| Name | Type | Expression | 
| --- | --- | --- | 
|  array  |  Array  |  Yes  | 

## Iterator node outputs



| Name | Type | 
| --- | --- | 
|  arrayItem  |  String \$1 Number \$1 Boolean \$1 Object \$1 Array  | 
|  arraySize  |  Number  | 

## Prompt node


A prompt node defines a prompt to use in the flow. The inputs to the prompt node are values to fill in the variables that you define for the prompt. The output is the generated response from the model. For more information, see [Reuse and share Amazon Bedrock prompts](prompt-mgmt.md).
+ **Node name** – Any
+ **Prompt** – The [prompt](prompt-mgmt.md) that the prompt node uses.
+ **Version** – The [prompt](prompt-mgmt.md) the version of the prompt to use.

You can assign a guardrail to a prompt node. When you create the prompt node, you can choose to create a new guardrail or select an existing guardrail. For more information, see [Safeguard your Amazon Bedrock app with a guardrail](guardrails.md).

### Prompt node inputs



| Name | Type | Expression | 
| --- | --- | --- | 
|  Any  |  String, Number, Boolean, Object and Array.  |  Yes  | 

### Prompt node outputs



| Name | Type | 
| --- | --- | 
|  modelCompletion  |  String  | 

## Knowledge Base node


A knowledge base node lets you send a query to a knowledge base and get response that the flow sends to the next node. For more information, see [Use a Local file as a data source](data-source-document.md).
+ **Node name** – Any
+ **Knowledge base** – The [Knowledge Base](data-source-document.md) that the node uses.
+ **Response type** – The model that the node uses to generate a response.

### Knowledge base node inputs



| Name | Type | Expression | 
| --- | --- | --- | 
|  retrievalQuery  |  String  |  Yes  | 

### Knowledge base node outputs



| Name | Type | 
| --- | --- | 
|  outputText  |  String  | 

You can assign a guardrail to a knowledge base node. When you create the knowledge base node, you can choose to create a new guardrail or select an existing guardrail. For more information, see [Safeguard your Amazon Bedrock app with a guardrail](guardrails.md).

## Agent node


An agent node lets you send a prompt to an agent, which orchestrates between foundation models and associated resources to identify and carry out actions for an end-user. The inputs into the node are the prompt for the agent and any associated prompt or session attributes. For more information, see [Control agent session context](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-session-state.html) in the *Amazon Bedrock user guide*.

### Agent node inputs



| Name | Type | Expression | 
| --- | --- | --- | 
|  agentInputText  |  String, Number, Boolean, Object, Array  |  string  | 
|  promptAttributes  |  Object  |  string  | 
|  sessionAttributes  |  Object  |  string  | 

### Agent node outputs



| Name | Type | 
| --- | --- | 
|  agentResponse  |  String  | 

## S3 storage node


An Amazon S3 Storage Node takes an object key and flow data as input. The flow data is stored in an Amazon S3 object specified by the object key, and the node returns the Amazon S3 URI where the data is stored. The Amazon S3 bucket can be specified in the configuration settings of the node.

### S3 storage node inputs



| Name | Type | Expression | 
| --- | --- | --- | 
|  content  |  String, Number, Boolean, Object, Array  |  Yes  | 
|  objectKey  |  String  |  Yes  | 

### S3 storage node outputs



| Name | Type | 
| --- | --- | 
|  s3Uri  |  String  | 

## S3 retrieval node


An Amazon S3 Retrieval Node takes an object key as input and returns the data from the Amazon S3 object specified by the object key. The Amazon S3 bucket can be specified in the configuration settings of the node.

### S3 retrieval node inputs



| Name | Type | Expression | 
| --- | --- | --- | 
|  objectKey  |  String  |  Yes  | 

### S3 retrieval node outputs



| Name | Type | 
| --- | --- | 
|  s3Content  |  String  | 

## Adding an Amazon S3 bucket


To use S3 storage and retrieval flow nodes, you must first set up a connection with an Amazon S3 bucket. Amazon S3 buckets that are already within your project can be connected to automatically through a default S3 connection. If you want to use an S3 bucket from outside of your project, you will need to set up an S3 connection using the steps below.

**To connect to an S3 bucket outside of your project:**

1. Navigate to the data section under your project overview.

1. Select **Add**.

1. Select **Add S3 location**, then select **Next**.

1. Follow the steps for Prerequisite option 2 in [Adding Amazon S3 data](adding-existing-s3-data.md) to create the S3 connection and configure the correct permissions. Alternatively, you can use the following JSON policy to add permissions for an S3 storage node:

   ```
   {
       "Sid": "WriteToS3Bucket",
       "Effect": "Allow",
       "Action": [
           "s3:PutObject"
       ],
       "Resource": [
           "arn:aws:s3:::${bucket-name}",
           "arn:aws:s3:::${bucket-name}/*"
       ],
       "Condition": {
           "StringEquals": {
               "aws:ResourceAccount": "${account-id}"
           }
       }
   }
   ```

   And the following JSON policy to add permissions for an S3 retrieval node:

   ```
   {
       "Sid": "AccessS3Bucket",
       "Effect": "Allow",
       "Action": [
           "s3:GetObject"
       ],
       "Resource": [
           "arn:aws:s3:::${bucket-name}/*"
       ],
       "Condition": {
           "StringEquals": {
               "aws:ResourceAccount": "${account-id}"
           }
       }
   }
   ```