

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 流程的節點類型
<a name="flows-nodes"></a>

Amazon Bedrock 流程提供下列可建置流程的節點類型。設定節點時，請提供下列欄位：
+ 名稱：輸入節點的唯一名稱。
+ 類型：在主控台中，您可以拖放要使用的節點類型。在 API 中，使用 `type` 欄位和 `configuration` 欄位中對應的 [FlowNodeConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowNodeConfiguration.html)。
+ 輸入：為每個輸入提供下列資訊：
  + 名稱：輸入的名稱。有些節點具有您必須使用的預先定義名稱或類型。若要了解哪些節點具有預先定義的名稱，請參閱[邏輯節點類型](#flows-nodes-logic-table)。
  + 表達式：定義要用作個別輸入的整個輸入部分。如需詳細資訊，請參閱[使用表達式在 Amazon Bedrock 流程中擷取整個輸入的相關部分來定義輸入使用表達式定義輸入](flows-expressions.md)。
  + 類型：輸入的資料類型。在執行時期達到此節點時，Amazon Bedrock 會將表達式套用至整個輸入，並驗證結果是否符合資料類型。
+ 輸出：為每個輸出提供下列資訊：
  + 名稱：輸出的名稱。有些節點具有您必須使用的預先定義名稱或類型。若要了解哪些節點具有預先定義的名稱，請參閱[邏輯節點類型](#flows-nodes-logic-table)。
  + 類型：輸出的資料類型。在執行時期達到此節點時，Amazon Bedrock 會驗證節點輸出是否符合資料類型。
+ 組態：您可以在主控台中在節點頂端定義節點特定的欄位。在 API 中，使用適當的 [FlowNodeConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowNodeConfiguration.html) 並填寫其欄位。

每個節點類型如下所述，並在 API 中提供其結構。展開區段以進一步了解該節點類型。

## 用於控制流程邏輯的節點
<a name="flows-nodes-logic"></a>

使用下列節點類型來控制流程的邏輯。

### 流程輸入節點
<a name="flows-nodes-input"></a>

每個流程僅包含一個流程輸入節點，且必須以其作為開頭。流程輸入節點會從 `InvokeFlow` 請求取得 `content`、驗證資料類型，並將其傳送至下列節點。

以下顯示 API 中輸入 [FlowNode](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowNode.html) 物件的一般結構：

```
{
    "name": "string",
    "type": "Input",
    "outputs": [
        {
            "name": "document",
            "type": "String | Number | Boolean | Object | Array",
        }
    ],
    "configuration": {
        "input": CONTEXT-DEPENDENT
    }
}
```

### 流程輸出節點
<a name="flows-nodes-output"></a>

流程輸出節點會根據定義的表達式，從上一個節點擷取輸入資料並將其傳回。在主控台中，輸出是在測試視窗中選擇**執行**後傳回的回應。在 API 中，會在 `InvokeFlow` 回應中 `flowOutputEvent` 的 `content` 欄位中傳回輸出。流程可以有多個流程輸出節點。

如果流程中有多個分支，則流程可以有多個流程輸出節點。

以下顯示輸出 [FlowNode](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowNode.html) 物件的一般結構：

```
{
    "name": "string",
    "type": "Output",
    "inputs": [
        {
            "name": "document",
            "type": "String | Number | Boolean | Object | Array",
            "expression": "string"
        }
    ],
    "configuration": {
        "output": CONTEXT-DEPENDENT
    }
}
```

### 條件節點
<a name="flows-nodes-condition"></a>

條件節點會根據定義的條件，將資料從先前的節點傳送至不同的節點。條件節點可以接受多個輸入。

如需範例，請參閱 [使用條件節點建立流程](flows-ex-condition.md)。

**定義條件節點**

1. 視需要新增任意數量的輸入，以評估您計劃新增的條件。

1. 輸入每個輸入的名稱、指定預期的類型，然後撰寫表達式從整個輸入中擷取相關部分。

1. 將每個輸入連結到上游節點中的相關輸出。

1. 視需要新增任意數量的條件。

1. 對於每個條件：

   1. 輸入條件的名稱。

   1. 使用關聯式和邏輯運算子來定義條件，此條件會將輸入與其他輸入或常數相比較。
**注意**  
系統會依序評估條件。若有多個條件同時滿足，則優先採用較早出現的條件。

   1. 將每個條件連結到您想要在滿足條件時傳送資料的下游節點。

#### 條件表達式
<a name="flows-nodes-condition-expr"></a>

若要定義條件，您可以依其名稱參考輸入，並使用下列任一關聯式運算子將其與值進行比較：


****  

| 運算子 | 意義 | 支援的資料類型 | 範例使用方式 | 範例意義 | 
| --- | --- | --- | --- | --- | 
| == | 等於 (資料類型也必須相等) | 字串、數字、布林值 | A == B | 如果 A 等於 B | 
| \!= | 不等於 | 字串、數字、布林值 | A \!= B | 如果 A 不等於 B | 
| > | Greater than | Number | A > B | 如果 A 大於 B | 
| >= | 大於或等於 | Number | A >= B | 如果 A 大於或等於 B | 
| < | Less than | Number | A < B | 如果 A 小於 B | 
| <= | 小於或等於 | Number | A <= B | 如果 A 小於或等於 B | 

您可以將輸入與其他輸入或條件表達式中的常數相比較。例如，如果您有一個名為 `profit` 的數值輸入，另一個名為 `expenses` 的數值輸入，則 **profit > expenses** 或 **profit <= 1000** 都是有效的表達式。

您可以使用下列邏輯運算子來結合表達式，以因應更複雜的條件。我們建議您使用括號來解析表達式分組中模棱兩可的情況：


****  

| 運算子 | 意義 | 範例使用方式 | 範例意義 | 
| --- | --- | --- | --- | 
| 及 | 兩個表達式都是 true | (A < B) 和 (C == 1) | 如果兩個表達式都是 true：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/flows-nodes.html) | 
| 或 | 至少一個表達式為 true | (A \!= 2) 或 (B > C) | 如果任一表達式為 true：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/flows-nodes.html) | 
| 非 | 表達式不是 true | 不是 (A > B) | 如果 A 不大於 B (相當於 A <= B) | 

