Notebooks
A
Amazon Web Services
Question Answering Jumpstart Knn

Question Answering Jumpstart Knn

data-scienceinferencearchivedamazon-sagemaker-examplesreinforcement-learningmachine-learningawsexamplesdeep-learningsagemakerjupyter-notebooktrainingmlops

Retrieval-Augmented Generation: Question Answering based on Custom Dataset


This notebook's CI test result for us-west-2 is as follows. CI test results in other regions can be found at the end of the notebook.

This us-west-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable


Many use cases such as building a chatbot require text (text2text) generation models like BloomZ 7B1, Flan T5 XXL, and Flan T5 UL2 to respond to user questions with insightful answers. The BloomZ 7B1, Flan T5 XXL, and Flan T5 UL2 models have picked up a lot of general knowledge in training, but we often need to ingest and use a large library of more specific information.

In this notebook we will demonstrate how to use BloomZ 7B1, Flan T5 XXL, and Flan T5 UL2 to answer questions using a library of documents as a reference, by using document embeddings and retrieval. The embeddings are generated from GPT-J-6B embedding model.

This notebook serves a template such that you can easily replace the example dataset by your own to build a custom question and asnwering application.

Step 1. Deploy large language model (LLM) in SageMaker JumpStart

To better illustrate the idea, let's first deploy all the models that are required to perform the demo. You can choose either deploying all three Flan T5 XL, BloomZ 7B1, and Flan UL2 models as the large language model (LLM) to compare their model performances, or select subset of the models based on your preference. To do that, you need modify the _MODEL_CONFIG_ python dictionary defined as below.

[ ]
[ ]
[ ]

Please uncomment the entries as below if you want to deploy multiple LLM models to compare their performance.

[ ]
[ ]

Step 2. Ask a question to LLM without providing the context

To better illustrate why we need retrieval-augmented generation (RAG) based approach to solve the question and anwering problem. Let's directly ask the model a question and see how they respond.

[ ]
[ ]

You can see the generated answer is wrong or doesn't make much sense.

Step 3. Improve the answer to the same question using prompt engineering with insightful context

To better answer the question well, we provide extra contextual information, combine it with a prompt, and send it to model together with the question. Below is an example.

[ ]
[ ]

Step 4. Use RAG based approach to identify the correct documents, and use them along with prompt and question to query LLM

We plan to use document embeddings to fetch the most relevant documents in our document knowledge library and combine them with the prompt that we provide to LLM.

To achieve that, we will do following.

  • Generate embedings for each of document in the knowledge library with the GPT-J-6B embedding model.
  • Identify top K most relevant documents based on user query.
    • For a query of your interest, generate the embedding of the query using the same embedding model.
    • Search the indexes of top K most relevant documents in the embedding space using the SageMaker KNN algorithm.
    • Use the indexes to retrieve the corresponded documents.
  • Combine the retrieved documents with prompt and question and send them into LLM.

Note: The retrieved document/text should be large enough to contain enough information to answer a question; but small enough to fit into the LLM prompt -- maximum sequence length of 1024 tokens.

4.1 Deploying the model endpoint for GPT-J-6B embedding model

[ ]
[ ]

4.2. Generate embedings for each of document in the knowledge library with the GPT-J-6B embedding model.

For the purpose of the demo we will use Amazon SageMaker FAQs as knowledge library. The data are formatted in a CSV file with two columns Question and Answer. We use only the Answer column as the documents of knowledge library, from which relevant documents are retrieved based on a query.

Each row in the CSV format dataset corresponds to a textual document. We will iterate each document to get its embedding vector via the GPT-J-6B embedding models. For your purpose, you can replace the example dataset of your own to build a custom question and answering application.

First, we download the dataset from our S3 bucket to the local.

[ ]
[ ]
[ ]
[ ]

Drop the Question column since it is not used in this notebook.

[ ]
[ ]
[ ]
[ ]
[ ]

Save the embedding data for further usage.

[ ]

4.3. Index the embedding knowledge library using SageMaker KNN algorithm

The SageMaker KNN will conduct following.

  1. Start a training job to index the embedding knowledge data. The underlying algorithm used to index the data is Faiss.
  2. Start an endpoint to take the embedding of the query as input and return the top K nearest indexes of the documents.

Note. For the KNN training job, the features are N by P matrix, where N is the number of documetns in the knowledge library, P is the embedding dimension, and each row corresponds to an embedding of a document. The labels are ordinal integers starting from 0. During inference, given an embedding of query, the labels of the top K nearest documents with respect to the query are used as indexes to retrieve the corresponded textual documents.

We first upload the prepared dataset to the S3 bucket.

[ ]

We want to retrieve the top 5 most relevant documents.

[ ]
[ ]

Deploy the KNN endpoint for retrieving indexes of top K most relevant docuemnts.

[ ]

4.4 Retrieve the most relevant documents

Given the embedding of a query, we will query the endpoint to get the indexes of top K most relevant documents and use the indexes to retrieve the corresponded textual documents.

Next, the textual documents are concatenated with maximum length of MAX_SECTION_LEN. This is to make sure the context we send into the prompt contains a good enough amount of information all the while not exceeding model's capacity.

[ ]
[ ]

4.5 Combine the retrieved documents, prompt, and question to query the LLM

[ ]

Notebook CI Test Results

This notebook was tested in multiple regions. The test results are as follows, except for us-west-2 which is shown at the top of the notebook.

This us-east-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This us-east-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This us-west-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ca-central-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This sa-east-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-west-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-west-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-west-3 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-central-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-north-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-southeast-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-southeast-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-northeast-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-northeast-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-south-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable