microsoft/generative-ai-with-javascript

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

Opened this issue ยท 1 comments

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

Hi, can I work on this issue ?