awslabs.cdk-mcp-server 0.0.10417__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- awslabs/__init__.py +2 -0
- awslabs/cdk_mcp_server/__init__.py +8 -0
- awslabs/cdk_mcp_server/core/__init__.py +1 -0
- awslabs/cdk_mcp_server/core/resources.py +271 -0
- awslabs/cdk_mcp_server/core/search_utils.py +182 -0
- awslabs/cdk_mcp_server/core/server.py +74 -0
- awslabs/cdk_mcp_server/core/tools.py +324 -0
- awslabs/cdk_mcp_server/data/__init__.py +1 -0
- awslabs/cdk_mcp_server/data/cdk_nag_parser.py +331 -0
- awslabs/cdk_mcp_server/data/construct_descriptions.py +32 -0
- awslabs/cdk_mcp_server/data/genai_cdk_loader.py +423 -0
- awslabs/cdk_mcp_server/data/lambda_powertools_loader.py +48 -0
- awslabs/cdk_mcp_server/data/schema_generator.py +666 -0
- awslabs/cdk_mcp_server/data/solutions_constructs_parser.py +782 -0
- awslabs/cdk_mcp_server/server.py +7 -0
- awslabs/cdk_mcp_server/static/CDK_GENERAL_GUIDANCE.md +232 -0
- awslabs/cdk_mcp_server/static/CDK_NAG_GUIDANCE.md +192 -0
- awslabs/cdk_mcp_server/static/__init__.py +5 -0
- awslabs/cdk_mcp_server/static/bedrock/agent/actiongroups.md +137 -0
- awslabs/cdk_mcp_server/static/bedrock/agent/alias.md +39 -0
- awslabs/cdk_mcp_server/static/bedrock/agent/collaboration.md +91 -0
- awslabs/cdk_mcp_server/static/bedrock/agent/creation.md +149 -0
- awslabs/cdk_mcp_server/static/bedrock/agent/custom_orchestration.md +74 -0
- awslabs/cdk_mcp_server/static/bedrock/agent/overview.md +78 -0
- awslabs/cdk_mcp_server/static/bedrock/agent/prompt_override.md +70 -0
- awslabs/cdk_mcp_server/static/bedrock/bedrockguardrails.md +188 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/chunking.md +137 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/datasources.md +225 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/kendra.md +81 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/overview.md +116 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/parsing.md +36 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/transformation.md +30 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/vector/aurora.md +185 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/vector/creation.md +80 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/vector/opensearch.md +56 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/vector/pinecone.md +66 -0
- awslabs/cdk_mcp_server/static/bedrock/profiles.md +153 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/agent/actiongroups.md +137 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/agent/alias.md +39 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/agent/collaboration.md +91 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/agent/creation.md +149 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/agent/custom_orchestration.md +74 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/agent/overview.md +78 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/agent/prompt_override.md +70 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/bedrockguardrails.md +188 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/chunking.md +137 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/datasources.md +225 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/kendra.md +81 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/overview.md +116 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/parsing.md +36 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/transformation.md +30 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/vector/aurora.md +185 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/vector/creation.md +80 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/vector/opensearch.md +56 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/vector/pinecone.md +66 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/profiles.md +153 -0
- awslabs/cdk_mcp_server/static/genai_cdk/opensearch-vectorindex/overview.md +135 -0
- awslabs/cdk_mcp_server/static/genai_cdk/opensearchserverless/overview.md +17 -0
- awslabs/cdk_mcp_server/static/lambda_powertools/bedrock.md +127 -0
- awslabs/cdk_mcp_server/static/lambda_powertools/cdk.md +99 -0
- awslabs/cdk_mcp_server/static/lambda_powertools/dependencies.md +45 -0
- awslabs/cdk_mcp_server/static/lambda_powertools/index.md +36 -0
- awslabs/cdk_mcp_server/static/lambda_powertools/insights.md +95 -0
- awslabs/cdk_mcp_server/static/lambda_powertools/logging.md +43 -0
- awslabs/cdk_mcp_server/static/lambda_powertools/metrics.md +93 -0
- awslabs/cdk_mcp_server/static/lambda_powertools/tracing.md +63 -0
- awslabs/cdk_mcp_server/static/opensearch-vectorindex/overview.md +135 -0
- awslabs/cdk_mcp_server/static/opensearchserverless/overview.md +17 -0
- awslabs_cdk_mcp_server-0.0.10417.dist-info/METADATA +14 -0
- awslabs_cdk_mcp_server-0.0.10417.dist-info/RECORD +72 -0
- awslabs_cdk_mcp_server-0.0.10417.dist-info/WHEEL +4 -0
- awslabs_cdk_mcp_server-0.0.10417.dist-info/entry_points.txt +2 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# Agent Collaboration
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Agent Collaboration enables multiple Bedrock Agents to work together on complex tasks. This feature allows agents to specialize in different areas and collaborate to provide more comprehensive responses to user queries.
|
|
6
|
+
|
|
7
|
+
## Collaboration Types
|
|
8
|
+
|
|
9
|
+
You can configure collaboration for an agent using the `agentCollaboration` and `agentCollaborators` properties in the `AgentProps` interface.
|
|
10
|
+
|
|
11
|
+
- **SUPERVISOR**: The agent acts as a supervisor that can delegate tasks to other agents.
|
|
12
|
+
- **SUPERVISOR_ROUTER**: The agent acts as a supervisor that can route requests to specialized agents.
|
|
13
|
+
- **DISABLED**: Collaboration is disabled (default).
|
|
14
|
+
|
|
15
|
+
## Example
|
|
16
|
+
|
|
17
|
+
### TypeScript
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { Agent, AgentCollaboratorType, RelayConversationHistoryType } from '@cdklabs/generative-ai-cdk-constructs';
|
|
21
|
+
|
|
22
|
+
// Create a specialized agent for customer support
|
|
23
|
+
const customerSupportAgent = new Agent(this, 'CustomerSupportAgent', {
|
|
24
|
+
name: 'CustomerSupportAgent',
|
|
25
|
+
instruction: 'You specialize in answering customer support questions about our products.',
|
|
26
|
+
foundationModel: bedrock.BedrockFoundationModel.AMAZON_NOVA_LITE_V1,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Create an agent alias for the specialized agent
|
|
30
|
+
const customerSupportAlias = new AgentAlias(this, 'CustomerSupportAlias', {
|
|
31
|
+
agent: customerSupportAgent,
|
|
32
|
+
aliasName: 'production',
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Create a main agent that can collaborate with the specialized agent
|
|
36
|
+
const mainAgent = new Agent(this, 'MainAgent', {
|
|
37
|
+
name: 'MainAgent',
|
|
38
|
+
instruction: 'You are a helpful assistant that can answer general questions and route specialized customer support questions to the customer support agent.',
|
|
39
|
+
foundationModel: bedrock.BedrockFoundationModel.AMAZON_NOVA_LITE_V1,
|
|
40
|
+
agentCollaboration: AgentCollaboratorType.SUPERVISOR,
|
|
41
|
+
agentCollaborators: [
|
|
42
|
+
new bedrock.AgentCollaborator({
|
|
43
|
+
agentAlias: customerSupportAlias,
|
|
44
|
+
collaborationInstruction: 'Route customer support questions to this agent.',
|
|
45
|
+
collaboratorName: 'CustomerSupport',
|
|
46
|
+
relayConversationHistory: true,
|
|
47
|
+
}),
|
|
48
|
+
],
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Python
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from cdklabs.generative_ai_cdk_constructs import (
|
|
56
|
+
bedrock,
|
|
57
|
+
AgentCollaboratorType,
|
|
58
|
+
RelayConversationHistoryType
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
# Create a specialized agent for customer support
|
|
62
|
+
customer_support_agent = bedrock.Agent(self, 'CustomerSupportAgent',
|
|
63
|
+
name='CustomerSupportAgent',
|
|
64
|
+
instruction='You specialize in answering customer support questions about our products.',
|
|
65
|
+
foundation_model=bedrock.BedrockFoundationModel.AMAZON_NOVA_LITE_V1,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
# Create an agent alias for the specialized agent
|
|
69
|
+
customer_support_alias = bedrock.AgentAlias(self, 'CustomerSupportAlias',
|
|
70
|
+
agent=customer_support_agent,
|
|
71
|
+
alias_name='production',
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
# Create a main agent that can collaborate with the specialized agent
|
|
75
|
+
main_agent = bedrock.Agent(self, 'MainAgent',
|
|
76
|
+
name='MainAgent',
|
|
77
|
+
instruction='You are a helpful assistant that can answer general questions and route specialized customer support questions to the customer support agent.',
|
|
78
|
+
foundation_model=bedrock.BedrockFoundationModel.AMAZON_NOVA_LITE_V1,
|
|
79
|
+
agent_collaboration=AgentCollaboratorType.SUPERVISOR,
|
|
80
|
+
agent_collaborators=[
|
|
81
|
+
bedrock.AgentCollaborator(
|
|
82
|
+
agent_alias= customer_support_alias,
|
|
83
|
+
collaboration_instruction= 'Route customer support questions to this agent.',
|
|
84
|
+
collaborator_name= 'CustomerSupport',
|
|
85
|
+
relay_conversation_history= true,
|
|
86
|
+
)
|
|
87
|
+
],
|
|
88
|
+
)
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
For more information on agent collaboration, refer to the [AWS Bedrock documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-collaboration.html).
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# Agent
|
|
2
|
+
|
|
3
|
+
## Properties
|
|
4
|
+
|
|
5
|
+
| Name | Type | Required | Description |
|
|
6
|
+
|---|---|---|---|
|
|
7
|
+
| name | string | No | The name of the agent. Defaults to a name generated by CDK |
|
|
8
|
+
| instruction | string | Yes | The instruction used by the agent that determines how it will perform its task. Must have a minimum of 40 characters |
|
|
9
|
+
| foundationModel | IInvokable | Yes | The foundation model used for orchestration by the agent |
|
|
10
|
+
| existingRole | iam.IRole | No | The existing IAM Role for the agent to use. Must have a trust policy allowing Bedrock service to assume the role. Defaults to a new created role |
|
|
11
|
+
| shouldPrepareAgent | boolean | No | Specifies whether to automatically update the `DRAFT` version of the agent after making changes. Defaults to false |
|
|
12
|
+
| idleSessionTTL | Duration | No | How long sessions should be kept open for the agent. Session expires if no conversation occurs during this time. Defaults to 1 hour |
|
|
13
|
+
| kmsKey | kms.IKey | No | The KMS key of the agent if custom encryption is configured. Defaults to AWS managed key |
|
|
14
|
+
| description | string | No | A description of the agent. Defaults to no description |
|
|
15
|
+
| knowledgeBases | IKnowledgeBase[] | No | The KnowledgeBases associated with the agent |
|
|
16
|
+
| actionGroups | AgentActionGroup[] | No | The Action Groups associated with the agent |
|
|
17
|
+
| guardrail | IGuardrail | No | The guardrail that will be associated with the agent |
|
|
18
|
+
| promptOverrideConfiguration | PromptOverrideConfiguration | No | Overrides some prompt templates in different parts of an agent sequence configuration |
|
|
19
|
+
| userInputEnabled | boolean | No | Select whether the agent can prompt additional information from the user when it lacks enough information. Defaults to false |
|
|
20
|
+
| codeInterpreterEnabled | boolean | No | Select whether the agent can generate, run, and troubleshoot code when trying to complete a task. Defaults to false |
|
|
21
|
+
| forceDelete | boolean | No | Whether to delete the resource even if it's in use. Defaults to true |
|
|
22
|
+
|
|
23
|
+
## Create an Agent
|
|
24
|
+
|
|
25
|
+
The following example creates an Agent with a simple instruction and default prompts that consults a Knowledge Base.
|
|
26
|
+
|
|
27
|
+
### Example
|
|
28
|
+
|
|
29
|
+
#### TypeScript
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
const agent = new bedrock.Agent(this, 'Agent', {
|
|
33
|
+
foundationModel: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_HAIKU_V1_0,
|
|
34
|
+
instruction: 'You are a helpful and friendly agent that answers questions about literature.',
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
agent.addKnowledgeBase(kb);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
#### Python
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
agent = bedrock.Agent(
|
|
44
|
+
self,
|
|
45
|
+
"Agent",
|
|
46
|
+
foundation_model=bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_HAIKU_V1_0,
|
|
47
|
+
instruction="You are a helpful and friendly agent that answers questions about insurance claims.",
|
|
48
|
+
)
|
|
49
|
+
agent.add_knowledge_base(kb)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Enable Cross Region Inference for Supported Models
|
|
53
|
+
|
|
54
|
+
You can also use system defined inference profiles to enable cross region inference requests for supported models. For instance:
|
|
55
|
+
|
|
56
|
+
### Example
|
|
57
|
+
|
|
58
|
+
#### TypeScript
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
const cris = bedrock.CrossRegionInferenceProfile.fromConfig({
|
|
62
|
+
geoRegion: bedrock.CrossRegionInferenceProfileRegion.US,
|
|
63
|
+
model: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V1_0,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const agent = new bedrock.Agent(this, 'Agent', {
|
|
67
|
+
foundationModel: cris,
|
|
68
|
+
instruction: 'You are a helpful and friendly agent that answers questions about agriculture.',
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
#### Python
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
cris = bedrock.CrossRegionInferenceProfile.from_config(
|
|
76
|
+
geo_region= bedrock.CrossRegionInferenceProfileRegion.US,
|
|
77
|
+
model= bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V1_0
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
agent = bedrock.Agent(
|
|
81
|
+
self,
|
|
82
|
+
"Agent",
|
|
83
|
+
foundation_model=cris,
|
|
84
|
+
instruction="You are a helpful and friendly agent that answers questions about agriculture.",
|
|
85
|
+
)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
For more information on cross region inference, please refer to [System defined inference profiles](#system-defined-inference-profiles)
|
|
89
|
+
|
|
90
|
+
## Prepare the Agent
|
|
91
|
+
|
|
92
|
+
The `Agent` constructs take an optional parameter `shouldPrepareAgent` to indicate that the Agent should be prepared after any updates to an agent, Knowledge Base association, or action group. This may increase the time to create and update those resources. By default, this value is false .
|
|
93
|
+
|
|
94
|
+
Creating an agent alias will not prepare the agent, so if you create an alias using the `AgentAlias` resource then you should set `shouldPrepareAgent` to **_true_**.
|
|
95
|
+
|
|
96
|
+
## Memory Configuration
|
|
97
|
+
|
|
98
|
+
Agents can maintain context across multiple sessions and recall past interactions using memory. This feature is useful for creating a more coherent conversational experience.
|
|
99
|
+
|
|
100
|
+
### Memory Options
|
|
101
|
+
|
|
102
|
+
You can configure memory for an agent using the `memory` property in the `AgentProps` interface. The memory configuration allows you to specify the type of memory and its properties.
|
|
103
|
+
|
|
104
|
+
### Example
|
|
105
|
+
|
|
106
|
+
#### TypeScript
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
import { Agent, Memory, SessionSummaryMemoryProps } from 'src/cdk-lib/bedrock/agents';
|
|
110
|
+
|
|
111
|
+
const agent = new Agent(this, 'MyAgent', {
|
|
112
|
+
name: 'MyAgent',
|
|
113
|
+
instruction: 'Your instruction here',
|
|
114
|
+
foundationModel: bedrock.BedrockFoundationModel.AMAZON_NOVA_LITE_V1,
|
|
115
|
+
memory: Memory.sessionSummary({
|
|
116
|
+
maxRecentSessions: 10, // Keep the last 20 session summaries
|
|
117
|
+
memoryDurationDays: 20, // Retain summaries for 30 days
|
|
118
|
+
}),
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
#### Python
|
|
123
|
+
|
|
124
|
+
```py
|
|
125
|
+
from src.cdk_lib.bedrock.agents import Agent, Memory, BedrockFoundationModel
|
|
126
|
+
|
|
127
|
+
agent = Agent(self, 'MyAgent',
|
|
128
|
+
name='MyAgent',
|
|
129
|
+
instruction='Your instruction here',
|
|
130
|
+
foundation_model=BedrockFoundationModel.AMAZON_NOVA_LITE_V1,
|
|
131
|
+
memory=Memory.session_summary(
|
|
132
|
+
max_recent_sessions=10, # Keep the last 10 session summaries
|
|
133
|
+
memory_duration_days=20, # Retain summaries for 20 days
|
|
134
|
+
),
|
|
135
|
+
)
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Memory Properties
|
|
139
|
+
|
|
140
|
+
- **memoryDurationDays**: Duration in days for which session summaries are retained (1-365). Default is 30 days.
|
|
141
|
+
- **maxRecentSessions**: Maximum number of recent session summaries to include (minimum 1). Default is 20.
|
|
142
|
+
|
|
143
|
+
### Memory Types
|
|
144
|
+
|
|
145
|
+
Currently, the following memory type is supported:
|
|
146
|
+
|
|
147
|
+
- **SESSION_SUMMARY**: Uses memory summarization to enhance accuracy by summarizing sessions.
|
|
148
|
+
|
|
149
|
+
For more information on memory configuration, refer to the [AWS Bedrock documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-memory.html).
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Custom Orchestration
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Custom Orchestration allows you to override the default agent orchestration flow with your own Lambda function. This enables more control over how the agent processes user inputs, handles knowledge base queries, and invokes action groups.
|
|
6
|
+
|
|
7
|
+
## Orchestration Types
|
|
8
|
+
|
|
9
|
+
You can configure the orchestration type using the `orchestrationType` and `customOrchestration` properties in the `AgentProps` interface.
|
|
10
|
+
|
|
11
|
+
- **DEFAULT**: The default orchestration provided by Bedrock (default).
|
|
12
|
+
- **CUSTOM_ORCHESTRATION**: Custom orchestration using a Lambda function.
|
|
13
|
+
|
|
14
|
+
## Example
|
|
15
|
+
|
|
16
|
+
### TypeScript
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import { Agent, OrchestrationType, OrchestrationExecutor } from '@cdklabs/generative-ai-cdk-constructs';
|
|
20
|
+
import * as lambda from 'aws-cdk-lib/aws-lambda';
|
|
21
|
+
import * as path from 'path';
|
|
22
|
+
|
|
23
|
+
// Create a Lambda function for custom orchestration
|
|
24
|
+
const orchestrationFunction = new lambda.Function(this, 'OrchestrationFunction', {
|
|
25
|
+
runtime: lambda.Runtime.PYTHON_3_10,
|
|
26
|
+
handler: 'index.handler',
|
|
27
|
+
code: lambda.Code.fromAsset(path.join(__dirname, 'lambda/orchestration')),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// Create an agent with custom orchestration
|
|
31
|
+
const agent = new Agent(this, 'CustomOrchestrationAgent', {
|
|
32
|
+
name: 'CustomOrchestrationAgent',
|
|
33
|
+
instruction: 'You are a helpful assistant with custom orchestration logic.',
|
|
34
|
+
foundationModel: bedrock.BedrockFoundationModel.AMAZON_NOVA_LITE_V1,
|
|
35
|
+
orchestrationType: OrchestrationType.CUSTOM_ORCHESTRATION,
|
|
36
|
+
customOrchestration: {
|
|
37
|
+
executor: OrchestrationExecutor.fromlambdaFunction(orchestrationFunction),
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Python
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from cdklabs.generative_ai_cdk_constructs import (
|
|
46
|
+
bedrock,
|
|
47
|
+
OrchestrationType,
|
|
48
|
+
OrchestrationExecutor
|
|
49
|
+
)
|
|
50
|
+
import aws_cdk.aws_lambda as lambda_
|
|
51
|
+
import os
|
|
52
|
+
|
|
53
|
+
# Create a Lambda function for custom orchestration
|
|
54
|
+
orchestration_function = lambda_.Function(self, 'OrchestrationFunction',
|
|
55
|
+
runtime=lambda_.Runtime.PYTHON_3_10,
|
|
56
|
+
handler='index.handler',
|
|
57
|
+
code=lambda_.Code.from_asset(os.path.join(os.path.dirname(__file__), 'lambda/orchestration')),
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
# Create an agent with custom orchestration
|
|
61
|
+
agent = bedrock.Agent(self, 'CustomOrchestrationAgent',
|
|
62
|
+
name='CustomOrchestrationAgent',
|
|
63
|
+
instruction='You are a helpful assistant with custom orchestration logic.',
|
|
64
|
+
foundation_model=bedrock.BedrockFoundationModel.AMAZON_NOVA_LITE_V1,
|
|
65
|
+
orchestration_type=OrchestrationType.CUSTOM_ORCHESTRATION,
|
|
66
|
+
custom_orchestration=bedrock.CustomOrchestration(
|
|
67
|
+
executor= OrchestrationExecutor.fromlambda_function(orchestration_function),
|
|
68
|
+
)
|
|
69
|
+
)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
The custom orchestration Lambda function receives events from Bedrock with the user's input and context, and it can control the flow of the conversation by deciding when to query knowledge bases, invoke action groups, or respond directly to the user.
|
|
73
|
+
|
|
74
|
+
For more information on custom orchestration, refer to the [AWS Bedrock documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-custom-orchestration.html).
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# Amazon Bedrock Agents
|
|
2
|
+
|
|
3
|
+
Amazon Bedrock Agents enable you to build AI assistants that can perform tasks by connecting foundation models to your data sources, APIs, and business systems.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
- [Amazon Bedrock Agents](#amazon-bedrock-agents)
|
|
8
|
+
- [Table of Contents](#table-of-contents)
|
|
9
|
+
- [Key Concepts](#key-concepts)
|
|
10
|
+
- [Agent Components](#agent-components)
|
|
11
|
+
- [When to Use Agents](#when-to-use-agents)
|
|
12
|
+
- [Related Resources](#related-resources)
|
|
13
|
+
- [Quick Start Example](#quick-start-example)
|
|
14
|
+
|
|
15
|
+
## Key Concepts
|
|
16
|
+
|
|
17
|
+
- **Agent**: An AI assistant that uses foundation models to understand user requests and take actions
|
|
18
|
+
- **Action Groups**: Functions your agent can call, defined by OpenAPI schemas and executed by Lambda functions
|
|
19
|
+
- **Knowledge Bases**: Data sources that provide context to your agent
|
|
20
|
+
- **Aliases**: Deployment targets for your agent, pointing to specific versions
|
|
21
|
+
- **Orchestration**: The process of coordinating agent interactions with action groups and knowledge bases
|
|
22
|
+
|
|
23
|
+
## Agent Components
|
|
24
|
+
|
|
25
|
+
```mermaid
|
|
26
|
+
graph TD
|
|
27
|
+
A[Agent] --> B[Foundation Model]
|
|
28
|
+
A --> C[Action Groups]
|
|
29
|
+
A --> D[Knowledge Bases]
|
|
30
|
+
A --> E[Aliases]
|
|
31
|
+
A --> F[Orchestration]
|
|
32
|
+
C --> G[Lambda Functions]
|
|
33
|
+
C --> H[OpenAPI Schema]
|
|
34
|
+
D --> I[Vector Stores]
|
|
35
|
+
D --> J[Data Sources]
|
|
36
|
+
F --> K[Default Orchestration]
|
|
37
|
+
F --> L[Custom Orchestration]
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## When to Use Agents
|
|
41
|
+
|
|
42
|
+
- **Task Automation**: Create assistants that can perform tasks like booking appointments or processing orders
|
|
43
|
+
- **Data-Driven Conversations**: Build agents that can answer questions using your organization's data
|
|
44
|
+
- **Multi-Step Workflows**: Implement complex workflows that require multiple steps and decisions
|
|
45
|
+
- **System Integration**: Connect foundation models to your existing systems and APIs
|
|
46
|
+
|
|
47
|
+
## Related Resources
|
|
48
|
+
|
|
49
|
+
- **Action Groups**: `genai-cdk-constructs://bedrock/agent/actiongroups`
|
|
50
|
+
- **Agent Aliases**: `genai-cdk-constructs://bedrock/agent/alias`
|
|
51
|
+
- **Agent Creation**: `genai-cdk-constructs://bedrock/agent/creation`
|
|
52
|
+
- **Agent Collaboration**: `genai-cdk-constructs://bedrock/agent/collaboration`
|
|
53
|
+
- **Custom Orchestration**: `genai-cdk-constructs://bedrock/agent/custom_orchestration`
|
|
54
|
+
- **Prompt Overrides**: `genai-cdk-constructs://bedrock/agent/prompt_override`
|
|
55
|
+
|
|
56
|
+
## Quick Start Example
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { bedrock } from '@cdklabs/generative-ai-cdk-constructs';
|
|
60
|
+
|
|
61
|
+
// Create a basic agent
|
|
62
|
+
const agent = new bedrock.Agent(this, 'Agent', {
|
|
63
|
+
foundationModel: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_HAIKU_V1_0,
|
|
64
|
+
instruction: 'You are a helpful assistant that answers questions about our company.',
|
|
65
|
+
shouldPrepareAgent: true,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Add a knowledge base
|
|
69
|
+
agent.addKnowledgeBase(knowledgeBase);
|
|
70
|
+
|
|
71
|
+
// Add an action group
|
|
72
|
+
agent.addActionGroup(actionGroup);
|
|
73
|
+
|
|
74
|
+
// Create an alias for deployment
|
|
75
|
+
const agentAlias = new bedrock.AgentAlias(this, 'AgentAlias', {
|
|
76
|
+
aliasName: 'latest',
|
|
77
|
+
agent: agent,
|
|
78
|
+
});
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# Prompt Overrides
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Bedrock Agents allows you to customize the prompts and LLM configuration for its different steps. You can disable steps or create a new prompt template. Prompt templates can be inserted from plain text files.
|
|
6
|
+
|
|
7
|
+
## Example
|
|
8
|
+
|
|
9
|
+
### TypeScript
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { readFileSync } from 'fs';
|
|
13
|
+
|
|
14
|
+
const file = readFileSync(prompt_path, 'utf-8');
|
|
15
|
+
|
|
16
|
+
const agent = new bedrock.Agent(this, 'Agent', {
|
|
17
|
+
foundationModel: bedrock.BedrockFoundationModel.AMAZON_NOVA_LITE_V1,
|
|
18
|
+
instruction: 'You are a helpful and friendly agent that answers questions about literature.',
|
|
19
|
+
userInputEnabled: true,
|
|
20
|
+
codeInterpreterEnabled: false,
|
|
21
|
+
shouldPrepareAgent:true,
|
|
22
|
+
promptOverrideConfiguration: bedrock.PromptOverrideConfiguration.fromSteps(
|
|
23
|
+
[
|
|
24
|
+
{
|
|
25
|
+
stepType: bedrock.AgentStepType.PRE_PROCESSING,
|
|
26
|
+
stepEnabled: true,
|
|
27
|
+
customPromptTemplate: file,
|
|
28
|
+
inferenceConfig: {
|
|
29
|
+
temperature: 0.0,
|
|
30
|
+
topP: 1,
|
|
31
|
+
topK: 250,
|
|
32
|
+
maximumLength: 1,
|
|
33
|
+
stopSequences: ["\n\nHuman:"],
|
|
34
|
+
},
|
|
35
|
+
foundationModel: bedrock.BedrockFoundationModel.AMAZON_NOVA_LITE_V1
|
|
36
|
+
}
|
|
37
|
+
]
|
|
38
|
+
)
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Python
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
orchestration = open('prompts/orchestration.txt', encoding="utf-8").read()
|
|
46
|
+
agent = bedrock.Agent(self, "Agent",
|
|
47
|
+
foundation_model=bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_V2_1,
|
|
48
|
+
instruction="You are a helpful and friendly agent that answers questions about insurance claims.",
|
|
49
|
+
user_input_enabled=True,
|
|
50
|
+
code_interpreter_enabled=False,
|
|
51
|
+
should_prepare_agent=True,
|
|
52
|
+
prompt_override_configuration= bedrock.PromptOverrideConfiguration.from_steps(
|
|
53
|
+
steps=[
|
|
54
|
+
bedrock.PromptStepConfiguration(
|
|
55
|
+
step_type=bedrock.AgentStepType.PRE_PROCESSING,
|
|
56
|
+
step_enabled= True,
|
|
57
|
+
custom_prompt_template= file,
|
|
58
|
+
inference_config=bedrock.InferenceConfiguration(
|
|
59
|
+
temperature=0.0,
|
|
60
|
+
top_k=250,
|
|
61
|
+
top_p=1,
|
|
62
|
+
maximum_length=1,
|
|
63
|
+
stop_sequences=['\n\nHuman:'],
|
|
64
|
+
),
|
|
65
|
+
foundationModel: bedrock.BedrockFoundationModel.AMAZON_NOVA_LITE_V1
|
|
66
|
+
),
|
|
67
|
+
]
|
|
68
|
+
),
|
|
69
|
+
)
|
|
70
|
+
```
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# Bedrock Guardrails
|
|
2
|
+
|
|
3
|
+
Amazon Bedrock's Guardrails feature enables you to implement robust governance and control mechanisms for your generative AI applications, ensuring alignment with your specific use cases and responsible AI policies. Guardrails empowers you to create multiple tailored policy configurations, each designed to address the unique requirements and constraints of different use cases. These policy configurations can then be seamlessly applied across multiple foundation models (FMs) and Agents, ensuring a consistent user experience and standardizing safety, security, and privacy controls throughout your generative AI ecosystem.
|
|
4
|
+
|
|
5
|
+
With Guardrails, you can define and enforce granular, customizable policies to precisely govern the behavior of your generative AI applications. You can configure the following policies in a guardrail to avoid undesirable and harmful content and remove sensitive information for privacy protection.
|
|
6
|
+
|
|
7
|
+
- Content filters – Adjust filter strengths to block input prompts or model responses containing harmful content.
|
|
8
|
+
|
|
9
|
+
- Denied topics – Define a set of topics that are undesirable in the context of your application. These topics will be blocked if detected in user queries or model responses.
|
|
10
|
+
|
|
11
|
+
- Word filters – Configure filters to block undesirable words, phrases, and profanity. Such words can include offensive terms, competitor names etc.
|
|
12
|
+
|
|
13
|
+
- Sensitive information filters – Block or mask sensitive information such as personally identifiable information (PII) or custom regex in user inputs and model responses.
|
|
14
|
+
|
|
15
|
+
You can create a Guardrail with a minimum blockedInputMessaging ,blockedOutputsMessaging and default content filter policy.
|
|
16
|
+
|
|
17
|
+
## Examples
|
|
18
|
+
|
|
19
|
+
### TypeScript
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
const guardrails = new bedrock.Guardrail(this, 'bedrockGuardrails', {
|
|
23
|
+
name: 'my-BedrockGuardrails',
|
|
24
|
+
description: 'Legal ethical guardrails.',
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// Optional - Add Sensitive information filters
|
|
28
|
+
|
|
29
|
+
guardrails.addPIIFilter({
|
|
30
|
+
type: PIIType.General.ADDRESS,
|
|
31
|
+
action: GuardrailAction.ANONYMIZE,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
guardrails.addRegexFilter({
|
|
35
|
+
name: 'TestRegexFilter',
|
|
36
|
+
description: 'This is a test regex filter',
|
|
37
|
+
pattern: '/^[A-Z]{2}d{6}$/',
|
|
38
|
+
action: bedrock.GuardrailAction.ANONYMIZE,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Optional - Add contextual grounding
|
|
42
|
+
|
|
43
|
+
guardrails.addContextualGroundingFilter({
|
|
44
|
+
type: ContextualGroundingFilterType.GROUNDING,
|
|
45
|
+
threshold: 0.95,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
guardrails.addContextualGroundingFilter({
|
|
49
|
+
type: ContextualGroundingFilterType.RELEVANCE,
|
|
50
|
+
threshold: 0.95,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Optional - Add Denied topics . You can use a Topic or create your custom Topic
|
|
54
|
+
|
|
55
|
+
guardrails.addDeniedTopicFilter(Topic.FINANCIAL_ADVICE);
|
|
56
|
+
guardrails.addDeniedTopicFilter(
|
|
57
|
+
Topic.custom({
|
|
58
|
+
name: 'Legal_Advice',
|
|
59
|
+
definition:
|
|
60
|
+
'Offering guidance or suggestions on legal matters, legal actions, interpretation of laws, or legal rights and responsibilities.',
|
|
61
|
+
examples: [
|
|
62
|
+
'Can I sue someone for this?',
|
|
63
|
+
'What are my legal rights in this situation?',
|
|
64
|
+
'Is this action against the law?',
|
|
65
|
+
'What should I do to file a legal complaint?',
|
|
66
|
+
'Can you explain this law to me?',
|
|
67
|
+
],
|
|
68
|
+
})
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
// Optional - Add Word filters. You can upload words from a file with addWordFilterFromFile function.
|
|
72
|
+
guardrails.addWordFilter('drugs');
|
|
73
|
+
guardrails.addManagedWordListFilter(ManagedWordFilterType.PROFANITY);
|
|
74
|
+
guardrails.addWordFilterFromFile('./scripts/wordsPolicy.csv');
|
|
75
|
+
|
|
76
|
+
// versioning - if you change any guardrail configuration, a new version will be created
|
|
77
|
+
guardrails.createVersion('testversion');
|
|
78
|
+
|
|
79
|
+
// Importing existing guardrail
|
|
80
|
+
const importedGuardrail = bedrock.Guardrail.fromGuardrailAttributes(stack, 'TestGuardrail', {
|
|
81
|
+
guardrailArn: 'arn:aws:bedrock:us-east-1:123456789012:guardrail/oygh3o8g7rtl',
|
|
82
|
+
guardrailVersion: '1', //optional
|
|
83
|
+
kmsKey: kmsKey, //optional
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// Importing Guardrails created through the L1 CDK CfnGuardrail construct
|
|
87
|
+
const cfnGuardrail = new CfnGuardrail(this, 'MyCfnGuardrail', {
|
|
88
|
+
blockedInputMessaging: 'blockedInputMessaging',
|
|
89
|
+
blockedOutputsMessaging: 'blockedOutputsMessaging',
|
|
90
|
+
name: 'namemycfnguardrails',
|
|
91
|
+
wordPolicyConfig: {
|
|
92
|
+
wordsConfig: [
|
|
93
|
+
{
|
|
94
|
+
text: 'drugs',
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const importedGuardrail = bedrock.Guardrail.fromCfnGuardrail(cfnGuardrail);
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Python
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
guardrail = bedrock.Guardrail(self, 'myGuardrails',
|
|
107
|
+
name='my-BedrockGuardrails',
|
|
108
|
+
description= "Legal ethical guardrails.")
|
|
109
|
+
|
|
110
|
+
# Optional - Add Sensitive information filters
|
|
111
|
+
|
|
112
|
+
guardrail.add_pii_filter(
|
|
113
|
+
type= bedrock.pii_type.General.ADDRESS,
|
|
114
|
+
action= bedrock.GuardrailAction.ANONYMIZE,
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
guardrail.add_regex_filter(
|
|
118
|
+
name= "TestRegexFilter",
|
|
119
|
+
description= "This is a test regex filter",
|
|
120
|
+
pattern= "/^[A-Z]{2}d{6}$/",
|
|
121
|
+
action= bedrock.GuardrailAction.ANONYMIZE,
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
# Optional - Add contextual grounding
|
|
125
|
+
|
|
126
|
+
guardrail.add_contextual_grounding_filter(
|
|
127
|
+
type= bedrock.ContextualGroundingFilterType.GROUNDING,
|
|
128
|
+
threshold= 0.95,
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
# Optional - Add Denied topics . You can use default Topic or create your custom Topic with createTopic function. The default Topics can also be overwritten.
|
|
132
|
+
|
|
133
|
+
guardrail.add_contextual_grounding_filter(
|
|
134
|
+
type= bedrock.ContextualGroundingFilterType.RELEVANCE,
|
|
135
|
+
threshold= 0.95,
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
guardrail.add_denied_topic_filter(bedrock.Topic.FINANCIAL_ADVICE)
|
|
139
|
+
|
|
140
|
+
guardrail.add_denied_topic_filter(
|
|
141
|
+
bedrock.Topic.custom(
|
|
142
|
+
name= "Legal_Advice",
|
|
143
|
+
definition=
|
|
144
|
+
"Offering guidance or suggestions on legal matters, legal actions, interpretation of laws, or legal rights and responsibilities.",
|
|
145
|
+
examples= [
|
|
146
|
+
"Can I sue someone for this?",
|
|
147
|
+
"What are my legal rights in this situation?",
|
|
148
|
+
"Is this action against the law?",
|
|
149
|
+
"What should I do to file a legal complaint?",
|
|
150
|
+
"Can you explain this law to me?",
|
|
151
|
+
]
|
|
152
|
+
)
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
# Optional - Add Word filters. You can upload words from a file with addWordFilterFromFile function.
|
|
156
|
+
guardrail.add_word_filter("drugs")
|
|
157
|
+
guardrail.add_managed_word_list_filter(bedrock.ManagedWordFilterType.PROFANITY)
|
|
158
|
+
guardrail.add_word_filter_from_file("./scripts/wordsPolicy.csv")
|
|
159
|
+
|
|
160
|
+
# versioning - if you change any guardrail configuration, a new version will be created
|
|
161
|
+
guardrail.create_version("testversion")
|
|
162
|
+
|
|
163
|
+
# Importing existing guardrail
|
|
164
|
+
imported_guardrail = bedrock.Guardrail.from_guardrail_attributes(self, "TestGuardrail",
|
|
165
|
+
guardrail_arn="arn:aws:bedrock:us-east-1:123456789012:guardrail/oygh3o8g7rtl",
|
|
166
|
+
guardrail_version="1",
|
|
167
|
+
kms_key=kms_key
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
# Importing Guardrails created through the L1 CDK CfnGuardrail construct
|
|
171
|
+
cfn_guardrail = cfnbedrock.CfnGuardrail(self, "MyCfnGuardrail",
|
|
172
|
+
blocked_input_messaging="blockedInputMessaging",
|
|
173
|
+
blocked_outputs_messaging="blockedOutputsMessaging",
|
|
174
|
+
name="name",
|
|
175
|
+
|
|
176
|
+
# the properties below are optional
|
|
177
|
+
word_policy_config=cfnbedrock.CfnGuardrail.WordPolicyConfigProperty(
|
|
178
|
+
words_config=[cfnbedrock.CfnGuardrail.WordConfigProperty(
|
|
179
|
+
text="drugs"
|
|
180
|
+
)]
|
|
181
|
+
)
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
imported_guardrail = bedrock.Guardrail.from_cfn_guardrail(cfn_guardrail)
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
[View full documentation](https://github.com/awslabs/generative-ai-cdk-constructs/tree/main/src/cdk-lib/bedrock#bedrock-guardrails)
|