本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
运行 Amazon Bedrock Flows 代码示例
以下代码示例假定您已满足以下先决条件:
-
设置一个拥有 Amazon Bedrock 操作权限的角色。如果您尚未设置,请参阅 Amazon Bedrock 入门。
-
设置您的凭证以使用 AWS API。如果您尚未设置,请参阅 API 入门。
-
创建服务角色以代表您执行与流程相关的操作。如果您尚未设置,请参阅 在 Amazon Bedrock 中为亚马逊 Bedrock Flows 创建服务角色。
要试用 Amazon Bedrock Flows 的一些代码示例,请选择您首选方法的选项卡,然后按照以下步骤操作:
- Python
-
-
使用带有以下节点的 Amazon Bedrock 代理构建时终端节点的CreateFlow请求创建流程:
-
一个输入节点。
-
一个提示节点,具有一个内联定义的提示,该提示使用两个变量(
genre
和number
)创建了一个音乐播放列表。 -
一个输出节点,用于返回模型完成情况。
运行以下代码片段来加载 AWS SDK for Python (Boto3),创建 Amazon Bedrock Agents 客户端,然后使用节点创建流程(将该
executionRoleArn
字段替换为您为流程创建的服务角色的 ARN):# Import Python SDK and create client import boto3 client = boto3.client(service_name='bedrock-agent') # Replace with the service role that you created. For more information, see https://docs.aws.amazon.com/bedrock/latest/userguide/flows-permissions.html FLOWS_SERVICE_ROLE = "arn:aws:iam::123456789012:role/MyFlowsRole" # Define each node # The input node validates that the content of the InvokeFlow request is a JSON object. input_node = { "type": "Input", "name": "FlowInput", "outputs": [ { "name": "document", "type": "Object" } ] } # This prompt node defines an inline prompt that creates a music playlist using two variables. # 1. {{genre}} - The genre of music to create a playlist for # 2. {{number}} - The number of songs to include in the playlist # It validates that the input is a JSON object that minimally contains the fields "genre" and "number", which it will map to the prompt variables. # The output must be named "modelCompletion" and be of the type "String". prompt_node = { "type": "Prompt", "name": "MakePlaylist", "configuration": { "prompt": { "sourceConfiguration": { "inline": { "modelId": "amazon.titan-text-express-v1", "templateType": "TEXT", "inferenceConfiguration": { "text": { "temperature": 0.8 } }, "templateConfiguration": { "text": { "text": "Make me a {{genre}} playlist consisting of the following number of songs: {{number}}." } } } } } }, "inputs": [ { "name": "genre", "type": "String", "expression": "$.data.genre" }, { "name": "number", "type": "Number", "expression": "$.data.number" } ], "outputs": [ { "name": "modelCompletion", "type": "String" } ] } # The output node validates that the output from the last node is a string and returns it as is. The name must be "document". output_node = { "type": "Output", "name": "FlowOutput", "inputs": [ { "name": "document", "type": "String", "expression": "$.data" } ] } # Create connections between the nodes connections = [] # First, create connections between the output of the flow input node and each input of the prompt node for input in prompt_node["inputs"]: connections.append( { "name": "_".join([input_node["name"], prompt_node["name"], input["name"]]), "source": input_node["name"], "target": prompt_node["name"], "type": "Data", "configuration": { "data": { "sourceOutput": input_node["outputs"][0]["name"], "targetInput": input["name"] } } } ) # Then, create a connection between the output of the prompt node and the input of the flow output node connections.append( { "name": "_".join([prompt_node["name"], output_node["name"]]), "source": prompt_node["name"], "target": output_node["name"], "type": "Data", "configuration": { "data": { "sourceOutput": prompt_node["outputs"][0]["name"], "targetInput": output_node["inputs"][0]["name"] } } } ) # Create the flow from the nodes and connections response = client.create_flow( name="FlowCreatePlaylist", description="A flow that creates a playlist given a genre and number of songs to include in the playlist.", executionRoleArn=FLOWS_SERVICE_ROLE, definition={ "nodes": [input_node, prompt_node, output_node], "connections": connections } ) flow_id = response.get("id")
-
-
通过运行以下代码片段向 Amazon Bedrock 代理构建时终端节点ListFlows提出请求,列出您账户中的流程,包括您刚刚创建的流程:
client.list_flows()
-
通过运行以下代码片段向适用于 Amazon Bedrock 的代理构建时终端节点发出GetFlow请求,获取有关您刚刚创建的流程的信息:
client.get_flow(flowIdentifier=flow_id)
-
准备好流程,以便应用工作草稿中的最新更改并准备好进行版本。运行以下代码片段,使用适用于 Amazon Bedrock 的代理构建时终端节点PrepareFlow发出请求:
client.prepare_flow(flowIdentifier=flow_id)
-
对流程的工作草稿进行版本化以创建流程的静态快照,然后通过以下操作检索有关流程的信息:
-
通过运行以下代码片段来创建版本,向适用于 Amazon Bedrock 的代理构建时终端节点CreateFlowVersion发出请求:
response = client.create_flow_version(flowIdentifier=flow_id) flow_version = response.get("version")
-
通过运行以下代码片段向适用于 Amazon Bedrock 的代理构建时终端节点发出ListFlowVersions请求,列出您的流程的所有版本:
client.list_flow_versions(flowIdentifier=flow_id)
-
通过运行以下代码片段向适用于 Amazon Bedrock 的代理构建时终端节点GetFlowVersion发出请求,获取有关版本的信息:
client.get_flow_version(flowIdentifier=flow_id, flowVersion=flow_version)
-
-
创建别名以指向您创建的流程版本,然后通过以下操作检索有关该版本的信息:
-
创建别名并将其指向您刚刚创建的版本,方法是运行以下代码片段,向适用于 Amazon Bedrock 的代理构建时终端节点CreateFlowAlias发出请求:
response = client.create_flow_alias( flowIdentifier="FLOW123456", name="latest", description="Alias pointing to the latest version of the flow.", routingConfiguration=[ { "flowVersion": flow_version } ] ) flow_alias_id = response.get("id")
-
通过运行以下代码片段向适用于 A mazon Bedrock 的代理构建时终端ListFlowAliass节点发出请求,列出流程的所有别名:
client.list_flow_aliases(flowIdentifier=flow_id)
-
通过运行以下代码片段向 Amazon Bedrock 代理构建时终端节点发出GetFlowAlias请求,获取有关您刚刚创建的别名的信息:
client.get_flow_alias(flowIdentifier=flow_id, aliasIdentifier=flow_alias_id)
-
-
运行以下代码片段,创建 Amazon Bedrock 代理运行时客户端并调用工作流。该请求在流程的提示中填写变量,并返回模型的响应,以便使用 Amazon Bedrock 代理运行时终端节点InvokeFlow发出请求:
client_runtime = boto3.client('bedrock-agent-runtime') response = client_runtime.invoke_flow( flowIdentifier=flow_id, flowAliasIdentifier=flow_alias_id, inputs=[ { "content": { "document": { "genre": "pop", "number": 3 } }, "nodeName": "FlowInput", "nodeOutputName": "document" } ] ) result = {} for event in response.get("responseStream"): result.update(event) if result['flowCompletionEvent']['completionReason'] == 'SUCCESS': print("Flow invocation was successful! The output of the flow is as follows:\n") print(result['flowOutputEvent']['content']['document']) else: print("The flow invocation completed because of the following reason:", result['flowCompletionEvent']['completionReason'])
响应应返回包含三首歌曲的流行音乐播放列表。
-
通过以下操作删除您创建的别名、版本和流程:
-
通过运行以下代码片段向适用于 Amazon Bedrock 的代理构建时终端节点DeleteFlowAlias发出请求,删除别名:
client.delete_flow_alias(flowIdentifier=flow_id, aliasIdentifier=flow_alias_id)
-
通过运行以下代码片段向适用于 Amazon Bedrock 的代理构建时终端节点DeleteFlowVersion发出请求,删除该版本:
client.delete_flow_version(flowIdentifier=flow_id, flowVersion=flow_version)
-
通过运行以下代码片段向适用于 Amazon Bedrock 的代理构建时终端节点DeleteFlow发出请求,删除流程:
client.delete_flow(flowIdentifier=flow_id)
-
-