Imagine harnessing the power of advanced language models to understand and answer your customers' queries. amazon Bedrock, a fully managed service that provides access to such models, makes this possible. Fitting large language models (LLMs) on domain-specific data powers tasks like answering product questions or generating relevant content.
In this post, we show how amazon Bedrock and amazon SageMaker Canvas, a no-code ai suite, enable business users without deep technical knowledge to tune and deploy LLM. You can transform customer engagement using data sets like product questions and answers in just a few clicks using the amazon Bedrock and amazon SageMaker JumpStart models.
Solution Overview
The following diagram illustrates this architecture.
In the following sections, we show you how to tune a model by preparing your data set, creating a new model, importing the data set, and selecting a base model. We also demonstrate how to analyze and test the model and then deploy it through amazon Bedrock.
Previous requirements
New users require an AWS account and an AWS Identity and Access Management (IAM) role with access to SageMaker, amazon Bedrock, and amazon Simple Storage Service (amazon S3).
To follow this post, complete the previous steps to create a domain and enable access to amazon Bedrock models:
- Create a SageMaker domain.
- On the domain details page, view user profiles.
- Choose Launch for your profile, and choose Canvas.
- Confirm that your SageMaker IAM role and domain roles have the necessary permissions and trusts.
- In the amazon Bedrock console, choose Access to the model in the navigation panel.
- Choose Manage model access.
- Select amazon to enable the amazon Titan model.
Prepare your data set
Complete the following steps to prepare your data set:
- Download the following CSV Dataset of Question and Answer Pairs.
- Confirm that your data set does not have formatting issues.
- Copy the data to a new sheet and delete the original.
Create a new model
SageMaker Canvas allows simultaneous tuning of multiple models, allowing you to compare and choose the best one from a leaderboard after tuning. However, this post focuses on amazon Titan Text G1-Express LLM. Complete the following steps to create your model:
- On the SageMaker canvas, choose my models in the navigation panel.
- Choose New model.
- For Model nameenter a name (for example,
MyModel
). - For Type of problemselect Adjust the base model.
- Choose Create.
The next step is to import your dataset into SageMaker Canvas:
- Create a data set named QA-Pairs.
- Upload the prepared CSV file or select it from an S3 bucket.
- Choose the data set, then choose Select data set.
Select a base model
After loading your data set, select a base model and fit it with your data set. Complete the following steps:
- About him Fine tune tab, in the Select basic models menu¸ select titan express.
- For Select input columnchoose ask.
- For Select output columnchoose answer.
- Choose Fine tune.
Allow 2 to 5 hours for SageMaker to finish fine-tuning your models.
Analyze the model
When the adjustment is complete, you will be able to see statistics about your new model, including:
- Training loss – The penalty for each error in predicting the next word during training. Lower values indicate better performance.
- Training Perplexity – A measure of the model's surprise when encountering text during training. Less perplexity suggests greater confidence in the model.
- Loss of validation and perplexity in validation. – Similar to training metrics, but measured during the validation stage.
For a detailed report on how your custom model performs across multiple dimensions, such as toxicity and precision, choose Generate evaluation report. Then select Download report.
Canvas offers a Python Jupyter notebook that details your tuning work, alleviating concerns about vendor lock-in associated with no-code tools and allowing details to be shared with data science teams for further validation and implementation.
If you selected multiple base models to create custom models from your data set, see the Model Leaderboard compare them on dimensions such as loss and perplexity.
Try the models
You now have access to custom models that can be tested in SageMaker Canvas. Complete the following steps to test the models:
- Choose Test on ready-to-use models and wait 15-30 minutes for your test endpoint to be deployed.
This test endpoint will only remain active for 2 hours to avoid unwanted costs.
When the deployment is complete, you will be redirected to the SageMaker Canvas playground, with your pre-selected model.
- Choose Compare and select the base model used for your custom model.
- Enter a phrase directly from your training data set, to ensure that the custom model at least answers that question best.
For this example, we enter the question, “Who developed the lie-detecting algorithm Fraudoscope?”
The fitted model answered correctly:
“The lie-detecting algorithm Fraudoscope was developed by Tselina Data Lab.”
amazon Titan responded incorrectly and in detail. However, it must be recognized that the model raised important ethical concerns and limitations of facial recognition technologies in general:
Let's ask a question about an NVIDIA chip, which powers amazon Elastic Compute Cloud (amazon EC2) P4d instances: “How much memory in an A100?”
Again, the custom model not only gets the most correct answer, but also answers as succinctly as you would want from a Q&A bot:
“An A100 GPU provides up to 40 GB of high-speed HBM2 memory.”
amazon Titan's answer is incorrect:
Deploy the model through amazon Bedrock
For production use, especially if you are considering providing access to dozens or even thousands of employees by embedding the model in an application, you can deploy the models as API endpoints. Complete the following steps to deploy your model:
- In the amazon Bedrock console, choose Foundation models in the navigation pane and then choose Custom models.
- Locate the model with the prefix Canvas- with amazon Titan as the source.
Alternatively, you can use the AWS Command Line Interface (AWS CLI): aws bedrock list-custom-models
- Take note of the
modelArn
which you will use in the next step, and themodelName
or save them directly as variables:
To start using your model, you must provision performance.
- In the amazon Bedrock console, choose Purchasing Provisioned Performance.
- Name it, set 1 model unit, no commitment period.
- Confirm the purchase.
Alternatively, you can use the AWS CLI:
Or, if you saved the values as variables in the previous step, use the following code:
After about five minutes, the model state changes from Creating to In service.
If you are using the AWS CLI, you can view the status through aws bedrock list-provisioned-model-throughputs
.
Use the model
You can access your optimized LLM through the amazon Bedrock console, API, CLI, or SDK.
In Chat Playground, choose the Tuned Models category, select your Canvas prefix model, and provisioned performance.
Enrich your existing Software as a Service (SaaS), software platforms, web portals or mobile applications with your optimized LLM using the API or SDKs. These allow you to send directions to the amazon Bedrock endpoint using your preferred programming language.
The answer demonstrates the model's personalized ability to answer these types of questions:
“The lie-detecting algorithm Fraudoscope was developed by Tselina Data Lab.”
This improves amazon Titan's response before tuning:
“Marston Morse developed the lie-detecting algorithm Fraudoscope.”
For a complete example of invoking models in amazon Bedrock, see the following GitHub repository. This repository provides a ready-to-use codebase that allows you to experiment with various LLMs and implement a versatile chatbot architecture within your AWS account. You now have the skills to use this with your custom model.
Another repository that can spark your imagination is amazon-bedrock-samples” target=”_blank” rel=”noopener”>amazon bedrock sampleswhich can help you get started with other use cases.
Conclusion
In this post, we show you how to tune an LLM to best fit your business needs, implement your custom model as an amazon Bedrock API endpoint, and use that endpoint in your application code. This opened up the power of the custom language model to a broader set of people within your company.
Although we used examples based on a sample data set, this post showed the capabilities of these tools and their potential applications in real-world scenarios. The process is simple and applicable to various data sets, such as your organization's FAQs, as long as they are in CSV format.
Take what you've learned and start thinking about ways to use custom ai models in your organization. For more inspiration, see Overcoming Common Contact Center Challenges with Generative ai and amazon SageMaker Canvas and AWS re:Invent 2023: New LLM capabilities in amazon SageMaker Canvas, with Bain & Company (AIM363).
About the authors
Yann Stoneman is a solutions architect at AWS focused on machine learning and serverless application development. With a background in software engineering and a combination of arts and technology education from Juilliard and Columbia, Yann brings a creative approach to ai challenges. He actively shares his experience through his YouTube channel, blog posts, and presentations.
Davide Gallitelli is a specialized solutions architect for ai/ML in the EMEA region. He is based in Brussels and works closely with clients across the Benelux. He has been a developer since a young age and started coding at the age of 7. He started learning ai/ML in his later years of college and has fallen in love with it ever since.