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.
Part 1 - Build the Smart Database with Amazon Kendra 🤖
Part 2 - Searching an Amanzon Kendra Index
Part 3 - Add Multilingual Features 🤖🌎: Detect the Language of the Text and Translate It
Part 4 - Create ENDPOINT to Invoke Generative AI Large Language Models (LLMs) 🚀
Part 5 - Summarize Answers Using the LLM
About | ||
---|---|---|
✅ AWS Level | Intermediate - 200 | |
⏱ Time to complete | 30 minutes | |
💰 Cost to complete | 1.56 USD X 1 hour. | |
🧩 Prerequisites | - AWS Account - Foundational knowledge of Python | |
📢 Feedback | Any feedback, issues, or just a 👍 / 👎 ? | |
⏰ Last Updated | 2023-08-21 |
- 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.
- The user asks the question.
- The language in which the query is made is detected using Amazon Comprehend.
- Using Amazon Translate, the question is translated into the data soruce language.
- The intelligent knowledge base is consulted.
- Use Amazon Kendra's answer and user question to ask the LLM for a summarized and improved answer.
- The answer is translated into the language of the question.
- Provide the summary answer and the source where it can be expanded.
- 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🚨.
🚨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.
1
2
3
4
5
6
7
8
9
import boto3
kendra_client = boto3.client("kendra")
def QueryKendra(index_id,query):
response = kendra_client.retrieve(
QueryText = query,
IndexId = index_id)
return response
- Amazon Comprehend, to detect the dominant language in which the question is asked, using DetectDominantLanguage from Boto3 Comprehend client
- Amazon Translate, to translate the question to the language of the Kendra knowledge base (English in this case) and translate the answer back to the language of the original question, using TranslateText from Boto3 Translate client.
- 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
import boto3
translate_client = boto3.client('translate')
def TranslateText(text,SourceLanguageCode,TargetLanguage):
response = translate_client.translate_text(
Text=text,
SourceLanguageCode=SourceLanguageCode,
TargetLanguageCode=TargetLanguage
)
translated_text = response['TranslatedText']
source_language_code = response['SourceLanguageCode'] #you need SourceLanguageCode to answer in the original language
return translated_text, source_language_code
🚨Note: Amazon Translate and Amazon Comprehend have Free Tier for up 12 months. Check pricing here and here.
1
2
3
4
5
6
def get_target_language_code(data_source_id,index_id):
response_data_source = kendra_client.describe_data_source(
Id = data_source_id,
IndexId = index_id
)
return response_data_source['LanguageCode']
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
text = "¿que es Amazon S3?"
index_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
data_source_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
target_language_code = get_target_language_code(data_source_id,index_id)
query,source_language_code = TranslateText(text,"auto",target_language_code)
response = QueryKendra(index_id,query)
#print the result
for query_result in response["ResultItems"]:
print("-------------------")
document_title = query_result['DocumentTitle']
document_title_translated,language = TranslateText(document_title,target_language_code,source_language_code)
print("DocumentTitle: " + document_title_translated)
document_content = query_result['Content']
document_content_translated,language = TranslateText(document_content,target_language_code,source_language_code)
print("Content: ",document_content_translated)
print("Go deeper: ", query_result['DocumentURI'])
- 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
1. Open the Amazon Sagemaker console | Image not found |
2. Find JumpStart on the left-hand navigation panel and choose Foundation models. | Image not found |
3. Search for a Flan UL2 model, and then click on View model. | Image not found |
4. Open notebook in Studio | Image not found |
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. | Image not found Image not found |
6. Follow the steps in jupyter notebook, explore it, and wait for me in step 5 | Image not found |
1
model_predictor.endpoint_name
- Console:
🚨Note: You have to be careful, because while the endpoint is active, you will be billing. Check pricing here.
1
2
3
4
5
6
7
8
9
10
import json
newline, bold, unbold = "\n", "\033[1m", "\033[0m"
parameters = {
"max_length": 50,
"max_time": 50,
"num_return_sequences": 3,
"top_k": 50,
"top_p": 0.95,
"do_sample": True,
}
- 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.
1
2
3
4
5
6
def query_endpoint_with_json_payload(encoded_json, endpoint_name):
client = boto3.client("runtime.sagemaker")
response = client.invoke_endpoint(
EndpointName=endpoint_name, ContentType="application/json", Body=encoded_json
)
return response
1
2
3
4
def parse_response_multiple_texts(query_response):
model_predictions = json.loads(query_response["Body"].read())
generated_text = model_predictions["generated_texts"]
return generated_text
text_inputs
in the code) composed of Amazon Kendra document part and the user's question, so that the model understands the context.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def summarization(text,query):
payload = {"text_inputs": f"{text}\n\nBased on the above article, answer a question. {query}", **parameters}
query_response = query_endpoint_with_json_payload(
json.dumps(payload).encode("utf-8"), endpoint_name=endpoint_name
)
generated_texts = parse_response_multiple_texts(query_response)
print(f"{bold} The {num_return_sequences} summarized results are{unbold}:{newline}")
for idx, each_generated_text in enumerate(generated_texts):
#Translate the answer to the original language of the question
answer_text_translated,language = TranslateText(each_generated_text,TargetLanguage,source_language_code)
print(f"{bold}Result {idx}{unbold}: {answer_text_translated}{newline}")
return
text_inputs
and discover the best one according to your needs.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
text = "¿que es Amazon S3?"
target_language_code = get_target_language_code(data_source_id,index_id)
query,source_language_code = TranslateText(text,"auto",target_language_code)
response = QueryKendra(index_id,query)
for query_result in response["ResultItems"]:
print("-------------------")
document_title = query_result['DocumentTitle']
document_title_translated,language = TranslateText(document_title,target_language_code,source_language_code)
print("DocumentTitle: " + document_title_translated)
document_content = query_result['Content']
document_content_translated,language = TranslateText(document_content,target_language_code,source_language_code)
print("Go deeper: ", query_result['DocumentURI'])
summarization(document_content,query)
- Open the Amazon Kendra console.
- In the navigation panel, choose Indexes, and then choose the index to delete.
- Choose Delete to delete the selected index.
1
2
3
# Delete the SageMaker endpoint
model_predictor.delete_model()
model_predictor.delete_endpoint()
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.