|
import OpenAI from "openai"; |
|
|
|
interface Course { |
|
title: string; |
|
description: string; |
|
duration: string; |
|
platform: string; |
|
level: string; |
|
benefitsInterim: string; |
|
benefitsDream: string; |
|
link: string; |
|
} |
|
|
|
interface Recommendation { |
|
courses: Course[]; |
|
roles: Array<{ |
|
title: string; |
|
description: string; |
|
timeline: string; |
|
salary: string; |
|
}>; |
|
} |
|
|
|
export async function getRecommendations( |
|
parsedData: any, |
|
dreamRole: string, |
|
questionnaireData?: any |
|
): Promise<Recommendation> { |
|
try { |
|
const openai = new OpenAI({ |
|
apiKey: process.env.OPENAI_API_KEY, |
|
}); |
|
|
|
const completion = await openai.chat.completions.create({ |
|
model: "gpt-4o-mini", |
|
messages: [ |
|
{ |
|
role: "system", |
|
content: |
|
"You are a career advisor creating personalized recommendations. you MUST provide a direct URL to the specific course page (not the platform's homepage). Use real, existing courses from platforms like Coursera, Udemy, Pluralsight, LinkedIn Learning,or edX.", |
|
}, |
|
{ |
|
role: "user", |
|
content: `Create detailed course recommendations based on: |
|
Skills: ${parsedData.skills.join(", ")} |
|
Experience: ${parsedData.experience.join("\n")} |
|
Education: ${parsedData.education.join("\n")} |
|
Dream Role: ${dreamRole} |
|
|
|
Additional Personal Context: |
|
- Current Status: ${questionnaireData.currentStatus} |
|
- Interested in Interim Role: ${questionnaireData.interimRole} |
|
- Motivation: ${questionnaireData.motivation} |
|
- Preferred Learning Style: ${questionnaireData.learningPreference} |
|
- Daily Time Commitment: ${questionnaireData.timeCommitment} hours |
|
- Main Challenges: ${questionnaireData.challenges.join(", ")} |
|
- Target Timeframe: ${questionnaireData.timeframe} |
|
|
|
For each course recommendation, you must include: |
|
1. A clear explanation of how it helps with interim roles (benefitsInterim) |
|
2. A clear explanation of how it contributes to the dream role (benefitsDream) |
|
3. A valid course URL from platforms like Coursera, Udemy, Pluralsight, LinkedIn Learning, or edX |
|
|
|
Return exactly this JSON structure: |
|
{ |
|
"courses": [ |
|
{ |
|
"title": "Course Name", |
|
"description": "Course Description", |
|
"duration": "Duration", |
|
"platform": "Platform Name", |
|
"learningStyle": "Learning Style", |
|
"benefitsInterim": "Provide a specific 1-2 sentence explanation of how this course helps with immediate employment opportunities", |
|
"benefitsDream": "Provide a specific 1-2 sentence explanation of how this course builds towards the dream role", |
|
"link": "Direct URL to the specific course"" |
|
} |
|
], |
|
"roles": [ |
|
{ |
|
"title": "Role Title", |
|
"description": "Role Description", |
|
"timeline": "Timeline", |
|
"salary": "Salary Range" |
|
} |
|
] |
|
}`, |
|
}, |
|
], |
|
response_format: { type: "json_object" }, |
|
}); |
|
|
|
if (!completion.choices[0]?.message?.content) { |
|
throw new Error("No recommendations generated"); |
|
} |
|
|
|
const result = JSON.parse(completion.choices[0].message.content); |
|
console.log("Raw API response:", result); |
|
|
|
|
|
const coursesWithLinks = result.courses.map((course: any) => { |
|
let fallbackLink; |
|
const encodedTitle = encodeURIComponent(course.title); |
|
|
|
switch (course.platform.toLowerCase()) { |
|
case "coursera": |
|
fallbackLink = `https://www.coursera.org/search?query=${encodedTitle}`; |
|
break; |
|
case "udemy": |
|
fallbackLink = `https://www.udemy.com/courses/search/?q=${encodedTitle}`; |
|
break; |
|
case "edx": |
|
fallbackLink = `https://www.edx.org/search?q=${encodedTitle}`; |
|
break; |
|
default: |
|
fallbackLink = `https://www.google.com/search?q=${encodedTitle}+course`; |
|
} |
|
|
|
return { |
|
...course, |
|
benefitsInterim: |
|
course.benefitsInterim || |
|
"This course provides foundational skills needed for entry-level positions.", |
|
benefitsDream: |
|
course.benefitsDream || |
|
"This course builds core competencies required for your target role.", |
|
link: course.link || fallbackLink, |
|
}; |
|
}); |
|
return { |
|
courses: coursesWithLinks, |
|
roles: result.roles || [], |
|
}; |
|
} catch (error) { |
|
console.error("Error getting recommendations:", error); |
|
return { |
|
courses: [], |
|
roles: [], |
|
}; |
|
} |
|
} |
|
|