Amazon Lex bot answering from an Amazon Bedrock knowledge base on Amazon S3 Vectors

Client → Amazon Lex QnAIntent → Amazon Bedrock Knowledge base → Amazon S3 Vectors Index → Amazon S3 Documents

An Amazon Lex V2 bot uses AMAZON.QnAIntent to answer questions from documents indexed by an Amazon Bedrock knowledge base that stores its vectors in Amazon S3 Vectors.

This pattern creates an Amazon Lex V2 bot whose built-in AMAZON.QnAIntent answers questions with a foundation model, using passages retrieved from an Amazon Bedrock knowledge base. The knowledge base reads documents from an Amazon S3 bucket, embeds them with Amazon Titan Text Embeddings V2 and stores the vectors in an Amazon S3 Vectors index.
There is no AWS Lambda function in the request path. You call the bot with the Lex runtime API (for example aws lexv2-runtime recognize-text). Lex asks the knowledge base for matching passages, sends them to the model with the question and returns the answer.
Ingestion is one explicit step: copy the sample documents to the bucket and start an ingestion job. Until the job completes, the knowledge base has nothing to retrieve. The model id and the Region are configurable, and the AWS IAM roles are scoped to the one knowledge base, the one index and the two model ARNs.

< Back to all patterns

GitHub icon Download this pattern (.zip)

GitHub icon View this pattern on GitHub


Clone repo

git clone https://github.com/aws-samples/serverless-patterns/cd serverless-patterns/lex-bedrock-kb/typescript/cdk

Deploy

npm installnpx cdk bootstrapnpx cdk deploy


Testing

Copy the files in sample-docs to the docs/ prefix of the bucket from the stack outputs, start a knowledge base ingestion job with aws bedrock-agent start-ingestion-job and wait until its status is COMPLETE.
Check that the knowledge base returns passages with aws bedrock-agent-runtime retrieve, then ask the bot a question that the documents answer with aws lexv2-runtime recognize-text, using the bot alias TSTALIASID and the locale en_US.
See the GitHub repo for the exact commands and the expected output.

Cleanup

Delete the stack: npx cdk destroy.

Created by:

Nithin Chandran R

Nithin Chandran R

Technical Account Manager at AWS