In this lab, you’ll design prompts to guide LLMs (Large Language Models) to produce specific, reliable, and well-structured outputs. Each exercise focuses on a different prompting technique that you will test, refine, and improve over time. Use trial and error to discover how your wording influences the AI’s responses.

Make sure you complete this exercise after attending the class on July 31 (see the slides in /topics/gpt-prompt-engineering/).


Starter Code Provided

You are given a Maven project containing:

  • All necessary dependencies.
  • Pre-written code and tests to validate your prompts.

The code and API used to invoke GPT is exactly the same that you will use for the course project!

Your task is to write prompts that pass the five JUnit tests provided. You only edit prompt files, not the test or main code. If you want you can only change the temperature and top-p values in the tests.

Instructions

  1. Fork the Prompt Engineering repository to your GitHub account, then clone your fork.
  2. Import the project into your preferred IDE (see the FAQ if you need help).
  3. Add the file apiproxy.config with your token and email address, just like you did for the Alpha. This is required to invoke GPT.
  4. Locate the test class src/test/java/nz/ac/auckland/se206/LabExercisesTest.java. Right-click on it in VS Code and choose Run Tests.

    Note: This file is in the test folder, not the main folder. Alternatively, you can run all tests using Maven:

    • Windows: .\mvnw.cmd clean test
    • Linux/Mac: ./mvnw clean test
  5. You will initially see a BUILD FAILURE. Example output:
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] LabExercisesTest.testExercise1FewShotLearning:36 Response should be exactly 'positive', 'negative', or 'neutral' ==> expected: <true> but was: <false>
[ERROR] LabExercisesTest.testExercise2JsonOutput:57 Response is not a valid JSON object: .....
[ERROR] LabExercisesTest.testExercise3RolePlaying:78 Response should mention evidence ==> expected: <true> but was: <false>
[ERROR] LabExercisesTest.testExercise4ChainOfThought:97 Response should show step-by-step reasoning ==> expected: <true> but was: <false>
[ERROR] LabExercisesTest.testExercise5TemplateGeneration:112 Email should have proper salutation ==> expected: <true> but was: <false>
[INFO]
[ERROR] Tests run: 5, Failures: 5, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 18.346 s
  1. Your task is to edit only the prompt files in src/main/resources/prompts. There is one file per exercise.
  2. Work on the tests one by one until each passes.
    • To save GPT tokens, you can right-click on a single test method in VS Code and choose Run Test at Cursor, rather than running the entire test suite repeatedly.

Exercise 1: Few-Shot Learning (Review Classification)

Goal: Teach the AI to classify movie reviews using examples.

Write your prompt in:
src/main/resources/prompts/exercise1_fewshot.txt

Requirements:

  • The AI must output exactly one word: "positive", "negative", or "neutral".
  • Learn from the few examples you provide (few-shot learning).
  • Do not include any extra text beyond the single word classification.

Exercise 2: Structured JSON Output (Movie Information)

Goal: Make the AI return strictly structured data about a movie.

Write your prompt in:
src/main/resources/prompts/exercise2_json.txt

Requirements:

  • Output only valid JSON, nothing else.
  • Include the following fields:
    • "title" (string)
    • "year" (number)
    • "director" (string)
    • "rating" (number)
  • Maintain correct data types and avoid extra explanations or text.

Exercise 3: Role-Playing (Detective Investigation)

Goal: Instruct the AI to act like a detective investigating a crime.

Write your prompt in:
src/main/resources/prompts/exercise3_detective.txt

Requirements:

  • Stay fully in character as a detective.
  • Present:
    • Evidence found at the crime scene.
    • Potential suspects.
    • Investigation theories.
  • Maintain a detective-like tone, vocabulary, and writing style.

Exercise 4: Reasoned Math Solution (Show Working)

Goal: Make the AI show its reasoning process for solving a math problem.

Write your prompt in:
src/main/resources/prompts/exercise4_math.txt

Requirements:

  • Break the problem into logical steps.
  • Show all calculations explicitly.
  • Provide the final answer in the correct format.
  • The response should show each step.
  • The final solution must include $14 as part of the result.

Exercise 5: Template Generation (Email Writing)

Goal: Create a prompt that generates a well-structured, professional email template.

Write your prompt in:
src/main/resources/prompts/exercise5_email.txt

Requirements:

  • Include:
    • Subject line
    • Greeting
    • Body paragraphs
    • Closing line and signature
  • Use a polite, professional tone.
  • Keep formatting clear.


When all tests pass, you are done! BUILD SUCCESS
Remember: Commit and push your changes to save your work.

Happy coding….ops…sorry!…. Happy prompting!

Valerio