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

Custom logger & error retries #177

Merged
merged 7 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .changeset/modern-papayas-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@instructor-ai/instructor": minor
---

add new option for providing custom logger
add new option for retrying on any error
26 changes: 19 additions & 7 deletions src/instructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,26 @@ class Instructor<C extends GenericClient | OpenAI> {
readonly mode: Mode
readonly provider: Provider
readonly debug: boolean = false
readonly retryAllErrors: boolean = false
readonly logger?: <T extends unknown[]>(level: LogLevel, ...args: T) => void

/**
* Creates an instance of the `Instructor` class.
* @param {OpenAILikeClient} client - An OpenAI-like client.
* @param {string} mode - The mode of operation.
*/
constructor({ client, mode, debug = false }: InstructorConfig<C>) {
constructor({
client,
mode,
debug = false,
logger = undefined,
retryAllErrors = false
}: InstructorConfig<C>) {
this.client = client
this.mode = mode
this.debug = debug
this.retryAllErrors = retryAllErrors
this.logger = logger ?? undefined

const provider =
typeof this.client?.baseURL === "string" ?
Expand Down Expand Up @@ -83,6 +93,10 @@ class Instructor<C extends GenericClient | OpenAI> {
}

private log<T extends unknown[]>(level: LogLevel, ...args: T) {
if (this.logger) {
this.logger(level, ...args)
}

if (!this.debug && level === "debug") {
return
}
Expand Down Expand Up @@ -224,7 +238,7 @@ class Instructor<C extends GenericClient | OpenAI> {

return { ...validation.data, _meta: data?._meta ?? {} }
} catch (error) {
if (!(error instanceof ZodError)) {
if (!this.retryAllErrors && !(error instanceof ZodError)) {
throw error
}

Expand Down Expand Up @@ -428,11 +442,9 @@ export type InstructorClient<C extends GenericClient | OpenAI> = Instructor<C> &
* @param args
* @returns
*/
export default function createInstructor<C extends GenericClient | OpenAI>(args: {
client: OpenAILikeClient<C>
mode: Mode
debug?: boolean
}): InstructorClient<C> {
export default function createInstructor<C extends GenericClient | OpenAI>(
args: InstructorConfig<C>
): InstructorClient<C> {
const instructor = new Instructor<C>(args)
const instructorWithProxy = new Proxy(instructor, {
get: (target, prop, receiver) => {
Expand Down
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export interface InstructorConfig<C> {
client: OpenAILikeClient<C>
mode: Mode
debug?: boolean
logger?: <T extends unknown[]>(level: LogLevel, ...args: T) => void
retryAllErrors?: boolean
}

export type InstructorChatCompletionParams<T extends z.AnyZodObject> = {
Expand Down
68 changes: 68 additions & 0 deletions tests/logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import Instructor from "@/instructor"
import { describe, expect, spyOn, test } from "bun:test"
import OpenAI from "openai"
import { z } from "zod"

const textBlock = `
In our recent online meeting, participants from various backgrounds joined to discuss the upcoming tech conference. The names and contact details of the participants were as follows:

- Name: John Doe, Email: [email protected], Twitter: @TechGuru44
- Name: Jane Smith, Email: [email protected], Twitter: @DigitalDiva88
- Name: Alex Johnson, Email: [email protected], Twitter: @CodeMaster2023

During the meeting, we agreed on several key points. The conference will be held on March 15th, 2024, at the Grand Tech Arena located at 4521 Innovation Drive. Dr. Emily Johnson, a renowned AI researcher, will be our keynote speaker.

The budget for the event is set at $50,000, covering venue costs, speaker fees, and promotional activities. Each participant is expected to contribute an article to the conference blog by February 20th.

A follow-up meeting is scheduled for January 25th at 3 PM GMT to finalize the agenda and confirm the list of speakers.
`

async function extractUser() {
const ExtractionValuesSchema = z.object({
users: z
.array(
z.object({
name: z.string(),
handle: z.string(),
twitter: z.string()
})
)
.min(3),
location: z.string(),
budget: z.number()
})

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

const client = Instructor({
client: oai,
mode: "TOOLS",
logger: (level, ...args) => console.log(`[CUSTOM LOGGER | ${level}]`, ...args)
})

const extraction = await client.chat.completions.create({
messages: [{ role: "user", content: textBlock }],
model: "gpt-4o",
response_model: { schema: ExtractionValuesSchema, name: "Extr" },
max_retries: 1,
seed: 1
})

return extraction
}

describe("Custom Logger", () => {
test("Should log using custom logger", async () => {
const consoleSpy = spyOn(console, "log")
await extractUser()

expect(consoleSpy.mock.calls.flatMap(args => args.join(" ")).toString()).toContain(
"[CUSTOM LOGGER"
)

consoleSpy.mockRestore()
})
})
Loading