Build a Slack bot that uses AI to query your PDFs. This solution combines vector search and LLMs (Retrieval-Augmented Generation). See my blog for how it works.
The code assumes OpenAI as the AI provider. Models and secrets can be configured in cdk.context.json:
{
"secrets": {
"openaiApiKey": "REDACTED",
"slackBotToken": "REDACTED",
"slackSigningSecret": "REDACTED"
},
"openaiSettings": {
"embeddingModel": "text-embedding-3-small",
"embeddingVectorLength": "1536",
"chatModel": "gpt-4o"
}
}Set up the CDK:
npm installThe deployment is broken up into two CDK (CloudFormation) stacks, in case you want the API and not the Slack integration.
Set your AWS credentials and deploy the API and PDF processor & query handler Lambdas:
cdk deploy AiPdfProcessorStackDeploy the Slack bot integration Lambdas:
cdk deploy AiSlackBotStack
# We need to force redeployment of the API Gateway stage, as we deployed it in the 1st stack
API_ID=$(aws cloudformation describe-stacks \
--stack-name AiPdfProcessorStack \
--query 'Stacks[0].Outputs[?OutputKey==`ApiUrl`].OutputValue' \
--output text | cut -d'/' -f3 | cut -d'.' -f1)
aws apigateway create-deployment --rest-api-id $API_ID --stage-name prodThe API is IAM-protected, so first install awscurl so we can make signed requests against it:
pip install awscurlYou can upload example PDFs to the S3 bucket with the script below. You can also upload your own PDFs directly to the S3 bucket via the AWS Console or AWS CLI. Note: This is not currently idempotent. Only upload individual PDFs once.
./scripts/upload_pdfs.shRun queries:
./scripts/run_queries.shAPI Gateway uses IAM authorization for all endpoints except /slack. This one must be public because Slack needs access, but verification is done in the Lambda.

