/
state-machine.ts
312 lines (287 loc) · 9.11 KB
/
state-machine.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import * as cdk from 'aws-cdk-lib';
import {
aws_bedrock as bedrock,
aws_dynamodb as dynamodb,
aws_events as events,
aws_iam as iam,
aws_logs as logs,
aws_ssm as ssm,
aws_stepfunctions as sfn,
aws_stepfunctions_tasks as tasks,
} from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { SsmParameterNames } from '../ssm-parameter-names';
type StateMachineProps = {
table: dynamodb.ITable;
}
export class StateMachine {
public readonly stateMachine: sfn.StateMachine;
constructor (scope: Construct, props: StateMachineProps){
// LogGroup for State Machine
const logGroup = new logs.LogGroup(scope, 'StateMachineLogs', {
logGroupName: `/aws/statemachine/${scope.toString()}`,
removalPolicy: cdk.RemovalPolicy.DESTROY,
retention: logs.RetentionDays.THREE_MONTHS
});
// -----------------------------
// Connection
// -----------------------------
const connection = new events.Connection(scope, 'LineEndpointConnection', {
authorization: events.Authorization.apiKey(
'Authorization',
cdk.SecretValue.unsafePlainText(`Bearer ${ssm.StringParameter.valueForStringParameter(scope, SsmParameterNames.LINE_CHANNEL_ACCESS_TOKEN)}`)
),
});
// -----------------------------
// IAM Role
// -----------------------------
// IAM Policy
const policyDocument = new iam.PolicyDocument({
statements:[
// new iam.PolicyStatement({
// effect: iam.Effect.ALLOW,
// actions: [
// 'logs:CreateLogGroup',
// 'logs:CreateLogStream',
// 'logs:PutLogEvents'
// ],
// resources: [
// logGroup.logGroupArn,
// `${logGroup.logGroupArn}:*`
// ],
// }),
// new iam.PolicyStatement({
// effect: iam.Effect.ALLOW,
// actions: [
// 'dynamodb:GettItem',
// 'dynamodb:PutItem',
// ],
// resources: [
// props.table.tableArn,
// ],
// }),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'states:InvokeHTTPEndpoint',
],
resources: [ '*' ],
}),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'events:RetrieveConnectionCredentials',
],
resources: [
connection.connectionArn,
],
}),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'secretsmanager:GetSecretValue',
'secretsmanager:DescribeSecret',
],
resources: [
connection.connectionSecretArn,
],
}),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'xray:PutTraceSegments',
'xray:PutTelemetryRecords',
'xray:GetSamplingRules',
'xray:GetSamplingTargets'
],
resources: [ '*' ]
}),
]
});
// IAM Role
const role = new iam.Role(scope, 'StateMachineRole', {
assumedBy: new iam.ServicePrincipal('states.amazonaws.com'),
inlinePolicies: {
'policy': policyDocument,
},
});
// -----------------------------
// Tasks
// -----------------------------
const taskLoadHistory = new tasks.DynamoGetItem(scope, 'Load History', {
table: props.table,
key: {
chat_id: tasks.DynamoAttributeValue.fromString(sfn.JsonPath.stringAt('$.chatId')),
},
inputPath: '$[0].body',
resultPath: '$[0].LoadHistory',
outputPath: '$[0]',
});
// const taskLoadHistory = new tasks.CallAwsService(scope, 'Load History', {
// action: 'getItem',
// iamResources: [
// props.table.tableArn,
// ],
// service: 'dynamodb',
// parameters: {
// "TableName": props.table.tableName,
// "Key": {
// "chat_id": {
// "S.$": "$.chatId"
// }
// }
// },
// inputPath: '$[0].body',
// resultPath: '$[0].LoadHistory',
// outputPath: '$[0]',
// });
const taskLoadPrompTemplate = new tasks.CallAwsService(scope, 'Load Prompt Template', {
action: 'getParameter',
iamResources: [
'*',
],
service: 'ssm',
parameters: {
"Name": SsmParameterNames.PROMPT_TEMPLATE
},
inputPath: '$[0].body',
resultSelector: {
"Value.$": "$.Parameter.Value"
},
resultPath: '$[0].LoadPromptTemplate',
outputPath: '$[0]',
});
const taskExistsHistory = new sfn.Pass(scope, 'Exists History', {
parameters: {
"body.$": "$.body",
"LoadHistory.$": "$.LoadHistory.Item",
},
});
const taskNotExistsHistory = new sfn.Pass(scope, 'Not Exists History', {
parameters: {
"body.$": "$.body",
"LoadHistory": {
"history": {
"S": ""
}
}
},
});
const choiceHasHistory = new sfn.Choice(scope, 'Has History').when(
sfn.Condition.isPresent('$.LoadHistory.Item'),
taskExistsHistory
).otherwise(
taskNotExistsHistory
);
const taskFlatten = new sfn.Pass(scope, 'Flatten', {
parameters: {
"body.$": "$[0].body",
"LoadHistory.$": "$[0].LoadHistory",
"LoadPromptTemplate.$": "$[1].LoadPromptTemplate"
}
});
const taskInvokeModel = new tasks.BedrockInvokeModel(scope, 'Invoke Model', {
model: bedrock.FoundationModel.fromFoundationModelId(scope, 'Model', bedrock.FoundationModelIdentifier.ANTHROPIC_CLAUDE_V2_1),
body: sfn.TaskInput.fromObject({
"prompt.$": "States.Format($.LoadPromptTemplate.Value, $.LoadHistory.history.S, $.body.message)",
"max_tokens_to_sample": 500,
"temperature": 0.5,
"top_p": 1.0,
"top_k": 250,
}),
accept: 'application/json',
contentType: 'application/json',
resultPath: '$.InvokeModel',
});
const taskSendLineResponse = new sfn.CustomState(scope, 'Send LINE Response', {
stateJson: {
"Type": "Task",
"Resource": "arn:aws:states:::http:invoke",
"Parameters": {
"ApiEndpoint": "https://api.line.me/v2/bot/message/reply",
"Method": "POST",
"RequestBody": {
"replyToken.$": "$.body.replyToken",
"messages": [
{
"type": "text",
"text.$": "$.InvokeModel.Body.completion"
}
]
},
"Authentication": {
"ConnectionArn": connection.connectionArn
}
},
"Retry": [
{
"ErrorEquals": [
"States.ALL"
],
"BackoffRate": 2,
"IntervalSeconds": 1,
"MaxAttempts": 3,
"JitterStrategy": "FULL"
}
],
"End": true,
"ResultPath": "$.SendLineResponse"
}
});
// const taskSaveHistory = new tasks.DynamoPutItem(scope, 'Save History', {
// table: props.table,
// item: {
// chat_id: tasks.DynamoAttributeValue.fromString(sfn.JsonPath.stringAt('$.body.chatId')),
// history: tasks.DynamoAttributeValue.fromString(sfn.JsonPath.stringAt('States.Format("{}\n\nHuman: {}\n\nAssistant: {}", $.LoadHistory.history.S, $.body.message, $.InvokeModel.Body.completion)')),
// ttl: tasks.DynamoAttributeValue.numberFromString(sfn.JsonPath.stringAt(`States.Format("{}", States.MathAdd($.body.timestampSecond, 3600))`)),
// },
// resultPath: '$.SaveHistory',
// });
const taskSaveHistory = new tasks.CallAwsService(scope, 'Save History', {
action: 'putItem',
iamResources: [
props.table.tableArn,
],
service: 'dynamodb',
parameters: {
"TableName": props.table.tableName,
"Item": {
"chat_id": {
"S.$": "$.body.chatId"
},
"history": {
"S.$": "States.Format('{}\n\nHuman: {}\n\nAssistant: {}', $.LoadHistory.history.S, $.body.message, $.InvokeModel.Body.completion)"
},
"ttl": {
"N.$": "States.Format('{}', States.MathAdd($.body.timestampSecond, 3600))"
}
}
},
resultPath: '$.SaveHistory',
});
const parallelPrepare = new sfn.Parallel(scope, 'Prepare')
.branch(taskLoadHistory)
.branch(taskLoadPrompTemplate);
const parallelResponse = new sfn.Parallel(scope, 'Parallel')
.branch(taskSendLineResponse)
.branch(taskSaveHistory);
taskLoadHistory.next(choiceHasHistory);
parallelPrepare.next(taskFlatten);
taskFlatten.next(taskInvokeModel);
taskInvokeModel.next(parallelResponse);
const stateMachine = new sfn.StateMachine(scope, 'StateMachine', {
role: role,
logs: {
destination: logGroup,
includeExecutionData: true,
level: sfn.LogLevel.ERROR,
},
timeout: cdk.Duration.seconds(60),
tracingEnabled: false,
definitionBody: sfn.DefinitionBody.fromChainable(
parallelPrepare
),
});
this.stateMachine = stateMachine;
}
}