Skip to content

Commit 09f04d1

Browse files
authored
Custom logger & error retries (#177)
1 parent 6dd4255 commit 09f04d1

File tree

4 files changed

+95
-7
lines changed

4 files changed

+95
-7
lines changed

.changeset/modern-papayas-sneeze.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@instructor-ai/instructor": minor
3+
---
4+
5+
add new option for providing custom logger
6+
add new option for retrying on any error

src/instructor.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,26 @@ class Instructor<C extends GenericClient | OpenAI> {
3131
readonly mode: Mode
3232
readonly provider: Provider
3333
readonly debug: boolean = false
34+
readonly retryAllErrors: boolean = false
35+
readonly logger?: <T extends unknown[]>(level: LogLevel, ...args: T) => void
3436

3537
/**
3638
* Creates an instance of the `Instructor` class.
3739
* @param {OpenAILikeClient} client - An OpenAI-like client.
3840
* @param {string} mode - The mode of operation.
3941
*/
40-
constructor({ client, mode, debug = false }: InstructorConfig<C>) {
42+
constructor({
43+
client,
44+
mode,
45+
debug = false,
46+
logger = undefined,
47+
retryAllErrors = false
48+
}: InstructorConfig<C>) {
4149
this.client = client
4250
this.mode = mode
4351
this.debug = debug
52+
this.retryAllErrors = retryAllErrors
53+
this.logger = logger ?? undefined
4454

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

8595
private log<T extends unknown[]>(level: LogLevel, ...args: T) {
96+
if (this.logger) {
97+
this.logger(level, ...args)
98+
}
99+
86100
if (!this.debug && level === "debug") {
87101
return
88102
}
@@ -224,7 +238,7 @@ class Instructor<C extends GenericClient | OpenAI> {
224238

225239
return { ...validation.data, _meta: data?._meta ?? {} }
226240
} catch (error) {
227-
if (!(error instanceof ZodError)) {
241+
if (!this.retryAllErrors && !(error instanceof ZodError)) {
228242
throw error
229243
}
230244

@@ -428,11 +442,9 @@ export type InstructorClient<C extends GenericClient | OpenAI> = Instructor<C> &
428442
* @param args
429443
* @returns
430444
*/
431-
export default function createInstructor<C extends GenericClient | OpenAI>(args: {
432-
client: OpenAILikeClient<C>
433-
mode: Mode
434-
debug?: boolean
435-
}): InstructorClient<C> {
445+
export default function createInstructor<C extends GenericClient | OpenAI>(
446+
args: InstructorConfig<C>
447+
): InstructorClient<C> {
436448
const instructor = new Instructor<C>(args)
437449
const instructorWithProxy = new Proxy(instructor, {
438450
get: (target, prop, receiver) => {

src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ export interface InstructorConfig<C> {
7171
client: OpenAILikeClient<C>
7272
mode: Mode
7373
debug?: boolean
74+
logger?: <T extends unknown[]>(level: LogLevel, ...args: T) => void
75+
retryAllErrors?: boolean
7476
}
7577

7678
export type InstructorChatCompletionParams<T extends z.AnyZodObject> = {

tests/logger.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import Instructor from "@/instructor"
2+
import { describe, expect, spyOn, test } from "bun:test"
3+
import OpenAI from "openai"
4+
import { z } from "zod"
5+
6+
const textBlock = `
7+
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:
8+
9+
- Name: John Doe, Email: [email protected], Twitter: @TechGuru44
10+
- Name: Jane Smith, Email: [email protected], Twitter: @DigitalDiva88
11+
- Name: Alex Johnson, Email: [email protected], Twitter: @CodeMaster2023
12+
13+
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.
14+
15+
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.
16+
17+
A follow-up meeting is scheduled for January 25th at 3 PM GMT to finalize the agenda and confirm the list of speakers.
18+
`
19+
20+
async function extractUser() {
21+
const ExtractionValuesSchema = z.object({
22+
users: z
23+
.array(
24+
z.object({
25+
name: z.string(),
26+
handle: z.string(),
27+
twitter: z.string()
28+
})
29+
)
30+
.min(3),
31+
location: z.string(),
32+
budget: z.number()
33+
})
34+
35+
const oai = new OpenAI({
36+
apiKey: process.env.OPENAI_API_KEY ?? undefined,
37+
organization: process.env.OPENAI_ORG_ID ?? undefined
38+
})
39+
40+
const client = Instructor({
41+
client: oai,
42+
mode: "TOOLS",
43+
logger: (level, ...args) => console.log(`[CUSTOM LOGGER | ${level}]`, ...args)
44+
})
45+
46+
const extraction = await client.chat.completions.create({
47+
messages: [{ role: "user", content: textBlock }],
48+
model: "gpt-4o",
49+
response_model: { schema: ExtractionValuesSchema, name: "Extr" },
50+
max_retries: 1,
51+
seed: 1
52+
})
53+
54+
return extraction
55+
}
56+
57+
describe("Custom Logger", () => {
58+
test("Should log using custom logger", async () => {
59+
const consoleSpy = spyOn(console, "log")
60+
await extractUser()
61+
62+
expect(consoleSpy.mock.calls.flatMap(args => args.join(" ")).toString()).toContain(
63+
"[CUSTOM LOGGER"
64+
)
65+
66+
consoleSpy.mockRestore()
67+
})
68+
})

0 commit comments

Comments
 (0)