Build Your Own Knowledge Base with Multilingual Q&A Powered by Generative AI

Use Amazon Kendra, Amazon Translate, Amazon Comprehend and Amazon SageMaker JumpStart to build a multilingual knowledge base that can summarize search results.

Elizabeth Fuentes
Amazon Employee
Published Aug 21, 2023
Last Modified Jun 26, 2024
Organizations often accumulate a wide range of documents, including project documentation, manuals, tenders, Salesforce data, code repositories, and more. Locating specific documents and then conducting searches within them amid this vast amount of information can be a tedious. What's more, once you find the desired document, it may be lengthy, and you might prefer a summary of its content.
Web applications that summarize information might seem like a simple solution, but using them could mean sharing your organization's sensitive information!
Luckily, there are better solutions. In this tutorial, we will build a comprehensive knowledge base using multiple sources. With this knowledge base you can seek answers to your queries and receive concise summaries along with links for further study. To ensure accessibility, we will facilitate this process through a convenient question-and-answer format available in multiple languages.
About
βœ… AWS LevelIntermediate - 200
⏱ Time to complete30 minutes
πŸ’° Cost to complete1.56 USD X 1 hour.
🧩 Prerequisites- AWS Account
- Foundational knowledge of Python
πŸ“’ FeedbackAny feedback, issues, or just a πŸ‘ / πŸ‘Ž ?
⏰ Last Updated2023-08-21

What You Will Learn

  • How to set up an intelligent search service powered by machine learning with Amazon Kendra.
  • How to utilize pretrained open-source Generative AI Large Language Models (LLMs).
  • How to use Artificial Intelligence service to detect the dominant language in texts.
  • How to use Artificial Intelligence service to translate text.

Solution Overview

We are going to build the solution in Amazon SageMaker Studio, where we can interact with AWS services from the same account without the need for additional credentials or security configurations, using the SageMaker Identity and Access Management Execution Role.
Architecture
In Fig 1 you can see what the solution consists of:
  1. The user asks the question.
  2. The language in which the query is made is detected using Amazon Comprehend.
  3. Using Amazon Translate, the question is translated into the data soruce language.
  4. The intelligent knowledge base is consulted.
  5. Use Amazon Kendra's answer and user question to ask the LLM for a summarized and improved answer.
  6. The answer is translated into the language of the question.
  7. Provide the summary answer and the source where it can be expanded.
We will build it in five parts:
  • Part 1 - Build the smart database with Amazon Kendra, using the sample data.πŸ€–
  • Part 2 - Queries to an index in Amazon Kendra.
  • Part 3 - Add multilingual features πŸ€–πŸŒŽ: detect the language of the text and translate it.
  • Part 4 - Create ENDPOINT to invoke Generative AI Large Language Model (LLM) πŸš€.
  • Part 5 - Summarize answer using the LLM.
  • Part 6 - 🚨Delete resources🚨.
Let’s get started!

Part 1 - Build the Smart Database with Amazon Kendra πŸ€–

Kendra is an intelligent search service powered by machine learning, where you can add, update, or delete automatically synchronize multiples data source, and also index web pages by providing the URLs to crawling.
First you need to create a Kendra Index, to hold the contents of your documents and structure them in a way to make the documents searchable. Follow the steps to create a Kendra Index in the console here.
create a kendra index
Once the Index is Active, add a data source to an Index (Fig. 3), select Add data source and then select Add dataset, add a name and select English(en) in Language.
add a data source to an Index
At the end of the data synchronization, you will have the knowledge base ready for queries.
Here you can see more ways to upload sources to Kendra.
🚨Note: You can get started for free with the Amazon Kendra Developer Edition, that provides free usage of up to 750 hours for the first 30 days, check pricing here.

Part 2 - Searching an Amanzon Kendra Index

To search an Amazon Kendra index, you use the Retrieve API and it returns information about the indexed documents of data sources. You can alternatively use the Query API. However, the Query API only returns excerpt passages of up to 100 token words, whereas with the Retrieve API, you can retrieve longer passages of up to 200 token words.
Amazon Kendra utilizes various factors to determine the most relevant documents based on the search terms entered. These factors include the text/body of the document, document title, searchable custom text fields, and other relevant fields.
Additionally, filters can be applied to the search to narrow down the results, such as filtering documents based on a specific custom field like "department" (e.g., returning only documents from the "legal" department). For more information, see Custom fields or attributes.
You can make search the Amazon Kendra Index in several ways.

Searching with the Console

Go to the navigation panel on the left, choose the Search indexed content option, then enter a query in the text box and press enter (Fig. 4).
Search in a Kendra Index
To search with AWS SDK for Python(Boto3) use this code:

Search in Other Ways

You can also search with AWS SDK for Java and Postman.

Part 3 - Add Multilingual Features πŸ€–πŸŒŽ: Detect the Language of the Text and Translate It

In this segment, you will use two AI/ML services that you can use with an API call:
The TranslateText API needs the following parameters:
  • Text (string): The text to translate.
  • SourceLanguageCode (string): One of the supported language codes for the source text, if you specify auto, Amazon Translate will call Amazon Comprehend to determine the source language.
  • TargetLanguageCode (string): One of the supported language codes for the target text.
