Programming

JavaScript: an AI agent and function calling

The AI does not run the function itself. It says functionCall, you run it and return functionResponse.

All lessons · JavaScript lessons: learn from scratch

A chatbot and an agent

A chatbot only writes text. It cannot run your database or your function. An agent acts, but it does not call the function itself. The tools list holds the name, the description and the parameters. The AI reads the question and returns a functionCall: a name and args. You run that function and send the result back with functionResponse. The AI writes the final sentence from it. If description is empty, it picks the wrong function. The type is in capital letters: STRING, NUMBER, OBJECT.

functionCall and the return

The first request goes with the tools. If parts[0].functionCall is there, there is no text, there is a command. You run the function. The second request has three parts: the question, the model's functionCall and the functionResponse. If you only console.log the result and do not send it back, the user never sees a clean sentence. A question like hello has no functionCall. text comes directly.

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

function havaMelumatiniVer(seher) {
  if (seher === "Baku") return "Bakıda 24 dərəcə, günəşlidir.";
  return "Bu şəhər üçün məlumat yoxdur.";
}

const aletler = [{
  functionDeclarations: [{
    name: "havaMelumatiniVer",
    description: "Verilmiş şəhər üçün hava mətnini qaytarır.",
    parameters: {
      type: "OBJECT",
      properties: { seher: { type: "STRING", description: "Şəhər, məsələn Baku" } },
      required: ["seher"]
    }
  }]
}];

async function agentSorgusuGonder(istifadeciSuali) {
  const ilkBody = {
    contents: [{ role: "user", parts: [{ text: istifadeciSuali }] }],
    tools: aletler
  };
  const cavab = await fetch(url, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(ilkBody)
  });
  const melumat = await cavab.json();
  const hisse = melumat.candidates[0].content.parts[0];
  if (!hisse.functionCall) {
    console.log(hisse.text);
    return;
  }
  const ad = hisse.functionCall.name;
  const netice = havaMelumatiniVer(hisse.functionCall.args.seher);
  const ikinciBody = {
    contents: [
      { role: "user", parts: [{ text: istifadeciSuali }] },
      { role: "model", parts: [{ functionCall: hisse.functionCall }] },
      { role: "user", parts: [{ functionResponse: { name: ad, response: { netice } } }] }
    ],
    tools: aletler
  };
  const cavab2 = await fetch(url, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(ikinciBody)
  });
  const melumat2 = await cavab2.json();
  console.log(melumat2.candidates[0].content.parts[0].text);
}

Questions

Does the AI run the function itself? No. It only says the name and the parameter. Your code runs it.

Why does description matter? The AI knows when to pick the function from this text. If it is empty or vague, it calls the wrong one.

What if STRING is written in small letters? Gemini returns an error. The types OBJECT, STRING and NUMBER are in capital letters.

Why does the result not reach the user? functionResponse is missing from the second request. console.log is not enough.

Author: Nibras Code