Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : API Atlas8 * File : examples/openai/tools/custom-tools/custom_tools.ts9 * Module : examples10 * Repository : https://git.spboucher.ai/doc-api11 * Created : 2026-09-1812 * Last Modified : 2026-09-1813 *14 * Author : Simon-Pierre Boucher15 * Position : Professor of Finance16 * Institution : Université du Québec en Outaouais (UQO)17 * Organization : SP Boucher18 * Website : https://spboucher.ai19 * Git : https://git.spboucher.ai20 *21 * Preferred Contact: contact@spboucher.ai22 * Development : dev@spboucher.ai23 * Support : support@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Custom tool with a regex grammar: the model must answer via the tool with `yes` or `no`.43// STATUS: LIVE_VERIFIED 2026-09-18 (gpt-5.4-nano). Run: node --env-file=.env --experimental-strip-types examples/openai/tools/custom-tools/custom_tools.ts44import OpenAI from "openai";45import { logRequest } from "../../../shared/tool-loop/log.ts";46const client = new OpenAI();47const r: any = await client.responses.create({ model: "gpt-5.4-nano", input: "Is 2+2 equal to 4? Use the tool.", max_output_tokens: 32, reasoning: { effort: "low" },48 tools: [{ type: "custom", name: "answer_yes_no", description: "Reply with exactly yes or no.", format: { type: "grammar", syntax: "regex", definition: "^(yes|no)$" } }],49 tool_choice: { type: "custom", name: "answer_yes_no" } });50logRequest("openai", "POST", "/v1/responses", 200, 0.0001, "custom_tools.ts");51const call = r.output.find((i: any) => i.type === "custom_tool_call");52console.log("custom_tool_call:", call.name, JSON.stringify(call.input), call.call_id);53if (!["yes", "no"].includes(call.input)) throw new Error("grammar not respected");54