-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathconditional_chain.py
173 lines (163 loc) · 6.64 KB
/
conditional_chain.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
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
from aws_cdk import (
Stack,
aws_bedrock as bedrock,
aws_stepfunctions as sfn,
aws_stepfunctions_tasks as tasks,
)
from constructs import Construct
class ConditionalChain(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
validate_input = tasks.BedrockInvokeModel(
self,
"Decide if input is a book",
model=bedrock.FoundationModel.from_foundation_model_id(
self,
"Model",
bedrock.FoundationModelIdentifier.ANTHROPIC_CLAUDE_3_HAIKU_20240307_V1_0,
),
body=sfn.TaskInput.from_object(
{
"anthropic_version": "bedrock-2023-05-31",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
# As the model if the input is a book or not
"text": sfn.JsonPath.format(
"""Does the following text in <text></text> XML tags refer to the name of a book?
<text>
{}
</text>
Start your response with an explanation of your reasoning, then provide a single 'yes' or 'no' indicating whether the text refers to a book.
Your response should be formatted as a JSON object.
An example of a valid response is below when the text does refer to a book, inside <example></example> XML tags.
<example>
\{
"reasoning": "Brief reasons for why I believe the text refers to a book...",
"is_book": "yes"
\}
</example>
Another example of a valid response is below when the text does NOT refer to a book, inside <example></example> XML tags.
<example>
\{
"reasoning": "Brief reasons for why I believe the text does not refer to a book...",
"is_book": "no"
\}
</example>
Do not include any other content other than the JSON object in your response. Do not include any XML tags in your response.""",
sfn.JsonPath.string_at("$$.Execution.Input"),
),
}
],
}
],
"max_tokens": 250,
"temperature": 1,
}
),
)
model_response_to_array = sfn.Pass(
self,
"Parse Model Response",
parameters={
"decision": sfn.JsonPath.string_to_json(
sfn.JsonPath.string_at("$.Body.content[0].text")
),
},
)
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 prompt and inference properties
body=sfn.TaskInput.from_object(
{
"anthropic_version": "bedrock-2023-05-31",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
# The main prompt
"text": "Write a 1-2 sentence summary for the book Pride & Prejudice.",
}
],
}
],
"max_tokens": 250,
"temperature": 1,
}
),
)
write_an_advertisement = tasks.BedrockInvokeModel(
self,
"Generate Book Advertisement",
model=bedrock.FoundationModel.from_foundation_model_id(
self,
"Model",
bedrock.FoundationModelIdentifier.ANTHROPIC_CLAUDE_3_HAIKU_20240307_V1_0,
),
body=sfn.TaskInput.from_object(
{
"anthropic_version": "bedrock-2023-05-31",
# Inject the previous output from the model as past conversation,
# then add the new prompt that relies on previous output as context.
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
# The previous step's prompt.
"text": "Write a 1-2 sentence summary for the book Pride & Prejudice.",
},
],
},
{
# The previous step's model output
"role": sfn.JsonPath.string_at("$.Body.role"),
"content": sfn.JsonPath.string_at("$.Body.content"),
},
{
"role": "user",
"content": [
{
"type": "text",
# The new prompt
"text": "Now write a short advertisement for the novel.",
},
],
},
],
"max_tokens": 250,
"temperature": 1,
}
),
# Extract the final response from the model as the result of the Step Functions execution
output_path="$.Body.content[0].text",
)
# Chain the steps together with a condition
book_decision = (
sfn.Choice(self, "Is it a book?")
.when(
sfn.Condition.string_equals("$.decision.is_book", "yes"),
get_summary.next(write_an_advertisement),
)
.otherwise(sfn.Fail(self, "Input was not a book"))
)
chain = validate_input.next(model_response_to_array).next(book_decision)
sfn.StateMachine(
self,
"ConditionalChainExample",
state_machine_name="Techniques-ConditionalChain",
definition_body=sfn.DefinitionBody.from_chainable(chain),
)