在 API 中，當傳送 [CreateFlow](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_CreateFlow.html) 或 [UpdateFlow](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_UpdateFlow.html) 請求時，您可以在 `definition` 欄位中定義下列項目：

1. `nodes` 陣列中的條件 [FlowNode](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowNode.html) 物件。一般格式如下 (請注意，條件節點沒有 `outputs`)：

   ```
   {
       "name": "string",
       "type": "Condition",
       "inputs": [
           {
               "name": "string",
               "type": "String | Number | Boolean | Object | Array",
               "expression": "string"
           }
       ],
       "configuration": {
           "condition": {
               "conditions": [
                   {
                       "name": "string",
                       "expression": "string"
                   },
                   ...
               ]
           }
       }
   }
   ```

1. 對於條件節點中的每個輸入，`connections` 陣列中的 [FlowConnection](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowConnection.html) 物件。在 `FlowConnection` 物件的 `configuration` 欄位中包含 [FlowDataConnectionConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowDataConnectionConfiguration.html) 物件。`FlowConnection` 物件的一般格式如下：

   ```
   {
       "name": "string",
       "source": "string",
       "target": "string",
       "type": "Data",
       "configuration": {
           "data": {
               "sourceOutput": "string",
               "expression": "string"
           }
       }
   }
   ```

1. 對於條件節點中的每個條件 (包括預設條件)，`connections` 陣列中的 [FlowConnection](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowConnection.html) 物件。在 `FlowConnection` 物件的 `configuration` 欄位中包含 [FlowConditionalConnectionConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowConditionalConnectionConfiguration.html) 物件。[FlowConnection](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowConnection.html) 物件的一般格式如下：

   ```
   {
       "name": "string",
       "source": "string",
       "target": "string",
       "type": "Conditional",
       "configuration": {
           "conditional": {
               "condition": "string"
           }
       }
   }
   ```

   使用關聯式和邏輯運算子來定義將此條件 `source` 節點連結到下游 `target` 節點的 `condition`。針對預設條件，請將條件指定為 **default**。

### 迭代器節點
<a name="flows-nodes-iterator"></a>

