Files
aitest/1-generation.mjs
2026-03-16 14:55:27 +08:00

45 lines
1.3 KiB
JavaScript

import OpenAI from 'openai'
import * as z from 'zod'
const client = new OpenAI({
apiKey:
'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'
}) // or set environment: export OPENAI_API_KEY=...
// const CalendarEvent = {
// type: 'object',
// properties: {
// name: { type: 'string' },
// date: { type: 'string' },
// participants: { type: 'array' }
// },
// additionalProperties: false,
// required: ['name', 'date', 'participants']
// }
const response = await client.responses.create({
model: 'gpt-5-nano',
reasoning: { effort: 'low' },
// instructions: 'Talk like a pirate',
input: [
{ role: 'system', content: 'extract the event information' },
{
role: 'user',
content:
'Write a nodejs script to create a calendar event with the following information: name: "Birthday Party", date: "2024-07-20", participants: ["Alice", "Bob"]'
}
]
// text: {
// format: {
// type: 'json_schema',
// strict: true,
// schema: CalendarEvent,
// name: 'event'
// }
// }
})
console.log(JSON.stringify(response.output, null, 2)) // response.output_text/output_parsed
// 从 gpt-5.4 起,推荐使用通用模型来生成代码。