Skip to content

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

@glaucia86

Description

@glaucia86

πŸ“‚ 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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions