-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathaws_service_invocation.py
75 lines (69 loc) · 2.75 KB
/
aws_service_invocation.py
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
from aws_cdk import (
Stack,
aws_bedrock as bedrock,
aws_sns as sns,
aws_stepfunctions as sfn,
aws_stepfunctions_tasks as tasks,
)
from constructs import Construct
class AwsServiceInvocationChain(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
get_summary = tasks.BedrockInvokeModel(
self,
"Generate Book Summary",
# Choose the model to invoke
model=bedrock.FoundationModel.from_foundation_model_id(
self,
"Model",
bedrock.FoundationModelIdentifier.ANTHROPIC_CLAUDE_3_HAIKU_20240307_V1_0,
),
# Provide the input to the model, including the templated prompt and inference properties
body=sfn.TaskInput.from_object(
{
"anthropic_version": "bedrock-2023-05-31",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
# The prompt is templated with the novel name as variable input.
# The input to the Step Functions execution could be:
# "Pride and Prejudice"
"text": sfn.JsonPath.format(
"Write a 1-2 sentence summary for the book {}.",
sfn.JsonPath.string_at("$$.Execution.Input"),
),
}
],
}
],
"max_tokens": 250,
"temperature": 1,
}
),
)
topic = sns.Topic(
self, "Topic", display_name="Notifications about generated book summaries"
)
notify_me = tasks.SnsPublish(
self,
"Notify Me",
topic=topic,
message=sfn.TaskInput.from_object(
{
"summary": sfn.JsonPath.string_at("$.Body.content[0].text"),
"book": sfn.JsonPath.string_at("$$.Execution.Input"),
}
),
result_path=sfn.JsonPath.DISCARD,
output_path="$.Body.content[0].text",
)
chain = get_summary.next(notify_me)
sfn.StateMachine(
self,
"AwsServiceInvocationExample",
state_machine_name="Techniques-AwsServiceInvocation",
definition_body=sfn.DefinitionBody.from_chainable(chain),
)