n+1 in yaml to accumulate integers.

modified:   ../app.py
	new file:   activity20-n-plus-1.yaml
	modified:   guarded_ai.py
This commit is contained in:
Russell Ballestrini 2024-08-10 14:39:37 -04:00
parent 87faba6379
commit e3d3bf0303
3 changed files with 133 additions and 10 deletions

16
app.py
View file

@ -2077,9 +2077,11 @@ def handle_activity_response(room_name, user_response, username):
step.get("tokens_for_ai", ""),
)
transition = step["transitions"].get(category, None)
if transition is None:
if category in step["transitions"]:
transition = step["transitions"][category]
elif int(category) in step["transitions"]:
transition = step["transitions"][int(category)]
else:
# Emit an error message and return early
socketio.emit(
"message",
@ -2132,7 +2134,9 @@ def handle_activity_response(room_name, user_response, username):
)
new_message = Message(
username="System", content=options_message, room_id=room.id
username="System",
content=options_message,
room_id=room.id,
)
db.session.add(new_message)
db.session.commit()
@ -2160,6 +2164,8 @@ def handle_activity_response(room_name, user_response, username):
for key, value in transition["metadata_add"].items():
if value == "the-users-response":
value = user_response
elif value == "n+1":
value = activity_state.dict_metadata.get(key, 0) + 1
new_metadata[key] = value
activity_state.add_metadata(key, value)
@ -2494,7 +2500,7 @@ def get_next_step(activity_content, current_section_id, current_step_id):
# Categorize the user's response.
def categorize_response(question, response, buckets, tokens_for_ai):
openai_client, model_name = get_openai_client_and_model()
bucket_list = ", ".join(buckets)
bucket_list = ", ".join([str(bucket) for bucket in buckets])
messages = [
{
"role": "system",

View file

@ -0,0 +1,102 @@
default_max_attempts_per_step: 3
sections:
- section_id: "section_1"
title: "History Quiz Challenge"
steps:
- step_id: "step_1"
title: "Question 1"
content_blocks:
- "Welcome to the History Quiz Challenge! 🏆"
- "Let's see how well you know your history. Answer the following questions:"
question: "Who was the first President of the United States? 🇺🇸"
buckets:
- george_washington
- incorrect
transitions:
george_washington:
content_blocks:
- "Correct! George Washington was the first President of the United States."
metadata_add:
correct_answers: "n+1"
next_section_and_step: "section_1:step_2"
incorrect:
content_blocks:
- "That's not correct. The first President was George Washington."
metadata_add:
incorrect_attempts: "n+1"
next_section_and_step: "section_1:step_2"
- step_id: "step_2"
title: "Question 2"
question: "What year did the Titanic sink? 🚢"
buckets:
- 1912
- incorrect
transitions:
1912:
content_blocks:
- "Correct! The Titanic sank in 1912."
metadata_add:
correct_answers: "n+1"
next_section_and_step: "section_1:step_3"
incorrect:
content_blocks:
- "That's not correct. The Titanic sank in 1912."
metadata_add:
incorrect_attempts: "n+1"
next_section_and_step: "section_1:step_3"
- step_id: "step_3"
title: "Question 3"
question: "Who painted the Mona Lisa? 🎨"
buckets:
- leonardo_da_vinci
- incorrect
transitions:
leonardo_da_vinci:
content_blocks:
- "Correct! Leonardo da Vinci painted the Mona Lisa."
metadata_add:
correct_answers: "n+1"
next_section_and_step: "section_2:step_1"
incorrect:
content_blocks:
- "That's not correct. The Mona Lisa was painted by Leonardo da Vinci."
metadata_add:
incorrect_attempts: "n+1"
next_section_and_step: "section_2:step_1"
- section_id: "section_2"
title: "Quiz Results"
steps:
- step_id: "step_1"
title: "Results"
content_blocks:
- "Congratulations on completing the quiz! 🎉"
- "Let's see how you did:"
- "Correct Answers: {{correct_answers}}"
- "Incorrect Attempts: {{incorrect_attempts}}"
question: "Do you want to try the quiz again or exit? Type 'retry' to start over or 'exit' to finish."
buckets:
- retry
- exit
transitions:
retry:
content_blocks:
- "Great! Let's start the quiz again. 🏆"
metadata_remove:
- correct_answers
- incorrect_attempts
next_section_and_step: "section_1:step_1"
exit:
content_blocks:
- "Thank you for playing the History Quiz Challenge! Have a great day! 🌟"
next_section_and_step: "section_3:step_1"
- section_id: "section_3"
title: "Goodbye"
steps:
- step_id: "step_1"
title: "Exit"
content_blocks:
- "Thank you for participating! We hope you enjoyed the quiz. Goodbye! 👋"

View file

@ -6,11 +6,13 @@ from openai import OpenAI
client = OpenAI()
# Load the YAML activity file
def load_yaml_activity(file_path):
with open(file_path, "r") as file:
return yaml.safe_load(file)
# Categorize the user's response using gpt-4o-mini
def categorize_response(question, response, buckets, tokens_for_ai):
bucket_list = ", ".join(buckets)
@ -39,6 +41,7 @@ def categorize_response(question, response, buckets, tokens_for_ai):
except Exception as e:
return f"Error: {e}"
# Generate AI feedback using gpt-4o-mini
def generate_ai_feedback(category, question, user_response, tokens_for_ai):
messages = [
@ -61,8 +64,11 @@ def generate_ai_feedback(category, question, user_response, tokens_for_ai):
except Exception as e:
return f"Error: {e}"
# Provide feedback based on the category
def provide_feedback(transition, category, question, user_response, user_language, tokens_for_ai):
def provide_feedback(
transition, category, question, user_response, user_language, tokens_for_ai
):
feedback = ""
if "ai_feedback" in transition:
tokens_for_ai += f" Provide the feedback in {user_language}. {transition['ai_feedback'].get('tokens_for_ai', '')}."
@ -73,6 +79,7 @@ def provide_feedback(transition, category, question, user_response, user_languag
return feedback
def get_next_section_and_step(activity_content, current_section_id, current_step_id):
for section in activity_content["sections"]:
if section["section_id"] == current_section_id:
@ -95,6 +102,7 @@ def get_next_section_and_step(activity_content, current_section_id, current_step
)
return None, None
def translate_text(text, target_language):
# Guard clause for default language
if target_language.lower() == "english":
@ -120,6 +128,7 @@ def translate_text(text, target_language):
except Exception as e:
return f"Error: {e}"
def simulate_activity(yaml_file_path):
yaml_content = load_yaml_activity(yaml_file_path)
max_attempts = yaml_content.get("default_max_attempts_per_step", 3)
@ -200,7 +209,12 @@ def simulate_activity(yaml_file_path):
print(translated_transition_content)
feedback = provide_feedback(
transition, category, question, user_response, user_language, step["tokens_for_ai"]
transition,
category,
question,
user_response,
user_language,
step["tokens_for_ai"],
)
print(f"\nFeedback: {feedback}")
@ -212,6 +226,8 @@ def simulate_activity(yaml_file_path):
for key, value in transition["metadata_add"].items():
if value == "the-users-response":
value = user_response
elif value == "n+1":
value = metadata.get(key, 0) + 1
metadata[key] = value
if "metadata_tmp_add" in transition:
@ -228,9 +244,7 @@ def simulate_activity(yaml_file_path):
# Handle metadata_random
if "metadata_random" in transition:
random_key = random.choice(
list(transition["metadata_random"].keys())
)
random_key = random.choice(list(transition["metadata_random"].keys()))
random_value = transition["metadata_random"][random_key]
metadata[random_key] = random_value
@ -273,6 +287,7 @@ def simulate_activity(yaml_file_path):
yaml_content, current_section_id, current_step_id
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Simulate an activity.")
parser.add_argument(