1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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();
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
September 4, 2024