Skip to content

Commit

Permalink
Merge branch 'main' into test-validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnl authored Jan 3, 2024
2 parents d491dea + 6dde1f6 commit 5d219af
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/classification/multi_prediction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const client = Instructor({
})

const createClassification = async (data: string): Promise<MultiClassification | undefined> => {
const classification: MultiClassification = await client.chat.completions.create({
const classification = await client.chat.completions.create({
messages: [{ role: "user", content: `"Classify the following support ticket: ${data}` }],
model: "gpt-3.5-turbo",
response_model: MultiClassificationSchema,
Expand Down
2 changes: 1 addition & 1 deletion examples/classification/simple_prediction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const client = Instructor({
})

const createClassification = async (data: string): Promise<SimpleClassification | undefined> => {
const classification: SimpleClassification = await client.chat.completions.create({
const classification = await client.chat.completions.create({
messages: [{ role: "user", content: `"Classify the following text: ${data}` }],
model: "gpt-3.5-turbo",
response_model: SimpleClassificationSchema,
Expand Down
15 changes: 13 additions & 2 deletions examples/extract_user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,22 @@ const client = Instructor({
mode: "FUNCTIONS"
})

const user: User = await client.chat.completions.create({
const user = await client.chat.completions.create({
messages: [{ role: "user", content: "Jason Liu is 30 years old" }],
model: "gpt-3.5-turbo",
response_model: UserSchema,
max_retries: 3
})
});


// let's now verify that the response type is inferred correctly

const age: number = user.age;
// @ts-expect-error - age is a number, not a string
const _age: string = user.age;
const name: string = user.name;

// @ts-expect-error - this property does not exist
user.missing;

console.log(user)
20 changes: 20 additions & 0 deletions examples/passthrough/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Instructor from "@/instructor"
import OpenAI from "openai"

const oai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY ?? undefined,
organization: process.env.OPENAI_ORG_ID ?? undefined
})

const client = Instructor({
client: oai,
mode: "FUNCTIONS"
})

// ensures that when no `response_model` is provided, the response type is `ChatCompletion`
const completion = await client.chat.completions.create({
messages: [{ role: "user", content: "Jason Liu is 30 years old" }],
model: "gpt-3.5-turbo",
max_retries: 3
}) satisfies OpenAI.Chat.ChatCompletion;

2 changes: 1 addition & 1 deletion examples/resolving-complex-entitities/exampleGraphMaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function createHtmlDocument(data) {
})
.join(",\n")

const edgeDefs = []
const edgeDefs: string[] = []
data.entities.forEach(entity => {
entity.dependencies.forEach(depId => {
// @ts-ignore
Expand Down
29 changes: 21 additions & 8 deletions src/instructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
ChatCompletionCreateParamsNonStreaming,
ChatCompletionMessageParam
} from "openai/resources/index.mjs"
import { ZodObject } from "zod"
import type { ZodObject, z } from "zod"

Check failure on line 17 in src/instructor.ts

View workflow job for this annotation

GitHub Actions / run-tests

Replace `ZodObject,·z` with `z,·ZodObject`
import zodToJsonSchema from "zod-to-json-schema"
import { fromZodError } from "zod-validation-error"

Expand All @@ -36,9 +36,9 @@ const MODE_TO_PARAMS = {
[MODE.JSON_SCHEMA]: OAIBuildMessageBasedParams
}

type PatchedChatCompletionCreateParams = ChatCompletionCreateParamsNonStreaming & {
interface PatchedChatCompletionCreateParams<Model extends ZodObject<any> | undefined> extends ChatCompletionCreateParamsNonStreaming {

Check failure on line 39 in src/instructor.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected any. Specify a different type

Check failure on line 39 in src/instructor.ts

View workflow job for this annotation

GitHub Actions / run-tests

Insert `⏎·`
//eslint-disable-next-line @typescript-eslint/no-explicit-any
response_model?: ZodObject<any>
response_model?: Model
max_retries?: number
}

Expand All @@ -65,11 +65,19 @@ class Instructor {
}

Check failure on line 65 in src/instructor.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected any. Specify a different type

Check failure on line 65 in src/instructor.ts

View workflow job for this annotation

GitHub Actions / run-tests

Delete `·`

Check failure on line 66 in src/instructor.ts

View workflow job for this annotation

GitHub Actions / run-tests

Delete `·`
/**
* Handles chat completion with retries.
* @param {PatchedChatCompletionCreateParams} params - The parameters for chat completion.
* @returns {Promise<any>} The response from the chat completion.
* Handles chat completion with retries and parses the response if a response model is provided.

Check failure on line 68 in src/instructor.ts

View workflow job for this annotation

GitHub Actions / run-tests

Replace `·⏎····Promise<Model·extends·ZodObject<any>⏎······?·z.infer<Model>·⏎······:··OpenAI.Chat.Completions.ChatCompletion·>·{⏎` with `·Promise<⏎····Model·extends·ZodObject<any>·?·z.infer<Model>·:·OpenAI.Chat.Completions.ChatCompletion⏎··>·{`
*

Check failure on line 69 in src/instructor.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected any. Specify a different type
* @param params - The parameters for chat completion.
* @returns The parsed response model if {@link PatchedChatCompletionCreateParams.response_model} is provided, otherwise the original chat completion.
*/
chatCompletion = async ({ max_retries = 3, ...params }: PatchedChatCompletionCreateParams) => {
async chatCompletion<Model extends ZodObject<any> | undefined = undefined>({
max_retries = 3,
...params
}: PatchedChatCompletionCreateParams<Model>):
Promise<Model extends ZodObject<any>
? z.infer<Model>
: OpenAI.Chat.Completions.ChatCompletion > {

let attempts = 0
let validationIssues = ""
let lastMessage: ChatCompletionMessageParam | null = null
Expand Down Expand Up @@ -97,7 +105,11 @@ class Instructor {
this.log("making completion call with params: ", resolvedParams)

const completion = await this.client.chat.completions.create(resolvedParams)
if (params.response_model === undefined) {
return completion;
}
const response = this.parseOAIResponse(completion)

Check failure on line 112 in src/instructor.ts

View workflow job for this annotation

GitHub Actions / run-tests

Delete `;`
this.log("Raw completion call response: ", response)
this.log("Parsed completion call response: ", resolvedParams)

Expand Down Expand Up @@ -127,8 +139,8 @@ class Instructor {
} else {
throw new Error("Validation failed.")
}
return validation.data

Check failure on line 142 in src/instructor.ts

View workflow job for this annotation

GitHub Actions / run-tests

Property 'data' does not exist on type 'SafeParseReturnType<{ [x: string]: any; }, { [x: string]: any; }>'.
}
return validation.data
} catch (error) {
if (attempts < max_retries) {
this.log("Retrying, attempt: ", attempts)
Expand Down Expand Up @@ -166,6 +178,7 @@ class Instructor {

this.log("JSON Schema from zod: ", jsonSchema)


const definition = {
name: "response_model",
...jsonSchema.definitions?.response_model
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"strict": false,
"strictNullChecks": true,
"noEmit": true,
"allowJs": true,
"jsx": "preserve",
Expand Down

0 comments on commit 5d219af

Please sign in to comment.