This is an QA application with LLMs and RAG project for CDK development with Python.
The cdk.json file tells the CDK Toolkit how to execute your app.
This project is set up like a standard Python project. The initialization
process also creates a virtualenv within this project, stored under the .venv
directory. To create the virtualenv it assumes that there is a python3
(or python for Windows) executable in your path with access to the venv
package. If for any reason the automatic creation of the virtualenv fails,
you can create the virtualenv manually.
To manually create a virtualenv on MacOS and Linux:
$ python3 -m venv .venv
After the init process completes and the virtualenv is created, you can use the following step to activate your virtualenv.
$ source .venv/bin/activate
If you are a Windows platform, you would activate the virtualenv like this:
% .venv\Scripts\activate.bat
Once the virtualenv is activated, you can install the required dependencies.
(.venv) $ pip install -r requirements.txt
To add additional dependencies, for example other CDK libraries, just add
them to your setup.py file and rerun the pip install -r requirements.txt
command.
Before deployment, you should uplad zipped code files to s3 like this example:
⚠️ Important: Replacelambda-layer-resourceswith your s3 bucket name for lambda layer zipped code. :warning: To create a bucket outside of theus-east-1region,aws s3api create-bucketcommand requires the appropriate LocationConstraint to be specified in order to create the bucket in the desired region. For more information, see these examples.
⚠️ Make sure you have Docker installed.
(.venv) $ aws s3api create-bucket --bucket lambda-layer-resources --region us-east-1 (.venv) $ cat <requirements-lambda_layer.txt > sagemaker==2.188 > cfnresponse==1.1.2 > urllib3==1.26.16 > EOF (.venv) $ docker run -v "$PWD":/var/task "public.ecr.aws/sam/build-python3.10" /bin/sh -c "pip install -r requirements-lambda_layer.txt -t python/lib/python3.10/site-packages/; exit" (.venv) $ zip -r sagemaker-python-sdk-lib.zip python > /dev/null (.venv) $ aws s3 cp sagemaker-python-sdk-lib.zip s3://lambda-layer-resources/pylambda-layer/
For more information about how to create a package for Amazon Lambda Layer, see here.
Before synthesizing the CloudFormation, you should set approperly the cdk context configuration file, cdk.context.json.
For example:
{
"opensearch_domain_name": "llm-rag-vectordb",
"sagemaker_domain_name": "rag-workshop-studio-in-vpc",
"lambda_layer_lib_s3_path": "s3://lambda-layer-resources/pylambda-layer/sagemaker-python-sdk-lib.zip",
"sagemaker_jumpstart_model_info": {
"model_id": "meta-textgeneration-llama-2-7b",
"endpoint_name": "llama-2-7b"
}
}
Now this point you can now synthesize the CloudFormation template for this code.
(.venv) $ export CDK_DEFAULT_ACCOUNT=$(aws sts get-caller-identity --query Account --output text)
(.venv) $ export CDK_DEFAULT_REGION=us-east-1 # your-aws-account-region
(.venv) $ cdk synth --all
Now we will be able to deploy all the CDK stacks at once like this:
(.venv) $ cdk deploy --require-approval never --all
Or, we can provision each CDK stack one at a time like this:
(.venv) $ cdk list
RAGHaystackVpcStack
RAGHaystackOpenSearchStack
RAGHaystackBastionHost
RAGHaystackSageMakerStudioStack
RAGHaystackSMPySDKLambdaLayerStack
RAGHaystackSMEndpointRoleStack
RAGHaystackSMJSModelDeployLambdaStack
RAGHaystackSMJSModelEndpointStack
(.venv) $ cdk deploy --require-approval never RAGHaystackVpcStack \
RAGHaystackOpenSearchStack \
RAGHaystackBastionHost
(.venv) $ cdk deploy --require-approval never RAGHaystackSageMakerStudioStack
(.venv) $ cdk deploy --require-approval never RAGHaystackSMPySDKLambdaLayerStack \
RAGHaystackSMEndpointRoleStack \
RAGHaystackSMJSModelDeployLambdaStack \
RAGHaystackSMJSModelEndpointStack
Once all CDK stacks have been successfully created, proceed with the remaining steps of the overall workflow.
Delete the CloudFormation stacks by running the below command.
(.venv) $ cdk destroy --all
cdk lslist all stacks in the appcdk synthemits the synthesized CloudFormation templatecdk deploydeploy this stack to your default AWS account/regioncdk diffcompare deployed stack with current statecdk docsopen CDK documentation
Enjoy!
- Build a powerful question answering bot with Amazon SageMaker, Amazon OpenSearch Service, Streamlit, and LangChain (2023-05-25)
- Use proprietary foundation models from Amazon SageMaker JumpStart in Amazon SageMaker Studio (2023-06-27)
- AWS Deep Learning Containers Images
- OpenSearch Popular APIs
- Using the Amazon SageMaker Studio Image Build CLI to build container images from your Studio notebooks (2020-09-14)
- SageMaker Python SDK - Deploy a Pre-Trained Model Directly to a SageMaker Endpoint
- AWS CDK TypeScript Example - Custom Resource
- How to create a Lambda layer using a simulated Lambda environment with Docker
$ cat <<EOF>requirements-lambda_layer.txt > sagemaker==2.188 > cfnresponse==1.1.2 > urllib3==1.26.16 > EOF $ docker run -v "$PWD":/var/task "public.ecr.aws/sam/build-python3.10" /bin/sh -c "pip install -r requirements-lambda_layer.txt -t python/lib/python3.10/site-packages/; exit" $ zip -r sagemaker-python-sdk-lib.zip python > /dev/null $ aws s3 mb s3://my-bucket-for-lambda-layer-packages $ aws s3 cp sagemaker-python-sdk-lib.zip s3://my-bucket-for-lambda-layer-packages/pylambda-layer/ - Connect using the EC2 Instance Connect CLI
$ sudo pip install ec2instanceconnectcli $ mssh --region us-east-1 ec2-user@i-001234a4bf70dec41EXAMPLE- Remotely access your Amazon OpenSearch Cluster using SSH tunnel from local machine
$ mssh --region {region} ec2-user@{bastion-ec2-instance-id} -N -L 9200:vpc-{opensearch-domain-name}-randomidentifier.{region}.es.amazonaws.com:443
- Remotely access your Amazon OpenSearch Cluster using SSH tunnel from local machine
- (AWS re:Post) Stack deletion stuck as DELETE_IN_PROGRESS
- (Video) How do I delete an AWS Lambda-backed custom resource that’s stuck deleting in AWS CloudFormation?
- (Stack Overflow)"cannot import name 'DEFAULT_CIPHERS' from 'urllib3.util.ssl_'" on AWS Lambda using a layer
- Error message:
cannot import name 'DEFAULT_CIPHERS' from 'urllib3.util.ssl_' (/opt/python/lib/python3.10/site-packages/urllib3/util/ssl_.py - Solution: You’ll need to explicitly pin to
urllib3<2in your project to ensureurllib3 2.0isn’t brought into your environment.urllib3<2
- Error message: