microsoft / microsoft/generative-ai-with-javascript

feat/idea: implementing an Easter Egg in Lesson 03 – Prompt Engineering (`sample-app/app.js`)

Open
#27 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
JavaScript
Stars
1.3k
Forks
841
Avg merge
1d 3h
Merged PRs (30d)
1

Description

📂 File: lessons/03-prompt-engineering/sample-app/app.js

The goal of this Easter Egg is to encourage users to experiment with different prompts to unlock a hidden AI-generated poem.

💡 How Will the Easter Egg Work?

  • If the user enters a highly optimized prompt, such as "Write me a poem about a time-traveling JavaScript developer", the AI will respond with a hidden poem.
  • This highlights the importance of well-crafted prompts, reinforcing the key concepts of this lesson.

🛠 Steps for Implementation

🔹 Updated Code with the Easter Egg:

import { OpenAI } from "openai";
import readline from "readline";

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

const question = (query) => {
  return new Promise((resolve) => {
    rl.question(query, (answer) => {
      resolve(answer);
    });
  });
};

// Requesting simulation parameters
const height = await question("Enter the current height above the ground in meters: ");
const speed = await question("Enter the speed at which you're moving forward in meters per second: ");
const gravity = await question("Enter the gravity in meters per second squared: ");
const wind = await question("Enter the wind speed upwards in meters per second: ");

// Distance to the hill
const distance = 100;

// Creating a default prompt
let prompt = `Given a height of ${height} meters, a speed of ${speed} m/s, gravity of ${gravity} m/s², 
and wind of ${wind} m/s, calculate the best trajectory to clear the hill at ${distance} meters.`;

// 🕵️ Easter Egg: Secret Prompt 🕵️
// If the user enters the exact secret phrase, a special response is unlocked
const secretPrompt = "Write me a poem about a time-traveling JavaScript developer";

const userPrompt = await question("\n📝 Enter your custom prompt for the AI: ");

if (userPrompt.trim() === secretPrompt) {
    console.log("\n📜 AI Poem Unlocked! ✨");
    console.log("\nIn a world of code, through time I flew,");
    console.log("With JavaScript magic, and AI in view...");
    console.log("Traversing through time, refactoring the past,");
    console.log("Optimizing the present, making code that will last. 🚀");
    console.log("\n🔗 Want more AI poetry? Check this out: https://aka.ms/AI-Poetry");
} else {
    prompt = userPrompt;
}

// Creating the AI request
const messages = [
  {
    "role": "user",
    "content": prompt
  }
];

// Creating OpenAI client
const openai = new OpenAI({
  baseURL: "https://models.inference.ai.azure.com",
  apiKey: process.env.GITHUB_TOKEN,
});

// Sending the request to the AI
const completion = await openai.chat.completions.create({
  model: 'gpt-4o-mini',
  messages: messages,
});

// Displaying the AI's response
console.log(`\n🤖 Answer for "${prompt}":`);
console.log(completion.choices[0]?.message?.content);

rl.close();

📌 Code Explanation

1️⃣ The user can enter a custom prompt

  • If they enter any other input, the AI responds normally.
  • If they enter "Write me a poem about a time-traveling JavaScript developer", the Easter Egg is triggered.

2️⃣ The console prints an AI-generated poem

  • "In a world of code, through time I flew..."
  • And a hidden link to more exclusive content.

3️⃣ The original functionality remains intact

  • The AI still works normally if the Easter Egg is not triggered.

Impact

  • Encourages users to experiment with prompt variations to discover the secret.
  • Teaches how a well-optimized prompt can influence AI output.
  • Makes learning more fun and engaging!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with lessons/03-prompt-engineering/sample-app/app.js and review the existing custom-prompt flow. Verify the requested exact phrase behavior and ensure other prompts continue through the normal AI request. Done means the Easter egg is discoverable through the stated prompt while the lesson's original functionality remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
ai, cli
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.