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// Function calling (Responses API, openai SDK): forced strict function -> function_call_output round trip -> streamed arguments.2// STATUS: LIVE_VERIFIED 2026-09-18 (gpt-5.4-nano). Run: node --env-file=.env --experimental-strip-types examples/openai/tools/function-calling/function_calling.ts3import OpenAI from "openai";4import { logRequest } from "../../../shared/tool-loop/log.ts";5const client = new OpenAI(); const MODEL = "gpt-5.4-nano";6const WEATHER: any = { type: "function", name: "get_weather", description: "Current weather for a city.", strict: true,7  parameters: { type: "object", properties: { city: { type: "string" } }, required: ["city"], additionalProperties: false } };89const r1: any = await client.responses.create({ model: MODEL, input: "Weather in Paris?", tools: [WEATHER], max_output_tokens: 64,10  tool_choice: { type: "function", name: "get_weather" }, reasoning: { effort: "low" } });11logRequest("openai", "POST", "/v1/responses", 200, 0.0001, "function_calling.ts forced");12const call = r1.output.find((i: any) => i.type === "function_call");13console.log("function_call:", call.name, call.arguments, call.call_id);14const r2: any = await client.responses.create({ model: MODEL, previous_response_id: r1.id, tools: [WEATHER], max_output_tokens: 32, reasoning: { effort: "low" },15  input: [{ type: "function_call_output", call_id: call.call_id, output: JSON.stringify({ temp_c: 18, sky: "clear" }) }] });16logRequest("openai", "POST", "/v1/responses", 200, 0.0001, "function_calling.ts round trip");17console.log("final:", r2.output_text);18const seen = new Set<string>();19const stream = await client.responses.create({ model: MODEL, input: "Weather in Rome?", tools: [WEATHER], tool_choice: "required", max_output_tokens: 64, reasoning: { effort: "low" }, stream: true });20for await (const ev of stream) seen.add(ev.type);21logRequest("openai", "POST", "/v1/responses", 200, 0.0001, "function_calling.ts stream");22console.log("stream events:", [...seen].sort());23