Skip to content

Commit

Permalink
updates and test for supporting zod defaults (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
roodboi authored Jan 22, 2024
1 parent 378f8a9 commit 205c6cb
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/three-swans-push.md
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
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
},
"homepage": "https://github.com/instructor-ai/instructor-js#readme",
"dependencies": {
"zod-stream": "^0.0.1",
"zod-stream": "^0.0.4",
"zod-to-json-schema": "^3.22.3",
"zod-validation-error": "^2.1.0"
},
Expand Down
49 changes: 49 additions & 0 deletions tests/zod-type.test.ts
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")
})
})

0 comments on commit 205c6cb

Please sign in to comment.