File size: 4,908 Bytes
849c2f4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
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);
// Add specific course URLs as fallbacks based on course title and platform
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: [],
};
}
}
|