diff --git a/apps/api/src/services/ai/ai.py b/apps/api/src/services/ai/ai.py index 9cd18064..5b8cc7b7 100644 --- a/apps/api/src/services/ai/ai.py +++ b/apps/api/src/services/ai/ai.py @@ -67,8 +67,11 @@ def ai_start_activity_chat_session( # Serialize Activity Content Blocks to a text comprehensible by the AI structured = structure_activity_content_by_type(content) + + isEmpty = structured == [] + ai_friendly_text = serialize_activity_text_to_ai_comprehensible_text( - structured, course, activity + structured, course, activity, isActivityEmpty=isEmpty ) # Get Activity Organization diff --git a/apps/api/src/services/courses/activities/activities.py b/apps/api/src/services/courses/activities/activities.py index 236ac7d6..be002190 100644 --- a/apps/api/src/services/courses/activities/activities.py +++ b/apps/api/src/services/courses/activities/activities.py @@ -25,7 +25,7 @@ async def create_activity( current_user: PublicUser | AnonymousUser, db_session: Session, ): - activity = Activity.from_orm(activity_object) + # CHeck if org exists statement = select(Chapter).where(Chapter.id == activity_object.chapter_id) @@ -40,6 +40,9 @@ async def create_activity( # RBAC check await rbac_check(request, chapter.chapter_uuid, current_user, "create", db_session) + # Create Activity + activity = Activity(**activity_object.dict()) + activity.activity_uuid = str(f"activity_{uuid4()}") activity.creation_date = str(datetime.now()) activity.update_date = str(datetime.now()) diff --git a/apps/api/src/services/courses/activities/utils.py b/apps/api/src/services/courses/activities/utils.py index 1b7b3913..c2904d18 100644 --- a/apps/api/src/services/courses/activities/utils.py +++ b/apps/api/src/services/courses/activities/utils.py @@ -4,6 +4,10 @@ from src.db.courses import CourseRead def structure_activity_content_by_type(activity): ### Get Headings, Texts, Callouts, Answers and Paragraphs from the activity as a big list of strings (text only) and return it + + if "content" not in activity or not activity["content"]: + return [] + content = activity["content"] headings = [] @@ -11,10 +15,12 @@ def structure_activity_content_by_type(activity): paragraphs = [] for item in content: - if 'content' in item: + if "content" in item: if item["type"] == "heading" and "text" in item["content"][0]: headings.append(item["content"][0]["text"]) - elif item["type"] in ["calloutInfo", "calloutWarning"] and all("text" in text_item for text_item in item["content"]): + elif item["type"] in ["calloutInfo", "calloutWarning"] and all( + "text" in text_item for text_item in item["content"] + ): callouts.append( "".join([text_item["text"] for text_item in item["content"]]) ) @@ -34,15 +40,29 @@ def structure_activity_content_by_type(activity): # Add Paragraphs data_array.append({"Paragraphs": paragraphs}) - print(data_array) - return data_array def serialize_activity_text_to_ai_comprehensible_text( - data_array, course: CourseRead, activity: ActivityRead + data_array, + course: CourseRead, + activity: ActivityRead, + isActivityEmpty: bool = False, ): - ### Serialize the text to a format that is comprehensible by the AI + + if isActivityEmpty: + text = ( + "Use this as a context " + + 'This is a course about "' + + course.name + + '". ' + + 'This is a lecture about "' + + activity.name + + '". ' + + "There is no content yet in this lecture." + ) + + return text # Serialize Headings serialized_headings = "" @@ -51,7 +71,6 @@ def serialize_activity_text_to_ai_comprehensible_text( # Serialize Callouts serialized_callouts = "" - for callout in data_array[1]["Callouts"]: serialized_callouts += callout + " "