Skip to content

Commit

Permalink
move back to the zod to json schema package
Browse files Browse the repository at this point in the history
  • Loading branch information
roodboi committed Jan 2, 2024
1 parent 79755a3 commit 4d7e262
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 233 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"url": "https://github.com/jxnl/instructor-js/issues"
},
"homepage": "https://github.com/jxnl/instructor-js#readme",
"dependencies": {
"zod-to-json-schema": "^3.22.3"
},
"peerDependencies": {
"openai": ">=4.24.1",
"zod": ">=3.22.4"
Expand Down
11 changes: 8 additions & 3 deletions src/instructor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createSchemaFunction } from "@/oai/fns/schema"
import OpenAI from "openai"
import { ChatCompletion, ChatCompletionCreateParamsNonStreaming } from "openai/resources/index.mjs"
import { ZodObject } from "zod"
import zodToJsonSchema from "zod-to-json-schema"

import { MODE } from "@/constants/modes"

Expand Down Expand Up @@ -138,12 +138,17 @@ export default class Instructor {
response_model,
...params
}: PatchedChatCompletionCreateParams): ChatCompletionCreateParamsNonStreaming => {
const { definition } = createSchemaFunction({ schema: response_model })
const jsonSchema = zodToJsonSchema(response_model, "response_model")

const definition = {
name: "response_model",
...jsonSchema.definitions.response_model
}

const paramsForMode = MODE_TO_PARAMS[this.mode](definition, params)

return {
stream: false, //TODO: not sure what streaming support looks like TBH
stream: false,
...paramsForMode
}
}
Expand Down
78 changes: 0 additions & 78 deletions src/oai/fns/index.ts

This file was deleted.

118 changes: 0 additions & 118 deletions src/oai/fns/schema.ts

This file was deleted.

41 changes: 7 additions & 34 deletions src/oai/params.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,25 @@
import { FunctionPayload } from "./fns"

export function OAIBuildFunctionParams(definition: FunctionPayload, params) {
export function OAIBuildFunctionParams(definition, params) {
return {
...params,
function_call: {
name: definition.name
},
functions: [
...(params?.functions ?? []),
{
name: definition.name,
description: definition.description,
parameters: {
type: "object",
properties: definition.parameters,
required: definition.required
}
}
]
functions: [...(params?.functions ?? []), definition]
}
}

export function OAIBuildToolFunctionParams(definition: FunctionPayload, params) {
export function OAIBuildToolFunctionParams(definition, params) {
return {
...params,
tool_choice: {
type: "function",
function: { name: definition.name }
},
tools: [
...(params?.tools ?? []),
{
type: "function",
function: {
name: definition.name,
description: definition.description,
parameters: {
type: "object",
properties: definition.parameters,
required: definition.required
}
}
}
]
tools: [...(params?.tools ?? []), definition]
}
}

export function OAIBuildMessageBasedParams(definition: FunctionPayload, params) {
export function OAIBuildMessageBasedParams(definition, params) {
return {
...params,
messages: [
Expand All @@ -58,8 +31,8 @@ export function OAIBuildMessageBasedParams(definition: FunctionPayload, params)
You will return no other prose. You will take into account the descriptions for each paramater within the schema
and return a valid JSON object that matches the schema and those instructions.
description: ${definition.description}
paramaters: ${JSON.stringify(definition.parameters)}
description: ${definition?.description}
json schema: ${JSON.stringify(definition)}
`
}
]
Expand Down

0 comments on commit 4d7e262

Please sign in to comment.