Skip to content

Checking to see if there is anything in the prompts. 2

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}) }

This answer doesnt work.its almost like the structure of the "shots": section needs to stay the same when were thinking about this solution. for example: "shots": [ {"text": prompt}, {"text": prompt2}, {"text": prompt3}, ] },

Here is the answer that doesnt work:

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] Then, dynamically build the "shots" section: shots = [{"text": prompt} for prompt in non_empty_prompts] 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"}) }

asked a year ago130 views

2 Answers
0

To address your requirement while maintaining the structure of the "shots" section, you can modify your code to keep all three prompt slots but only fill them with actual content when available. Here's how you can adjust your Lambda function:

  1. First, create a list of prompts, replacing empty ones with None:

prompts = [ body.get('prompt') or None, body.get('prompt2') or None, body.get('prompt3') or None ]

  1. Then, update your model_input dictionary:

model_input = { "taskType": "MULTI_SHOT_MANUAL", "multiShotManualParams": { "shots": [ {"text": prompt} if prompt is not None else {"text": ""} for prompt in prompts ] }, "videoGenerationConfig": { "seed": 33, "fps": 24, "dimension": "1280x720", }, }

This approach ensures that:

  • The structure of the "shots" section remains the same, always containing three elements.
  • Only non-empty prompts are included with actual content.
  • Empty prompts are replaced with an empty string.
  1. Add a check to ensure at least one prompt is provided:

if all(prompt is None for prompt in prompts): return { "statusCode": 400, "body": json.dumps({"error": "At least one prompt must be provided"}) }

This solution maintains the required structure while allowing for flexibility in the number of prompts provided. It will work whether you have one, two, or three prompts, and it will always send three "shot" elements to the Bedrock runtime, with empty strings for any missing prompts.

Sources
Checking to see if there is anything in the prompts. | AWS re:Post

answered a year ago

AWS
SUPPORT ENGINEER

reviewed a year ago

0

You're close! Since Bedrock requires all three shots objects to be present, but not necessarily filled, here’s how to keep the structure consistent while dynamically handling missing prompts:

python Copy Edit import json import boto3

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

def lambda_handler(event, context): body = json.loads(event['body'])

prompts = [
    body.get('prompt') or "",
    body.get('prompt2') or "",
    body.get('prompt3') or ""
]

if all(p == "" for p in prompts):
    return {
        "statusCode": 400,
        "body": json.dumps({"error": "At least one prompt must be provided."})
    }

model_input = {
    "taskType": "MULTI_SHOT_MANUAL",
    "multiShotManualParams": {
        "shots": [{"text": p} for p in prompts]
    },
    "videoGenerationConfig": {
        "seed": 33,
        "fps": 24,
        "dimension": "1280x720"
    }
}

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

    invocation_id = invocation['invocationArn'].split('/')[-1]

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

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

This ensures:

You always send 3 shots as required by the model.

Empty prompts are replaced with "" so the structure stays intact.

You gracefully handle the case where all prompts are missing.

Let me know if you'd like a version that validates length or characters too!

answered 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.