Skip to content

Commit

Permalink
Merge pull request #216 from oconpa/feat/typesnpackages
Browse files Browse the repository at this point in the history
feat: Layer Types and Additional Packages
  • Loading branch information
dineshSajwan authored Jan 25, 2024
2 parents 4b6575d + b913199 commit f0f3e82
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.json

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

2 changes: 2 additions & 0 deletions .gitattributes

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

2 changes: 2 additions & 0 deletions .gitignore

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

4 changes: 4 additions & 0 deletions .projen/deps.json

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

2 changes: 2 additions & 0 deletions .projen/files.json

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

4 changes: 2 additions & 2 deletions .projen/tasks.json

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

16 changes: 16 additions & 0 deletions .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import { ProjenStruct, Struct } from '@mrgrain/jsii-struct-builder';
import { JsonPatch, awscdk } from 'projen';
import { NpmAccess } from 'projen/lib/javascript';
import {
Expand Down Expand Up @@ -51,6 +52,7 @@ const project = new awscdk.AwsCdkConstructLibrary({
'eslint-plugin-header',
'husky',
'pinst',
'@mrgrain/jsii-struct-builder',
],
deps: ['cdk-nag'],

Expand Down Expand Up @@ -157,6 +159,20 @@ project.eslint?.addRules({
'header/header': [2, 'header.js'],
});

project.eslint?.addIgnorePattern('LangchainProps.ts');
project.eslint?.addIgnorePattern('AdapterProps.ts');

// Shared interfaces extending pre-existing CDK interfaces
new ProjenStruct(project, { name: 'LangchainProps', filePath: 'src/patterns/gen-ai/aws-langchain-common-layer/LangchainProps.ts' })
.mixin(Struct.fromFqn('aws-cdk-lib.aws_lambda.LayerVersionProps'))
.withoutDeprecated()
.omit('code', 'compatibleRuntimes', 'compatibleArchitectures');

new ProjenStruct(project, { name: 'AdapterProps', filePath: 'src/patterns/gen-ai/aws-langchain-common-layer/AdapterProps.ts' })
.mixin(Struct.fromFqn('aws-cdk-lib.aws_lambda.LayerVersionProps'))
.withoutDeprecated()
.omit('code');

const packageJson = project.tryFindObjectFile('package.json');
packageJson?.patch(JsonPatch.add('/scripts/prepare', 'husky install')); // yarn 1
packageJson?.patch(JsonPatch.add('/scripts/postinstall', 'husky install')); // yarn 2
Expand Down
1 change: 1 addition & 0 deletions package.json

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

13 changes: 8 additions & 5 deletions src/common/helpers/python-lambda-layer-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import * as cdk from 'aws-cdk-lib';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as s3assets from 'aws-cdk-lib/aws-s3-assets';
import { Construct } from 'constructs';
import { LangchainProps } from '../../patterns/gen-ai/aws-langchain-common-layer/LangchainProps';

export interface LayerProps {
export interface LayerProps extends LangchainProps {
runtime: lambda.Runtime;
architecture: lambda.Architecture;
path: string;
description: string;
autoUpgrade?: boolean;
additionalPackages?: string[];
local?: 'python' | 'python3';
}

Expand All @@ -31,9 +32,12 @@ export class Layer extends Construct {
constructor(scope: Construct, id: string, props: LayerProps) {
super(scope, id);

const { runtime, architecture, path, description, autoUpgrade, local } = props;
const { runtime, architecture, path, additionalPackages, autoUpgrade, local } = props;

const args = local ? [] : ['-t /asset-output/python'];
if (additionalPackages) {
args.push(...additionalPackages);
}
if (autoUpgrade) {
args.push('--upgrade');
}
Expand Down Expand Up @@ -70,8 +74,7 @@ export class Layer extends Construct {
code: lambda.Code.fromBucket(layerAsset.bucket, layerAsset.s3ObjectKey),
compatibleRuntimes: [runtime],
compatibleArchitectures: [architecture],
removalPolicy: cdk.RemovalPolicy.DESTROY,
description: description,
...props,
});

this.layer = layer;
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
export * from './patterns/gen-ai/aws-rag-appsync-stepfn-opensearch';
export * from './patterns/gen-ai/aws-summarization-appsync-stepfn';
export * from './patterns/gen-ai/aws-langchain-common-layer';
export * from './patterns/gen-ai/aws-langchain-common-layer/LangchainProps';
export * from './patterns/gen-ai/aws-langchain-common-layer/AdapterProps';
export * from './patterns/gen-ai/aws-qa-appsync-opensearch';
export * from './patterns/gen-ai/aws-model-deployment-sagemaker';

export { version } from './common/helpers/utils';
44 changes: 44 additions & 0 deletions src/patterns/gen-ai/aws-langchain-common-layer/AdapterProps.ts

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

32 changes: 32 additions & 0 deletions src/patterns/gen-ai/aws-langchain-common-layer/LangchainProps.ts

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

31 changes: 27 additions & 4 deletions src/patterns/gen-ai/aws-langchain-common-layer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,25 @@ Where:
new LangchainCommonDepsLayer(scope: Construct, id: string, props: LangchainLayerProps)
```

#### Parameters

- scope [Construct](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Construct.html)
- id string
- props [LangchainLayerProps](https://github.com/awslabs/generative-ai-cdk-constructs/blob/main/src/patterns/gen-ai/aws-langchain-common-layer/index.ts#L23)

#### Pattern Construct Props

| **Name** | **Type** | **Required** |**Description** |
|:-------------|:----------------|-----------------|-----------------|
| runtime | [lambda.Runtime](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Runtime.html) | ![Required](https://img.shields.io/badge/required-ff0000) | Lambda function runtime compatible with this layer. |
| architecture | [lambda.Architecture](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Architecture.html)| ![Required](https://img.shields.io/badge/required-ff0000) | Lambda function architecture compatible with this layer. |
| autoUpgrade | boolean | ![Optional](https://img.shields.io/badge/optional-4169E1) | Add '--upgrade' to pip install requirements.txt. In case of a LangchainCommonLayer, this parameter is not used. |
| additionalPackages | string[] | ![Optional](https://img.shields.io/badge/optional-4169E1) | A prop allowing additional python pip libraries to be installed with this langchain layer. |
| description | string | ![Optional](https://img.shields.io/badge/optional-4169E1) | Default: Dependencies to build gen ai applications with the langchain client |
| layerVersionName | string | ![Optional](https://img.shields.io/badge/optional-4169E1) | The name of the layer |
| license | string | ![Optional](https://img.shields.io/badge/optional-4169E1) | The SPDX licence identifier or URL to the license file for this layer |
| removalPolicy | [RemovalPolicy](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.RemovalPolicy.html) | ![Optional](https://img.shields.io/badge/optional-4169E1) | Whether to retain this version of the layer when a new version is added or when the stack is deleted. Default: DESTROY |

```
new LangchainCommonLayer(scope: Construct, id: string, props: LangchainLayerProps)
```
Expand All @@ -174,15 +193,19 @@ Parameters

- scope [Construct](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Construct.html)
- id string
- props LangchainLayerProps
- props [AdapterProps](https://github.com/awslabs/generative-ai-cdk-constructs/blob/main/src/AdapterProps.ts)

## Pattern Construct Props
#### Pattern Construct Props

| **Name** | **Type** | **Required** |**Description** |
|:-------------|:----------------|-----------------|-----------------|
| runtime | [lambda.Runtime](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Runtime.html) | ![Required](https://img.shields.io/badge/required-ff0000) | Lambda function runtime compatible with this layer. |
| architecture | [lambda.Architecture](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Architecture.html)| ![Required](https://img.shields.io/badge/required-ff0000) | Lambda function architecture compatible with this layer. |
| compatibleRuntimes | [lambda.Runtime[]](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Runtime.html) | ![Required](https://img.shields.io/badge/required-ff0000) | Lambda function runtime compatible with this layer. |
| compatibleArchitectures | [lambda.Architecture[]](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Architecture.html)| ![Required](https://img.shields.io/badge/required-ff0000) | Lambda function architecture compatible with this layer. |
| autoUpgrade | boolean | ![Optional](https://img.shields.io/badge/optional-4169E1) | Add '--upgrade' to pip install requirements.txt. In case of a LangchainCommonLayer, this parameter is not used. |
| description | string | ![Optional](https://img.shields.io/badge/optional-4169E1) | Default: Dependencies to build gen ai applications with the langchain client |
| layerVersionName | string | ![Optional](https://img.shields.io/badge/optional-4169E1) | The name of the layer |
| license | string | ![Optional](https://img.shields.io/badge/optional-4169E1) | The SPDX licence identifier or URL to the license file for this layer |
| removalPolicy | [RemovalPolicy](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.RemovalPolicy.html) | ![Optional](https://img.shields.io/badge/optional-4169E1) | Whether to retain this version of the layer when a new version is added or when the stack is deleted. Default: DESTROY |
| local | "python" or "python3" | ![Optional](https://img.shields.io/badge/optional-4169E1) | Local compute will be used when installing requirements.txt when set. By default, a docker container will be spun up to install requirements. |

## Pattern Properties
Expand Down
48 changes: 27 additions & 21 deletions src/patterns/gen-ai/aws-langchain-common-layer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@
import * as path from 'path';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import { Construct } from 'constructs';
import { AdapterProps } from './AdapterProps';
import { LangchainProps } from './LangchainProps';
import { Layer } from '../../../common/helpers/python-lambda-layer-helper';

/**
* The properties for the LangchainLayerProps class.
*/
export interface LangchainLayerProps {
export interface LangchainLayerProps extends LangchainProps {
/**
* Required. Lambda function runtime compatible with this Layer.
*
* @default - none
*/
readonly runtime: lambda.Runtime;
/**
* Required. Lambda function architecture compatible with this Layer.
*
* @default - none
*/
readonly architecture: lambda.Architecture;
/**
Expand All @@ -39,7 +37,12 @@ export interface LangchainLayerProps {
*/
readonly autoUpgrade?: boolean;
/**
* Optional: Local compute will be used when installing requirements.txt.
* A prop allowing additional python pip libraries to be installed with this langchain layer
*
* @default - none
*/
readonly additionalPackages?: string[];
/** Optional: Local compute will be used when installing requirements.txt.
* By default, a docker container will be spun up to install requirements. To override this behavior, use the python alias string of `python` or `python3`
* The string value will be the python alias used to install requirements.
*
Expand All @@ -58,7 +61,7 @@ export class LangchainCommonDepsLayer extends Construct {
public readonly layer: lambda.LayerVersion;

/**
* @summary Constructs a new instance of the LangchainCommonDepsLayer class.
* @summary This construct creates a lambda layer loaded with relevant libraries to run genai applications. Libraries include boto3, botocore, requests, requests-aws4auth, langchain, opensearch-py and openai.
* @param {cdk.App} scope - represents the scope for all the resources.
* @param {string} id - this is a a scope-unique id.
* @param {LangchainLayerProps} props - user provided props for the construct.
Expand All @@ -68,21 +71,25 @@ export class LangchainCommonDepsLayer extends Construct {
constructor(scope: Construct, id: string, props: LangchainLayerProps) {
super(scope, id);

const layer = new Layer(this, 'langchaincommonlayer', {
runtime: props.runtime,
architecture: props.architecture,
const layer = new Layer(this, 'Langchain Layer', {
path: path.join(__dirname, '../../../../layers/langchain-common-deps'),
description: 'Dependencies to build gen ai applications with the langchain client',
autoUpgrade: props.autoUpgrade,
local: props.local,
...props,
});

this.layer = layer.layer;
}
}

/**
* @summary The LangchainCommonLayer class.
* @summary LangchainCommonLayer allows developers to instantiate a llm client adapter on bedrock, sagemaker or openai following best practise.
*
* @example
* from genai_core.adapters.registry import registry
* from genai_core.clients import get_bedrock_client
*
* adapter = registry.get_adapter(f"{provider}.{model_id}")
* bedrock_client = get_bedrock_client()
*/
export class LangchainCommonLayer extends Construct {
/**
Expand All @@ -91,21 +98,20 @@ export class LangchainCommonLayer extends Construct {
public readonly layer: lambda.LayerVersion;

/**
* @summary Constructs a new instance of the LangchainCommonLayer class.
* @summary This construct allows developers to instantiate a llm client adapter on bedrock, sagemaker or openai following best practise.
* @param {cdk.App} scope - represents the scope for all the resources.
* @param {string} id - this is a a scope-unique id.
* @param {LangchainLayerProps} props - user provided props for the construct.
* @param {AdapterProps} props - user provided props for the construct.
* @since 0.0.0
* @access public
*/
constructor(scope: Construct, id: string, props: LangchainLayerProps) {
constructor(scope: Construct, id: string, props: AdapterProps) {
super(scope, id);

const layer = new lambda.LayerVersion(this, 'langchaincommonlayer', {
compatibleRuntimes: [props.runtime],
compatibleArchitectures: [props.architecture],
code: lambda.Code.fromAsset(path.join(__dirname, '../../../../layers/langchain-common-layer')),
description: 'Utilities to build gen ai applications with the langchain client',
const layer = new lambda.LayerVersion(this, 'Model Adapter Layer', {
code: lambda.Code.fromAsset(path.join(__dirname, '../../../../layers/model-adapter-layer')),
description: 'Utilities to instantiate a llm client adapter. Adapters include bedrock, sagemaker, and openai',
...props,
});

this.layer = layer;
Expand Down
Loading

0 comments on commit f0f3e82

Please sign in to comment.