Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prettier #38

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/instructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
ChatCompletionCreateParamsNonStreaming,
ChatCompletionMessageParam
} from "openai/resources/index.mjs"
import type { ZodObject, z } from "zod"
import type { z, ZodObject } 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,7 +36,8 @@
[MODE.JSON_SCHEMA]: OAIBuildMessageBasedParams
}

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

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

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 `⏎·`
extends ChatCompletionCreateParamsNonStreaming {
//eslint-disable-next-line @typescript-eslint/no-explicit-any
response_model?: Model
max_retries?: number
Expand All @@ -61,15 +62,13 @@
*
* @param params - The parameters for chat completion.
* @returns The parsed response model if {@link PatchedChatCompletionCreateParams.response_model} is provided, otherwise the original chat completion.
*/

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 `·`
async chatCompletion<Model extends ZodObject<any> | undefined = undefined>({
max_retries = 3,
async chatCompletion<Model extends ZodObject<any> | undefined = undefined>({

Check failure on line 66 in src/instructor.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected any. Specify a different type

Check failure on line 66 in src/instructor.ts

View workflow job for this annotation

GitHub Actions / run-tests

Delete `·`
max_retries = 3,
...params

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⏎··>·{`
}: PatchedChatCompletionCreateParams<Model>):
Promise<Model extends ZodObject<any>
? z.infer<Model>
: OpenAI.Chat.Completions.ChatCompletion > {

}: PatchedChatCompletionCreateParams<Model>): Promise<

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
Model extends ZodObject<any> ? z.infer<Model> : OpenAI.Chat.Completions.ChatCompletion

Check failure on line 70 in src/instructor.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected any. Specify a different type
> {
let attempts = 0
let validationIssues = ""
let lastMessage: ChatCompletionMessageParam | null = null
Expand All @@ -96,8 +95,8 @@

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

Check failure on line 99 in src/instructor.ts

View workflow job for this annotation

GitHub Actions / run-tests

Delete `;`
const response = this.parseOAIResponse(completion)
return response
} catch (error) {
Expand All @@ -109,8 +108,8 @@
try {
const data = await makeCompletionCall()
if (params.response_model === undefined) {
return data;
return data
} else {

Check failure on line 112 in src/instructor.ts

View workflow job for this annotation

GitHub Actions / run-tests

Delete `;`
const validation = params.response_model.safeParse(data)
if (!validation.success) {
if ("error" in validation) {
Expand All @@ -118,7 +117,7 @@
role: "assistant",
content: JSON.stringify(data)
}

validationIssues = fromZodError(validation.error).message
throw validation.error
} else {
Expand Down Expand Up @@ -148,7 +147,7 @@
private buildChatCompletionParams = ({
response_model,
...params
}: PatchedChatCompletionCreateParams<any>): ChatCompletionCreateParamsNonStreaming => {

Check failure on line 150 in src/instructor.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected any. Specify a different type
const jsonSchema = zodToJsonSchema(response_model, "response_model")

const definition = {
Expand Down
Loading