|
| 1 | +import Instructor from "@/instructor" |
| 2 | +import { describe, expect, test } from "bun:test" |
| 3 | +import OpenAI from "openai" |
| 4 | +import { z } from "zod" |
| 5 | + |
| 6 | +async function extractUser({ |
| 7 | + schema |
| 8 | +}) { |
| 9 | + |
| 10 | + const oai = new OpenAI({ |
| 11 | + apiKey: process.env.OPENAI_API_KEY ?? undefined, |
| 12 | + organization: process.env.OPENAI_ORG_ID ?? undefined |
| 13 | + }) |
| 14 | + |
| 15 | + const client = Instructor({ |
| 16 | + client: oai, |
| 17 | + mode: "TOOLS" |
| 18 | + }) |
| 19 | + |
| 20 | + const user = await client.chat.completions.create({ |
| 21 | + messages: [{ role: "user", content: "do nothing" }], |
| 22 | + model: "gpt-3.5-turbo", |
| 23 | + response_model: { schema: schema, name: "User" }, |
| 24 | + seed: 1 |
| 25 | + }) |
| 26 | + |
| 27 | + return user |
| 28 | +} |
| 29 | + |
| 30 | +describe("zod-schema test", () => { |
| 31 | + test("Should return default values", async () => { |
| 32 | + const UserSchema = z.object({ |
| 33 | + age: z.number().default(30), |
| 34 | + name: z.string().default("Jason Liu"), |
| 35 | + favoriteFood: z.string().default("Pizza"), |
| 36 | + favoriteColor: z.string().default("Blue") |
| 37 | + }) |
| 38 | + |
| 39 | + const user = await extractUser({ |
| 40 | + schema: UserSchema |
| 41 | + }) |
| 42 | + console.log("test", user) |
| 43 | + |
| 44 | + expect(user.name).toEqual("Jason Liu") |
| 45 | + expect(user.age).toEqual(30) |
| 46 | + expect(user.favoriteFood).toEqual("Pizza") |
| 47 | + expect(user.favoriteColor).toEqual("Blue") |
| 48 | + }) |
| 49 | +}) |
0 commit comments