日志记录

为了查看发送给 OpenAI 的请求和接收到的响应,您可以在初始化 Instructor 时将 debug 设置为 true。这将显示发送给 OpenAI 的请求和响应。这对于调试和理解发送给 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: "TOOLS",
  debug: true // <== HERE
})

const UserSchema = z.object({
  // Description will be used in the prompt
  age: z.number().describe("The age of the user"),
  name: z.string()
})

// User will be of type z.infer<typeof UserSchema>
const user = await client.chat.completions.create({
  messages: [{ role: "user", content: "Jason Liu is 30 years old" }],
  model: "gpt-3.5-turbo",
  response_model: {
    schema: UserSchema,
    name: "User"
  }
})
// [Instructor:DEBUG] 2024-03-28T13:42:00.178Z: User making completion call with params:  {
//   messages: [ { role: 'user', content: 'Jason Liu is 30 years old' } ],
//   model: 'gpt-3.5-turbo',
//   stream: false,
//   tool_choice: { type: 'function', function: { name: 'User' } },
//   tools: [ { type: 'function', function: [Object] } ]
// }
// [Instructor:DEBUG] 2024-03-28T13:42:00.846Z: User Completion validation:  { success: true, data: { age: 30, name: 'Jason Liu' } }

console.log(user)
// { age: 30, name: "Jason Liu" }