Code viewer for World: version -2 (clone by Must...

// Clone by Mustafa Kamal of:
// "version -2 " by Vaseekaran
// https://ancientbrain.com/world.php?world=9792189608
// Please leave this clone trail here.

// Clone by Janavi Ravi of:
// "Chat with GPT model" by Starter user
// https://ancientbrain.com/world.php?world=2850716357
// Please leave this clone trail here.

// ===========================================
// Ancient Brain pure JS World
// ===========================================

// ------ UI ------
document.write(`              
  <h3>Select a Question:</h3>
  <select id="questionDropdown"></select>

  <br><br>
  <button onclick="askQuestion()">Ask both APIs</button>

  <hr> 
  <div id="results" style="font-size: 18px;"></div>
  <hr>
  <div id="score" style="font-size: 18px;"></div>
`);

// ===========================================
// 15-Question Ground Truth Dataset
// ===========================================
const QA = [
  {q: "Capital of France?", a: "Paris"},
  {q: "Largest planet?", a: "Jupiter"},
  {q: "Square root of 16?", a: "4"},
  {q: "Author of Hamlet?", a: "Shakespeare"},
  {q: "Speed of light (km/s)?", a: "300000"},
  {q: "Chemical symbol for water?", a: "H2O"},
  {q: "Year WW2 ended?", a: "1945"},
  {q: "Currency of Japan?", a: "Yen"},
  {q: "Tallest mountain?", a: "Everest"},
  {q: "Painter of Mona Lisa?", a: "Da Vinci"},
  {q: "Smallest prime number?", a: "2"},
  {q: "First man on moon?", a: "Neil Armstrong"},
  {q: "Pi to two decimals?", a: "3.14"},
  {q: "Main gas in air?", a: "Nitrogen"},
  {q: "US Declaration year?", a: "1776"}
];

// ===========================================
// Score System
// ===========================================
let goodCorrect = 0;
let badCorrect = 0;

// Populate dropdown
let drop = document.getElementById("questionDropdown");
QA.forEach((item, idx) => {
  let opt = document.createElement("option");
  opt.value = idx;
  opt.text = item.q;
  drop.appendChild(opt);
});

// ===========================================
// BACKEND API KEYS (STORE HERE)
// ===========================================

// Gemini Key (Good API)
const apiKey1 = "AIzaSyA_-5JBMn4EjVNZSi4Orbh-SwDHX1iz0Ik";

// Mistral Key (Bad API)
const mistralKey = "8NHiPnJ9PMCAkaeyD3XjIgGvZ5te50vC";

// ===========================================
// GOOD API → GEMINI
// ===========================================
function goodAPI(question) {
  return fetch(
    "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=" + apiKey1,
    {
      method: "POST",
      headers: {"Content-Type": "application/json"},
      body: JSON.stringify({
        contents: [{ parts: [{ text: question }] }]
      })
    }
  )
  .then(r => r.json())
  .then(d => d?.candidates?.[0]?.content?.parts?.[0]?.text || "No answer found")
  .catch(() => "Error contacting API1");
}

// ===========================================
// BAD API → MISTRAL CODESTRAL
// ===========================================
const MISTRAL_URL = "https://api.mistral.ai/v1/chat/completions";
const MISTRAL_MODEL = "codestral-2405";

async function callMistral(promptText) {
  const body = {
    model: MISTRAL_MODEL,
    messages: [{ role: "user", content: promptText }]
  };

  const res = await fetch(MISTRAL_URL, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "Authorization": "Bearer " + mistralKey
    },
    body: JSON.stringify(body)
  });

  const data = await res.json();

  if (data.error) return "Mistral Error: " + data.error.message;

  return data?.choices?.[0]?.message?.content || "No output.";
}

function badAPI(question) {
  return callMistral(question)
    .catch(() => "Error contacting API2");
}

// ===========================================
// ASK QUESTION FUNCTION
// ===========================================
window.askQuestion = async function() {
  let idx = drop.value;

  let baseQuestion = QA[idx].q;
  let question = baseQuestion + " Just answer with one word only.";  
  let trueAnswer = QA[idx].a;

  // Call APIs
  let goodAnswer = await goodAPI(question);
  let badAnswer = await badAPI(question);

  // Compare with truth
  let goodIsCorrect = (goodAnswer === trueAnswer);
  let badIsCorrect = (badAnswer === trueAnswer);

  if (goodIsCorrect) goodCorrect++;
  if (badIsCorrect) badCorrect++;

  // Display results
  document.getElementById("results").innerHTML =
    "<b>Question:</b> " + question +
    "<br><b>Ground Truth:</b> " + trueAnswer +
    "<br><b>Good API (Gemini):</b> " + goodAnswer +
      " [" + (goodIsCorrect ? "<span style='color:green'>Correct</span>" : "<span style='color:red'>Wrong</span>") + "]" +
    "<br><b>Bad API (Mistral):</b> " + badAnswer +
      " [" + (badIsCorrect ? "<span style='color:green'>Correct</span>" : "<span style='color:red'>Wrong</span>") + "]";

  document.getElementById("score").innerHTML =
    "<b>Good API correct count:</b> " + goodCorrect + "<br>" +
    "<b>Bad API correct count:</b> " + badCorrect;
};