import { OpenAI } from "npm:openai";
import { zodResponseFormat } from "npm:openai/helpers/zod";
import { z } from "npm:zod";
const AddressSchema = z.object({
name: z.string(),
state: z.string(),
country: z.string(),
});
const client = new OpenAI({ apiKey: Deno.env.get("OPENAI_API_KEY") });
async function main() {
const stream = client.beta.chat.completions.stream({
model: "gpt-4o-mini",
messages: [{
role: "user",
content: "Generate an object with a name, state, and country.",
}],
response_format: zodResponseFormat(AddressSchema, "address"),
}).on("content.delta", ({ snapshot, parsed }) => {
console.log(`snapshot: ${snapshot}`);
console.log(`parsed: ${JSON.stringify(parsed)}`);
});
}
main();