다음 노드가 있는 Amazon Bedrock Agents 빌드 타임 엔드포인트를 사용하여 CreateFlow 요청으로 흐름을 만듭니다.
다음 코드 스니펫을 실행하여 AWS SDK for Python (Boto3)을 로드하고, Amazon Bedrock Agents 클라이언트를 만들고, 노드를 사용하여 흐름을 만듭니다(흐름을 위해 만든 서비스 역할의 ARN으로 executionRoleArn 필드를 바꿉니다).
# 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.nova-lite-v1:0",
"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 Agents Runtime 클라이언트를 만들고 흐름을 간접적으로 호출합니다. 요청은 흐름의 프롬프트에 있는 변수를 채우고 모델의 응답을 반환하여 Amazon Bedrock Agents 런타임 엔드포인트를 사용해 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'])
응답은 3개의 노래가 포함된 팝 음악 재생 목록을 반환해야 합니다.