-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updates and test for supporting zod defaults (#86)
- Loading branch information
Showing
4 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@instructor-ai/instructor": patch | ||
--- | ||
|
||
update zodstream and schema stream to support zod defaults |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Instructor from "@/instructor" | ||
import { describe, expect, test } from "bun:test" | ||
import OpenAI from "openai" | ||
import { z } from "zod" | ||
|
||
async function extractUser({ | ||
schema | ||
}) { | ||
|
||
const oai = new OpenAI({ | ||
apiKey: process.env.OPENAI_API_KEY ?? undefined, | ||
organization: process.env.OPENAI_ORG_ID ?? undefined | ||
}) | ||
|
||
const client = Instructor({ | ||
client: oai, | ||
mode: "TOOLS" | ||
}) | ||
|
||
const user = await client.chat.completions.create({ | ||
messages: [{ role: "user", content: "do nothing" }], | ||
model: "gpt-3.5-turbo", | ||
response_model: { schema: schema, name: "User" }, | ||
seed: 1 | ||
}) | ||
|
||
return user | ||
} | ||
|
||
describe("zod-schema test", () => { | ||
test("Should return default values", async () => { | ||
const UserSchema = z.object({ | ||
age: z.number().default(30), | ||
name: z.string().default("Jason Liu"), | ||
favoriteFood: z.string().default("Pizza"), | ||
favoriteColor: z.string().default("Blue") | ||
}) | ||
|
||
const user = await extractUser({ | ||
schema: UserSchema | ||
}) | ||
console.log("test", user) | ||
|
||
expect(user.name).toEqual("Jason Liu") | ||
expect(user.age).toEqual(30) | ||
expect(user.favoriteFood).toEqual("Pizza") | ||
expect(user.favoriteColor).toEqual("Blue") | ||
}) | ||
}) |