Generate and Use Context for an Operations Chatbot using Amazon SageMaker, Amazon Bedrock, Python Notebooks, and PowerCLI

3 minute read
Content level: Intermediate
2

With the GA release of Amazon Bedrock, I thought it would be interesting to see how we can use foundational models to provide an operations style chatbot using data collected by PowerCLI as context in just a few steps.

Task A customer wants to export data about their VMware environment and use it as context for an operations style chatbot.

Assumptions

  1. This article assumes you have already installed VMware PowerCLI and have connected to your VMware Cloud on AWS vCenter with Connect-VIServer
  2. This article assumes you have already installed the AWS CLI and configured it to use your AWS account. AWS CLI Setup
  3. This article assumes you have already created and setup an Amazon SageMaker domain and S3 bucket for your data. Onboard to Amazon SageMaker Domain using Quick setup

Collect Data and Upload to S3

You can choose whatever data you want, this is just a small example of collecting a subset of data about the VMs in the environment (CPU, Memory, Disk, etc.).

$vms = get-vm
$date = '{0:yyyyMMddmmss}' -f (get-Date)
$filename = "result-" + $date + ".csv"
$Output = foreach ($VM in $VMs){
	Get-VM $VM | select @{N="DateTime";E={ $date}}, Name, @{N=”FolderName”;E={ $_.Folder.Name}}, NumCpu, MemoryGB, @{N=”CPUUsage”;E={ $_.ExtensionData.Summary.QuickStats.OverallCpuUsage}}, @{N=”MemoryUsage”;E={$_.ExtensionData.Summary.QuickStats.GuestMemoryUsage}}, ProvisionedSpaceGB, UsedSpaceGB, PowerState, VMHost, @{N=”Datastore”;E={$_.ExtensionData.Config.DatastoreUrl.Name}}, @{N=”Network”;E={$_.Guest.Nics[0]}}, @{N=”IPAddress”;E={$_.Guest.IPAddress[0]}}, @{N=”DNSName”;E={$_.ExtensionData.Guest.Hostname}}
}
$Output | Export-Csv $filename -NoTypeInformation
aws s3 sync s3://your_S3_bucket/S3_bucket_folder/ .

Now that we have our data that will be used for the chatbot context, we can go to the following link to prepare our environment: Amazon Bedrock Workshop You will want to follow the steps in the Prerequisites and Environment Setup before proceeding. Once you have completed the prerequisites, we will make some changes to the 00_Chatbot_AI21 notebook.

Log in to the AWS Management Console and navigate to your user in the SageMaker domain and launch Studio:

Launch Studio

Changes

In cell 2, set your region and role to the ones that you will be using (the role that was given the proper permissions during setup).

os.environ["AWS_DEFAULT_REGION"] = "us-east-1"  # E.g. "us-east-1"
# os.environ["AWS_PROFILE"] = "<YOUR_PROFILE>"
os.environ["BEDROCK_ASSUME_ROLE"] = "arn:aws:iam::ACCOUNT:role/service-role/ROLE_THAT_WAS_CREATED"  # E.g. "arn:aws:..."

In cell 8, I made the following changes to provide a bit more readability to the chat. This addition will allow you to resize the input box that the chatbot uses:

self.name = ipw.Text(description="You:",  placeholder='q to quit')

Short-length input box

to

self.name = ipw.Text(description="You:", layout=ipw.Layout(width='50%', height='80px'), placeholder='q to quit')

Extended-length input box

In cell 13, we make the following changes to use our uploaded data as the context instead of the default data:

s3_path = "s3://jumpstart-cache-prod-us-east-2/training-datasets/Amazon_SageMaker_FAQs/Amazon_SageMaker_FAQs.csv"
!aws s3 cp $s3_path ./rag_data/Amazon_SageMaker_FAQs.csv

loader = CSVLoader("./rag_data/Amazon_SageMaker_FAQs.csv") # --- > 219 docs with 400 chars

to

s3_path = f"s3://your_S3_bucket/S3_bucket_folder/uploaded_file.csv"
!aws s3 cp $s3_path ./rag_data/uploaded_file.csv

loader = CSVLoader("./rag_data/uploaded_file.csv") # --- > 219 docs with 400 chars

Once we have made these changes and run the cells, you can begin to use the Chatbot (the typo is on purpose to show that the bot can adjust):

Chatbot in use.

While this is a very rudimentary chatbot that would require more time to develop and customize for a production workload, we have demonstrated how quickly we can leverage Amazon Bedrock foundational models to provide insights into custom datasets.