import OpenAI from 'openai' const openai = new OpenAI({ apiKey: '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' }) // or set environment: export OPENAI_API_KEY=... const my = { model: 'gpt-5-nano' } const tools = [ { type: 'function', name: 'get_horoscope', description: "Get today's horoscope for an astrological sign.", parameters: { type: 'object', properties: { sign: { type: 'string', description: 'An astrological sign like Taurus or Aquarius' } }, required: ['sign'] } } ] const api = { get_horoscope ({ sign } = {}) { return sign + ' Next Tuesday you will befriend a baby otter.' } } // Create a running input list we will add to over time let input = [ { role: 'user', content: 'What is my horoscope? I am an Aquarius.' } ] // 2. Prompt the model with tools defined let response = await openai.responses.create({ model: my.model, tools, input }) console.log('==========Response 1. round') console.log(JSON.stringify(response, null, 2)) // response.output_text/output_parsed input.push(...response.output) // add the model's response to the input, including any function calls, otherwise the next request reports: 'BadRequestError: 400 No tool call found for function call output with call_id ...' response.output.forEach(item => { if (item.type == 'function_call') { if (typeof api[item.name] === 'function') { console.log('function call =', item) const function_call_output = api[item.name](JSON.parse(item.arguments)) console.log('function call output =', function_call_output) // 4. Provide function call results to the model input.push({ type: 'function_call_output', call_id: item.call_id, output: function_call_output.toString() }) } } }) console.log('=========Input appended with function call output') console.log(JSON.stringify(input, null, 2)) response = await openai.responses.create({ model: my.model, //instructions: 'Respond only with a horoscope generated by a tool.', tools, input }) // 5. The model should be able to give a response! console.log('============Final output:') console.log(JSON.stringify(response.output, null, 2))