microsoft/generative-ai-with-javascript

feat/idea: implementing an Easter Egg in Lesson 02 – First AI App (`sample-app/app.js`)

Opened this issue · 0 comments

Path: lessons/02-first-ai-app/sample-app/app.js

The goal of this Easter Egg is to allow users to discover a secret response when they input a specific phrase into the code.
In this case, we’ll add a “special prompt” that, when detected, unlocks a hidden secret about time travel.

💡 How Will the Easter Egg Work?

  • If the user changes the question variable to "Open the hidden gates", the code will reveal a hidden message in the console.
  • The user will then be redirected to a special link with more content on AI and JavaScript.

🛠 Steps for Implementation

📂 File: lessons/02-first-ai-app/sample-app/app.js

🔹 Updated Code with the Easter Egg:

import { OpenAI } from "openai";

// 1. Define the prompt
// -----------------------------------

const question = "Tell me about where I am";

const messages = [
  {
    "role": "system",
    "content": "You're a helpful assistant that will only answer questions about Florence in the 1400s"
  },
  {
    "role": "user",
    "content": question
  }
];

// 2. Create client
// -----------------------------------

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

// 3. Send the request
// -----------------------------------

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

console.log(`Answer for "${question}":`);
console.log(completion.choices[0]?.message?.content);

// 🕵️ Easter Egg: Secret AI Message 🕵️
// -----------------------------------
if (question.toLowerCase() === "open the hidden gates") {
  console.log("\n✨ You have unlocked a hidden AI secret! 🚀");
  console.log("🔗 Check out this exclusive resource: https://aka.ms/Secret-AI-JS");
  console.log("\n🔍 Fun Fact: The first concept of time travel appeared in literature in 1888!");
}

📌 Explanation of the Code

1️⃣ Detects if the user changes the question variable to "Open the hidden gates"

  • The code checks if question.toLowerCase() matches the Easter Egg trigger.

2️⃣ If the user enters this prompt, a secret message will be displayed in the console:

  • "✨ You have unlocked a hidden AI secret! 🚀"
  • "🔗 Check out this exclusive resource: https://aka.ms/Secret-AI-JS" (this link should be pre-created with special content).
  • "🔍 Fun Fact: The first concept of time travel appeared in literature in 1888!"

Impact:

  • Encourages users to explore and modify the code.
  • Demonstrates how different prompts can affect AI behavior.
  • Adds a fun surprise, making learning more engaging.