-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathprompt_templating.py
57 lines (53 loc) · 2.25 KB
/
prompt_templating.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
from aws_cdk import (
Stack,
aws_bedrock as bedrock,
aws_stepfunctions as sfn,
aws_stepfunctions_tasks as tasks,
)
from constructs import Construct
class PromptTemplating(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,
}
),
# Extract the final response from the model as the result of the Step Functions execution
output_path="$.Body.content[0].text",
)
sfn.StateMachine(
self,
"PromptTemplatingExample",
state_machine_name="Techniques-PromptTemplating",
definition_body=sfn.DefinitionBody.from_chainable(get_summary),
)