Programming

JavaScript: systemInstruction, a hidden rule

Tell the AI who it is and what it must refuse, with systemInstruction.

All lessons · JavaScript lessons: learn from scratch

A hidden rule

A system prompt is a rule the user does not see. The programmer writes it. contents is the user's question. The rule tells the AI who it is, how long the answer should be, and which topic to refuse.

const sorguMelumati = {
  systemInstruction: {
    parts: [{ text: "Sən Nibras Code müəllimisən. Yalnız qısa cümlə ilə cavab ver." }]
  },
  contents: [
    { parts: [{ text: "JavaScript nədir?" }] }
  ]
};

The role changes, the code does not

In the same function only the systemInstruction text changes. A teacher refuses an off-topic question. An Arabic teacher writes the word with vowel marks and one sentence, and adds no extra explanation. The key comes from process.env.

const apiKey = process.env.GEMINI_API_KEY;
const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${apiKey}`;

async function muellimBot(istifadeciSuali) {
  const sorguMelumati = {
    systemInstruction: {
      parts: [{
        text: "Sən JavaScript müəllimisən. Yalnız proqramlaşdırma sualına cavab ver. Başqa mövzuda belə yaz: Üzr istəyirəm, mən yalnız proqramlaşdırma üzrə kömək edə bilərəm."
      }]
    },
    contents: [{ parts: [{ text: istifadeciSuali }] }]
  };
  const cavab = await fetch(url, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(sorguMelumati)
  });
  const melumat = await cavab.json();
  console.log(melumat.candidates[0].content.parts[0].text);
}

Questions

How is the name written? systemInstruction. systeminstruction and system_instruction are wrong. The I is capital.

Why is the rule broken? The instruction contradicts itself. Do not ask for a long explanation and one sentence at the same time. Write it short and clear.

Can I put the rule inside contents? It works, but it is forgotten in a long chat. The rule belongs in systemInstruction.

What goes in a strong instruction? Identity, length, a ban and the output shape. The four must not contradict each other.

Author: Nibras Code