Skip to content

Commit

Permalink
feat: inject comment2 (#372)
Browse files Browse the repository at this point in the history
* feat: inject-comments

* feat: inject-comments2

* feat: inject-comments2
  • Loading branch information
LIEN-YUHSIANG authored Nov 8, 2023
1 parent 7e3d306 commit 3da2bca
Show file tree
Hide file tree
Showing 6 changed files with 361 additions and 1 deletion.
72 changes: 72 additions & 0 deletions lib/constructs/common/lambda-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1197,3 +1197,75 @@ export class ForumThreadAIFunctions extends Construct {
});
}
}

export class ForumCommentAIFunctions extends Construct {
readonly injectFunction: lambda.Function;

constructor(scope: Construct, id: string, props: FunctionsProps) {
super(scope, id);

const latestBoto3Layer = new lambda_py.PythonLayerVersion(
this,
'Boto3PythonLayerVersion',
{
entry: 'lib/configs/lambda/python_packages',
compatibleRuntimes: [lambda.Runtime.PYTHON_3_9],
layerVersionName: 'latest-boto3-python-layer',
description: 'Layer containing updated boto3 and botocore',
},
);

const bedrockAccessPolicy = new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ['bedrock:InvokeModel'],
resources: ['*'],
});

const DBSyncRole: iam.LazyRole = new iam.LazyRole(
this,
'dynamodb-s3-forum-comment-ai-role',
{
assumedBy: new iam.ServicePrincipal(AwsServicePrincipal.LAMBDA),
description:
'Allow lambda function to perform crud operation on dynamodb and s3',
path: `/service-role/${AwsServicePrincipal.LAMBDA}/`,
roleName: 'dynamodb-s3-forum-thread-ai-role',
managedPolicies: [
iam.ManagedPolicy.fromManagedPolicyArn(
this,
'basic-exec1',
'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole',
),
iam.ManagedPolicy.fromManagedPolicyArn(
this,
'db-full-access',
'arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess',
),
iam.ManagedPolicy.fromManagedPolicyArn(
this,
's3-full-access',
'arn:aws:iam::aws:policy/AmazonS3FullAccess',
),
],
},
);
DBSyncRole.addToPolicy(bedrockAccessPolicy);

this.injectFunction = new lambda_py.PythonFunction(
this,
'inject-comments',
{
entry: 'src/lambda/inject-comments',
description: 'inject ai generated comment data into the database',
functionName: 'inject-comment',
logRetention: logs.RetentionDays.ONE_MONTH,
memorySize: 128,
role: DBSyncRole,
runtime: lambda.Runtime.PYTHON_3_9,
timeout: Duration.seconds(60),
environment: props.envVars,
layers: [latestBoto3Layer],
},
);
}
}
31 changes: 31 additions & 0 deletions lib/constructs/persistence/data-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export enum Worker {
THREADIMG,
ADS,
FORUMAI,
COMMENTAI,
}

export interface DataPipelineProps {
Expand Down Expand Up @@ -355,3 +356,33 @@ export class ForumThreadAIDataPipeline extends AbstractDataPipeline {
).injectFunction;
}
}

export class ForumCommentAIDataPipeline extends AbstractDataPipeline {
readonly dataSource?: s3.Bucket;
readonly processor: lambda.Function;
readonly dataWarehouse: dynamodb.Table;
readonly commentWarehouse: dynamodb.Table;

constructor(scope: Construct, id: string, props: DataPipelineProps) {
super(scope, id);

// this.dataSource = props.dataSource!;
this.dataWarehouse = props.threadWareHouse!;
this.commentWarehouse = props.commentWareHouse!;

const UID = process.env.UID!;

this.processor = new ForumThreadAIFunctions(
this,
'forum-thread-ai-function',
{
envVars: {
// ['BUCKET_NAME']: this.dataSource.bucketName,
['THREAD_TABLE_NAME']: this.dataWarehouse.tableName,
['COMMENT_TABLE_NAME']: this.commentWarehouse.tableName,
['UID']: UID,
},
},
).injectFunction;
}
}
12 changes: 12 additions & 0 deletions lib/stacks/persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ThreadImgDataPipeline,
AdsDataPipeline,
ForumThreadAIDataPipeline,
ForumCommentAIDataPipeline,
Worker,
} from '../constructs/persistence/data-pipeline';
import { Collection, DynamoDatabase } from '../constructs/persistence/database';
Expand Down Expand Up @@ -64,6 +65,17 @@ export class WasedaTimePersistenceLayer extends PersistenceLayer {
);
this.dataPipelines[Worker.FORUMAI] = forumThreadAIDataPipeline;

const forumCommentAIDataPipeline = new ForumCommentAIDataPipeline(
this,
'forum-comment-ai-data-pipeline', // Error fixed
{
// dataSource: syllabusDataPipeline.dataWarehouse,
threadWareHouse: dynamoDatabase.tables[Collection.THREAD],
commentWareHouse: dynamoDatabase.tables[Collection.COMMENT],
},
);
this.dataPipelines[Worker.COMMENTAI] = forumCommentAIDataPipeline;

//! New pipeline for ads
const adsDataPipeline = new AdsDataPipeline(this, 'ads-data-pipeline', {
dataWarehouse: dynamoDatabase.tables[Collection.ADS],
Expand Down
2 changes: 1 addition & 1 deletion src/lambda/get-ads/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_imgs_list(board_id, ad_id):

# If the count propperty doesn't exist yet, set to 1, if existed increase by 1
table.update_item(
Key={
Key={
"board_id": board_id,
"ad_id": ad_id,
},
Expand Down
34 changes: 34 additions & 0 deletions src/lambda/inject-comments/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from datetime import datetime
from utils import JsonPayloadBuilder, resp_handler, UID, table_comment, UNIV_ID, get_bedrock_response


@resp_handler
def inject_comment(content, thread_id):

dt_now = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'

comment_item = {
"thread_id": thread_id,
"created_at": dt_now,
"updated_at": dt_now,
"body": content,
"uid": UID
}

table_comment.put_item(Item=comment_item)

body = JsonPayloadBuilder().add_status(
True).add_data('').add_message('').compile()
return body


def handler(event, context):
resp = get_bedrock_response()

content, thread_id = resp

if resp is None:
# No threads were found; end the Lambda function.
return

return inject_comment(content, thread_id)
Loading

0 comments on commit 3da2bca

Please sign in to comment.