From 7ceb9303ea9d301b07c1d3408ae42a6f9ef1ab99 Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Sun, 4 Aug 2024 08:33:55 -0400 Subject: [PATCH] metadata_add and metadata_remove to allow for items to be consumed. modified: app.py modified: research/activity14-choose-adventure.yaml modified: research/activity15-choose-adventure.yaml modified: research/activity16.yaml --- app.py | 37 +++++++++++++++-------- research/activity14-choose-adventure.yaml | 8 ++--- research/activity15-choose-adventure.yaml | 32 +++++++++++--------- research/activity16.yaml | 6 ++-- 4 files changed, 48 insertions(+), 35 deletions(-) diff --git a/app.py b/app.py index ccc4e9b..7e702fe 100644 --- a/app.py +++ b/app.py @@ -117,11 +117,17 @@ class ActivityState(db.Model): def dict_metadata(self, value): self.json_metadata = json.dumps(value) - def update_metadata(self, key, value): + def add_metadata(self, key, value): metadata = self.dict_metadata metadata[key] = value self.dict_metadata = metadata + def remove_metadata(self, key): + metadata = self.dict_metadata + if key in metadata: + del metadata[key] + self.dict_metadata = metadata + def get_room(room_name): """Utility function to get room from room name.""" @@ -1718,7 +1724,7 @@ def get_activity_content(file_path): """ if app.config["LOCAL_ACTIVITIES"]: # Load the activity YAML from a local file - with open(file_path, 'r') as file: + with open(file_path, "r") as file: activity_yaml = file.read() else: # Load the activity YAML from S3 @@ -2061,15 +2067,19 @@ def handle_activity_response(room_name, user_response, username): ) # Update metadata based on user actions - if "metadata_updates" in step["transitions"][category]: + if "metadata_add" in step["transitions"][category]: for key, value in step["transitions"][category][ - "metadata_updates" + "metadata_add" ].items(): - activity_state.update_metadata(key, value) + activity_state.add_metadata(key, value) - # Commit the changes after the loop - db.session.add(activity_state) - db.session.commit() + if "metadata_remove" in step["transitions"][category]: + for key in step["transitions"][category]["metadata_remove"]: + activity_state.remove_metadata(key) + + # Commit the changes after the loop + db.session.add(activity_state) + db.session.commit() # if "correct" or max_attempts reached. if ( @@ -2314,10 +2324,7 @@ def categorize_response(question, response, buckets, tokens_for_ai): temperature=0, ) category = ( - completion.choices[0] - .message.content.strip() - .lower() - .replace(" ", "_") + completion.choices[0].message.content.strip().lower().replace(" ", "_") ) return category except Exception as e: @@ -2384,7 +2391,11 @@ if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--profile", help="AWS profile name", default=None) - parser.add_argument("--local-activities", action="store_true", help="Use local activity files instead of S3") + parser.add_argument( + "--local-activities", + action="store_true", + help="Use local activity files instead of S3", + ) args = parser.parse_args() # Set profile_name as a global attribute of the app object diff --git a/research/activity14-choose-adventure.yaml b/research/activity14-choose-adventure.yaml index 9d77ad4..6965ba7 100644 --- a/research/activity14-choose-adventure.yaml +++ b/research/activity14-choose-adventure.yaml @@ -30,7 +30,7 @@ sections: next_section_and_step: "section_2:step_1" ai_feedback: tokens_for_ai: "Provide positive reinforcement and guide the user to make the next choice. Use emojis like πŸ‘ and 🌟." - metadata_updates: + metadata_add: key: true examine_book: content_blocks: @@ -40,7 +40,7 @@ sections: next_section_and_step: "section_3:step_1" ai_feedback: tokens_for_ai: "Provide positive reinforcement and guide the user to make the next choice. Use emojis like πŸ‘ and 🌟." - metadata_updates: + metadata_add: password: true try_open_safe: content_blocks: @@ -86,7 +86,7 @@ sections: next_section_and_step: "section_1:step_1" ai_feedback: tokens_for_ai: "Provide positive reinforcement and guide the user to make the next choice. Use emojis like πŸ‘ and 🌟." - metadata_updates: + metadata_add: key: true continue_exploring: content_blocks: @@ -139,7 +139,7 @@ sections: next_section_and_step: "section_1:step_1" ai_feedback: tokens_for_ai: "Provide positive reinforcement and guide the user to make the next choice. Use emojis like πŸ‘ and 🌟." - metadata_updates: + metadata_add: password: true continue_exploring: content_blocks: diff --git a/research/activity15-choose-adventure.yaml b/research/activity15-choose-adventure.yaml index ec30243..6d1e2b5 100644 --- a/research/activity15-choose-adventure.yaml +++ b/research/activity15-choose-adventure.yaml @@ -72,7 +72,7 @@ sections: content_blocks: - "You chose to take the key. πŸ”‘" next_section_and_step: "section_1:step_1" - metadata_updates: + metadata_add: key: true continue_exploring: next_section_and_step: "section_1:step_1" @@ -84,7 +84,7 @@ sections: next_section_and_step: "section_1:step_1" ai_feedback: tokens_for_ai: "The player found a hidden coin. Congratulate them by name with your feedback. Use emojis like πŸ‘ and 🌟." - metadata_updates: + metadata_add: coin: true go_back: next_section_and_step: "section_1:step_1" @@ -121,7 +121,7 @@ sections: content_blocks: - "You chose to take note of the password. πŸ”‘" next_section_and_step: "section_1:step_1" - metadata_updates: + metadata_add: password: true continue_exploring: next_section_and_step: "section_1:step_1" @@ -133,7 +133,7 @@ sections: next_section_and_step: "section_1:step_1" ai_feedback: tokens_for_ai: "The player found a hidden paper with a cryptic message. Congratulate them by name with your feedback. Use emojis like πŸ‘ and 🌟." - metadata_updates: + metadata_add: paper: true go_back: next_section_and_step: "section_1:step_1" @@ -175,7 +175,7 @@ sections: next_section_and_step: "section_4:step_1" ai_feedback: tokens_for_ai: "Provide positive reinforcement and guide the user to the final encounter. Be flexible to classify actions that lead to finding hidden items. Use emojis like πŸ‘ and 🌟." - metadata_updates: + metadata_add: second_safe: true exit_key: true continue_exploring: @@ -188,6 +188,8 @@ sections: metadata_conditions: coin: true second_safe: true + metadata_remove: + - coin next_section_and_step: "section_5:step_1" ai_feedback: tokens_for_ai: "Provide positive reinforcement and guide the user to the final encounter. Be flexible to classify actions that lead to finding hidden items. Use emojis like πŸ‘ and 🌟." @@ -267,52 +269,52 @@ sections: prize_1: content_blocks: - "You won Prize 1: A golden keychain. πŸ—οΈ" - metadata_updates: + metadata_add: prize: golden_keychain prize_2: content_blocks: - "You won Prize 2: A mysterious amulet. 🧿" - metadata_updates: + metadata_add: prize: mysterious_amulet prize_3: content_blocks: - "You won Prize 3: A rare gemstone. πŸ’Ž" - metadata_updates: + metadata_add: prize: rare_gemstone prize_4: content_blocks: - "You won Prize 4: An ancient scroll. πŸ“œ" - metadata_updates: + metadata_add: prize: ancient_scroll prize_5: content_blocks: - "You won Prize 5: A magical wand. πŸͺ„" - metadata_updates: + metadata_add: prize: magical_wand prize_6: content_blocks: - "You won Prize 6: A treasure map. πŸ—ΊοΈ" - metadata_updates: + metadata_add: prize: treasure_map prize_7: content_blocks: - "You won Prize 7: A silver coin. πŸͺ™" - metadata_updates: + metadata_add: prize: silver_coin prize_8: content_blocks: - "You won Prize 8: A mystical ring. πŸ’" - metadata_updates: + metadata_add: prize: mystical_ring prize_9: content_blocks: - "You won Prize 9: A rare book. πŸ“š" - metadata_updates: + metadata_add: prize: rare_book prize_10: content_blocks: - "You won Prize 10: A magical potion. πŸ§ͺ" - metadata_updates: + metadata_add: prize: magical_potion - section_id: "section_7" diff --git a/research/activity16.yaml b/research/activity16.yaml index 882171f..becd264 100644 --- a/research/activity16.yaml +++ b/research/activity16.yaml @@ -116,7 +116,7 @@ sections: next_section_and_step: "introduction:intro_step_2" ai_feedback: tokens_for_ai: "Provide positive reinforcement and guide the user back to the introduction. Use emojis like πŸ‘ and 🌟." - metadata_updates: + metadata_add: lesson_1_completed: true not_understand: content_blocks: @@ -188,7 +188,7 @@ sections: next_section_and_step: "introduction:intro_step_2" ai_feedback: tokens_for_ai: "Provide positive reinforcement and guide the user back to the introduction. Use emojis like πŸ‘ and 🌟." - metadata_updates: + metadata_add: lesson_2_completed: true not_understand: content_blocks: @@ -260,7 +260,7 @@ sections: next_section_and_step: "introduction:intro_step_2" ai_feedback: tokens_for_ai: "Provide positive reinforcement and guide the user back to the introduction. Use emojis like πŸ‘ and 🌟." - metadata_updates: + metadata_add: lesson_3_completed: true not_understand: content_blocks: