Skip to content

Commit 265a9e5

Browse files
authored
Testing Typescript inference (#72)
1 parent b1f01f7 commit 265a9e5

File tree

6 files changed

+116
-2
lines changed

6 files changed

+116
-2
lines changed

.changeset/plenty-eggs-grow.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@instructor-ai/instructor": patch
3+
---
4+
5+
Implements testing for typescript inference

bun.lockb

390 Bytes
Binary file not shown.

bunfig.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
[install]
2-
peer = true
2+
peer = true
3+
4+
[test]
5+
coverage = true

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"homepage": "https://github.com/instructor-ai/instructor-js#readme",
5050
"dependencies": {
5151
"schema-stream": "1.6.0",
52+
"ts-inference-check": "^0.3.0",
5253
"zod-to-json-schema": "^3.22.3",
5354
"zod-validation-error": "^2.1.0"
5455
},

src/instructor.ts

-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,6 @@ class Instructor {
293293
params: P
294294
): ReturnTypeBasedOnParams<P> => {
295295
if ("response_model" in params) {
296-
console.log(params.response_model.name)
297296
return this.chatCompletion(params) as ReturnTypeBasedOnParams<P>
298297
} else {
299298
return this.chatCompletionWithoutModel(params) as ReturnTypeBasedOnParams<P>

tests/inference.test.ts

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
 (0)