Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor and fix prompt issue #495

Merged
merged 7 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 18 additions & 77 deletions source/infrastructure/lib/api/api-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* and limitations under the License. *
*********************************************************************************************************************/

import { Aws, Size, StackProps } from "aws-cdk-lib";
import { Aws, Duration, Size, StackProps } from "aws-cdk-lib";
import * as apigw from "aws-cdk-lib/aws-apigateway";
import * as s3 from "aws-cdk-lib/aws-s3";
import { Runtime, Code } from 'aws-cdk-lib/aws-lambda';
Expand All @@ -30,6 +30,8 @@ import { ChatStackOutputs } from "../chat/chat-stack";
import { UserConstructOutputs } from "../user/user-construct";
import { LambdaFunction } from "../shared/lambda-helper";
import { Constants } from "../shared/constants";
import { PythonFunction } from "@aws-cdk/aws-lambda-python-alpha";
import { BundlingFileAccess } from 'aws-cdk-lib/core';

interface ApiStackProps extends StackProps {
config: SystemConfig;
Expand Down Expand Up @@ -65,8 +67,6 @@ export class ApiConstruct extends Construct {
const messageQueue = props.chatStackOutputs.messageQueue;

const lambdaLayers = new LambdaLayers(this);
// const apiLambdaExecutorLayer = lambdaLayers.createExecutorLayer();
const apiLambdaEmbeddingLayer = lambdaLayers.createEmbeddingLayer();
const apiLambdaOnlineSourceLayer = lambdaLayers.createOnlineSourceLayer();
const apiLambdaAuthorizerLayer = lambdaLayers.createAuthorizerLayer();

Expand Down Expand Up @@ -143,39 +143,6 @@ export class ApiConstruct extends Construct {

// if (props.config.knowledgeBase.knowledgeBaseType.intelliAgentKb.enabled) {
if (props.config.knowledgeBase.enabled && props.config.knowledgeBase.knowledgeBaseType.intelliAgentKb.enabled) {
const embeddingLambda = new LambdaFunction(this, "lambdaEmbedding", {
code: Code.fromAsset(join(__dirname, "../../../lambda/embedding")),
vpc: vpc,
securityGroups: securityGroups,
environment: {
ETL_MODEL_ENDPOINT: props.modelConstructOutputs.defaultKnowledgeBaseModelName,
REGION: Aws.REGION,
RES_BUCKET: resBucketName,
},
layers: [apiLambdaEmbeddingLayer],
statements: [
this.iamHelper.esStatement,
this.iamHelper.s3Statement,
this.iamHelper.endpointStatement,
],
});

const aosLambda = new LambdaFunction(this, "AOSLambda", {
code: Code.fromAsset(join(__dirname, "../../../lambda/aos")),
vpc: vpc,
securityGroups: securityGroups,
environment: {
opensearch_cluster_domain: domainEndpoint,
embedding_endpoint: props.modelConstructOutputs.defaultEmbeddingModelName,
},
layers: [apiLambdaEmbeddingLayer],
statements: [
this.iamHelper.esStatement,
this.iamHelper.s3Statement,
this.iamHelper.endpointStatement,
],
});

const executionManagementLambda = new LambdaFunction(this, "ExecutionManagementLambda", {
code: Code.fromAsset(join(__dirname, "../../../lambda/etl")),
handler: "execution_management.lambda_handler",
Expand All @@ -199,27 +166,6 @@ export class ApiConstruct extends Construct {
statements: [this.iamHelper.s3Statement],
});

// Define the API Gateway Lambda Integration with proxy and no integration responses
const lambdaEmbeddingIntegration = new apigw.LambdaIntegration(
embeddingLambda.function,
{ proxy: true },
);

// Define the API Gateway Method
const apiResourceEmbedding = api.root.addResource("extract");
apiResourceEmbedding.addMethod("POST", lambdaEmbeddingIntegration, this.genMethodOption(api, auth, null),);

// Define the API Gateway Lambda Integration with proxy and no integration responses
const lambdaAosIntegration = new apigw.LambdaIntegration(aosLambda.function, {
proxy: true,
});

// All AOS wrapper should be within such lambda
const apiResourceAos = api.root.addResource("aos");
apiResourceAos.addMethod("POST", lambdaAosIntegration, this.genMethodOption(api, auth, null),);
// Add Get method to query & search index in OpenSearch, such embedding lambda will be updated for online process
apiResourceAos.addMethod("GET", lambdaAosIntegration, this.genMethodOption(api, auth, null),);

const apiResourceStepFunction = api.root.addResource("knowledge-base");
const apiKBExecution = apiResourceStepFunction.addResource("executions");
if (props.knowledgeBaseStackOutputs.sfnOutput !== undefined) {
Expand Down Expand Up @@ -405,21 +351,21 @@ export class ApiConstruct extends Construct {

const promptManagementLambda = new LambdaFunction(this, "PromptManagementLambda", {
runtime: Runtime.PYTHON_3_12,
code: Code.fromAsset(join(__dirname, "../../../lambda/prompt_management")),
handler: "prompt_management.lambda_handler",
code: Code.fromAsset(join(__dirname, '../../../lambda/deployment_assets/lambda_assets/prompt_management.zip')),
environment: {
PROMPT_TABLE_NAME: props.chatStackOutputs.promptTableName,
},
layers: [apiLambdaOnlineSourceLayer],
statements: [this.iamHelper.dynamodbStatement,
this.iamHelper.logStatement],
statements: [this.iamHelper.dynamodbStatement, this.iamHelper.logStatement],
});


const intentionLambda = new LambdaFunction(this, "IntentionLambda", {
const intentionLambda = new PythonFunction(this, "IntentionLambda", {
runtime: Runtime.PYTHON_3_12,
code: Code.fromAsset(join(__dirname, "../../../lambda/intention")),
handler: "intention.lambda_handler",
entry: join(__dirname, "../../../lambda/intention"),
index: "intention.py",
handler: "lambda_handler",
timeout: Duration.minutes(15),
vpc: vpc,
securityGroups: securityGroups,
environment: {
Expand All @@ -435,15 +381,14 @@ export class ApiConstruct extends Construct {
BEDROCK_REGION: props.config.chat.bedrockRegion,
},
layers: [apiLambdaOnlineSourceLayer],
statements: [this.iamHelper.dynamodbStatement,
this.iamHelper.logStatement,
this.iamHelper.secretStatement,
this.iamHelper.esStatement,
this.iamHelper.s3Statement,
this.iamHelper.bedrockStatement,
this.iamHelper.endpointStatement,
],
});
intentionLambda.addToRolePolicy(this.iamHelper.dynamodbStatement);
intentionLambda.addToRolePolicy(this.iamHelper.logStatement);
intentionLambda.addToRolePolicy(this.iamHelper.secretStatement);
intentionLambda.addToRolePolicy(this.iamHelper.esStatement);
intentionLambda.addToRolePolicy(this.iamHelper.s3Statement);
intentionLambda.addToRolePolicy(this.iamHelper.bedrockStatement);
intentionLambda.addToRolePolicy(this.iamHelper.endpointStatement);

const chatbotManagementLambda = new LambdaFunction(this, "ChatbotManagementLambda", {
runtime: Runtime.PYTHON_3_12,
Expand Down Expand Up @@ -619,10 +564,6 @@ export class ApiConstruct extends Construct {
// // })
// }
// );




// const apiResourceChatbot = apiResourceChatbotManagement.addResource("chatbot");
// const apiResourceChatbotDetail = apiResourceChatbot.addResource('{chatbotId}')
// apiResourceChatbotDetail.addMethod("GET", lambdaChatbotIntegration, this.genMethodOption(api, auth, null));
Expand Down Expand Up @@ -657,7 +598,7 @@ export class ApiConstruct extends Construct {
apiResourcePromptProxy.addMethod("GET", lambdaPromptIntegration, this.genMethodOption(api, auth, null));

// Define the API Gateway Lambda Integration to manage intention
const lambdaIntentionIntegration = new apigw.LambdaIntegration(intentionLambda.function, {
const lambdaIntentionIntegration = new apigw.LambdaIntegration(intentionLambda, {
proxy: true,
});
const apiResourceIntentionManagement = api.root.addResource("intention");
Expand Down
10 changes: 5 additions & 5 deletions source/infrastructure/lib/chat/chat-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ export class ChatStack extends NestedStack implements ChatStackOutputs {
handler: "lambda_main.main.lambda_handler",
code: Code.fromCustomCommand(
"/tmp/online_lambda_function_codes",
['bash', '-c',[
['bash', '-c', [
"mkdir -p /tmp/online_lambda_function_codes",
`cp -r ${join(__dirname, "../../../lambda/online/*")} /tmp/online_lambda_function_codes`,
`cp ${join(__dirname, "../../../lambda/job/dep/llm_bot_dep/sm_utils.py")} /tmp/online_lambda_function_codes/`,
`cp -r ${join(__dirname, "../../../lambda/online/*")} /tmp/online_lambda_function_codes`,
`cp ${join(__dirname, "../../../lambda/job/dep/llm_bot_dep/sm_utils.py")} /tmp/online_lambda_function_codes/`,
].join(' && ')
]
]
),
memorySize: 4096,
vpc: vpc,
Expand Down Expand Up @@ -177,7 +177,7 @@ export class ChatStack extends NestedStack implements ChatStackOutputs {
"cloudformation:List*",
"cloudformation:ValidateTemplate",
"cloudformation:Detect*"
],
],
effect: iam.Effect.ALLOW,
resources: ["*"],
}),
Expand Down
6 changes: 3 additions & 3 deletions source/infrastructure/lib/model/model-construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class ModelConstruct extends NestedStack implements ModelConstructOutputs

if (props.config.chat.useOpenSourceLLM) {
const modelTriggerLambda = new Function(this, "ModelTriggerLambda", {
runtime: Runtime.PYTHON_3_11,
runtime: Runtime.PYTHON_3_12,
handler: "pipeline_monitor.post_model_deployment",
code: Code.fromAsset(join(__dirname, "../../../lambda/pipeline_monitor")),
timeout: Duration.minutes(10),
Expand All @@ -123,7 +123,7 @@ export class ModelConstruct extends NestedStack implements ModelConstructOutputs
});
modelTriggerLambda.addToRolePolicy(this.modelIamHelper.dynamodbStatement);
const pipelineMonitorLambda = new Function(this, "PipelineMonitorLambda", {
runtime: Runtime.PYTHON_3_11,
runtime: Runtime.PYTHON_3_12,
handler: "pipeline_monitor.lambda_handler",
code: Code.fromAsset(join(__dirname, "../../../lambda/pipeline_monitor")),
timeout: Duration.minutes(10),
Expand Down Expand Up @@ -311,7 +311,7 @@ export class ModelConstruct extends NestedStack implements ModelConstructOutputs

// Custom resource to update ETL endpoint autoscaling setting
const crLambda = new Function(this, "ETLCustomResource", {
runtime: Runtime.PYTHON_3_11,
runtime: Runtime.PYTHON_3_12,
code: Code.fromAsset(join(__dirname, "../../../lambda/etl")),
handler: "etl_custom_resource.lambda_handler",
environment: {
Expand Down
51 changes: 15 additions & 36 deletions source/infrastructure/lib/shared/lambda-layers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,72 +19,51 @@ import * as pyLambda from "@aws-cdk/aws-lambda-python-alpha";
export class LambdaLayers {
constructor(private scope: Construct) { }

createEmbeddingLayer() {
const LambdaEmbeddingLayer = new LayerVersion(
this.scope,
"APILambdaEmbeddingLayer",
{
code: Code.fromAsset(
path.join(__dirname, "../../../lambda/embedding"),
{
bundling: {
image: Runtime.PYTHON_3_12.bundlingImage,
command: [
"bash",
"-c",
`pip install -r requirements.txt -t /asset-output/python`,
],
},
},
),
compatibleRuntimes: [Runtime.PYTHON_3_12],
description: `LLM Bot - API layer`,
},
);
return LambdaEmbeddingLayer;
}

createOnlineSourceLayer() {
const LambdaOnlineSourceLayer = new pyLambda.PythonLayerVersion(
const sharedLayer = new pyLambda.PythonLayerVersion(
this.scope,
"APILambdaOnlineSourceLayer",
"AICSSharedLayer",
{
entry: path.join(__dirname, "../../../lambda/online"),
compatibleRuntimes: [Runtime.PYTHON_3_12],
description: `AI-Customer-Service - Online Source layer`,
bundling: {
"command": [
"bash", "-c", "pip install -r requirements.txt -t /asset-output/python"],
"assetExcludes": ["*.pyc","*/__pycache__/*","*.xls","*.xlsx","*.csv","*.png","lambda_main/retail/size/*"],
"bash", "-c", "pip install -r requirements.txt -t /asset-output/python"],
"assetExcludes": [
"*.pyc", "*/__pycache__/*",
"*.xls", "*.xlsx", "*.csv",
"*.png",
"lambda_main/retail/size/*"],
}
},
);
return LambdaOnlineSourceLayer;
return sharedLayer;
}

createJobSourceLayer() {
const LambdaJobSourceLayer = new pyLambda.PythonLayerVersion(
const etlLayer = new pyLambda.PythonLayerVersion(
this.scope,
"APILambdaJobSourceLayer",
"AICSETLLayer",
{
entry: path.join(__dirname, "../../../lambda/job/dep/llm_bot_dep"),
compatibleRuntimes: [Runtime.PYTHON_3_12],
description: `AI Customer Service agent - Job Source layer`,
},
);
return LambdaJobSourceLayer;
return etlLayer;
}

createAuthorizerLayer() {
const LambdaAuthorizerLayer = new pyLambda.PythonLayerVersion(
const authorizerLayer = new pyLambda.PythonLayerVersion(
this.scope,
"APILambdaAuthorizerLayer",
"AICSAuthorizerLayer",
{
entry: path.join(__dirname, "../../../lambda/authorizer"),
compatibleRuntimes: [Runtime.PYTHON_3_12],
description: `LLM Bot - Authorizer layer`,
},
);
return LambdaAuthorizerLayer;
return authorizerLayer;
}
}
Loading
Loading