迭代器節點採用陣列，並反覆將其項目作為輸出傳回下游節點。迭代器節點的輸入會逐一處理，而不是彼此平行處理。流程輸出節點會傳回不同回應中每個輸入的最終結果。除了陣列的大小之外，您也可以使用迭代器節點中下游收集器節點，來收集反覆運算的回應，並將其傳回為陣列。

以下顯示迭代器 [FlowNode](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowNode.html) 物件的一般結構：

```
{
    "name": "string",
    "type": "Iterator",
    "inputs": [
        {
            "name": "array",
            "type": "Array",
            "expression": "string"
        }
    ],
    "outputs": [
        {
            "name": "arrayItem",
            "type": "String | Number | Boolean | Object | Array",
        },
        {
            "name": "arraySize",
            "type": "Number"
        }
    ],
    "configuration": {
        "iterator": CONTEXT-DEPENDENT
    }
}
```

### 收集器節點
<a name="flows-nodes-collector"></a>

除了陣列的大小之外，收集器節點還會採用反覆輸入，並以陣列的形式將其傳回。您可以在迭代器節點的下游使用收集器節點，在將反覆運算的項目傳送至若干節點後進行收集。

以下顯示收集器 [FlowNode](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowNode.html) 物件的一般結構：

```
{
    "name": "string",
    "type": "Collector",
    "inputs": [
        {
            "name": "arrayItem",
            "type": "String | Number | Boolean | Object | Array",
            "expression": "string"
        },
        {
            "name": "arraySize",
            "type": "Number"
        }
    ],
    "outputs": [
        {
            "name": "collectedArray",
            "type": "Array"
        },
    ],
    "configuration": {
        "collector": CONTEXT-DEPENDENT
    }
}
```

### DoWhile 迴圈節點
<a name="flows-nodes-dowhile"></a>

DoWhile 迴圈節點會重複執行一系列節點，而指定的條件仍為 true。迴圈在評估條件之前至少執行一次，因此非常適合需要執行動作的情況，然後檢查是否應該根據結果重複執行。

DoWhile 迴圈節點會取得輸入資料，並將其傳遞至迴圈內文。每次迭代後都會評估條件，以決定是否要繼續迴圈或結束。只要條件評估為 true，或未超過 `maxIterations`，迴圈就會繼續。

以下顯示 DoWhile 迴圈 [FlowNode](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowNode.html) 物件的一般結構：

```
{
    "name": "string",
    "type": "DoWhile",
    "inputs": [
        {
            "name": "loopInput",
            "type": "String | Number | Boolean | Object | Array",
            "expression": "string"
        }
    ],
    "outputs": [
        {
            "name": "loopOutput",
            "type": "String | Number | Boolean | Object | Array"
        },
        {
            "name": "iterationCount",
            "type": "Number"
        }
    ],
    "configuration": {
        "doWhile": {
            "condition": "string",
            "maxIterations": "number"
        }
    }
}
```

在組態中：
+ `condition`：布林表達式，可決定是否要繼續迴圈。使用與條件節點相同的關聯式和邏輯運算子。每次迭代後都會評估條件。
+ `maxIterations`：迭代數量的上限。預設為 10。您必須指定正數。此參數可協助您避免無限迴圈。

**注意**  
`maxIterations` 參數的預設值為 10，且只接受正數。當條件變為 false 或達到迭代次數上限時，迴圈就會結束。

## 用於處理流程中資料的節點
<a name="flows-nodes-data"></a>

使用下列節點類型來處理流程中的資料：

### 提示節點
<a name="flows-nodes-prompt"></a>

提示節點會定義要在流程中使用的提示。您可以使用提示管理中的提示，或在節點中以內嵌的方式定義提示。如需詳細資訊，請參閱[在 Amazon Bedrock 中使用提示管理來建構和存放可重複使用的提示](prompt-management.md)。

如需範例，請參閱 [嘗試範例流程](flows-ex.md)。

提示節點的輸入是填入變數的值。輸出是從模型產生的回應。

以下顯示提示 [FlowNode](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowNode.html) 物件的一般結構：

