import { ChatCompletionsParams } from "premai/resources/chat";
import PremAI from "premai";
type PersonCV = {
name: string;
email?: string;
phone?: string;
address?: string;
education?: string[];
work_experience?: string[];
skills?: string[];
awards_and_honors?: string[];
};
const schema = {
name: "PersonCV",
schema: {
type: "object",
properties: {
name: { type: "string" },
email: { type: "string" },
phone: { type: "string" },
address: { type: "string" },
education: { type: "array", items: { type: "string" } },
work_experience: { type: "array", items: { type: "string" } },
skills: { type: "array", items: { type: "string" } },
awards_and_honors: { type: "array", items: { type: "string" } }
},
required: ["name"]
}
};
const cvText = `
John Doe
123 Main Street, Anytown, USA | [email protected] | 555-123-4567
Education
Bachelor of Science in Computer Science, University of Example, 2020-2024
Relevant Coursework: Data Structures, Algorithms, Database Management
Work Experience
Software Engineer Intern, Example Corp, Summer 2023
- Assisted in developing and testing software applications.
- Wrote and maintained technical documentation.
Skills
Programming Languages: Python, Java, C++
Databases: SQL, NoSQL
Operating Systems: Windows, Linux
Awards and Honors
Dean's List, University of Example, 2021-2024
`;
const client = new PremAI({
apiKey: process.env.PREMAI_API_KEY || "",
});
const messages: ChatCompletionsParams.Message[] = [
{ role: "system", content: "You are a helpful assistant that can parse a person's info and return a structured object." },
{ role: "user", content: cvText }
] as any;
const response = await client.chat.completions({
model: "phi-4",
messages,
response_format: {
type: "json_schema",
json_schema: schema
},
});
const raw = response.choices[0].message.content || "";
const start = raw.indexOf("{");
const end = raw.lastIndexOf("}") + 1;
const content = raw.slice(start, end);
const parsed: PersonCV = JSON.parse(content);
console.log(JSON.stringify(parsed, null, 2));