3 and 4 completed
This commit is contained in:
1 parent
3573709d99
commit
97436c6823
21 files changed
+2168
-558
No files matched your search
@@ -6,7 +6,7 @@ import log from "@/lib/logger";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
const { userId, useExternalModel } = await req.json();
|
||||
const { userId, useExternalModel, modelProvider } = await req.json();
|
||||
|
||||
if (!userId) {
|
||||
return NextResponse.json(
|
||||
@@ -43,7 +43,78 @@ export async function POST(req: Request) {
|
||||
|
||||
let parsedResponse;
|
||||
|
||||
if (useExternalModel) {
|
||||
// Determine which AI provider to use
|
||||
const provider =
|
||||
modelProvider || (useExternalModel ? "deepseek" : "ollama");
|
||||
|
||||
if (provider === "openai") {
|
||||
// Use OpenAI
|
||||
const openaiApiKey = process.env.OPENAI_API_KEY;
|
||||
|
||||
if (!openaiApiKey) {
|
||||
return NextResponse.json(
|
||||
{ error: "OpenAI API key not configured" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
|
||||
log.debug("Using OpenAI model", { userId });
|
||||
|
||||
const openaiResponse = await fetch(
|
||||
"https://api.openai.com/v1/chat/completions",
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${openaiApiKey}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: "gpt-4o-mini",
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
content:
|
||||
'You are a professional fitness trainer and nutritionist. Always respond with valid JSON only, no markdown or code blocks. The response must have this exact structure: {"recommendationText": "string", "activityPlan": "string", "dietPlan": "string"}',
|
||||
},
|
||||
{
|
||||
role: "user",
|
||||
content: prompt,
|
||||
},
|
||||
],
|
||||
temperature: 0.7,
|
||||
max_tokens: 1500,
|
||||
response_format: { type: "json_object" },
|
||||
}),
|
||||
},
|
||||
);
|
||||
|
||||
if (!openaiResponse.ok) {
|
||||
const errorText = await openaiResponse.text();
|
||||
log.error("OpenAI API request failed", new Error(errorText), {
|
||||
status: openaiResponse.status,
|
||||
});
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to generate recommendation from OpenAI" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
|
||||
const openaiData = await openaiResponse.json();
|
||||
log.debug("Received OpenAI response", { openaiData });
|
||||
|
||||
try {
|
||||
const content = openaiData.choices[0].message.content;
|
||||
parsedResponse = JSON.parse(content);
|
||||
} catch (e) {
|
||||
log.error("Failed to parse OpenAI response", e, {
|
||||
response: openaiData,
|
||||
});
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid response format from OpenAI" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
} else if (provider === "deepseek") {
|
||||
// Use DeepSeek AI
|
||||
const deepseekApiKey = process.env.DEEPSEEK_API_KEY;
|
||||
|
||||
@@ -203,11 +274,12 @@ export async function POST(req: Request) {
|
||||
id: crypto.randomUUID(),
|
||||
userId,
|
||||
fitnessProfileId: profile.id,
|
||||
type: "ai_plan",
|
||||
recommendationText: parsedResponse.recommendationText,
|
||||
activityPlan: parsedResponse.activityPlan,
|
||||
dietPlan: parsedResponse.dietPlan,
|
||||
status: "pending",
|
||||
generatedAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
|
||||
return NextResponse.json(recommendation);
|
||||
|
||||
Reference in new issue
Block a user