Files
hack/tools/scripts/llm-key-hunter/results/_cache/content/aws-samples_amazon-bedrock-serverless-prompt-chaining.json

1 line
10 KiB
JSON

{"repo": "aws-samples/amazon-bedrock-serverless-prompt-chaining", "files": {"techniques_step_functions/stacks/human_input_chain.py": {"043853a8d694d9c936c00a32d2c6e63522773935": {"t": 1785674633.5532365, "n": 4714, "h": "63c58c9fa7c9559b", "c": "from aws_cdk import (\n aws_iam as iam,\n Duration,\n Stack,\n aws_bedrock as bedrock,\n aws_sns as sns,\n aws_stepfunctions as sfn,\n aws_stepfunctions_tasks as tasks,\n)\nfrom constructs import Construct\nfrom .inference_profile import InferenceProfile\n\n\nclass HumanInputChain(Stack):\n def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:\n super().__init__(scope, construct_id, **kwargs)\n\n model = InferenceProfile(self, \"Model\", \"global.anthropic.claude-haiku-4-5-20251001-v1:0\")\n\n get_advertisement = tasks.BedrockInvokeModel(\n self,\n \"Generate Advertisement\",\n # Choose the model to invoke\n model=model,\n # Provide the input to the model, including the templated prompt and inference properties\n body=sfn.TaskInput.from_object(\n {\n \"anthropic_version\": \"bedrock-2023-05-31\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": [\n {\n \"type\": \"text\",\n # The prompt is templated with the novel name as variable input.\n # The input to the Step Functions execution could be:\n # \"Pride and Prejudice\"\n \"text\": sfn.JsonPath.format(\n \"Write a short advertisement for the book {}.\",\n sfn.JsonPath.string_at(\"$$.Execution.Input\"),\n ),\n }\n ],\n }\n ],\n \"max_tokens\": 250,\n \"temperature\": 1,\n }\n ),\n )\n\n # Send the generated advertisement to a SNS topic.\n # The human receiving the notification is expected to approve or reject the advertisement.\n # The human's decision should be sent to the Step Functions execution using the task token.\n # aws stepfunctions send-task-success --task-output \"{\\\"decision\\\": \\\"yes\\\"}\" --task-token \"AQB8A...\"\n topic = sns.Topic(\n self, \"Topic\", display_name=\"Human input topic for techniques example\"\n )\n publish_ad_for_approval = tasks.SnsPublish(\n self,\n \"Get Approval For Advertisement\",\n topic=topic,\n integration_pattern=sfn.IntegrationPattern.WAIT_FOR_TASK_TOKEN,\n message=sfn.TaskInput.from_object(\n {\n \"advertisement\": sfn.JsonPath.string_at(\"$.Body.content[0].text\"),\n \"task_token\": sfn.JsonPath.task_token,\n }\n ),\n result_path=\"$.human_input\",\n )\n\n extract_ad = sfn.Pass(\n self,\n \"Extract Advertisement\",\n parameters={\n \"advertisement\": sfn.JsonPath.string_at(\"$.Body.content[0].text\"),\n },\n )\n\n handle_user_decision = (\n sfn.Choice(self, \"Is Advertisement Approved?\")\n .when(\n # Human approved the ad - finish the Step Functions execution\n sfn.Condition.string_equals(\"$.human_input.decision\", \"yes\"),\n extract_ad,\n )\n .when(\n # Human rejected the ad - loop back to generate a new ad\n sfn.Condition.string_equals(\"$.human_input.decision\", \"no\"),\n get_advertisement,\n )\n .otherwise(\n sfn.Fail(\n self,\n \"Invalid Advertisement Approval Value\",\n cause=\"Unknown user choice (decision must be yes or no)\",\n error=\"Unknown user choice (decision must be yes or no)\",\n )\n )\n )\n\n chain = get_advertisement.next(publish_ad_for_approval).next(\n handle_user_decision\n )\n\n state_machine = sfn.StateMachine(\n self,\n \"HumanInputExample\",\n state_machine_name=\"Techniques-HumanInput\",\n definition_body=sfn.DefinitionBody.from_chainable(chain),\n timeout=Duration.minutes(10),\n )\n\n # Add IAM permission for the foundation model\n state_machine.add_to_role_policy(\n iam.PolicyStatement(\n effect=iam.Effect.ALLOW,\n actions=[\"bedrock:InvokeModel\"],\n resources=[model.get_foundation_model_arn_pattern()],\n )\n )\n"}, "92098a6052a8dae63f3cf4becd066489704c7068": {"t": 1785846911.3163767, "n": 4714, "h": "63c58c9fa7c9559b", "c": "from aws_cdk import (\n aws_iam as iam,\n Duration,\n Stack,\n aws_bedrock as bedrock,\n aws_sns as sns,\n aws_stepfunctions as sfn,\n aws_stepfunctions_tasks as tasks,\n)\nfrom constructs import Construct\nfrom .inference_profile import InferenceProfile\n\n\nclass HumanInputChain(Stack):\n def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:\n super().__init__(scope, construct_id, **kwargs)\n\n model = InferenceProfile(self, \"Model\", \"global.anthropic.claude-haiku-4-5-20251001-v1:0\")\n\n get_advertisement = tasks.BedrockInvokeModel(\n self,\n \"Generate Advertisement\",\n # Choose the model to invoke\n model=model,\n # Provide the input to the model, including the templated prompt and inference properties\n body=sfn.TaskInput.from_object(\n {\n \"anthropic_version\": \"bedrock-2023-05-31\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": [\n {\n \"type\": \"text\",\n # The prompt is templated with the novel name as variable input.\n # The input to the Step Functions execution could be:\n # \"Pride and Prejudice\"\n \"text\": sfn.JsonPath.format(\n \"Write a short advertisement for the book {}.\",\n sfn.JsonPath.string_at(\"$$.Execution.Input\"),\n ),\n }\n ],\n }\n ],\n \"max_tokens\": 250,\n \"temperature\": 1,\n }\n ),\n )\n\n # Send the generated advertisement to a SNS topic.\n # The human receiving the notification is expected to approve or reject the advertisement.\n # The human's decision should be sent to the Step Functions execution using the task token.\n # aws stepfunctions send-task-success --task-output \"{\\\"decision\\\": \\\"yes\\\"}\" --task-token \"AQB8A...\"\n topic = sns.Topic(\n self, \"Topic\", display_name=\"Human input topic for techniques example\"\n )\n publish_ad_for_approval = tasks.SnsPublish(\n self,\n \"Get Approval For Advertisement\",\n topic=topic,\n integration_pattern=sfn.IntegrationPattern.WAIT_FOR_TASK_TOKEN,\n message=sfn.TaskInput.from_object(\n {\n \"advertisement\": sfn.JsonPath.string_at(\"$.Body.content[0].text\"),\n \"task_token\": sfn.JsonPath.task_token,\n }\n ),\n result_path=\"$.human_input\",\n )\n\n extract_ad = sfn.Pass(\n self,\n \"Extract Advertisement\",\n parameters={\n \"advertisement\": sfn.JsonPath.string_at(\"$.Body.content[0].text\"),\n },\n )\n\n handle_user_decision = (\n sfn.Choice(self, \"Is Advertisement Approved?\")\n .when(\n # Human approved the ad - finish the Step Functions execution\n sfn.Condition.string_equals(\"$.human_input.decision\", \"yes\"),\n extract_ad,\n )\n .when(\n # Human rejected the ad - loop back to generate a new ad\n sfn.Condition.string_equals(\"$.human_input.decision\", \"no\"),\n get_advertisement,\n )\n .otherwise(\n sfn.Fail(\n self,\n \"Invalid Advertisement Approval Value\",\n cause=\"Unknown user choice (decision must be yes or no)\",\n error=\"Unknown user choice (decision must be yes or no)\",\n )\n )\n )\n\n chain = get_advertisement.next(publish_ad_for_approval).next(\n handle_user_decision\n )\n\n state_machine = sfn.StateMachine(\n self,\n \"HumanInputExample\",\n state_machine_name=\"Techniques-HumanInput\",\n definition_body=sfn.DefinitionBody.from_chainable(chain),\n timeout=Duration.minutes(10),\n )\n\n # Add IAM permission for the foundation model\n state_machine.add_to_role_policy(\n iam.PolicyStatement(\n effect=iam.Effect.ALLOW,\n actions=[\"bedrock:InvokeModel\"],\n resources=[model.get_foundation_model_arn_pattern()],\n )\n )\n"}}}}