Open
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!