- Newest
- Most votes
- Most comments
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:
- 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 ]
- 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.
- 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
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
Relevant content
asked a year ago
asked a year ago
asked a year ago
- AWS OFFICIALUpdated 5 years ago
- AWS OFFICIALUpdated 5 years ago
