Skip to content

Commit 0887f9d

Browse files
committed
feat: fix models
Signed-off-by: Arya Pratap Singh <notaryasingh@gmail.com>
1 parent acde716 commit 0887f9d

7 files changed

Lines changed: 427 additions & 250 deletions

File tree

frontend/app/api/ai-content/generate/route.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { getConvexClient } from "@/lib/convex";
66
import { auth } from "@clerk/nextjs/server";
77

88
// Initialize Gemini model (match project's existing pattern)
9-
const model = google("gemini-1.5-flash");
9+
const model = google("gemini-2.0-flash");
1010

1111
type ReqBody = {
1212
prompt: string;
@@ -111,7 +111,7 @@ export async function POST(request: NextRequest) {
111111
contentType: contentType ?? "ai_content",
112112
prompt,
113113
generatedText: assembled,
114-
model: "gemini-1.5-flash",
114+
model: "gemini-2.0-flash",
115115
tokens: undefined,
116116
usage: undefined,
117117
metadata: options
@@ -128,7 +128,7 @@ export async function POST(request: NextRequest) {
128128
// contentType: contentType ?? 'ai_content',
129129
// prompt,
130130
// generatedText: assembled,
131-
// model: "gemini-1.5-flash",
131+
// model: "gemini-2.0-flash",
132132
// metadata: options ? { rawOptions: JSON.stringify(options) } : undefined,
133133
// visibility: "private",
134134
// });

frontend/app/api/ai-study/generate/route.ts

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { api } from "@/convex/_generated/api";
55
import { getConvexClient } from "@/lib/convex";
66
import { auth } from "@clerk/nextjs/server";
77

8-
const model = google("gemini-1.5-flash");
8+
const model = google("gemini-2.0-flash");
99

1010
type ReqBody = {
1111
context?: string; // optional context (e.g., recent sessions, assignments)
@@ -15,35 +15,41 @@ type ReqBody = {
1515

1616
export async function POST(request: NextRequest) {
1717
try {
18-
const reqBody: ReqBody = await request.json();
19-
const { context, userId: clientUserId, options } = reqBody;
20-
21-
// Derive authenticated userId from Clerk for any persistence. We still allow
22-
// unauthenticated requests to stream content, but persistence requires auth.
23-
const { userId } = await auth();
24-
25-
// Build prompt + system instruction depending on requested contentType
26-
const contentType = options?.contentType;
27-
const userPromptNote = options?.userPrompt ? `\n\nUser request: ${options.userPrompt}` : "";
28-
29-
let prompt: string;
30-
let systemInstruction: string;
31-
32-
if (contentType === 'study_plan_title') {
33-
// Return a short, 3-8 word title for the provided plan text
34-
const planText = context || "";
35-
prompt = `Generate a concise, human-friendly title (3-8 words) for the following study plan or description:\n\n${planText}\n\nOnly output the title on a single line. Do not include any extra commentary.` + userPromptNote;
36-
systemInstruction = `You are an assistant that creates short titles for study plans. Return only a single short title.`;
37-
} else {
38-
// Default: full study plan generation
39-
prompt = `Create a detailed, actionable study plan for this student using the following context:\n${context || "No additional context provided."}${userPromptNote}\n\nProduce a structured plan with 3-6 recommended study sessions/tasks. For each item include:\n- A short title\n- An estimated duration (in minutes)\n- Priority (high/medium/low)\n- A one-sentence justification\n- Concrete next steps or exercises to do during the session\n\nOutput the plan in Markdown, with clear headings and bullet points.`;
40-
systemInstruction = `You are EduBox Study Planner. Produce a helpful, step-by-step study plan aimed at a college student. Favor slightly longer, concrete recommendations with next-step actions and example exercises.`;
41-
}
18+
const reqBody: ReqBody = await request.json();
19+
const { context, userId: clientUserId, options } = reqBody;
20+
21+
// Derive authenticated userId from Clerk for any persistence. We still allow
22+
// unauthenticated requests to stream content, but persistence requires auth.
23+
const { userId } = await auth();
24+
25+
// Build prompt + system instruction depending on requested contentType
26+
const contentType = options?.contentType;
27+
const userPromptNote = options?.userPrompt
28+
? `\n\nUser request: ${options.userPrompt}`
29+
: "";
30+
31+
let prompt: string;
32+
let systemInstruction: string;
33+
34+
if (contentType === "study_plan_title") {
35+
// Return a short, 3-8 word title for the provided plan text
36+
const planText = context || "";
37+
prompt =
38+
`Generate a concise, human-friendly title (3-8 words) for the following study plan or description:\n\n${planText}\n\nOnly output the title on a single line. Do not include any extra commentary.` +
39+
userPromptNote;
40+
systemInstruction = `You are an assistant that creates short titles for study plans. Return only a single short title.`;
41+
} else {
42+
// Default: full study plan generation
43+
prompt = `Create a detailed, actionable study plan for this student using the following context:\n${
44+
context || "No additional context provided."
45+
}${userPromptNote}\n\nProduce a structured plan with 3-6 recommended study sessions/tasks. For each item include:\n- A short title\n- An estimated duration (in minutes)\n- Priority (high/medium/low)\n- A one-sentence justification\n- Concrete next steps or exercises to do during the session\n\nOutput the plan in Markdown, with clear headings and bullet points.`;
46+
systemInstruction = `You are EduBox Study Planner. Produce a helpful, step-by-step study plan aimed at a college student. Favor slightly longer, concrete recommendations with next-step actions and example exercises.`;
47+
}
4248

43-
const messages = [
44-
{ role: "system" as const, content: systemInstruction },
45-
{ role: "user" as const, content: prompt },
46-
];
49+
const messages = [
50+
{ role: "system" as const, content: systemInstruction },
51+
{ role: "user" as const, content: prompt },
52+
];
4753

4854
const result = await streamText({
4955
model,
@@ -69,7 +75,8 @@ export async function POST(request: NextRequest) {
6975
while (true) {
7076
const { done, value } = await reader.read();
7177
if (done) break;
72-
const chunkText = typeof value === "string" ? value : decoder.decode(value);
78+
const chunkText =
79+
typeof value === "string" ? value : decoder.decode(value);
7380
assembled += chunkText;
7481
controller.enqueue(encoder.encode(chunkText));
7582
}
@@ -79,21 +86,27 @@ export async function POST(request: NextRequest) {
7986
// Persist assembled recommendations to Convex if userId present
8087
// Skip persistence for title-extraction calls (contentType === 'study_plan_title')
8188
// Persist only when we have an authenticated userId.
82-
if (userId && contentType !== 'study_plan_title') {
89+
if (userId && contentType !== "study_plan_title") {
8390
try {
8491
const convex = getConvexClient();
8592
await convex.mutation(api.generations.createGeneration, {
8693
// Use server-derived Clerk userId (ignore client-supplied value).
8794
userId,
88-
title: options?.title ?? (options?.contentType === 'study_plan' ? 'Study Plan' : 'Study Recommendations'),
95+
title:
96+
options?.title ??
97+
(options?.contentType === "study_plan"
98+
? "Study Plan"
99+
: "Study Recommendations"),
89100
contentType: options?.contentType ?? "study_recommendations",
90101
prompt,
91102
generatedText: assembled,
92-
model: "gemini-1.5-flash",
103+
model: "gemini-2.0-flash",
93104
tokens: undefined,
94105
usage: undefined,
95106
// Avoid passing nested arbitrary objects; store a compact string instead
96-
metadata: options ? { rawOptions: JSON.stringify(options) } : undefined,
107+
metadata: options
108+
? { rawOptions: JSON.stringify(options) }
109+
: undefined,
97110
visibility: "private",
98111
});
99112
} catch (err) {
@@ -112,7 +125,10 @@ export async function POST(request: NextRequest) {
112125
});
113126
} catch (err: any) {
114127
console.error("ai-study generate error:", err);
115-
return NextResponse.json({ error: "Failed to generate study recommendations" }, { status: 500 });
128+
return NextResponse.json(
129+
{ error: "Failed to generate study recommendations" },
130+
{ status: 500 }
131+
);
116132
}
117133
}
118134

0 commit comments

Comments
 (0)