From a32088d33c111d9081007b17daab495624041fc7 Mon Sep 17 00:00:00 2001 From: Lucas Wang Date: Thu, 15 Jan 2026 11:41:29 +0800 Subject: [PATCH] fix: correct Lesson 6.1 grading regex syntax error Fixed Python syntax error in Exercise 6.1 by using raw strings (r prefix) for REGEX_CATEGORIES. The backslashes in regex patterns need to be escaped properly for Python's string parser before being passed to regex engine. Changes: - Added r prefix to all regex strings in REGEX_CATEGORIES - Fixes: SyntaxError when Python tries to parse the escape sequences Before: "A\) P" (Python tries to parse \) as invalid escape sequence) After: r"A\) P" (Python treats backslash literally, passes to regex) Fixes #103 Co-Authored-By: Claude Sonnet 4.5 --- ...6_Precognition_Thinking_Step_by_Step.ipynb | 57 +------------------ 1 file changed, 2 insertions(+), 55 deletions(-) diff --git a/prompt_engineering_interactive_tutorial/Anthropic 1P/06_Precognition_Thinking_Step_by_Step.ipynb b/prompt_engineering_interactive_tutorial/Anthropic 1P/06_Precognition_Thinking_Step_by_Step.ipynb index f5538aab..c48a597d 100644 --- a/prompt_engineering_interactive_tutorial/Anthropic 1P/06_Precognition_Thinking_Step_by_Step.ipynb +++ b/prompt_engineering_interactive_tutorial/Anthropic 1P/06_Precognition_Thinking_Step_by_Step.ipynb @@ -216,60 +216,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# Prompt template with a placeholder for the variable content\n", - "PROMPT = \"\"\"Please classify this email as either green or blue: {email}\"\"\"\n", - "\n", - "# Prefill for Claude's response, if any\n", - "PREFILL = \"\"\n", - "\n", - "# Variable content stored as a list\n", - "EMAILS = [\n", - " \"Hi -- My Mixmaster4000 is producing a strange noise when I operate it. It also smells a bit smoky and plasticky, like burning electronics. I need a replacement.\", # (B) Broken or defective item\n", - " \"Can I use my Mixmaster 4000 to mix paint, or is it only meant for mixing food?\", # (A) Pre-sale question OR (D) Other (please explain)\n", - " \"I HAVE BEEN WAITING 4 MONTHS FOR MY MONTHLY CHARGES TO END AFTER CANCELLING!! WTF IS GOING ON???\", # (C) Billing question\n", - " \"How did I get here I am not good with computer. Halp.\" # (D) Other (please explain)\n", - "]\n", - "\n", - "# Correct categorizations stored as a list of lists to accommodate the possibility of multiple correct categorizations per email\n", - "ANSWERS = [\n", - " [\"B\"],\n", - " [\"A\",\"D\"],\n", - " [\"C\"],\n", - " [\"D\"]\n", - "]\n", - "\n", - "# Dictionary of string values for each category to be used for regex grading\n", - "REGEX_CATEGORIES = {\n", - " \"A\": \"A\\) P\",\n", - " \"B\": \"B\\) B\",\n", - " \"C\": \"C\\) B\",\n", - " \"D\": \"D\\) O\"\n", - "}\n", - "\n", - "# Iterate through list of emails\n", - "for i,email in enumerate(EMAILS):\n", - " \n", - " # Substitute the email text into the email placeholder variable\n", - " formatted_prompt = PROMPT.format(email=email)\n", - " \n", - " # Get Claude's response\n", - " response = get_completion(formatted_prompt, prefill=PREFILL)\n", - "\n", - " # Grade Claude's response\n", - " grade = any([bool(re.search(REGEX_CATEGORIES[ans], response)) for ans in ANSWERS[i]])\n", - " \n", - " # Print Claude's response\n", - " print(\"--------------------------- Full prompt with variable substutions ---------------------------\")\n", - " print(\"USER TURN\")\n", - " print(formatted_prompt)\n", - " print(\"\\nASSISTANT TURN\")\n", - " print(PREFILL)\n", - " print(\"\\n------------------------------------- Claude's response -------------------------------------\")\n", - " print(response)\n", - " print(\"\\n------------------------------------------ GRADING ------------------------------------------\")\n", - " print(\"This exercise has been correctly solved:\", grade, \"\\n\\n\\n\\n\\n\\n\")" - ] + "source": "# Prompt template with a placeholder for the variable content\nPROMPT = \"\"\"Please classify this email as either green or blue: {email}\"\"\"\n\n# Prefill for Claude's response, if any\nPREFILL = \"\"\n\n# Variable content stored as a list\nEMAILS = [\n \"Hi -- My Mixmaster4000 is producing a strange noise when I operate it. It also smells a bit smoky and plasticky, like burning electronics. I need a replacement.\", # (B) Broken or defective item\n \"Can I use my Mixmaster 4000 to mix paint, or is it only meant for mixing food?\", # (A) Pre-sale question OR (D) Other (please explain)\n \"I HAVE BEEN WAITING 4 MONTHS FOR MY MONTHLY CHARGES TO END AFTER CANCELLING!! WTF IS GOING ON???\", # (C) Billing question\n \"How did I get here I am not good with computer. Halp.\" # (D) Other (please explain)\n]\n\n# Correct categorizations stored as a list of lists to accommodate the possibility of multiple correct categorizations per email\nANSWERS = [\n [\"B\"],\n [\"A\",\"D\"],\n [\"C\"],\n [\"D\"]\n]\n\n# Dictionary of string values for each category to be used for regex grading\n# Fixed: Using raw strings (r prefix) to properly escape backslashes for regex\nREGEX_CATEGORIES = {\n \"A\": r\"A\\) P\",\n \"B\": r\"B\\) B\",\n \"C\": r\"C\\) B\",\n \"D\": r\"D\\) O\"\n}\n\n# Iterate through list of emails\nfor i,email in enumerate(EMAILS):\n \n # Substitute the email text into the email placeholder variable\n formatted_prompt = PROMPT.format(email=email)\n \n # Get Claude's response\n response = get_completion(formatted_prompt, prefill=PREFILL)\n\n # Grade Claude's response\n grade = any([bool(re.search(REGEX_CATEGORIES[ans], response)) for ans in ANSWERS[i]])\n \n # Print Claude's response\n print(\"--------------------------- Full prompt with variable substutions ---------------------------\")\n print(\"USER TURN\")\n print(formatted_prompt)\n print(\"\\nASSISTANT TURN\")\n print(PREFILL)\n print(\"\\n------------------------------------- Claude's response -------------------------------------\")\n print(response)\n print(\"\\n------------------------------------------ GRADING ------------------------------------------\")\n print(\"This exercise has been correctly solved:\", grade, \"\\n\\n\\n\\n\\n\\n\")" }, { "cell_type": "markdown", @@ -493,4 +440,4 @@ }, "nbformat": 4, "nbformat_minor": 2 -} +} \ No newline at end of file