Skip to content

Latest commit

 

History

History

openai

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Secure AI Agents With Langchain, OpenAI’s GPT-4, and AWS

Requirements

Architecture Overview

Alt text

Deployment Instructions

  1. Clone the repository and navigate it:
git clone https://github.com/aws-samples/langchain-agents.git
cd langchain-agents/openai
  1. Install the project dependencies:
npm install
  1. Export the OpenAI API Key to store it in AWS SSM Parameter Store:
export OPENAI_API_KEY=<your-openai-api-key>
  1. The Cognito User Pool Domain must be unique. You can either go to the lib/ai-stack.ts and manually change the name or, if you are on a Mac, run the command below.
timestamp=$(date +%Y%m%d%H%M%S)
sed -i '' "s/ai-domain/ai-domain-$timestamp/" lib/ai-stack.ts

Note: The first time you deploy an AWS CDK app into an environment (account/region), you’ll need to install a “bootstrap stack”. This stack includes resources that are needed for the toolkit’s operation. Use the following command to install the bootstrap stack into the environment:

npx cdk bootstrap
  1. Use AWS CDK to synthesize an AWS CloudFormation:
npx cdk synth
  1. Use AWS CDK to deploy the AWS resources for the pattern:
npx cdk deploy

During the deployment you will be asked to confirm the security-sensitive changes. Review it and enter y to deploy the stack and create the resources.

  1. Save the following from the outputted values:

    a. UserPoolClientIdWeb.

    b. UserPoolId.

    c. FunctionUrl.

  2. Navigate back to the root directory and change directory to the frontend directory:

cd ../front-end
  1. Install the project dependencies:
npm install
  1. Configure a new AWS Amplify project:
amplify init

(Optional) If you are running into errors run:

amplify configure
  1. Import your existing auth resource to your local back-end
amplify import auth

Select the "Cognito User Pool and Identity Pool" option and select the values you saved previously.

  1. Provision cloud back-end resources with the latest local changes:
amplify push

Create .env.local file to store environmental variable and replace the variables with the Lambda Function URL and corresponding AWS Region:

echo "NEXT_PUBLIC_LAMBDA_URL=<your-lambda-url>" > .env.local
echo "NEXT_PUBLIC_AWS_REGION=<your-aws-region>" >> .env.local

Run application:

npm run dev

Cleanup

  1. To remove the Amplify auth, run:
amplify remove auth
  1. To delete the Amplify project and associated backend resources, run:
amplify delete
  1. To delete the stack, run:
npx cdk destroy

Useful commands

  • cdk ls list all stacks in the app
  • cdk synth emits the synthesized CloudFormation template
  • cdk deploy deploy this stack to your default AWS account/region
  • cdk diff compare deployed stack with current state
  • cdk docs open CDK documentation