From 65e05a0a979369b9006a86f24c8244e713f315df Mon Sep 17 00:00:00 2001 From: Jason Park <93040528+JasonNotJson@users.noreply.github.com> Date: Mon, 23 Oct 2023 23:21:19 +0900 Subject: [PATCH] feat: finalizing injecting thread (#370) --- src/lambda/inject-threads/index.py | 36 +++++++++++++++++-- src/lambda/inject-threads/utils.py | 55 +++++++++++++++++++++--------- 2 files changed, 71 insertions(+), 20 deletions(-) diff --git a/src/lambda/inject-threads/index.py b/src/lambda/inject-threads/index.py index 6b884c389..91e138a2a 100644 --- a/src/lambda/inject-threads/index.py +++ b/src/lambda/inject-threads/index.py @@ -1,8 +1,38 @@ from datetime import datetime, timedelta -from utils import JsonPayloadBuilder, resp_handler, get_bedrock_response +from utils import JsonPayloadBuilder, resp_handler, select_thread, build_thread_id, UID, table, select_school, UNIV_ID + + +@resp_handler +def inject_thread(topic, content): + thread_id = build_thread_id() + selected_school = select_school() + + dt_now = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z' + + thread_item = { + "board_id": topic, + "created_at": dt_now, + "updated_at": dt_now, + "title": "default", + "body": content, + "uid": UID, + "thread_id": thread_id, + "tag_id": "default", + "group_id": selected_school, + "univ_id": UNIV_ID, + "views": 0, + "comment_count": 0, + "new_comment": False, + } + + table.put_item(Item=thread_item) + + body = JsonPayloadBuilder().add_status( + True).add_data('').add_message('').compile() + return body def handler(event, context): - resp = get_bedrock_response() + resp = select_thread() - print(resp) + return inject_thread(**resp) diff --git a/src/lambda/inject-threads/utils.py b/src/lambda/inject-threads/utils.py index 8373503e0..352abb1fb 100644 --- a/src/lambda/inject-threads/utils.py +++ b/src/lambda/inject-threads/utils.py @@ -7,6 +7,7 @@ import uuid from boto3.dynamodb.conditions import Key import random +import re db = boto3.resource("dynamodb", region_name="ap-northeast-1") table = db.Table(os.getenv('THREAD_TABLE_NAME')) @@ -14,6 +15,11 @@ s3_client = boto3.client('s3') bucket = os.getenv('BUCKET_NAME') +UID = os.getenv('UID') + +UNIV_ID = "1" + + file_key = 'syllabus/SILS.json' bedrock_client = boto3.client('bedrock-runtime', region_name='ap-northeast-1') @@ -141,33 +147,20 @@ def fetch_timetable(): def generate_prompt(): threads = fetch_threads() - classes = fetch_timetable() prompt_recent_threads = f'''\n\nHuman: Use the following example threads as your reference for topics and writing style of the students : {threads} You are a helpful international university student who is active in an online university forum. - Generate 3 new forum posts based on given topics like if you are an university student. - Ensure: - - Do not repeat the examples. - Assistant: - ''' - - prompt_timetable = f'''\n\nHuman: - Use the following syllabus data as your timetable as reference : {classes} - Generate 3 new forum posts based on the given topics like if you are an university student. + Generate 4 new forum posts based on given topics like if you are an university student. Ensure: - Do not repeat the examples. - You are a helpful international university student who is active in an online university forum. + - Do not make any offers. Assistant: ''' - prompt_list = [prompt_recent_threads, prompt_timetable] + logging.debug(f"Chosen Prompt : {prompt_recent_threads}") - chosen_prompt = random.sample(prompt_list, 1) - - logging.debug(f"Chosen Prompt : {chosen_prompt}") - - return chosen_prompt[0] + return prompt_recent_threads def get_bedrock_response(): @@ -196,3 +189,31 @@ def get_bedrock_response(): completion: dict = response_body.get('completion') return completion + + +def select_thread(): + completion = get_bedrock_response() + + pattern = re.compile( + r"(Academic|Job|Life|WTF):([\s\S]*?)(?=(Academic|Job|Life|WTF):|$)", re.IGNORECASE) + + matches = pattern.findall(completion) + + forum_posts = [{"topic": match[0], "content": match[1].strip()} + for match in matches] + + for post in forum_posts: + post['topic'] = post['topic'].lower() + + selected_thread = random.choice(forum_posts) + + return selected_thread + + +def select_school(): + school_options = ['SILS', 'PSE', 'SSS', + 'FSE', 'SOC', 'CSE', 'CJL', 'G_SICCS'] + + selected_school = random.choice(school_options) + + return selected_school