How to Build a Contextual Chatbot for Financial Services using Amazon Sage Maker JumpStart, Llama 2
Tags: huggingface, sagemaker, chatbot, financial
Published: 2024-01-17 21:21:00

<!-- wp:paragraph -->
<p>You might have heard about FinServ – Financial Service Industry. The FinServ industry has different and unique generative AI requirements. These requirements work well with domain specific data, like for data security, industry compliance standards and regulatory controls. Many customers are trying to figure out the best ML model that could fit into their business use cases. Remember that not every ML model is desirable, you have to mould it according to your needs and requirements with the help of<a href="https://platform.openai.com/docs/guides/fine-tuning"> fine-tuning</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As far as FinServ is concerned, Amazon SageMaker JumpStart is the most appropriate pick for the generative AI use cases. Why? Well, it fulfils all the necessary data requirements of FinServ. Apart from this all, we will be discussing RAG (Retrieval Augmented Generation) and its usage with LLMs in SageMaker JumpStart.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":1} -->
<h1 class="wp-block-heading"><strong>What is SageMaker?</strong></h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>SageMaker is an AWS service that makes use of a set of tools to enable low-cost and high performing machine learning use cases. It is used for building, training and deploying ML models at scale using various tools like debuggers, MLOps, pipelines, profilers etc.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Using SageMaker JumpStart, the ML practitioners can choose models for use cases like</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Content writing</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Image generation</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Code generation</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Copywriting</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Question answering</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Summarization</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Classification</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Retrieving information</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>The flow goes like this; ML practitioners have foundation models, they deploy these models to the SageMaker instances (specifically from a network isolated environment) and then perform customization on the models for training and deployment.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading"><strong>Advantages of SageMaker</strong></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>You will get the following advantages by using the Amazon SageMaker.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><strong>More Customization Control</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>SageMaker makes sure that the people don’t find any difficulty while following the procedure. So it provides example notebooks and a daily post for step-by-step guidance regarding domain adaptation of foundation models. These resources can be used for fine-tuning or to build RAG-applications.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><strong>More Data Security</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>SageMaker ensures the data security by deploying models in network isolation providing a single-tenancy endpoint. Also you can ensure the individual security requirements by managing control access through a private model hub capability.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><strong>Controls and Compliances</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If we were to talk about the core feature of Sage Maker then it would be the compliance with standards like HIPAA BAA, SOC123, and PCI etc. It ensures the alignment with the regulatory landscape specifically of the financial sector.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><strong>Broad model choices</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>With SageMaker you will have the option to choose models that rank at the top. Some of such models are:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Llama 2</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Falcon 40B</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Al21 J2 Ultra</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>AI21 Summarize</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2 class="wp-block-heading"><strong>Role in Chatbot Development</strong></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Well, in chatbot development the SageMaker Jumpstart plays a very important role. Some of the features include:</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><strong>Accelerated Model Training</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>SageMaker provides pre-trained models so that the developers don’t waste their time on training the model for the chatbot development.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><strong>Access to Industry Practices</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Developers can take advantage from the ML practitioners who have contributed broadly in the pre-built models.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><strong>Reduced Complexity</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Due to the presence of pre-built models, the chatbot development process has been simplified. The developers can now focus on the integration part rather than the training part.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><strong>End-to-End Solutions</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>SageMaker provides end-to-end solutions for machine learning model deployment, development and management.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":1} -->
<h1 class="wp-block-heading"><strong>Llama 2 – Overview and Key Features</strong></h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><a href="https://ai.meta.com/llama/">Llama 2</a> is a tool as well as a platform that assists data scientists and developers to build and deploy AI solutions. It provides advanced capabilities and features to streamline the process of chatbot development. Despite its older version, this version offers everything twice in terms of scalability, flexibility and efficiency. These all features make Llama 2 a valuable tool for creating chatbot interactions.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading"><strong>How Llama 2 Enhances Chatbot Interactions?</strong></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>There are many features included in Llama 2 which enhance the chatbot interactions.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><strong>Better Understanding of User Intent</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Llama 2 uses advanced NLP capabilities which help in understanding the user query better than before.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><strong>Contextual Responses</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>It also enables chatbot to maintain context in multiple turns</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><strong>Multi Modal Interactions</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Llama 2 enables multi-modal interactions that actually allows a chatbot to generate a response to a text, visual inputs and speech combination.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading"><strong>Amazon OpenSearch Server less</strong></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Amazon offers an OpenSearch Service, to have its on-demand server less configuration we use Amazon OpenSearch Server less. All the complexities associated with OpenSearch clusters will be removed by using server less. The operational complexities include provisioning, tuning and configuring the OpenSearch clusters.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading"><strong>Vector Engine</strong></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The vector engine as clear from its name is a simple vector storage for Amazon OpenSearch server less that provides search capability to the developers to build various AI and ML models without managing the vector database.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading"><strong>Advantages for Search and Analysis</strong></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Following are the search and analysis advantages that you will get by using a vector engine.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Faster Similarity Search</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Efficient and convenient handling of high-dimensional data</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>More Personalization Control</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Parallel Processing</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Real Time Analytics</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Enhancements in machine learning models</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Data Representation Flexibility</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2 class="wp-block-heading"><strong>What is RAG?</strong></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The full abbreviation of<a href="https://docs.aws.amazon.com/sagemaker/latest/dg/jumpstart-foundation-models-customize-rag.html"> RAG</a> is Retrieval Augmented Generation. It is typically a framework whose job is to improve the text generation quality. It does this by combining an Information Retrieval System with a Large Language Model. Another important component, knowledge base is used by the IR system and the LLM generated text to retrieve the relevant data. Once the information is successfully retrieved, the LLM’s input is augmented which in return improves the accuracy of the model generated text.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading"><strong>Advantages</strong></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>RAG has the following advantages.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Used to perform text generation tasks</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Used to perform the summarization of the question answers</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>It improves the quality of text generation models</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2 class="wp-block-heading"><strong>RAG WorkFlow</strong></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>RAG operates in the following manner:</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><strong>Data Sources</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>RAG collects information from various data sources which includes documents or APIs etc.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><strong>Data Formatting</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The data fetched from documents and the user query both are converted into a common format to perform a comparison.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><strong>Embedding</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>All the data is converted into their relevant numerical representation known as embedding.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><strong>Search</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The relevant embedding of user query and the embedding of document’s collection are compared to run a similarity search.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><strong>Context Enhancement</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The context is enhanced by joining the relevant text with the user’s original prompt.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><strong>LLM Processing</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now the resultant context is provided as an input to the LLM which in return produces precise output.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The diagram below depicts the proper work flow of RAG.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":33123,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image size-full"><img src="https://sudoconsultants.com/wp-content/uploads/2024/01/RAG-Workflow-edited.png" alt="" class="wp-image-33123"/></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2 class="wp-block-heading"><strong>Requirements</strong></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Use SageMaker JumpStart GPT-J-6B embedding model. It will be used to generate relevant embedding for PDF documents in the Amazon S3 upload directory.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>To identify the relevant document, perform the following steps.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Use the above mentioned model to generate the embedding for user’s query</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Now use OpenSearch Server less (with the vector engine feature) to search for the top most relevant document indexes</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Retrieve the required document</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Combine the user’s prompt with the retrieved document and pass it to the SageMaker in order for it to generate response</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2 class="wp-block-heading"><strong>Prerequisites</strong></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Following prerequisites will be needed to build a contextual chatbot.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>AWS Account with proper IAM permissions i.e. IAM Identity and Access Management</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Setup your Amazon SageMaker Studio for accessing domain and user by following the proper<a href="https://docs.aws.amazon.com/sagemaker/latest/dg/onboard-quick-start.html"> instructions</a></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Amazon OpenSearch Server less collection</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>SageMaker execution role having access to OpenSearch Server less</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2 class="wp-block-heading"><strong>Building the Chatbot</strong></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Here is the <a href="https://github.com/aws-samples/amazon-sagemaker-generativeai/blob/main/finserv-llama2-chatbot/finserv-llama2-aoss-chatbot.ipynb">complete github code for your chatbot</a> ,where you just have to make changes as per your aws credentials and region.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can directly import this link in<a href="https://studiolab.sagemaker.aws/"> AWS Sagemaker </a>to make changes to the file accordingly or you can download the github file and upload in jupyter notebook of sagemaker.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":33118,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://sudoconsultants.com/wp-content/uploads/2024/01/image-6-edited.png" alt="" class="wp-image-33118"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>You can chose the python kernel or sage-maker distribution python based on your preferences</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":33120,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://sudoconsultants.com/wp-content/uploads/2024/01/image-7-edited.png" alt="" class="wp-image-33120"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p> </p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><strong>Integration and Deployment using AWS</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Following AWS services are required for the integration and deployment of chatbot.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>SageMaker</strong> – For building, training and deploying ML models</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Amazon S3</strong> – For storing documents, datasets and scalable object</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>OpenSearch Service</strong> – Managed service for elastic search</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Amazon API Gateway</strong> – For creating, deploying and managing APIs</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>AWS Lambda</strong> – For running code without managing servers</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>AWS Identity and Access Management</strong> – For a secure access control to AWS resources</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It is really important to keep a check and balance on the chatbot once it has been deployed. Real-time monitoring can help a lot in this cause. It will provide an immediate bug report so we can fix it as soon as we are told.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><strong>Security and Compliance</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>You can ensure data security and privacy through encryption, by adjusting the access controls, conducting regular security audits and using data masking techniques. All these things will definitely lead to a more secure environment.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading"><strong>Conclusion</strong></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In a nutshell, we learned how to build a contextual chatbot using the RAG approach. It was used to provide domain specific content to LLMs. Then we used a bunch of advanced services and tools like vector DB, SageMaker and OpenSearch server less etc. to create an enhanced version of it. This will allow users to bring custom data and not only the domain specific data.</p>
<!-- /wp:paragraph -->