Skip to content

Commit

Permalink
Merge branch 'main' into feat/automate_model_gen
Browse files Browse the repository at this point in the history
  • Loading branch information
krokoko authored Feb 1, 2024
2 parents b65bbea + 963b8dd commit 8c18223
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 65 deletions.
4 changes: 2 additions & 2 deletions .projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const project = new awscdk.AwsCdkConstructLibrary({
projenVersion: '~0.78.8',
constructsVersion: '10.3.0',
defaultReleaseBranch: 'main',
jsiiVersion: '~5.1.0',
jsiiVersion: '~5.3.0',
name: '@' + PUBLICATION_NAMESPACE + '/' + PROJECT_NAME,
projenrcTs: true,
repositoryUrl: 'https://github.com/' + GITHUB_USER + '/' + PROJECT_NAME,
Expand Down
6 changes: 3 additions & 3 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions src/patterns/gen-ai/aws-langchain-common-layer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
| **Language** | **Package** |
|:-------------|-----------------|
|![Typescript Logo](https://docs.aws.amazon.com/cdk/api/latest/img/typescript32.png) Typescript|`@cdklabs/generative-ai-cdk-constructs`|
|![Python Logo](https://docs.aws.amazon.com/cdk/api/latest/img/python32.png) Python|`cdklabs.generative_ai_cdk_constructs`|

## Table of contents

Expand Down Expand Up @@ -79,6 +80,48 @@ const lambdaCommonLayer = new LangchainCommonLayer(this, 'lambdagenaicommonlayer
//Then pass the layers above to your lambda function constructor
```

Python
``` python
from constructs import Construct
from aws_cdk import Aws, aws_lambda as lambda_
from cdklabs.generative_ai_cdk_constructs import (
LangchainCommonDepsLayer,
LangchainCommonLayer,
)

lambda_architecture = lambda_.Architecture.ARM_64
lambda_runtime = lambda_.Runtime.PYTHON_3_10

# This is one way of getting a lambda powertools layer
powertools_arn = (
f'arn:aws:lambda:{Aws.REGION}:017000801446:'
'layer:AWSLambdaPowertoolsPythonV2-Arm64:42'
)

if lambda_architecture == lambda_.Architecture.X86_64:
powertools_arn = (
f'arn:aws:lambda:{Aws.REGION}:017000801446:'
'layer:AWSLambdaPowertoolsPythonV2:42'
)

lambda_deps_layer = LangchainCommonDepsLayer(
self,
'lambdagenaidepslayer',
runtime=lambda_runtime,
architecture=lambda_architecture,
auto_upgrade=True,
)

lambda_common_layer = LangchainCommonLayer(
self,
'lambdagenaicommonlayer',
compatible_runtimes=[lambda_runtime],
compatible_architectures=[lambda_architecture],
)

# Then pass the layers above to your lambda function constructor
```

Here is an example of a Lambda handler which uses the layers above:

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
| **Language** | **Package** |
|:-------------|-----------------|
|![Typescript Logo](https://docs.aws.amazon.com/cdk/api/latest/img/typescript32.png) Typescript|`@cdklabs/generative-ai-cdk-constructs`|
|![Python Logo](https://docs.aws.amazon.com/cdk/api/latest/img/python32.png) Python|`cdklabs.generative_ai_cdk_constructs`|

## Table of contents

Expand Down Expand Up @@ -45,6 +46,7 @@ env: {

Here is a minimal deployable pattern definition:

Typescript
```typescript

import { Construct } from 'constructs';
Expand All @@ -64,6 +66,35 @@ new CustomSageMakerEndpoint(this, 'customModel', {
});
```

Python
```python
from constructs import Construct
from cdklabs.generative_ai_cdk_constructs import (
CustomSageMakerEndpoint,
DeepLearningContainerImage,
SageMakerInstanceType,
)

# Need an existing bucket containing model artifacts that this construct can access
bucket_name = 'bucket'
key_name = 'key'

CustomSageMakerEndpoint(
self,
'customModel',
model_id='bgeinf2',
instance_type=SageMakerInstanceType.ML_INF2_XLARGE,
container=DeepLearningContainerImage.from_deep_learning_container_image(
'huggingface-pytorch-inference-neuronx',
'1.13.1-transformers4.34.1-neuronx-py310-sdk2.15.0-ubuntu20.04',
),
model_data_url=f's3://{bucket_name}/{key_name}/model.tar.gz',
endpoint_name='testbgebase',
instance_count=1,
volume_size_in_gb=100,
)
```

## Initializer

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
| **Language** | **Package** |
|:-------------|-----------------|
|![Typescript Logo](https://docs.aws.amazon.com/cdk/api/latest/img/typescript32.png) Typescript|`@cdklabs/generative-ai-cdk-constructs`|
|![Python Logo](https://docs.aws.amazon.com/cdk/api/latest/img/python32.png) Python|`cdklabs.generative_ai_cdk_constructs`|

## Table of contents

Expand Down Expand Up @@ -45,6 +46,7 @@ env: {

Here is a minimal deployable pattern definition:

Typescript
```typescript

import { Construct } from 'constructs';
Expand All @@ -65,6 +67,29 @@ new HuggingFaceSageMakerEndpoint(this, 'Mistral', {
});
```

Python
```python
from constructs import Construct
from cdklabs.generative_ai_cdk_constructs import (
HuggingFaceSageMakerEndpoint,
DeepLearningContainerImage,
SageMakerInstanceType,
)

HuggingFaceSageMakerEndpoint(
self,
'Mistral',
model_id='mistralai/Mistral-7B-Instruct-v0.1',
instance_type=SageMakerInstanceType.ML_G5_2_XLARGE,
container=DeepLearningContainerImage.HUGGINGFACE_PYTORCH_TGI_INFERENCE_2_0_1_TGI1_1_0_GPU_PY39_CU118_UBUNTU20_04,
environment={
'SM_NUM_GPUS': '1',
'MAX_INPUT_LENGTH': '2048',
'MAX_TOTAL_TOKENS': '4096',
},
)
```

## Initializer

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
| **Language** | **Package** |
|:-------------|-----------------|
|![Typescript Logo](https://docs.aws.amazon.com/cdk/api/latest/img/typescript32.png) Typescript|`@cdklabs/generative-ai-cdk-constructs`|
|![Python Logo](https://docs.aws.amazon.com/cdk/api/latest/img/python32.png) Python|`cdklabs.generative_ai_cdk_constructs`|

## Table of contents

Expand Down Expand Up @@ -45,6 +46,7 @@ env: {

Here is a minimal deployable pattern definition:

Typescript
```typescript

import { Construct } from 'constructs';
Expand All @@ -58,6 +60,23 @@ new JumpStartSageMakerEndpoint(this, 'LLAMA2', {
});
```

Python
```python
from constructs import Construct
from cdklabs.generative_ai_cdk_constructs import (
JumpStartSageMakerEndpoint,
JumpStartModel,
SageMakerInstanceType,
)

JumpStartSageMakerEndpoint(
self,
'LLAMA2',
model=JumpStartModel.META_TEXTGENERATION_LLAMA_2_7_B_F_2_0_2,
instance_type=SageMakerInstanceType.ML_G5_2_XLARGE,
)
```

## Initializer

```
Expand Down
36 changes: 36 additions & 0 deletions src/patterns/gen-ai/aws-qa-appsync-opensearch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
| **Language** | **Package** |
|:-------------|-----------------|
|![Typescript Logo](https://docs.aws.amazon.com/cdk/api/latest/img/typescript32.png) Typescript|`@cdklabs/generative-ai-cdk-constructs`|
|![Python Logo](https://docs.aws.amazon.com/cdk/api/latest/img/python32.png) Python|`cdklabs.generative_ai_cdk_constructs`|

## Table of contents

Expand Down Expand Up @@ -82,6 +83,41 @@ const rag_source = new QaAppsyncOpensearch(
)
```

Python

``` python
from constructs import Construct
from aws_cdk import (
aws_opensearchservice as os,
aws_cognito as cognito,
)
from cdklabs.generative_ai_cdk_constructs import QaAppsyncOpensearch

# get an existing OpenSearch provisioned cluster
os_domain = os.Domain.from_domain_attributes(
self,
'osdomain',
domain_arn='arn:aws:es:us-east-1:XXXXXX:resource-id',
domain_endpoint='https://XXXXX.us-east-1.es.amazonaws.com',
)

# get an existing userpool
cognito_pool_id = 'us-east-1_XXXXX';
user_pool_loaded = cognito.UserPool.from_user_pool_id(
self,
'myuserpool',
user_pool_id=cognito_pool_id,
)

rag_source = QaAppsyncOpensearch(
self,
'QaAppsyncOpensearch',
existing_opensearch_domain=os_domain,
open_search_index_name='demoindex',
cognito_user_pool=user_pool_loaded,
)
```

After deploying the CDK stack, the QA process can be invoked using GraphQL APIs. The API Schema details are present here: resources/gen-ai/aws-qa-appsync-opensearch/schema.graphql.

The code below provides an example of a mutation call and associated subscription to trigger a question and get response notifications. The subscription call will wait for mutation requests to send the notifications.
Expand Down
4 changes: 2 additions & 2 deletions src/patterns/gen-ai/aws-qa-appsync-opensearch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { NagSuppressions } from 'cdk-nag';
import { Construct } from 'constructs';
import * as opensearch_helper from '../../../common/helpers/opensearch-helper';
import * as s3_bucket_helper from '../../../common/helpers/s3-bucket-helper';
import { version } from '../../../common/helpers/utils';
import { version, lambdaMemorySizeLimiter } from '../../../common/helpers/utils';
import * as vpc_helper from '../../../common/helpers/vpc-helper';

/**
Expand Down Expand Up @@ -533,7 +533,7 @@ export class QaAppsyncOpensearch extends Construct {
tracing: lambda_tracing,
vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
securityGroups: [this.securityGroup],
memorySize: 1_769 * 4,
memorySize: lambdaMemorySizeLimiter(this, 1_769 * 4),
timeout: Duration.minutes(15),
role: question_answering_function_role,
environment: {
Expand Down
37 changes: 37 additions & 0 deletions src/patterns/gen-ai/aws-rag-appsync-stepfn-opensearch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
| **Language** | **Package** |
|:-------------|-----------------|
|![Typescript Logo](https://docs.aws.amazon.com/cdk/api/latest/img/typescript32.png) Typescript|`@cdklabs/generative-ai-cdk-constructs`|
|![Python Logo](https://docs.aws.amazon.com/cdk/api/latest/img/python32.png) Python|`cdklabs.generative_ai_cdk_constructs`|

## Table of contents

Expand Down Expand Up @@ -87,6 +88,42 @@ const rag_source = new RagAppsyncStepfnOpensearch(
}
)
```

Python
``` python
from constructs import Construct
from aws_cdk import (
aws_opensearchservice as os,
aws_cognito as cognito,
)
from cdklabs.generative_ai_cdk_constructs import RagAppsyncStepfnOpensearch

# get an existing OpenSearch provisioned cluster in the same VPC as of RagAppsyncStepfnOpensearch construct
# Security group for the existing opensearch cluster should allow traffic on 443.
os_domain = os.Domain.from_domain_attributes(
self,
'osdomain',
domain_arn='arn:aws:es:us-east-1:XXXXXX:resource-id',
domain_endpoint='https://XXXXX.us-east-1.es.amazonaws.com',
)

# get an existing userpool
cognito_pool_id = 'us-east-1_XXXXX';
user_pool_loaded = cognito.UserPool.from_user_pool_id(
self,
'myuserpool',
user_pool_id=cognito_pool_id,
)

rag_source = RagAppsyncStepfnOpensearch(
self,
'RagAppsyncStepfnOpensearch',
existing_opensearch_domain=os_domain,
open_search_index_name='demoindex',
cognito_user_pool=user_pool_loaded,
)
```

After deploying the CDK stack, the document summarization workflow can be invoked using GraphQL APIs. The API schema details are here: resources/gen-ai/aws-rag-appsync-stepfn-opensearch/schema.graphql.

The code below provides an example of a mutation call and associated subscription to trigger a pipeline call and get status notifications:
Expand Down
Loading

0 comments on commit 8c18223

Please sign in to comment.