Amazon Bedrock Flows Samples を利用しテンプレートからフローを作成してみた
こんにちは!クラウド事業本部コンサルティング部のたかくに(@takakuni_)です。
みなさん、Amazon Bedrock Prompt Flow 使っていますでしょうか。
GUI でぽちぽち作成できるのが特徴的ですが、いざ 0 から作ろうとすると、少しハードルが高くなりがちです。
何かテンプレート的なものから改良していきたい方もいらっしゃるのではないでしょうか。
なんと最近、 Amazon Bedrock Flows Samples なるものが登場しました。
合わせて、ドキュメントも作成されていました。
Amazon Bedrock Flows Samples
Amazon Bedrock Flows Samples は Amazon Bedrock Prompt Flow のテンプレートを作成する補助ツールです。
次のユースケースのフローをテンプレートとして提供しています。モリモリですね。
- Amazon Bedrock Knowledge bases を利用したフロー
- Amazon Bedrock Agent と複数ターンの会話を想定したフロー
- 条件ノードを利用したフロー
- Amazon Bedrock Guardrails を利用したフロー
- イテレーター/コレクターを利用したフロー
- Amazon Bedrock Agent のマルチエージェント機能を想定したフロー
想定されるフローテンプレートは、以下の README の内容から確認できます。
やってみる
百聞は一見にしかずということで、実際に Amazon Bedrock Flows Samples にトライしてみます。
利用方法は Quick Start に記載があるため、そちらにしたがっていきます。
# Clone repository
git clone https://github.com/aws-samples/amazon-bedrock-flows-samples.git
cd amazon-bedrock-flows-samples
# Set up virtual environment
python -m venv src/venv
source src/venv/bin/activate # Linux/Mac
# or
.\src\venv\Scripts\activate # Windows
# Install requirements
pip install -r requirements.txt
# Run flow manager
python src/bedrock_flow_manager.py \
--test-input "What is Amazon Bedrock?" \
--cleanup
すると、どのパターンを利用するか選択画面に移りました。
(venv) takakuni@ amazon-bedrock-flows-samples % python src/bedrock_flow_manager.py \
--test-input "What is Amazon Bedrock?" \
--cleanup
📋 Configuration:
Region: ap-northeast-1
Profile: takakuni-tf
Templates Directory: ./templates
Test Input:
• What is Amazon Bedrock?
=== Amazon Bedrock Flow Manager ===
Region: ap-northeast-1
Profile: takakuni-tf
Using existing IAM role: BedrockFlowsRole
📂 Available Templates:
--------------------------------------------------
1. rag_kb_flow.json
Description: This is a template for a knowledgebase node. It is used to run the query against a knowledgebase and get the response.
--------------------------------------------------
2. multi_agent_flow.json
Description: This is a template for an Agent Node. Flow demonstrates user input handled by an agent.
--------------------------------------------------
3. multi_turn_agent_flow.json
Description: This is a flow template for a Multi-turn Agent Node. Flow demonstrates a multi-turn conversation flow with an agent.
--------------------------------------------------
4. conditions_flow.json
Description: This is a template for a condition node. It is used to conditionally execute a prompt or a knowledge base.
--------------------------------------------------
5. prompt_guardrail_flow.json
Description: This is a template for a Prompt node with Guardrail. It is used to execute a prompt with a guardrail attached.
--------------------------------------------------
6. iterator_collector_flow.json
Description: This is a template for an Iterator and Collector Node. Template demonstrates a Flow that Iterates over an array of input, sends them to prompt and collects the results for display.
--------------------------------------------------
Select a template number:
Enter number (or 'q' to quit):
今回は 6 番の iterator_collector_flow.json
を選択します。6 番の場合はモデル ID のみ追加の回答が必要なため apac.anthropic.claude-3-5-sonnet-20241022-v2:0
を選択しました。すると、sample-iterator-collector-flow
というフロー名でフローが作成され、終了次第削除されていることがわかります。
Select a template number:
Enter number (or 'q' to quit): 6
📝 Step 2: Processing Template
------------------------------
Loading template: iterator_collector_flow.json
Template Variables Found:
------------------------------
• $$PROMPT_MODEL_ID
🔄 Variable Replacement
Enter values for each variable:
Enter value for $$PROMPT_MODEL_ID: apac.anthropic.claude-3-5-sonnet-20241022-v2:0
✅ Template processing complete
Template Metadata:
• Name: sample-iterator-collector-flow
• Description: This is a template for an Iterator and Collector Node. Template demonstrates a Flow that Iterates over an array of input, sends them to prompt and collects the results for display.
• Tags:
- Environment: dev
- Project: sample-bedrock-iterator-collector-flow
🚀 Step 3: Creating Flow
------------------------------
Creating flow: sample-iterator-collector-flow
Using flow name from template
Processing flow definition...
✅ Flow created successfully!
Flow ID: GR86Y5EPKD
Flow Name: sample-iterator-collector-flow
⚙️ Step 4: Preparing Flow
------------------------------
Preparing flow...
Creating flow version...
Created version: 1
Creating flow alias...
✅ Flow preparation complete!
Flow Details:
• Version: 1
• Alias ID: 7TY1CFQ0O2
🧪 Step 5: Testing Flow
------------------------------
Invoking flow...
✅ Flow execution successful! (7.49s)
📊 Flow Response:
------------------------------
Iterator Response:
[Response 1]
--------------------
╭─────────────────────────────────────────────────────────────────────────────── Text Response 1 ───────────────────────────────────────────────────────────────────────────────╮
│ Amazon Bedrock is a fully managed service offered by AWS (Amazon Web Services) that provides access to high-performing foundation models (FMs) from leading AI companies │
│ through a single API. Here are its key features and characteristics: │
│ │
│ 1. Foundation Models Access: │
│ - Provides access to models from AI companies like Anthropic, AI21 Labs, Stability AI, and Amazon's own models │
│ - Includes models for various tasks like text generation, image generation, and embedding │
│ │
│ 2. Key Benefits: │
│ - No need to manage infrastructure │
│ - Single API integration │
│ - Private and secure access to models │
│ - Ability to customize models with your own data │
│ - Built-in AWS security and compliance features │
│ │
│ 3. Main Use Cases: │
│ - Text generation and summarization │
│ - Code generation │
│ - Image generation │
│ - Question answering │
│ - Content creation │
│ - Data analysis │
│ │
│ 4. Features: │
│ - Model customization through fine-tuning │
│ - Agent capabilities for task automation │
│ - Knowledge bases for context-aware responses │
│ - Integration with other AWS services │
│ - Managed scaling and availability │
│ │
│ 5. Security: │
│ - Data encryption │
│ - Private endpoints │
│ - Access control through IAM │
│ - No data retention by default │
│ - Compliance with AWS security standards │
│ │
│ Amazon Bedrock is designed to make it easier for businesses to incorporate AI capabilities into their applications without having to deal with the complexity of managing AI │
│ infrastructure directly. │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
🧹 Cleaning Up Resources
------------------------------
1. Deleting flow alias...
✅ Alias deleted
2. Deleting flow version...
✅ Version deleted
3. Deleting flow...
✅ Flow deleted
✨ Cleanup completed
✨ Operation completed successfully!
試しに test-input に複数の文章を指定すると、それぞれでフローの実行が行われました。
(venv) takakuni@ amazon-bedrock-flows-samples % python src/bedrock_flow_manager.py \
--test-input "What is Amazon Bedrock?", "When do we use Amazon Bedrock?" \
--cleanup
📋 Configuration:
Region: ap-northeast-1
Profile: takakuni-tf
Templates Directory: ./templates
Test Input:
1. What is Amazon Bedrock?,
2. When do we use Amazon Bedrock?
=== Amazon Bedrock Flow Manager ===
Region: ap-northeast-1
Profile: takakuni-tf
Using existing IAM role: BedrockFlowsRole
Using existing IAM role: BedrockFlowsRole
📂 Available Templates:
--------------------------------------------------
1. rag_kb_flow.json
Description: This is a template for a knowledgebase node. It is used to run the query against a knowledgebase and get the response.
--------------------------------------------------
2. multi_agent_flow.json
Description: This is a template for an Agent Node. Flow demonstrates user input handled by an agent.
--------------------------------------------------
3. multi_turn_agent_flow.json
Description: This is a flow template for a Multi-turn Agent Node. Flow demonstrates a multi-turn conversation flow with an agent.
--------------------------------------------------
4. conditions_flow.json
Description: This is a template for a condition node. It is used to conditionally execute a prompt or a knowledge base.
--------------------------------------------------
5. prompt_guardrail_flow.json
Description: This is a template for a Prompt node with Guardrail. It is used to execute a prompt with a guardrail attached.
--------------------------------------------------
6. iterator_collector_flow.json
Description: This is a template for an Iterator and Collector Node. Template demonstrates a Flow that Iterates over an array of input, sends them to prompt and collects the results for display.
--------------------------------------------------
Select a template number:
Enter number (or 'q' to quit): 6
📝 Step 2: Processing Template
------------------------------
Loading template: iterator_collector_flow.json
Template Variables Found:
------------------------------
• $$PROMPT_MODEL_ID
🔄 Variable Replacement
Enter values for each variable:
Enter value for $$PROMPT_MODEL_ID: apac.anthropic.claude-3-5-sonnet-20241022-v2:0
✅ Template processing complete
Template Metadata:
• Name: sample-iterator-collector-flow
• Description: This is a template for an Iterator and Collector Node. Template demonstrates a Flow that Iterates over an array of input, sends them to prompt and collects the results for display.
• Tags:
- Environment: dev
- Project: sample-bedrock-iterator-collector-flow
🚀 Step 3: Creating Flow
------------------------------
Creating flow: sample-iterator-collector-flow
Using flow name from template
Processing flow definition...
✅ Flow created successfully!
Flow ID: 8JGXK5RUGP
Flow Name: sample-iterator-collector-flow
⚙️ Step 4: Preparing Flow
------------------------------
Preparing flow...
Creating flow version...
Created version: 1
Creating flow alias...
✅ Flow preparation complete!
Flow Details:
• Version: 1
• Alias ID: 2UUNHT8M06
🧪 Step 5: Testing Flow
------------------------------
Invoking flow...
✅ Flow execution successful! (16.40s)
📊 Flow Response:
------------------------------
Iterator Response:
[Response 1]
--------------------
╭─────────────────────────────────────────────────────────────────────────────── Text Response 1 ───────────────────────────────────────────────────────────────────────────────╮
│ Amazon Bedrock is a fully managed service offered by AWS (Amazon Web Services) that provides access to high-performing foundation models (FMs) through an API. Here are the │
│ key aspects of Amazon Bedrock: │
│ │
│ Key Features: │
│ 1. Foundation Models Access │
│ - Offers access to various FM models from leading AI companies like Anthropic, AI21 Labs, Stability AI, and Amazon │
│ - Includes models for text, images, and code generation │
│ │
│ 2. Security & Privacy │
│ - Provides private, secure access to FMs │
│ - Data remains private and is not used to train models │
│ - Supports encryption and VPC endpoints │
│ │
│ 3. Customization │
│ - Allows model fine-tuning for specific use cases │
│ - Supports custom knowledge bases │
│ - Enables model evaluation and testing │
│ │
│ 4. Integration │
│ - Seamlessly integrates with other AWS services │
│ - Supports serverless architecture │
│ - Provides SDK support for various programming languages │
│ │
│ Benefits: │
│ 1. Cost-effective (pay-per-use model) │
│ 2. No infrastructure management required │
│ 3. Enterprise-grade security │
│ 4. Flexibility in model selection │
│ 5. Easy integration with existing applications │
│ │
│ Common Use Cases: │
│ 1. Content generation │
│ 2. Text summarization │
│ 3. Code generation │
│ 4. Image generation │
│ 5. Chatbots and conversational AI │
│ 6. Document analysis │
│ 7. Language translation │
│ │
│ Amazon Bedrock makes it easier for organizations to leverage AI capabilities without the complexity of managing infrastructure or dealing with multiple AI providers │
│ directly. │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
[Response 2]
--------------------
╭─────────────────────────────────────────────────────────────────────────────── Text Response 2 ───────────────────────────────────────────────────────────────────────────────╮
│ Amazon Bedrock is used in several scenarios where you need AI/ML capabilities without managing the underlying infrastructure. Here are the main use cases: │
│ │
│ 1. When you need Foundation Models (FMs): │
│ - Text generation and summarization │
│ - Code generation │
│ - Image generation │
│ - Question-answering systems │
│ - Language translation │
│ - Content creation │
│ │
│ 2. When you want: │
│ - Access to multiple AI models from different providers (Anthropic, AI21 Labs, Stability AI, etc.) │
│ - A single API interface to work with various models │
│ - Pay-as-you-go pricing without long-term commitments │
│ │
│ 3. Enterprise scenarios: │
│ - When you need enterprise-grade security and privacy │
│ - When data sovereignty and compliance are important │
│ - When you want to customize models with your own data │
│ - When you need integration with other AWS services │
│ │
│ 4. Development scenarios: │
│ - Rapid prototyping of AI applications │
│ - Building production-ready AI solutions │
│ - When you want to avoid vendor lock-in with a single model provider │
│ - When you need flexibility to switch between different models │
│ │
│ 5. Cost considerations: │
│ - When you want to avoid the costs of training and maintaining your own models │
│ - When you need scalable AI capabilities without infrastructure management │
│ - When you want to experiment with different models cost-effectively │
│ │
│ 6. Integration needs: │
│ - When you need to integrate AI capabilities into existing AWS applications │
│ - When you want to build AI-powered features into your applications │
│ │
│ Remember: Amazon Bedrock is particularly useful when you want managed AI capabilities with enterprise-grade features and the flexibility to choose from multiple model │
│ providers. │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
🧹 Cleaning Up Resources
------------------------------
1. Deleting flow alias...
✅ Alias deleted
2. Deleting flow version...
✅ Version deleted
3. Deleting flow...
✅ Flow deleted
✨ Cleanup completed
✨ Operation completed successfully!
作成したフローを引き続き使っていきたい場合は、--cleanup
オプションを外します。先ほどと同じく一連の流れがあったのちに、
(venv) takakuni@ amazon-bedrock-flows-samples % python src/bedrock_flow_manager.py \
--test-input "What is Amazon Bedrock?"
📋 Configuration:
Region: ap-northeast-1
Profile: takakuni-tf
Templates Directory: ./templates
Test Input:
• What is Amazon Bedrock?
=== Amazon Bedrock Flow Manager ===
Region: ap-northeast-1
Profile: takakuni-tf
Using existing IAM role: BedrockFlowsRole
Using existing IAM role: BedrockFlowsRole
📂 Available Templates:
--------------------------------------------------
1. rag_kb_flow.json
Description: This is a template for a knowledgebase node. It is used to run the query against a knowledgebase and get the response.
--------------------------------------------------
2. multi_agent_flow.json
Description: This is a template for an Agent Node. Flow demonstrates user input handled by an agent.
--------------------------------------------------
3. multi_turn_agent_flow.json
Description: This is a flow template for a Multi-turn Agent Node. Flow demonstrates a multi-turn conversation flow with an agent.
--------------------------------------------------
4. conditions_flow.json
Description: This is a template for a condition node. It is used to conditionally execute a prompt or a knowledge base.
--------------------------------------------------
5. prompt_guardrail_flow.json
Description: This is a template for a Prompt node with Guardrail. It is used to execute a prompt with a guardrail attached.
--------------------------------------------------
6. iterator_collector_flow.json
Description: This is a template for an Iterator and Collector Node. Template demonstrates a Flow that Iterates over an array of input, sends them to prompt and collects the results for display.
--------------------------------------------------
Select a template number:
Enter number (or 'q' to quit): 6
📝 Step 2: Processing Template
------------------------------
Loading template: iterator_collector_flow.json
Template Variables Found:
------------------------------
• $$PROMPT_MODEL_ID
🔄 Variable Replacement
Enter values for each variable:
Enter value for $$PROMPT_MODEL_ID: apac.anthropic.claude-3-5-sonnet-20241022-v2:0
✅ Template processing complete
Template Metadata:
• Name: sample-iterator-collector-flow
• Description: This is a template for an Iterator and Collector Node. Template demonstrates a Flow that Iterates over an array of input, sends them to prompt and collects the results for display.
• Tags:
- Environment: dev
- Project: sample-bedrock-iterator-collector-flow
🚀 Step 3: Creating Flow
------------------------------
Creating flow: sample-iterator-collector-flow
Using flow name from template
Processing flow definition...
✅ Flow created successfully!
Flow ID: X3IRRIQ5W0
Flow Name: sample-iterator-collector-flow
⚙️ Step 4: Preparing Flow
------------------------------
Preparing flow...
Creating flow version...
Created version: 1
Creating flow alias...
✅ Flow preparation complete!
Flow Details:
• Version: 1
• Alias ID: STIEP4BVF1
🧪 Step 5: Testing Flow
------------------------------
Invoking flow...
✅ Flow execution successful! (7.13s)
📊 Flow Response:
------------------------------
Iterator Response:
[Response 1]
--------------------
╭─────────────────────────────────────────────────────────────────────────────── Text Response 1 ───────────────────────────────────────────────────────────────────────────────╮
│ Amazon Bedrock is a fully managed service offered by AWS that provides access to high-performing foundation models (FMs) from leading AI companies through a single API. Here │
│ are its key features and characteristics: │
│ │
│ 1. Foundation Models Access: │
│ - Offers models from AI companies like Anthropic, AI21 Labs, Stability AI, and Amazon │
│ - Includes models for various tasks like text generation, image generation, and embedding │
│ │
│ 2. Key Benefits: │
│ - No need to manage infrastructure │
│ - Pay-as-you-go pricing │
│ - Private and secure access to models │
│ - Ability to customize models with your own data │
│ │
│ 3. Main Features: │
│ - Model customization through fine-tuning │
│ - Built-in security and compliance │
│ - Integration with AWS services │
│ - API consistency across different models │
│ - Private endpoints for secure access │
│ │
│ 4. Use Cases: │
│ - Text generation and summarization │
│ - Code generation │
│ - Image generation │
│ - Content creation │
│ - Question answering │
│ - Language translation │
│ │
│ 5. Development Tools: │
│ - SDKs for various programming languages │
│ - Integration with AWS development tools │
│ - Support for popular ML frameworks │
│ │
│ Amazon Bedrock makes it easier for developers and organizations to build AI-powered applications without having to manage the underlying infrastructure or deal with multiple │
│ model providers separately. │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✨ Operation completed successfully!
マネジメントコンソールをみてみると、フローが削除されず、残っていますね。
まとめ
以上、「Amazon Bedrock Flows Samples を利用しテンプレートからフローを作成してみた」でした。
テンプレートから改修していく方が得意な方の場合、非常に便利な機能なのではないかと思います。
このブログがどなたかの参考になれば幸いです。クラウド事業本部コンサルティング部のたかくに(@takakuni_)でした!