This sample application allows you to ask natural language questions of any PDF document you upload. It combines the text generation and analysis capabilities of an LLM with a vector search of the document content. The solution uses serverless services such as Amazon Bedrock to access foundational models, AWS Lambda to run LangChain, and Amazon DynamoDB for conversational memory.
- Amazon Bedrock for serverless embedding and inference
- LangChain to orchestrate a Q&A LLM chain
- PineCone vector store
- Amazon DynamoDB for serverless conversational memory
- AWS Lambda for serverless compute
- Frontend built in React, TypeScript, TailwindCSS, and Vite.
- Run locally or deploy to AWS Amplify Hosting
- Amazon Cognito for authentication
- Uploads a PDF document into an Amazon S3 using pre-signed URL
- The new document model is created in DynamoDB (For Metadata and saving conversations)
- Post that metadata extraction & document embedding takes place.
- The process converts the text in the document into vectors. The vectors are loaded into a vector index and stored in Pinecone DB for later use.
- When a user chats with a PDF document and sends a prompt to the backend, a Lambda function retrieves the index from Pinecone and searches for information related to the prompt.
- A LLM then uses the results of this vector search, previous messages in the conversation to formulate a response to the user.
- AWS SAM CLI
- NodeJS 20.x or greater
This application can be used with a variety of LLMs via Amazon Bedrock. See Supported models in Amazon Bedrock for a complete list.
By default, this application uses amazon.titan-text-express-v1 to generate embeddings and responses.
Create a pinecone dashboard, copy and paste the following details in constants file depending on the index name
API_KEY : "febe9883-869a-4c4e-bb8c-356a5b356f00",
PINECONE_ENV : "gcp-starter",
PINECONE_INDEX_NAME : "industrility-index",-
Change to the
backenddirectory and build the application:cd backend sam build -
Setup credentials using AWS Identity and Access Management
-
Deploy the application into your AWS account:
sam deploy --guided
-
For Stack Name, choose
serverless-pdf-chat. -
For the remaining options, keep the defaults by pressing the enter key.
AWS SAM will now provision the AWS resources defined in the backend/template.yaml template. Once the deployment is completed successfully, you will see a set of output values similar to the following:
CloudFormation outputs from deployed stack
-------------------------------------------------------------------------------
Outputs
-------------------------------------------------------------------------------
Key CognitoUserPool
Description -
Value us-east-1_gxKtRocFs
Key CognitoUserPoolClient
Description -
Value 874ghcej99f8iuo0lgdpbrmi76k
Key ApiGatewayBaseUrl
Description -
Value https://abcd1234.execute-api.us-east-1.amazonaws.com/dev/
-------------------------------------------------------------------------------You can find the same outputs in the Outputs tab of the serverless-pdf-chat stack in the AWS CloudFormation console. In the next section, you will use these outputs to run the React frontend locally and connect to the deployed resources in AWS.
Create a file named .env.development in the frontend directory. Vite will use this file to set up environment variables when we run the application locally.
Copy the following file content and replace the values with the outputs provided by AWS SAM: (You can keep them as it is, as the backend stack is up and running)
VITE_REGION=us-east-1
VITE_API_ENDPOINT=https://abcd1234.execute-api.us-east-1.amazonaws.com/dev/
VITE_USER_POOL_ID=us-east-1_gxKtRocFs
VITE_USER_POOL_CLIENT_ID=874ghcej99f8iuo0lgdpbrmi76k
Next, install the frontend's dependencies by running the following command in the frontend directory:
npm ciFinally, to start the application locally, run the following command in the frontend directory:
npm run devVite will now start the application under http://localhost:5173. As the application uses Amazon Cognito for authentication, you will be greeted by a login screen. In the next step, you will create a user to access the application.
