Skip to content

Instantly share code, notes, and snippets.

@rayl15
Created January 25, 2025 06:56
Show Gist options
  • Select an option

  • Save rayl15/4c93f0cd1ff98bedda44ee0034fc5d03 to your computer and use it in GitHub Desktop.

Select an option

Save rayl15/4c93f0cd1ff98bedda44ee0034fc5d03 to your computer and use it in GitHub Desktop.
import boto3
import json
from botocore.exceptions import ClientError
def lambda_handler(event, context):
# Set the AWS Region
region = "us-east-1"
# Initialize the Bedrock Runtime client
client = boto3.client("bedrock-runtime", region_name=region)
# Get the prompt from the event payload
prompt = event.get("prompt")
# Validate the input prompt
if not prompt:
return {
"statusCode": 400,
"body": json.dumps({"error": "Missing 'prompt' in the event payload."})
}
# Configure inference parameters
inference_parameters = {
"inputText": prompt,
"textGenerationConfig": {
"maxTokenCount": 512, # Limit the response length
"temperature": 0.5, # Control the randomness of the output
},
}
# Convert the request payload to JSON
request_payload = json.dumps(inference_parameters)
try:
# Invoke the model
response = client.invoke_model(
modelId="amazon.titan-text-express-v1", # Replace with your model ID
body=request_payload,
contentType="application/json",
accept="application/json"
)
# Decode the response body
response_body = json.loads(response["body"].read())
# Extract the generated text
generated_text = response_body["results"][0]["outputText"]
# Return the generated text
return {
"statusCode": 200,
"body": json.dumps({
"generatedText": generated_text
})
}
except ClientError as e:
# Handle AWS client errors
return {
"statusCode": 500,
"body": json.dumps({
"error": e.response['Error']['Message']
})
}
except Exception as e:
# Handle any other errors
return {
"statusCode": 500,
"body": json.dumps({
"error": str(e)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment