Skip to content

Commit

Permalink
feat: finalizing injecting thread (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonNotJson authored Oct 23, 2023
1 parent c450d88 commit 65e05a0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 20 deletions.
36 changes: 33 additions & 3 deletions src/lambda/inject-threads/index.py
Original file line number Diff line number Diff line change
@@ -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)
55 changes: 38 additions & 17 deletions src/lambda/inject-threads/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
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'))

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')
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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

0 comments on commit 65e05a0

Please sign in to comment.