{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "toc_visible": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# 📋 Day 1: In-Class Assignment\n",
        "# LLM Business Application Prototype\n",
        "\n",
        "## Time: 90 minutes\n",
        "\n",
        "---\n",
        "\n",
        "## Scenario\n",
        "\n",
        "You are a consultant hired by **RetailMax**, a mid-size retail company with 50 stores.\n",
        "They want to understand how Large Language Models could help their business.\n",
        "\n",
        "Your task is to create a **working prototype** demonstrating one valuable LLM application.\n",
        "\n",
        "---\n",
        "\n",
        "## Deliverables\n",
        "\n",
        "| # | Deliverable | Points |\n",
        "|---|-------------|--------|\n",
        "| 1 | Working implementation | 30 |\n",
        "| 2 | Business value explanation | 25 |\n",
        "| 3 | Three example inputs/outputs | 25 |\n",
        "| 4 | Limitations and improvements | 20 |\n",
        "| | **Total** | **100** |\n",
        "\n",
        "---\n",
        "\n",
        "## Choose Your Application\n",
        "\n",
        "Select ONE of the following (or propose your own with instructor approval):\n",
        "\n",
        "1. **Customer Review Response Generator** - Generate professional responses to customer reviews\n",
        "2. **Product Description Writer** - Create descriptions from product specifications\n",
        "3. **Internal Memo Summarizer** - Summarize company memos for busy executives\n",
        "4. **Job Posting Generator** - Create job postings from basic requirements\n",
        "5. **Your Own Idea** - Get instructor approval first\n",
        "\n",
        "---\n",
        "\n",
        "## 🎯 Quality Checklist\n",
        "\n",
        "Before submitting, verify your outputs meet these standards:\n",
        "\n",
        "- [ ] Output follows the required format\n",
        "- [ ] No invented facts (only uses information from input)\n",
        "- [ ] Missing info is labeled or handled gracefully\n",
        "- [ ] Output is concise and within constraints\n",
        "- [ ] Spot-checked at least 2 outputs manually\n",
        "- [ ] Edge cases considered (what if input is unusual?)"
      ],
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Setup"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "!pip -q install -U google-genai"
      ],
      "metadata": {},
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "import json\n",
        "import time\n",
        "import re\n",
        "from datetime import datetime, timezone\n",
        "from google import genai\n",
        "from google.genai import types\n",
        "\n",
        "# --- Load API key ---\n",
        "try:\n",
        "    from google.colab import userdata\n",
        "    os.environ[\"GEMINI_API_KEY\"] = userdata.get(\"GEMINI_API_KEY\")\n",
        "except Exception:\n",
        "    pass\n",
        "\n",
        "if not os.environ.get(\"GEMINI_API_KEY\"):\n",
        "    import getpass\n",
        "    os.environ[\"GEMINI_API_KEY\"] = getpass.getpass(\"Paste your GEMINI_API_KEY (input hidden): \")\n",
        "\n",
        "# Initialize client\n",
        "client = genai.Client()\n",
        "MODEL_ID = \"gemini-2.0-flash-lite\"\n",
        "\n",
        "# --- Prompt Logging ---\n",
        "PROMPT_LOG = []\n",
        "\n",
        "def _now():\n",
        "    return datetime.now(timezone.utc).isoformat().replace('+00:00', 'Z')\n",
        "\n",
        "def generate(prompt, temperature=0.7, max_tokens=500, log=True):\n",
        "    \"\"\"Generate text using Gemini with logging.\"\"\"\n",
        "    t0 = time.time()\n",
        "    response = client.models.generate_content(\n",
        "        model=MODEL_ID,\n",
        "        contents=prompt,\n",
        "        config=types.GenerateContentConfig(\n",
        "            temperature=temperature,\n",
        "            max_output_tokens=max_tokens\n",
        "        )\n",
        "    )\n",
        "    text = response.text or \"\"\n",
        "    latency = time.time() - t0\n",
        "\n",
        "    if log:\n",
        "        PROMPT_LOG.append({\n",
        "            \"ts\": _now(),\n",
        "            \"temperature\": temperature,\n",
        "            \"prompt\": prompt,\n",
        "            \"output\": text,\n",
        "            \"latency_s\": round(latency, 3)\n",
        "        })\n",
        "    return text\n",
        "\n",
        "def try_parse_json(text):\n",
        "    \"\"\"Attempt to parse JSON from LLM output.\"\"\"\n",
        "    try:\n",
        "        return True, json.loads(text)\n",
        "    except json.JSONDecodeError:\n",
        "        pass\n",
        "    json_match = re.search(r'```(?:json)?\\s*([\\s\\S]*?)```', text)\n",
        "    if json_match:\n",
        "        try:\n",
        "            return True, json.loads(json_match.group(1))\n",
        "        except json.JSONDecodeError:\n",
        "            pass\n",
        "    json_match = re.search(r'[\\[\\{][\\s\\S]*[\\]\\}]', text)\n",
        "    if json_match:\n",
        "        try:\n",
        "            return True, json.loads(json_match.group())\n",
        "        except json.JSONDecodeError:\n",
        "            pass\n",
        "    return False, None\n",
        "\n",
        "print(\"✅ Setup complete!\")"
      ],
      "metadata": {},
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "---\n",
        "\n",
        "## Part 1: Business Value Explanation (25 points)\n",
        "\n",
        "### 1.1 Application Name and Description\n",
        "\n",
        "**Application Name:** [Your application name]\n",
        "\n",
        "**Brief Description:** [1-2 sentences describing what it does]\n",
        "\n",
        "### 1.2 Business Problem Solved\n",
        "\n",
        "**Current Pain Point:** [Describe the current manual process and its problems]\n",
        "\n",
        "**Who Benefits:** [Which roles/departments will use this?]\n",
        "\n",
        "### 1.3 Expected Value\n",
        "\n",
        "**Time Savings:** [e.g., \"Reduces task time from 15 minutes to 2 minutes\"]\n",
        "\n",
        "**Quality Improvement:** [e.g., \"More consistent tone across all responses\"]\n",
        "\n",
        "**Other Benefits:** [e.g., \"Enables 24/7 response capability\"]"
      ],
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "---\n",
        "\n",
        "## Part 2: Implementation (30 points)"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "# YOUR MAIN FUNCTION\n",
        "#\n",
        "# Requirements:\n",
        "# - Clear function name and docstring\n",
        "# - Well-structured prompt with explicit rules\n",
        "# - Include \"do not invent facts\" instruction\n",
        "# - Appropriate temperature setting\n",
        "\n",
        "def your_application_function(input_parameter):\n",
        "    \"\"\"\n",
        "    [Describe what your function does]\n",
        "\n",
        "    Args:\n",
        "        input_parameter: [Describe the input]\n",
        "\n",
        "    Returns:\n",
        "        str: [Describe the output]\n",
        "    \"\"\"\n",
        "\n",
        "    prompt = f\"\"\"[Your role/context here]\n",
        "\n",
        "[Your task description here]\n",
        "\n",
        "Input:\n",
        "{input_parameter}\n",
        "\n",
        "Rules:\n",
        "- Do not invent facts not present in the input\n",
        "- [Add other specific rules]\n",
        "- [Specify output format]\n",
        "\n",
        "Output:\"\"\"\n",
        "\n",
        "    temperature = 0.5  # Adjust based on task (lower for consistency, higher for creativity)\n",
        "\n",
        "    return generate(prompt, temperature=temperature)"
      ],
      "metadata": {},
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "---\n",
        "\n",
        "## Part 3: Example Inputs/Outputs (25 points)\n",
        "\n",
        "### Example 1: Typical Use Case"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "example1_input = \"\"\"[Your first example input - a typical case]\"\"\"\n",
        "\n",
        "print(\"=\"*60)\n",
        "print(\"EXAMPLE 1: Typical Use Case\")\n",
        "print(\"=\"*60)\n",
        "print(\"\\n📥 INPUT:\")\n",
        "print(example1_input)\n",
        "print(\"\\n📤 OUTPUT:\")\n",
        "print(your_application_function(example1_input))"
      ],
      "metadata": {},
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Example 1 Quality Assessment:**\n",
        "- Does output follow format? [Yes/No]\n",
        "- Any invented facts? [Yes/No]\n",
        "- Overall quality: [Good/Needs improvement]\n",
        "- Notes: [Your observations]\n",
        "\n",
        "### Example 2: Different Scenario"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "example2_input = \"\"\"[Your second example - a different scenario]\"\"\"\n",
        "\n",
        "print(\"=\"*60)\n",
        "print(\"EXAMPLE 2: Different Scenario\")\n",
        "print(\"=\"*60)\n",
        "print(\"\\n📥 INPUT:\")\n",
        "print(example2_input)\n",
        "print(\"\\n📤 OUTPUT:\")\n",
        "print(your_application_function(example2_input))"
      ],
      "metadata": {},
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Example 2 Quality Assessment:**\n",
        "- Does output follow format? [Yes/No]\n",
        "- Any invented facts? [Yes/No]\n",
        "- Overall quality: [Good/Needs improvement]\n",
        "- Notes: [Your observations]\n",
        "\n",
        "### Example 3: Edge Case"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "example3_input = \"\"\"[Your third example - an edge case or challenging input]\"\"\"\n",
        "\n",
        "print(\"=\"*60)\n",
        "print(\"EXAMPLE 3: Edge Case\")\n",
        "print(\"=\"*60)\n",
        "print(\"\\n📥 INPUT:\")\n",
        "print(example3_input)\n",
        "print(\"\\n📤 OUTPUT:\")\n",
        "print(your_application_function(example3_input))"
      ],
      "metadata": {},
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Example 3 Quality Assessment:**\n",
        "- Does output follow format? [Yes/No]\n",
        "- Any invented facts? [Yes/No]\n",
        "- How did it handle the edge case? [Description]\n",
        "- Notes: [Your observations]"
      ],
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "---\n",
        "\n",
        "## Part 4: Limitations and Improvements (20 points)\n",
        "\n",
        "### 4.1 Current Limitations\n",
        "\n",
        "List at least 3 limitations of your current implementation:\n",
        "\n",
        "1. **Limitation 1:** [e.g., \"May hallucinate details not in input\"]\n",
        "\n",
        "2. **Limitation 2:** [e.g., \"Output length is inconsistent\"]\n",
        "\n",
        "3. **Limitation 3:** [e.g., \"Doesn't handle non-English input\"]\n",
        "\n",
        "### 4.2 Potential Improvements\n",
        "\n",
        "How would you improve this for production use?\n",
        "\n",
        "1. **Technical:** [e.g., \"Add output validation to check format\"]\n",
        "\n",
        "2. **Business:** [e.g., \"Integrate with CRM to pull customer history\"]\n",
        "\n",
        "3. **Quality:** [e.g., \"Add human review step for negative sentiment\"]\n",
        "\n",
        "### 4.3 When NOT to Use This Tool\n",
        "\n",
        "Describe scenarios where this tool should NOT be used without human review:\n",
        "\n",
        "[Your answer here]"
      ],
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "---\n",
        "\n",
        "## Export Your Work"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "import pandas as pd\n",
        "\n",
        "# Save prompt log\n",
        "df = pd.DataFrame(PROMPT_LOG)\n",
        "df.to_csv(\"day1_assignment_prompt_log.csv\", index=False)\n",
        "print(f\"✅ Saved {len(df)} prompts to: day1_assignment_prompt_log.csv\")"
      ],
      "metadata": {},
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "---\n",
        "\n",
        "## ✅ Submission Checklist\n",
        "\n",
        "Before submitting, ensure you have:\n",
        "\n",
        "- [ ] Completed business value explanation (Part 1)\n",
        "- [ ] Working implementation with clear prompt (Part 2)\n",
        "- [ ] Three diverse examples with quality assessments (Part 3)\n",
        "- [ ] Thoughtful limitations analysis (Part 4)\n",
        "- [ ] All cells executed with outputs\n",
        "- [ ] Prompt log exported\n",
        "\n",
        "---\n",
        "\n",
        "## 📊 Grading Rubric\n",
        "\n",
        "| Criterion | Excellent (90-100%) | Good (70-89%) | Needs Work (<70%) |\n",
        "|-----------|---------------------|---------------|-------------------|\n",
        "| **Implementation (30)** | Code runs perfectly, well-structured prompt with rules | Code runs with minor issues | Code has errors |\n",
        "| **Business Value (25)** | Clear, specific value with quantified benefits | Value present but vague | Weak justification |\n",
        "| **Examples (25)** | 3 diverse examples with quality assessments | 3 examples but limited analysis | Fewer than 3 |\n",
        "| **Limitations (20)** | Thoughtful, specific with actionable improvements | Generic limitations | Missing |\n",
        "\n",
        "**Score:** ___ / 100"
      ],
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "---\n",
        "\n",
        "# 📎 APPENDIX: Sample Solutions\n",
        "\n",
        "Use for reference AFTER attempting your own solution."
      ],
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Sample A: Customer Review Response Generator"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "def generate_review_response(review_text, rating, product_name, store_name=\"RetailMax\"):\n",
        "    \"\"\"\n",
        "    Generate a professional response to a customer review.\n",
        "\n",
        "    Args:\n",
        "        review_text (str): The customer's review\n",
        "        rating (int): Star rating (1-5)\n",
        "        product_name (str): Name of the product reviewed\n",
        "        store_name (str): Name of the store\n",
        "\n",
        "    Returns:\n",
        "        str: Professional response to the review\n",
        "    \"\"\"\n",
        "    tone_guide = {\n",
        "        5: \"enthusiastic and grateful\",\n",
        "        4: \"appreciative and warm\",\n",
        "        3: \"understanding and helpful\",\n",
        "        2: \"empathetic and solution-focused\",\n",
        "        1: \"deeply apologetic and action-oriented\"\n",
        "    }\n",
        "\n",
        "    tone = tone_guide.get(rating, tone_guide[3])\n",
        "\n",
        "    prompt = f\"\"\"You are a customer service representative for {store_name}.\n",
        "Write a response to this customer review.\n",
        "\n",
        "Product: {product_name}\n",
        "Rating: {rating}/5 stars\n",
        "Review: {review_text}\n",
        "\n",
        "Be {tone}.\n",
        "\n",
        "Rules:\n",
        "- Thank them for feedback\n",
        "- Reference specific points from their review (do not be generic)\n",
        "- If rating <= 3, offer a specific solution or next step\n",
        "- Keep under 100 words\n",
        "- Do not invent facts not in the review\n",
        "- Sign off as \"The {store_name} Team\"\n",
        "\n",
        "Response:\"\"\"\n",
        "\n",
        "    return generate(prompt, temperature=0.4)\n",
        "\n",
        "\n",
        "# Test\n",
        "print(\"=== 5-STAR ===\")\n",
        "print(generate_review_response(\n",
        "    \"Amazing headphones! Battery lasts forever!\",\n",
        "    5, \"SoundWave Pro\"\n",
        "))\n",
        "\n",
        "print(\"\\n=== 2-STAR ===\")\n",
        "print(generate_review_response(\n",
        "    \"Stopped working after 2 weeks. Very disappointed.\",\n",
        "    2, \"SoundWave Pro\"\n",
        "))"
      ],
      "metadata": {},
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Sample B: Product Description Writer"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "def generate_product_description(product_name, specifications, target_audience):\n",
        "    \"\"\"\n",
        "    Generate a product description from specifications.\n",
        "\n",
        "    Args:\n",
        "        product_name (str): Name of the product\n",
        "        specifications (dict): Product specs\n",
        "        target_audience (str): Who it's for\n",
        "\n",
        "    Returns:\n",
        "        str: Compelling product description\n",
        "    \"\"\"\n",
        "    specs_text = \"\\n\".join([f\"- {k}: {v}\" for k, v in specifications.items()])\n",
        "\n",
        "    prompt = f\"\"\"Write a product description for an e-commerce website.\n",
        "\n",
        "Product: {product_name}\n",
        "\n",
        "Specifications:\n",
        "{specs_text}\n",
        "\n",
        "Target Audience: {target_audience}\n",
        "\n",
        "Rules:\n",
        "- Engaging opening (no generic \"Introducing...\")\n",
        "- Translate specs into customer benefits\n",
        "- Include 2-3 use cases\n",
        "- Only mention features from the specifications above\n",
        "- Do not invent features not listed\n",
        "- 100-150 words\n",
        "\n",
        "Description:\"\"\"\n",
        "\n",
        "    return generate(prompt, temperature=0.6)\n",
        "\n",
        "\n",
        "# Test\n",
        "specs = {\n",
        "    \"Weight\": \"250g\",\n",
        "    \"Battery\": \"40 hours\",\n",
        "    \"Connectivity\": \"Bluetooth 5.0\",\n",
        "    \"ANC\": \"Hybrid with transparency mode\"\n",
        "}\n",
        "\n",
        "print(generate_product_description(\n",
        "    \"SoundWave Pro Headphones\",\n",
        "    specs,\n",
        "    \"Remote professionals\"\n",
        "))"
      ],
      "metadata": {},
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Sample C: Internal Memo Summarizer"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "def summarize_memo(memo_text, summary_format=\"executive\"):\n",
        "    \"\"\"\n",
        "    Summarize an internal company memo.\n",
        "\n",
        "    Args:\n",
        "        memo_text (str): Full memo\n",
        "        summary_format (str): \"executive\", \"action\", or \"team\"\n",
        "\n",
        "    Returns:\n",
        "        str: Formatted summary\n",
        "    \"\"\"\n",
        "    formats = {\n",
        "        \"executive\": \"3-sentence summary: key decision, impact, timeline.\",\n",
        "        \"action\": \"Numbered list of action items with owners and deadlines.\",\n",
        "        \"team\": \"TL;DR (1 sentence), Key Points (bullets), Action Items.\"\n",
        "    }\n",
        "\n",
        "    prompt = f\"\"\"Summarize this internal memo.\n",
        "\n",
        "Memo:\n",
        "{memo_text}\n",
        "\n",
        "Format: {formats.get(summary_format, formats['executive'])}\n",
        "\n",
        "Rules:\n",
        "- Only include information from the memo\n",
        "- If owner/deadline not mentioned, write \"Not specified\"\n",
        "- Do not invent facts\n",
        "\n",
        "Summary:\"\"\"\n",
        "\n",
        "    return generate(prompt, temperature=0.2)\n",
        "\n",
        "\n",
        "# Test\n",
        "memo = \"\"\"\n",
        "TO: All Department Heads\n",
        "FROM: CEO\n",
        "RE: Q1 2025 Initiatives\n",
        "\n",
        "We're increasing digital investment by 25% ($2M to IT).\n",
        "Sarah Chen leads this with Deloitte support.\n",
        "\n",
        "New CRM training for all stores by March 15.\n",
        "Regional managers: submit schedules to HR by Jan 25.\n",
        "\n",
        "Travel freeze until Q2. Requests over $500 need VP approval.\n",
        "\n",
        "3% cost-of-living raise effective Feb 1.\n",
        "\n",
        "Q&A session: Jan 22 at 2pm.\n",
        "\"\"\"\n",
        "\n",
        "print(\"=== EXECUTIVE ===\")\n",
        "print(summarize_memo(memo, \"executive\"))\n",
        "\n",
        "print(\"\\n=== ACTIONS ===\")\n",
        "print(summarize_memo(memo, \"action\"))"
      ],
      "metadata": {},
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Sample D: Job Posting Generator"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "def generate_job_posting(job_title, department, requirements, nice_to_have=None):\n",
        "    \"\"\"\n",
        "    Generate a job posting from requirements.\n",
        "\n",
        "    Args:\n",
        "        job_title (str): Position title\n",
        "        department (str): Department\n",
        "        requirements (list): Required qualifications\n",
        "        nice_to_have (list): Optional qualifications\n",
        "\n",
        "    Returns:\n",
        "        str: Complete job posting\n",
        "    \"\"\"\n",
        "    reqs = \"\\n\".join([f\"- {r}\" for r in requirements])\n",
        "    nice = \"\\n\".join([f\"- {n}\" for n in (nice_to_have or [])])\n",
        "\n",
        "    prompt = f\"\"\"Write a job posting for RetailMax, a mid-size retail company.\n",
        "\n",
        "Position: {job_title}\n",
        "Department: {department}\n",
        "\n",
        "Required Qualifications:\n",
        "{reqs}\n",
        "\n",
        "{\"Nice to Have:\" if nice_to_have else \"\"}\n",
        "{nice if nice_to_have else \"\"}\n",
        "\n",
        "Include these sections:\n",
        "1. Engaging opening paragraph about the role\n",
        "2. \"What You'll Do\" (5 responsibilities based on the role)\n",
        "3. \"What You'll Bring\" (from requirements above)\n",
        "4. \"Why Join Us\" (standard retail company benefits)\n",
        "\n",
        "Rules:\n",
        "- Only use requirements listed above\n",
        "- Do not add qualifications not provided\n",
        "- 300-400 words\n",
        "- Professional but approachable tone\n",
        "\n",
        "Job Posting:\"\"\"\n",
        "\n",
        "    return generate(prompt, temperature=0.5)\n",
        "\n",
        "\n",
        "# Test\n",
        "reqs = [\n",
        "    \"3+ years retail management\",\n",
        "    \"Strong leadership skills\",\n",
        "    \"Inventory management experience\",\n",
        "    \"Flexible schedule including weekends\"\n",
        "]\n",
        "\n",
        "nice = [\n",
        "    \"Bachelor's in Business\",\n",
        "    \"CRM experience\",\n",
        "    \"Bilingual English/Spanish\"\n",
        "]\n",
        "\n",
        "print(generate_job_posting(\"Store Manager\", \"Retail Operations\", reqs, nice))"
      ],
      "metadata": {},
      "execution_count": null,
      "outputs": []
    }
  ]
}
