fix: empty activities AI issue

This commit is contained in:
swve 2024-02-05 21:47:02 +01:00
parent bc84d03ac4
commit 5ed05ade09
3 changed files with 34 additions and 9 deletions

View file

@ -67,8 +67,11 @@ def ai_start_activity_chat_session(
# Serialize Activity Content Blocks to a text comprehensible by the AI # Serialize Activity Content Blocks to a text comprehensible by the AI
structured = structure_activity_content_by_type(content) structured = structure_activity_content_by_type(content)
isEmpty = structured == []
ai_friendly_text = serialize_activity_text_to_ai_comprehensible_text( ai_friendly_text = serialize_activity_text_to_ai_comprehensible_text(
structured, course, activity structured, course, activity, isActivityEmpty=isEmpty
) )
# Get Activity Organization # Get Activity Organization

View file

@ -25,7 +25,7 @@ async def create_activity(
current_user: PublicUser | AnonymousUser, current_user: PublicUser | AnonymousUser,
db_session: Session, db_session: Session,
): ):
activity = Activity.from_orm(activity_object)
# CHeck if org exists # CHeck if org exists
statement = select(Chapter).where(Chapter.id == activity_object.chapter_id) statement = select(Chapter).where(Chapter.id == activity_object.chapter_id)
@ -40,6 +40,9 @@ async def create_activity(
# RBAC check # RBAC check
await rbac_check(request, chapter.chapter_uuid, current_user, "create", db_session) 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.activity_uuid = str(f"activity_{uuid4()}")
activity.creation_date = str(datetime.now()) activity.creation_date = str(datetime.now())
activity.update_date = str(datetime.now()) activity.update_date = str(datetime.now())

View file

@ -4,6 +4,10 @@ from src.db.courses import CourseRead
def structure_activity_content_by_type(activity): 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 ### 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"] content = activity["content"]
headings = [] headings = []
@ -11,10 +15,12 @@ def structure_activity_content_by_type(activity):
paragraphs = [] paragraphs = []
for item in content: for item in content:
if 'content' in item: if "content" in item:
if item["type"] == "heading" and "text" in item["content"][0]: if item["type"] == "heading" and "text" in item["content"][0]:
headings.append(item["content"][0]["text"]) 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( callouts.append(
"".join([text_item["text"] for text_item in item["content"]]) "".join([text_item["text"] for text_item in item["content"]])
) )
@ -34,15 +40,29 @@ def structure_activity_content_by_type(activity):
# Add Paragraphs # Add Paragraphs
data_array.append({"Paragraphs": paragraphs}) data_array.append({"Paragraphs": paragraphs})
print(data_array)
return data_array return data_array
def serialize_activity_text_to_ai_comprehensible_text( 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 # Serialize Headings
serialized_headings = "" serialized_headings = ""
@ -51,7 +71,6 @@ def serialize_activity_text_to_ai_comprehensible_text(
# Serialize Callouts # Serialize Callouts
serialized_callouts = "" serialized_callouts = ""
for callout in data_array[1]["Callouts"]: for callout in data_array[1]["Callouts"]:
serialized_callouts += callout + " " serialized_callouts += callout + " "