Skip to content

Checking to see if there is anything in the prompts.

0

In my aws lambda function i have prompts that i recieve from Android Studio in Kotlin. Sometimes there will be one prompt, sometimes there will be three. if there is only one, in the "shots" section i only need that one prompt. if theres 2 i need both in there. The thing is, is i need to send all 3, whether there is any actaul prompt in all 3 or not, it could just be one like i said. so down in the shots section, i need the prompts that are actually prompts to be there. my codes below will help show this.

import json import boto3

bedrock_runtime = boto3.client("bedrock-runtime")

def lambda_handler(event, context):

body = json.loads(event['body'])
prompt = body['prompt']
prompt2 = body['prompt2']
prompt3 = body['prompt3']


model_input = {
"taskType": "MULTI_SHOT_MANUAL",
"multiShotManualParams": {
    "shots": 
    [
        {"text": prompt},
        {"text": prompt2},
        {"text": prompt3},
    ]
},
"videoGenerationConfig": {
    "seed": 33,
    "fps": 24,  # Must be 24
    "dimension": "1280x720",  # Must be "1280x720"
},

}

try:
    # Start the asynchronous video generation job
    invocation = bedrock_runtime.start_async_invoke(
        modelId="amazon.nova-reel-v1:1",
        modelInput=model_input,
        outputDataConfig={
            "s3OutputDataConfig": {
                "s3Uri": "s3://novabucket"
            }
        }
    )

    # Extract the invocation ID and construct the future object key
    invocation_id = invocation['invocationArn'].split('/')[-1]
    future_object_key = f"{invocation_id}/output.mp4"

    return {
        "statusCode": 200,
        "body": json.dumps({
            "invocationArn": invocation.get('invocationArn'),
            "requestId": invocation.get('requestId'),
            "futureObjectKey": f"{invocation_id}/output.mp4"
        })
    }

except Exception as e:
    message = str(e)
    return {
        "statusCode": 500,
        "body": json.dumps({"error": message})
    }

asked a year ago91 views

1 Answer
1

To handle varying numbers of prompts in your AWS Lambda function, you can modify your code to dynamically build the "shots" section based on the non-empty prompts received. Here's how you can adjust your lambda_handler function:

  1. First, create a list of non-empty prompts:
prompts = [body['prompt'], body['prompt2'], body['prompt3']]
non_empty_prompts = [p for p in prompts if p]
  1. Then, dynamically build the "shots" section:
shots = [{"text": prompt} for prompt in non_empty_prompts]
  1. Update your model_input dictionary:
model_input = {
    "taskType": "MULTI_SHOT_MANUAL",
    "multiShotManualParams": {
        "shots": shots
    },
    "videoGenerationConfig": {
        "seed": 33,
        "fps": 24,
        "dimension": "1280x720",
    },
}

This approach ensures that only non-empty prompts are included in the "shots" section, while still allowing for up to three prompts. If there's only one prompt, the "shots" list will contain only that prompt. If there are two or three, it will include all of them.

Remember to handle potential errors if no prompts are provided:

if not non_empty_prompts:
    return {
        "statusCode": 400,
        "body": json.dumps({"error": "At least one prompt must be provided"})
    }

This modification allows your Lambda function to flexibly handle varying numbers of prompts without sending empty ones to the Bedrock runtime.

Sources
Changing AWS Lambda Function prompt value with an event instead of changing environment variable. | AWS re:Post

answered a year ago

EXPERT

reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.