```
{
    "name": "string",
    "type": "prompt",
    "inputs": [
        {
            "name": "content",
            "type": "String | Number | Boolean | Object | Array",
            "expression": "string"
        },
        ...
    ],
    "outputs": [
        {
            "name": "modelCompletion",
            "type": "String"
        }
    ],
    "configuration": {
        "prompt": {
            "sourceConfiguration": [PromptFlowNodeSourceConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_PromptFlowNodeSourceConfiguration.html) object (see below),
            "guardrailConfiguration": {
                "guardrailIdentifier": "string",
                "guardrailVersion": "string"
            }
        }
    }
}
```

[PromptFlowNodeSourceConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_PromptFlowNodeSourceConfiguration.html) 物件取決於您使用提示管理的提示，還是以內嵌的方式定義提示：
+ 如果您使用提示管理中的提示，則物件應位於下列一般結構中：

  ```
  {
      "resource": {
          "promptArn": "string"
      }
  }
  ```
+ 如果您以內嵌的方式定義提示，請遵循 [使用提示管理建立提示](prompt-management-create.md) 的 API 索引標籤中定義變體的指引 (但請注意，此物件中沒有 `name` 欄位)。您使用的物件應位於下列一般結構中：

  ```
  {
      "inline": {
          "modelId": "string",
          "templateType": "TEXT",
          "templateConfiguration": {
              "text": {
                  "text": "string",
                  "inputVariables": [
                      {
                          "name": "string"
                      },
                      ...
                  ]
              }
          },
          "inferenceConfiguration": {
              "text": {
                  "maxTokens": int,
                  "stopSequences": ["string", ...],
                  "temperature": float,
                  "topP": float
              }
          },
          "additionalModelRequestFields": {
              "key": "value",
              ...
          }
      }
  }
  ```

若要將防護機制從 Amazon Bedrock 防護機制套用至提示或從中產生的回應，請包含 `guardrailConfiguration` 欄位，並在 `guardrailIdentifier` 欄位指定防護機制的 ID 或 ARN，並在 `guardrailVersion` 欄位指定防護機制的版本。

### 代理程式節點
<a name="flows-nodes-agent"></a>

代理程式節點可讓您將提示傳送給代理程式，該代理程式會在 FM 與相關資源之間協調，以識別和執行最終使用者的動作。如需詳細資訊，請參閱[使用 AI 代理程式自動執行應用程式中的任務](agents.md)。

在組態中，指定要使用之代理程式別名的 Amazon Resource Name (ARN)。節點的輸入是代理程式的提示，以及任何相關聯的[提示或工作階段屬性](agents-session-state.md)。節點會將代理程式的回應傳回作為輸出。

代理程式節點可以支援多回合調用，在流程執行期間啟用使用者與代理程式之間的互動對話。當代理程式節點需要其他資訊或說明時，其可以暫停流程執行，並向使用者請求特定輸入。一旦使用者提供請求的資訊，代理程式節點會繼續處理新的輸入。這會持續到代理程式節點擁有完成其執行所需的所有必要資訊為止

以下顯示代理程式 [FlowNode](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowNode.html) 物件的一般結構：

```
{
    "name": "string",
    "type": "Agent",
    "inputs": [
       {
            "name": "agentInputText"
            "type": "String | Number | Boolean | Object | Array",
            "expression": "string"
        },
        {
            "name": "promptAttributes"
            "type": "Object",
            "expression": "string"
        },
        {
            "name": "sessionAttributes"
            "type": "Object",
            "expression": "string"
        }
    ],
    "outputs": [
        {
            "name": "agentResponse",
            "type": "String"
        }
    ],
    "configuration": {
        "agent": {
            "agentAliasArn": "string"
        }
    }
}
```

### 知識庫節點
<a name="flows-nodes-kb"></a>

知識庫節點可讓您從 Amazon Bedrock 知識庫將查詢傳送至知識庫。如需詳細資訊，請參閱[使用 Amazon Bedrock 知識庫擷取資料並產生 AI 回應](knowledge-base.md)。

在組態中至少提供 `knowledgeBaseId`。根據使用案例，您可以選擇包含下列欄位：
+ `modelId`：如果您想要根據擷取的結果產生回應，請包含要使用的[模型 ID](models-supported.md)。若要以陣列傳回擷取的結果，請省略模型 ID。
+ `guardrailConfiguration`：包含 `guardrailIdentifier` 欄位中 Amazon Bedrock 防護機制中定義的防護機制 ID 或 ARN，以及 `guardrailVersion` 欄位中防護機制的版本。
**注意**  
只有在知識庫節點中使用 `RetrieveAndGenerate` 時，才能套用防護機制。

