Gen AI on AWS - Lambda



AWS Lambda is a serverless computing service provided by AWS that allows you to run code without managing servers. It automatically scales your applications according to incoming requests and ensures that resources are only used when required.

In case of Generative AI, AWS Lambda can be used to execute tasks such as real-time inference, preprocessing data, or orchestrating workflows for AI models. You can also integrate it with other AWS services like SageMaker or EC2 to build a complete solution for training, deploying, and running Gen AI models.

Features of AWS Lambda for Generative AI

Listed here are some of the key features of AWS Lambda which can be useful for training and deploying Generative AI −

  • Serverless Execution
  • Event-Driven Architecture
  • Auto-Scaling
  • Cost effectiveness

Using AWS Lambda for Real-Time Inference in Generative AI

AWS Lambda can be used with trained Generative AI models to provide real-time inference capabilities.

For example, once a text generation model is deployed using SageMaker, Lambda can be used to trigger predictions in real time when a new input is received. It is useful for applications like Chatbots and Content Creation.

Implementation Example

The following example will show how to do real-time text generation with AWS Lambda and SageMaker.

Step 1: Prerequisites

The prerequisites for implementing this example are −

  • An AWS SageMaker model deployed as an endpoint. Example: GPT-2 model
  • The boto3 library installed which you can use to invoke the AWS SageMaker endpoints from the Lanbda function.

If you dont have boto3 installed, you can install it using the following command −

pip install boto3

Step 2: AWS Lambda Function

Given below is the Python code for an AWS Lambda function that calls a SageMaker endpoint for real-time text generation −

import boto3
import json

# Initialize the SageMaker runtime client
sagemaker_runtime = boto3.client('sagemaker-runtime')

# Specify your SageMaker endpoint name 
# The model must already be deployed
SAGEMAKER_ENDPOINT_NAME = 'your-sagemaker-endpoint-name'

def lambda_handler(event, context):
   # Extract input text from the Lambda event 
   # For example, user input from a chatbot
   user_input = event.get('input_text', 'Hello!')

   # Create a payload for the SageMaker model
   # Prepare input for text generation
   payload = json.dumps({'inputs': user_input})

   # Call the SageMaker endpoint to generate text
   response = sagemaker_runtime.invoke_endpoint(
      EndpointName = SAGEMAKER_ENDPOINT_NAME,
      ContentType = 'application/json',      
      Body = payload                         
   )

   # Parse the response from SageMaker
   result = json.loads(response['Body'].read().decode())
	
   # Extract the generated text from the response
   generated_text = result.get('generated_text', 'No response generated.')

   # Return the generated text to the user (as Lambda output)
   return {
      'statusCode': 200,
      'body': json.dumps({
         'input_text': user_input,
         'generated_text': generated_text
      })
   }

Step 3: Deploying the Lambda Function

Once you have written the Lambda function, we need to deploy it. Follow the steps given below −

Create the Lambda Function

  • First, log in to your AWS Lambda
  • Create a new Lambda function and select Python 3.x as the runtime.
  • Finally, add the code above to your Lambda function.

Set up IAM Permissions

The Lambda function's execution role should have the permissions to invoke SageMaker endpoints. For this, attach AmazonSageMakerFullAccess or a custom role with SageMaker access.

Step 4: Test the Lambda Function

Now, you can manually test the Lambda function by passing a sample event with an input_text field as follows −

{
   "input_text": "Once upon a time"
}

The output will be a JSON response with the users input and the text generated by the model as follows −

{
   "input_text": "Once upon a time",
   "generated_text": "Once upon a time, there was a king who ruled a beautiful kingdom..."
}
Advertisements