SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
13 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
1.9 KB · 23 lines typescript
Raw Blame History
1// Gemini function calling with the @google/genai SDK: forced call (FunctionCallingConfigMode.ANY) + functionResponse round trip.2// STATUS: LIVE_VERIFIED 2026-09-18 — `node --env-file=.env --experimental-strip-types examples/gemini/tools/function-calling/function_calling.ts` (gemini-3.5-flash-lite, < $0.001).3import { GoogleGenAI, FunctionCallingConfigMode, Type } from "@google/genai";4import { logRequest } from "../../../shared/tool-loop/log.ts";56const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });7const MODEL = "gemini-3.5-flash-lite";8const getWeather = { name: "get_weather", description: "Get the current weather for a city.",9  parameters: { type: Type.OBJECT, properties: { city: { type: Type.STRING } }, required: ["city"] } };10const contents: any[] = [{ role: "user", parts: [{ text: "Weather in Paris?" }] }];1112const r1 = await ai.models.generateContent({ model: MODEL, contents,13  config: { tools: [{ functionDeclarations: [getWeather] }], toolConfig: { functionCallingConfig: { mode: FunctionCallingConfigMode.ANY, allowedFunctionNames: ["get_weather"] } }, maxOutputTokens: 64 } });14logRequest("gemini", "POST", "/v1beta/models/{model}:generateContent", 200, 0.0001, "function_calling.ts forced");15const modelTurn = r1.candidates![0].content!;               // keep the parts verbatim (thoughtSignature is required on echo)16const call = r1.functionCalls![0];17console.log("functionCall:", call.name, JSON.stringify(call.args), "id:", call.id);1819contents.push(modelTurn, { role: "user", parts: [{ functionResponse: { name: call.name!, response: { temperature_c: 18, conditions: "light rain" } } }] });20const r2 = await ai.models.generateContent({ model: MODEL, contents, config: { tools: [{ functionDeclarations: [getWeather] }], maxOutputTokens: 32 } });21logRequest("gemini", "POST", "/v1beta/models/{model}:generateContent", 200, 0.0001, "function_calling.ts functionResponse");22console.log("final:", r2.text, "| usage:", r2.usageMetadata?.totalTokenCount);23