節點的輸入是對知識庫的查詢。輸出可以是模型回應、字串或擷取結果的陣列。

以下顯示知識庫 [FlowNode](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowNode.html) 物件的一般結構：

```
{
    "name": "string",
    "type": "KnowledgeBase",
    "inputs": [
       {
            "name": "retrievalQuery",
            "type": "String",
            "expression": "string"
        }
    ],
    "outputs": [
        {
            "name": "retrievalResults" | "outputText",
            "type": "Array | String"
        }
    ],
    "configuration": {
        "knowledgeBase": {
            "knowledgeBaseId": "string",
            "modelId": "string",
            "guardrailConfiguration": {
                "guardrailIdentifier": "string",
                "guardrailVersion": "string"
            }
        }
    }
}
```

### S3 儲存節點
<a name="flows-nodes-storage"></a>

S3 儲存節點可讓您將資料存放在 Amazon S3 儲存貯體的流程中。您可以在組態中指定要用於資料儲存的 S3 儲存貯體。節點的輸入是要存放的內容和[物件金鑰](https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html)。此節點會傳回 S3 位置的 URI 作為其輸出。

以下顯示 S3 儲存 [FlowNode](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowNode.html) 物件的一般結構：

```
{
    "name": "string",
    "type": "Storage",
    "inputs": [
        {
            "name": "content",
            "type": "String | Number | Boolean | Object | Array",
            "expression": "string"
        },
        {
            "name": "objectKey",
            "type": "String",
            "expression": "string"
        }
    ],
    "outputs": [
        {
            "name": "s3Uri",
            "type": "String"
        }
    ],
    "configuration": {
        "retrieval": {
            "serviceConfiguration": {
                "s3": {
                    "bucketName": "string"
                }
            }
        }
    }
}
```

### S3 擷取節點
<a name="flows-nodes-retrieval"></a>

S3 擷取節點可讓您從 Amazon S3 位置擷取資料，以引入流程。您可以在組態中指定要從中擷取資料的 S3 儲存貯體。節點的輸入是[物件金鑰](https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html)。節點會傳回 S3 位置中的內容作為輸出。

**注意**  
目前，S3 位置中的資料必須是 UTF-8 編碼字串。

以下顯示 S3 擷取 [FlowNode](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowNode.html) 物件的一般結構：

```
{
    "name": "string",
    "type": "Retrieval",
    "inputs": [
        {
            "name": "objectKey",
            "type": "String",
            "expression": "string"
        }
    ],
    "outputs": [
        {
            "name": "s3Content",
            "type": "String"
        }
    ],
    "configuration": {
        "retrieval": {
            "serviceConfiguration": {
                "s3": {
                    "bucketName": "string"
                }
            }
        }
    }
}
```

### Lambda 函式節點
<a name="flows-nodes-lambda"></a>

Lambda 函式節點可讓您呼叫 Lambda 函式，您可以在其中定義程式碼來執行商業邏輯。當您在流程中包含 Lambda 節點時，Amazon Bedrock 會將輸入事件傳送至您指定的 Lambda 函式。

在組態中，指定 Lambda 函式的 Amazon Resource Name (ARN)。定義要在 Lambda 輸入事件中傳送的輸入。您可以根據這些輸入編寫程式碼，並定義函數傳回的內容。會在輸出傳回函數回應。

以下顯示 Lambda 函式 [FlowNode](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowNode.html) 物件的一般結構：

```
{
    "name": "string",
    "type": "LambdaFunction",
    "inputs": [
       {
            "name": "codeHookInput",
            "type": "String | Number | Boolean | Object | Array",
            "expression": "string"
        },
        ...
    ],
    "outputs": [
        {
            "name": "functionResponse",
            "type": "String | Number | Boolean | Object | Array"
        }
    ],
    "configuration": {
        "lambdaFunction": {
            "lambdaArn": "string"
        }
    }
}
```

#### 流程的 Lambda 輸入事件
<a name="flows-nodes-lambda-input"></a>

傳送至 Lambda 節點中 Lambda 函式的輸入事件格式如下：

