const Instructor = require("@instructor-ai/instructor");
const OpenAI = require("openai");
const { z } = require("zod");
const { createHeaders } = require('portkey-ai');
// You need to run portkey SDK for this npx @portkey-ai/gateway
const PORTKEY_GATEWAY_URL = "http://localhost:8787/v1"
console.log(PORTKEY_GATEWAY_URL)
const porkeyClient = new OpenAI({
apiKey: process.env.ANTHROPIC_API_KEY,
baseURL: PORTKEY_GATEWAY_URL,
defaultHeaders: createHeaders({
provider: "anthropic",
apiKey: "PORTKEY_API_KEY",
model: 'claude-3-sonnet-20240229',
max_tokens: 512
})
});
const oai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY ?? undefined,
organization: process.env.OPENAI_ORG_ID ?? undefined
})
const client = Instructor.default({
client: porkeyClient,
mode: "TOOLS"
})
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>
async function main(){
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"
}
})
console.log(user)
}
// { age: 30, name: "Jason Liu" }
main()