AWS AI Project: ELearning App using API Gateway, Lambda and Knowledge Base

A. First step is Creation of a Knowledge Base in AWS

  1. Create an admin user which will be separate than the root user

Got to the IAM section, select Users on left and then click on Create User

  1. Give the User Name for the admin user

  1. In the next section for Permissions, click on Attach Policies. Then select the Policy as AdministratorAccess

  1. In the next step, review and create the user.

  2. Once the user is created, click on the user and select the Security Credentials tab.

  1. Click on Enable Console Access and then note the Console URL for the new user. Then note username and password on the next screen.

  1. Console URL noted in step above need to be used for the admin user login.

  2. Create S3 Bucket to store the Knowledge Documents

Login with the admin IAM user created in AWS West region and search for S3 service.

Give the name to the S3 bucket

Once the S3 bucket is created, you will see success message as this:

  1. Go to the newly created S3 bucket and upload some files for internal knowledge base data.

  1. Go to Amazon Bedrock and click on Knowledge Bases in left menu

  1. Provide the base details for Knowledge Base

  1. Configure the Data Source S3 for the Knowledge Base

  1. Browse and select the S3 bucket that was created, and Linux commands documents were uploaded in it.

  1. Select the Chunking strategy for the documents

  1. In the next step select the embedding model and choose the vector data store

B. Use Retrieve API or RetrieveAndGenerate API with Lambda Function

Retrieve API from Amazon Bedrock queries a Knowledge Base and retrieves information from it.

RetriveAndGenerate API queries a Knowledge Base and also generates the responses based on the retrieved results.

  1. In US West 2 region, create a Lambda function. Search for Lambda service in Console and select it.

  1. Give a name to the Lambda function and select Python runtime for Lambda function

  1. Once the Lambda function is created, go to the Configuration section and click Edit.

Update the Timeout to 1 minute and 3 seconds as Lambda function call to Foundation Model can take some time

  1. Go to the Execution Role in order to attach Policy for Amazon Bedrock Full Access

Click on Attach Policies in drop down on right side

Select the Policy AmazonBedrockFullAccess and Attach it to the Role.

  1. In Lambda function, add this code for Bedrock RetrieveAndGenerate API

import json

import boto3

client_bedrock = boto3.client('bedrock-agent-runtime')

def lambda_handler(event, context):

print(event['prompt'])

user_prompt = event[‘prompt’]

client_bedrock_response = client_bedrock.retrieve_and_generate(

input={

‘text’: user_prompt

},

retrieveAndGenerateConfiguration={

‘type’: 'KNOWLEDGE_BASE',

'knowledgeBaseConfiguration': {

'knowledgeBaseId' : 'CopyFromYourKnowledgeBaseCreated',

'modelArn' : 'arn:aws:bedrock:us-west-2::foundation-model.. }

}

)

print(client_bedrock_response)

final_response = client_bedrock_response['output']['text']

return {

'statusCode': 200,

'body': final_response

}

knowledgeBaseId in Lambda code above must be the ID of the KnowledgeBase created in the previous step.

C. Create an API gateway which will access Lambda created

  1. Click on the Build option for the REST API

  1. Give the name to the API Gateway and build the API

  1. Click the button Create Method

  1. In the Create Method, select GET as Method type and add ARN of the Lambda function created in the previous step

  1. Click on Create Method and get the method created

  1. Once the Method is created, go to the Method Request section and click on Edit

  1. Add a Request Validator for the Get Method

  1. Add a URL query string parameter as ‘prompt’

  1. Click on the Integration Request for the GET Method

Add a Mapping Template for this Integration Request

  1. In the mapping template, add the prompt details

  1. Deploy the API with a Dev stage

  1. Go to the Test section of the GET method

  1. Add a query string for the TEST method and Test the API GET method which will call the Lambda and knowledge base.

C. Clean up the Knowledge Base so that we are not charged after the Test

  1. Go to the Knowledge Base, select the Knowledge Base and click on Delete

  1. Go to Amazon OpenSearch Service and delete the Collection created

Go to the Serverless → Collections section and delete the Vectorsearch DB

Once the OpenSearch service Vector collection is deleted then we will not get charges for the Knowledge Base.

This completes this tutorial on eLearning App using Lambda, API Gateway and Knowledge Base.

About me: I am an independent technical writer and if you are an organization that want to hire me then I can be contacted at .