DEV Community

Sunil Yaduvanshi
Sunil Yaduvanshi

Posted on

Creating Your Own Chatbot with Amazon Bedrock, API Gateway, and Lambda AWS

In this tutorial, we'll walk you through the steps to create a simple yet powerful chatbot using Amazon Bedrock, API Gateway, and AWS Lambda. We'll also demonstrate how to test your chatbot using Postman.
Step 1: Set Up Amazon Bedrock
Amazon Bedrock is a managed service that allows you to build, customize, and deploy machine learning models for a variety of applications. In this guide, we'll use Bedrock to leverage a foundation model for generating responses to user queries.

AWS Bedrock Console
1.Access Amazon Bedrock Console:

  • Log in to your AWS account and navigate to the Amazon Bedrock console.

2.Create a New Model:
List of Available AI Models

  • Choose a pre-built foundation model provided by AWS (e.g., LLAMA 3 8B model) to generate responses.
  • Configure the model according to your requirements, adjusting parameters like temperature, max tokens, etc.
  • Deploy the model and note down the model endpoint URL.

Available AI Model
Step 2: Create a Lambda Function
AWS Lambda allows you to run code in response to events and manage computing resources automatically. We'll use Lambda to process the input from the user and interact with the Bedrock model.

Lambda Console
1.Create a Lambda Function:

  • Go to the AWS Lambda console and click "Create function."
  • Choose "Author from scratch" and name your function (e.g., GenAILlamaTest).
  • Select a runtime (e.g., Python 3.8) and click "Create function."

2.Write the Lambda Code:

  • In the function's code editor, write a script to handle requests, send input to the Bedrock model, and return the model's response. Here’s a basic Python example:
import boto3
import botocore.config
import json

from datetime import datetime

def blog_generate_using_bedrock(blogtopic:str)-> str:
    prompt=f"""<s>[INST]Human: {blogtopic}
    Assistant:[/INST]
    """

    body={
        "prompt":prompt,
        "max_gen_len":512,
        "temperature":0.5,
        "top_p":0.9
    }

    try:
        bedrock=boto3.client("bedrock-runtime",region_name="us-east-1",
                             config=botocore.config.Config(read_timeout=300,retries={'max_attempts':3}))
        response=bedrock.invoke_model(body=json.dumps(body),modelId="your-ai-model-id")

        response_content=response.get('body').read()
        response_data=json.loads(response_content)
        print(response_data)
        blog_details=response_data['generation']
        return blog_details
    except Exception as e:
        print(f"Error generating the blog:{e}")
        return ""


def lambda_handler(event, context):
    print(event)
    blogtopic=event.get('blog_topic')
    print(blogtopic)

    generate_blog=blog_generate_using_bedrock(blogtopic=blogtopic)
    print(generate_blog)

    return{
        'statusCode':200,
        'body': generate_blog
    }

Enter fullscreen mode Exit fullscreen mode

3.Add Necessary Permissions:

  • Ensure your Lambda function has the necessary IAM role permissions to invoke the Bedrock model In my case i have provided AWSBedrock Full Access.

4.Deploy the Function:

  • Save and deploy your Lambda function. Step 3: Set Up API Gateway API Gateway will serve as the frontend for your Lambda function, allowing you to create a RESTful API that users can interact with.

1. Create a New API:

  • In the API Gateway console, click "Create API" and choose REST API.
  • Name your API (e.g., gen-ai-test) and click "Create API."

API Gateway Console

2. Create a New Resource and Method:

  • Under the Resources section
  • Add a POST method to the / resource.
  • Set the POST method to trigger your Lambda function.

3. Configure the Method Request:

  • Under the POST method, configure the Method Request to accept request body called blog_topic.

4. Deploy the API:

  • Deploy your API by creating a new deployment stage (e.g., test).
  • Note down the API endpoint URL. Step 4: Test the Chatbot with Postman Postman is a great tool for testing APIs. We'll use it to send requests to our API Gateway endpoint and verify the chatbot's responses.

1. Open Postman:

2. Create a New POST Request:

  • In Postman, create a new POST request.
  • Enter your API Gateway endpoint URL, For example:

    https://your-api-id.execute-api.region.amazonaws.com/test/
    

3. Add Request Body Parameter:

  • Add inside request body parameter named blog_topic with the value of your input (e.g., could you write a node js lambda function to put item in S3 bucket).

4. Send the Request:

  • Click "Send" to send the request to your API Gateway.
  • The response from the Bedrock model should appear in the response body.

PostMan Console
Once i filter it it looks like this

exports.handler = async (event) => {
    const AWS = require('aws-sdk');
    const s3 = new AWS.S3({ region: 'your-region' });
    const params = { Bucket: 'your-bucket-name', Key: 'your-item-key', Body: 'Hello World!' };
    try {
        await s3.putObject(params).promise();
        console.log('Item uploaded to S3 bucket');
        return { statusCode: 200 };
    } catch (err) {
        console.log('Error uploading item to S3 bucket:', err);
        return { statusCode: 500 };
    }
};

Enter fullscreen mode Exit fullscreen mode

Another Request:

PostMan Console
Conclusion
Congratulations! You’ve successfully created and tested a simple chatbot using Amazon Bedrock, API Gateway, Lambda, and Postman. This is just a starting point—feel free to expand the chatbot's capabilities, customize the model, or integrate it into a more complex application, Similarly You can create AI Image Generator , AI Video Generator etc using AWS Servicess.

Top comments (0)