This is the function we will use to perform the translation:
If you want to know more about these services as API calls, you can visit this blog: All the things that Comprehend, Rekognition, Textract, Polly, Transcribe, and Others Do.
🚨Note: Amazon Translate and Amazon Comprehend have Free Tier for up 12 months. Check pricing here and here.
To know the language code for documents in the data source in Amazon Kendra, you use Describe Data Source API:
πŸ₯³ The code of the multilingual Q&A intelligent knowledge base is:
Amazon Kendra delivers a list of answers, which could be big (Fig. 5). Wouldn't a summarized result be better?
Amazon Kendra answer result in spanish

Part 4 - Create ENDPOINT to Invoke Generative AI Large Language Models (LLMs) πŸš€

In this part you are going to use Amazon SageMaker JumpStart, which provides pre-trained, open-source models for a wide range of problem types (including our summarization problem) to help you get started with machine learning. The best part is that you can also access models using the SageMaker Python SDK.
To summarize, you will use Flan UL2 fundamental model, a Text2Text Generation model based on the FLAN-T5 architecture, a popular open-source LLM, for:
  • Text summarization
  • Common sense reasoning / natural language inference
  • Question and answering
  • Sentence / sentiment classification
  • Translation (at the time of writing this blog, between fewer languages than Amazon Translate)
  • Pronoun resolution
πŸ‘·πŸ»β€β™€οΈπŸ§° Now let's start working with it:
1. Open the Amazon Sagemaker console
Amazon Sagemaker console
2. Find JumpStart on the left-hand navigation panel and choose Foundation models.
JumpStart Foundation models
3. Search for a Flan UL2 model, and then click on View model.
Flan UL2 search
4. Open notebook in Studio
Flan UL2 search
5. Create a Sagemaker Domain using Quick setup, this takes a few minutes⏳... or Select domain and user profile if you already have one created.
Create a Sagemaker Domain
Select domain and user profile
6. Follow the steps in jupyter notebook, explore it, and wait for me in step 5
Jupyter notebook
In the jupyter notebook you can explore the functionalities of the FLAN-T5 model.
Go to part 3 in jupyter notebook to deploy a sagemaker endpoint. This is the call to do real-time inference to ML model as an API call, using Boto3 and AWS credentials.
You can get the Sagemaker Endpoint in two ways:
  • Console:
Find Inference on the left-hand navigation panel and choose Endpoints.
🚨Note: You have to be careful, because while the endpoint is active, you will be billing. Check pricing here.

Part 5 - Summarize Answers Using the LLM

In step 5 on Jupyter notebook, you can see the advanced parameters to control the generated text while performing inference definition that this model supports.
Let's define the parameters as follows:
Where:
  • num_return_sequences: corresponds to the number of answers per query that the LLM will deliver.
  • max_length: the maximum number of tokens that the model will generate.
  • top_k: limit random sampling to choose k value of sample with the highest probabilities.
  • top_p: Select an output using the random-weighted strategy with the top-ranked consecutive results by probability and with a cumulative probability <= p.
  • do_sample: Set True because Flan-T5 model use sampling technique.
To get inferences from the model hosted at the specified endpoint you need to use the InvokeEndpoint API from the Amazon SageMaker Runtime, you do it with the following function:
To make the response readable to humans, use the following function:
For the LLM to generate an improved answer, you provide a prompt (or text_inputs in the code) composed of Amazon Kendra document part and the user's question, so that the model understands the context.
A good prompt provides a good result. If you want to know more about how to improve the prompt, I leave you this Prompt Engineering Guide.
Play with the text_inputs and discover the best one according to your needs.
Bring all the code together and Build your own knowledge base with multilingual Q&A powered by Generative AI πŸ₯³.
In Fig 6 you can see 3 results of the summarized text. This is because you set num_return_sequences parameter to 3:
Summarized results

Part 6 - 🚨 Delete Resources 🚨

If your intention was to create to learn and you are not going to continue using the services, you must eliminate them so as not to overspend.

Delete Amazon Kendra Index

  1. In the navigation panel, choose Indexes, and then choose the index to delete.
  2. Choose Delete to delete the selected index.

Delete Amazon Sagemaker Model and Endpoint

In the notebook in Sagemaker Studio where you deploy an Endpoint, execute the following lines:

Conclusion

Thank you for joining me on this journey, where you gather all the code and build your own knowledge base with multilingual Q&A powered by generative AI. This database allows you to make inquiries in any language, receiving summarized responses in the desired language, all prioritizing data privacy.
You can improve the LLM response by applying Prompt Engineering Techniques. An interesting one is Self-Consistency, that is, answering in the same way we did here but for the most relevant documents from Amazon Kendra, and then use the LLM to answer based on all responses (most consistent answer). Just try by yourself!
Then you can place the code in an AWS Lambda Function and, to improve the performance of this application, you can introduce a caching mechanism by incorporating an Amazon DynamoDB table. In this table, you can store the responses obtained from Amazon Kendra, utilizing the response as the partition key and the summary as the sort key. By implementing this approach, you can first consult the table before generating the summary, thereby delivering faster responses and optimizing the overall user experience.
Some links for you to continue learning:

Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.

Comments