ai integration
This commit is contained in:
46
src/app/api/ai-summary/route.ts
Normal file
46
src/app/api/ai-summary/route.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { CalendarEvent } from "@/lib/types";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const { events } = await request.json();
|
||||
|
||||
if (!events || !Array.isArray(events)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid events array" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const res = await fetch("https://openrouter.ai/api/v1/chat/completions", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`, // Server-side only
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: "@preset/i-cal-editor-summarize", // FREE model
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
content: `You summarize a list of events in natural language. Include date, time, and title. Be concise.`,
|
||||
},
|
||||
{ role: "user", content: JSON.stringify(events) },
|
||||
],
|
||||
temperature: 0.4,
|
||||
// max_tokens: 300,
|
||||
}),
|
||||
});
|
||||
|
||||
const data = await res.json();
|
||||
const summary =
|
||||
data?.choices?.[0]?.message?.content || "No summary generated.";
|
||||
return NextResponse.json({ summary });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to summarize events" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user