ai integration
This commit is contained in:
51
src/app/api/ai-event/route.ts
Normal file
51
src/app/api/ai-event/route.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const { prompt } = await request.json();
|
||||
|
||||
const systemPrompt = `
|
||||
You are an assistant that converts natural language event descriptions into JSON objects
|
||||
matching this TypeScript type EXACTLY:
|
||||
{
|
||||
title: string,
|
||||
description?: string,
|
||||
location?: string,
|
||||
url?: string,
|
||||
start: string, // ISO datetime like 2024-06-14T13:00:00Z
|
||||
end?: string,
|
||||
allDay?: boolean
|
||||
}
|
||||
Today is ${new Date().toISOString().split("T")[0]}.
|
||||
If no time is given, assume allDay event.
|
||||
If no end time is given and not allDay, make it 1 hour after start.
|
||||
Output ONLY valid JSON, nothing else.
|
||||
`;
|
||||
|
||||
const res = await fetch("https://openrouter.ai/api/v1/chat/completions", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: "openai/gpt-3.5-turbo", // Or 'mistral/mistral-tiny' for cheaper
|
||||
messages: [
|
||||
{ role: "system", content: systemPrompt },
|
||||
{ role: "user", content: prompt },
|
||||
],
|
||||
}),
|
||||
});
|
||||
|
||||
const data = await res.json();
|
||||
|
||||
try {
|
||||
const content = data.choices[0].message.content;
|
||||
const parsed = JSON.parse(content);
|
||||
return NextResponse.json(parsed);
|
||||
} catch (e) {
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to parse AI output", raw: data },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user