|
| 1 | +// cases of type inference testing: |
| 2 | +// 1. no response_model, stream |
| 3 | +// 2. no response_model, no stream |
| 4 | +// 3. response_model, stream, no max_retries |
| 5 | +// 4. response_model, no stream, no max_retries |
| 6 | +// 5. response_model, stream, max_retries |
| 7 | +// 6. response_model, no stream, max_retries |
| 8 | + |
| 9 | +import Instructor from "@/instructor" |
| 10 | +import { describe, expect, test } from "bun:test" |
| 11 | +import OpenAI from "openai" |
| 12 | +import { Stream } from "openai/streaming.mjs" |
| 13 | +import { type } from "ts-inference-check" |
| 14 | +import { z } from "zod" |
| 15 | + |
| 16 | +describe("Inference Checking", () => { |
| 17 | + const UserSchema = z.object({ |
| 18 | + age: z.number(), |
| 19 | + name: z.string() |
| 20 | + }) |
| 21 | + |
| 22 | + const oai = new OpenAI({ |
| 23 | + apiKey: process.env.OPENAI_API_KEY ?? undefined, |
| 24 | + organization: process.env.OPENAI_ORG_ID ?? undefined |
| 25 | + }) |
| 26 | + |
| 27 | + const client = Instructor({ |
| 28 | + client: oai, |
| 29 | + mode: "FUNCTIONS" |
| 30 | + }) |
| 31 | + |
| 32 | + test("no response_model, no stream", async () => { |
| 33 | + const user = await client.chat.completions.create({ |
| 34 | + messages: [{ role: "user", content: "Jason Liu is 30 years old" }], |
| 35 | + model: "gpt-3.5-turbo", |
| 36 | + seed: 1, |
| 37 | + stream: false |
| 38 | + }) |
| 39 | + |
| 40 | + expect(type(user).strictly.is<OpenAI.Chat.ChatCompletion>(true)).toBe(true) |
| 41 | + }) |
| 42 | + |
| 43 | + test("no response_model, stream", async () => { |
| 44 | + const userStream = await client.chat.completions.create({ |
| 45 | + messages: [{ role: "user", content: "Jason Liu is 30 years old" }], |
| 46 | + model: "gpt-3.5-turbo", |
| 47 | + seed: 1, |
| 48 | + stream: true |
| 49 | + }) |
| 50 | + |
| 51 | + expect(type(userStream).strictly.is<Stream<OpenAI.Chat.ChatCompletionChunk>>(true)).toBe(true) |
| 52 | + }) |
| 53 | + |
| 54 | + test("response_model, no stream", async () => { |
| 55 | + const user = await client.chat.completions.create({ |
| 56 | + messages: [{ role: "user", content: "Jason Liu is 30 years old" }], |
| 57 | + model: "gpt-3.5-turbo", |
| 58 | + response_model: { schema: UserSchema, name: "User" }, |
| 59 | + seed: 1, |
| 60 | + stream: false |
| 61 | + }) |
| 62 | + |
| 63 | + expect(type(user).strictly.is<z.infer<typeof UserSchema>>(true)).toBe(true) |
| 64 | + }) |
| 65 | + |
| 66 | + test("response_model, stream", async () => { |
| 67 | + const userStream = await client.chat.completions.create({ |
| 68 | + messages: [{ role: "user", content: "Jason Liu is 30 years old" }], |
| 69 | + model: "gpt-3.5-turbo", |
| 70 | + response_model: { schema: UserSchema, name: "User" }, |
| 71 | + seed: 1, |
| 72 | + stream: true |
| 73 | + }) |
| 74 | + |
| 75 | + expect( |
| 76 | + type(userStream).strictly.is<AsyncGenerator<z.infer<typeof UserSchema>, void, unknown>>(true) |
| 77 | + ).toBe(true) |
| 78 | + }) |
| 79 | + |
| 80 | + test("response_model, stream, max_retries", async () => { |
| 81 | + const userStream = await client.chat.completions.create({ |
| 82 | + messages: [{ role: "user", content: "Jason Liu is 30 years old" }], |
| 83 | + model: "gpt-3.5-turbo", |
| 84 | + response_model: { schema: UserSchema, name: "User" }, |
| 85 | + seed: 1, |
| 86 | + stream: true, |
| 87 | + max_retries: 3 |
| 88 | + }) |
| 89 | + |
| 90 | + expect( |
| 91 | + type(userStream).strictly.is<AsyncGenerator<z.infer<typeof UserSchema>, void, unknown>>(true) |
| 92 | + ).toBe(true) |
| 93 | + }) |
| 94 | + |
| 95 | + test("response_model, no stream, max_retries", async () => { |
| 96 | + const user = await client.chat.completions.create({ |
| 97 | + messages: [{ role: "user", content: "Jason Liu is 30 years old" }], |
| 98 | + model: "gpt-3.5-turbo", |
| 99 | + response_model: { schema: UserSchema, name: "User" }, |
| 100 | + seed: 1, |
| 101 | + max_retries: 3 |
| 102 | + }) |
| 103 | + |
| 104 | + expect(type(user).strictly.is<z.infer<typeof UserSchema>>(true)).toBe(true) |
| 105 | + }) |
| 106 | +}) |
0 commit comments