```
{
   "messageVersion": "1.0",
   "flow": {
        "flowArn": "string",
        "flowAliasArn": "string"
   },
   "node": {
        "name": "string",
        "inputs": [
            {
               "name": "string",
               "type": "String | Number | Boolean | Object | Array",
               "expression": "string",
               "value": ...
            },
            ...
        ]
   }
}
```

每個輸入的欄位，與您在定義 Lambda 節點時指定的欄位相符，而 `value` 欄位的值，會填入在表達式解析後在節點中的完整輸入內容。例如，如果節點的完整輸入是 `[1, 2, 3]` 且表達式是 `$.data[1]`，則在輸入事件中傳送至 Lambda 函式的值會是 `2`。

如需詳細資訊，請參閱《[AWS Lambda 開發人員指南](https://docs.aws.amazon.com/lambda/latest/dg/)》中的 [Lambda 概念](https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-concepts.html#gettingstarted-concepts-event)。

#### 流程的 Lambda 回應
<a name="flows-nodes-lambda-response"></a>

當您撰寫 Lambda 函式時，您可以定義其傳回的回應。此回應會傳回至流程作為 Lambda 節點的輸出。

### 內嵌程式碼節點
<a name="flows-nodes-inline-code"></a>

內嵌程式碼節點可讓您直接在流程中編寫和執行程式碼，不需使用外部 Lambda 函式即可啟用資料轉換、自訂邏輯和整合。當您在流程中包含內嵌程式碼節點時，Amazon Bedrock 會在未與任何人共用且無法存取網際網路的隔離受 AWS 管環境中執行您的程式碼。

**注意**  
內嵌程式碼節點是 Amazon Bedrock 的預覽版本，且可能隨時變更。

在節點組態中，指定要與程式設計語言一起執行的程式碼 (`Python_3` 目前是唯一的選項）。定義您的程式碼可存取的輸入作為變數。程式碼中最後執行的一行的結果將作為節點輸出傳回。

下列範例顯示內嵌程式碼 [FlowNode](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowNode.html) 物件的一般結構：

```
{
    "name": "string",
    "type": "InlineCode",
    "inputs": [{
            "name": "string",
            "type": "String | Number | Boolean | Object | Array",
            "expression": "string"
        },
        ...
    ],
    "outputs": [{
        "name": "response",
        "type": "String | Number | Boolean | Object | Array"
    }],
    "configuration": {
        "inlineCode": {
            "code": "string",
            "language": "Python_3"
        }
    }
}
```

#### 使用內嵌程式碼節點時的考量事項
<a name="flows-nodes-inline-code-usage"></a>

在流程中使用內嵌程式碼節點時，請考量下列事項：

**重要**  
我們建議您先測試程式碼，再將其新增至內嵌程式碼節點。
+ [非同步流程執行](flows-create-async.md)中不支援內嵌程式碼節點。
+ 目前，內嵌程式碼節點支援的唯一程式設計語言是 Python 3.12 (`Python_3)`。
+ 內嵌程式碼會作為互動式 Python 工作階段執行。只會擷取最後執行的一行的結果，並將其作為節點輸出傳回。
+ Python 主控台輸出 (例如來自 `print` 函數的輸出) 則不會擷取。
+ 內嵌程式碼節點的輸入會在您的程式碼中作為 Python 變數提供使用。使用確切名稱來參考節點輸入。
+ 正確設定輸入和輸出類型，以避免執行時期錯誤。您最多可以設定五個節點輸入。
+ 每個流程最多可以有五個內嵌程式碼節點。
+ 每個 AWS 帳戶最多可以有 25 個執行中的內嵌程式碼節點。
+ 您的程式碼不可超過 5 MB。

#### 內嵌程式碼節點輸入
<a name="flows-nodes-inline-code-input"></a>

您為內嵌程式碼節點定義的輸入會在程式碼中作為 Python 變數提供使用。例如，如果您定義名為 `userData` 的輸入，則可以直接在程式碼中將其存取為 `userData`。

會根據您定義的表達式填入每個輸入的值。例如，如果節點的輸入是 `{"name": "John", "age": 30}`，則表達式是 `$.name`，則輸入變數的值會是 `"John"`。

#### 內嵌程式碼節點輸出
<a name="flows-nodes-inline-code-output"></a>

程式碼中最後執行的一行的結果將作為內嵌程式碼節點的輸出傳回。此輸出可供流程中的後續節點使用。

例如，下列程式碼會傳回字典作為節點輸出：

```
# Process input data
result = {"processed": True, "data": userData}

# The last line's result is returned as the node output
result
```

### Lex 節點
<a name="flows-nodes-lex"></a>

**注意**  
Lex 節點依賴 Amazon Lex 服務，該服務可能會存放和使用客戶內容，以開發和持續改善其他服務 AWS 。身為 AWS 客戶，您可以選擇不儲存內容或將其用於改善服務。若要了解如何為 Amazon Lex 實作選擇退出政策，請參閱 [AI 服務選擇退出政策](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_ai-opt-out.html)。

Lex 節點可讓您呼叫 Amazon Lex 機器人，使用自然語言處理來處理表達用語，並根據機器人定義識別意圖。如需詳細資訊，請參閱《[Amazon Lex 開發人員指南](https://docs.aws.amazon.com/lex/latest/dg/)》。

在組態中，指定要使用之機器人別名的 Amazon Resource Name (ARN) 和要使用的地區設定。節點的輸入是表達用語和任何隨附的[請求屬性](https://docs.aws.amazon.com/lexv2/latest/dg/context-mgmt-request-attribs.html)或[工作階段屬性](https://docs.aws.amazon.com/lexv2/latest/dg/context-mgmt-request-attribs.html)。節點會傳回已識別的意圖作為輸出。

**注意**  
目前，Lex 節點不支援多回合對話。單一 Lex 節點只能處理單一表達用語。

以下顯示 Lex [FlowNode](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FlowNode.html) 物件的一般結構：

```
{
    "name": "string",
    "type": "Lex",
    "inputs": [
       {
            "name": "inputText",
            "type": "String | Number | Boolean | Object | Array",
            "expression": "string"
        },
        {
            "name": "requestAttributes",
            "type": "Object",
            "expression": "string"
        },
        {
            "name": "sessionAttributes",
            "type": "Object",
            "expression": "string"
        }
    ],
    "outputs": [
        {
            "name": "predictedIntent",
            "type": "String"
        }
    ],
    "configuration": {
        "lex": {
            "botAliasArn": "string",
            "localeId": "string"
        }
    }
}
```

## 節點類型的摘要資料表
<a name="flows-nodes-summary-table"></a>

下表摘要說明每個節點類型允許的輸入和輸出。注意下列事項：
+ 如果名稱標記為**任何**，則可以提供任何字串作為名稱。否則，您必須使用資料表中指定的值。
+ 如果類型標記為**任何**，您可以指定下列任何資料類型：字串、數字、布林值、物件、陣列。否則，您必須使用資料表中指定的類型。
+ 您可以為**條件**、**提示**、**Lambda 函式**和**內嵌程式碼**節點定義多個輸入。


**邏輯節點類型**  
<a name="flows-nodes-logic-table"></a>
<table>
<thead>
  <tr><th></th><th colspan="3">輸入資訊</th><th colspan="3">輸出資訊</th></tr>
  <tr><th>Node type</th><th>Input</th><th>名稱</th><th>類型</th><th>Output</th><th>名稱</th><th>類型</th></tr>
</thead>
<tbody>
  <tr><td>輸入</td><td>N/A</td><td>N/A</td><td>N/A</td><td>InvokeFlow 請求中的 content 欄位。</td><td>document</td><td>任何</td></tr>
  <tr><td>輸出</td><td>InvokeFlow 回應中要傳回的資料。</td><td>document</td><td>任何</td><td>N/A</td><td>N/A</td><td>N/A</td></tr>
  <tr><td>Condition</td><td>根據條件要傳送的資料。<br />(允許多個輸入)</td><td>任何</td><td>任何</td><td>根據條件要傳送的資料。<br />(指定不同路徑的條件)</td><td>任何</td><td>任何</td></tr>
  <tr><td rowspan="2">迭代器</td><td rowspan="2">您想要將下列節點反覆套用至每個成員的陣列。</td><td rowspan="2">array</td><td rowspan="2">陣列</td><td>陣列中的每個項目</td><td>arrayItem</td><td>任何</td></tr>
  <tr><td>輸入陣列的大小</td><td>arraySize</td><td>Number</td></tr>
  <tr><td rowspan="2">收集器</td><td>您想要合併至陣列的迭代。</td><td>arrayItem</td><td>任何</td><td rowspan="2">陣列，其中附加上一個節點中的所有輸出。</td><td rowspan="2">collectedArray</td><td rowspan="2">陣列</td></tr>
  <tr><td>輸出陣列的大小</td><td>arraySize</td><td>Number</td></tr>
  <tr><td rowspan="2">DoWhile 迴圈</td><td>要在迴圈中處理的資料。</td><td>loopInput</td><td>任何</td><td>所有迭代後來自迴圈的最終輸出。</td><td>loopOutput</td><td>任何</td></tr>
  <tr><td>執行的迭代次數。</td><td>iterationCount</td><td>Number</td><td></td><td></td><td></td></tr>
</tbody>
</table>



**資料處理節點類型**  
<a name="flows-nodes-data-table"></a>
<table>
<thead>
  <tr><th></th><th colspan="3">輸入資訊</th><th colspan="3">輸出資訊</th></tr>
  <tr><th>Node type</th><th>Input</th><th>名稱</th><th>類型</th><th>Output</th><th>名稱</th><th>類型</th></tr>
</thead>
<tbody>
  <tr><td>提示</td><td>在提示中填入變數的值 (允許多個輸入)。</td><td>{{${variable-name}}}</td><td>任何</td><td>模型傳回的回應。</td><td>modelCompletion</td><td>String</td></tr>
  <tr><td rowspan="2">S3 儲存體</td><td>要在 S3 儲存貯體存放的資料。</td><td>content</td><td>任何</td><td rowspan="2">S3 位置的 URI。</td><td rowspan="2">s3Uri</td><td rowspan="2">String</td></tr>
  <tr><td>用於 S3 物件的[物件金鑰](https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html)。</td><td>objectKey</td><td>String</td></tr>
  <tr><td>S3 擷取</td><td>S3 物件的[物件金鑰](https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html)。</td><td>objectKey</td><td>String</td><td>從 S3 儲存貯體擷取的資料。</td><td>s3Content</td><td>任何</td></tr>
  <tr><td rowspan="3">代理程式</td><td>傳送給代理程式的提示。</td><td>agentInputText</td><td>String</td><td rowspan="3">從代理程式傳回的回應。</td><td rowspan="3">agentResponse</td><td rowspan="3">String</td></tr>
  <tr><td>隨提示一起傳送的任何[提示屬性](agents-session-state.md)。</td><td>promptAttributes</td><td>物件</td></tr>
  <tr><td>隨提示一起傳送的任何[工作階段屬性](agents-session-state.md)。</td><td>sessionAttributes</td><td>物件</td></tr>
  <tr><td>知識庫</td><td>要傳送至知識庫的查詢。</td><td>retrievalQuery</td><td>String</td><td>從知識庫傳回的結果或產生的回應。</td><td>retrievalResults</td><td>陣列</td></tr>
  <tr><td> Lambda 函式</td><td>要傳送至函數的資料 (允許多個輸入)。</td><td>任何</td><td>任何</td><td>從函數傳回的回應。</td><td>functionResponse</td><td>任何</td></tr>
  <tr><td>內嵌程式碼節點</td><td>要執行的程式碼變數 (允許多個輸入)。</td><td>任何</td><td>任何</td><td>程式碼中執行的最後一行。</td><td>response</td><td>任何</td></tr>
  <tr><td rowspan="3">Lex</td><td>要傳送給機器人的表達用語。</td><td>inputText</td><td>String</td><td rowspan="3">機器人針對該表達用語預測的意圖。</td><td rowspan="3">predictedIntent</td><td rowspan="3">String</td></tr>
  <tr><td>與表達用語一起傳送的任何[請求屬性](https://docs.aws.amazon.com/lexv2/latest/dg/context-mgmt-request-attribs.html)。</td><td>requestAttributes</td><td>物件</td></tr>
  <tr><td>與表達用語一起傳送的任何[工作階段屬性](https://docs.aws.amazon.com/lexv2/latest/dg/context-mgmt-session-attribs.html)。</td><td>sessionAttributes</td><td>物件</td></tr>
</tbody>
</table>
