|
|
import { NextResponse } from "next/server"; |
|
|
import { GoogleGenerativeAI } from "@google/generative-ai"; |
|
|
|
|
|
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!); |
|
|
const MODEL_ID = "gemini-2.0-flash"; |
|
|
|
|
|
export async function POST(request: Request) { |
|
|
try { |
|
|
const formData = await request.formData(); |
|
|
const file = formData.get("file") as File; |
|
|
const schema = JSON.parse(formData.get("schema") as string); |
|
|
|
|
|
|
|
|
const buffer = await file.arrayBuffer(); |
|
|
const base64 = Buffer.from(buffer).toString("base64"); |
|
|
|
|
|
const model = genAI.getGenerativeModel({ |
|
|
model: MODEL_ID, |
|
|
generationConfig: { |
|
|
responseMimeType: "application/json", |
|
|
responseSchema: schema, |
|
|
}, |
|
|
}); |
|
|
|
|
|
const prompt = "Extract the structured data from the following PDF file"; |
|
|
|
|
|
const result = await model.generateContent([ |
|
|
prompt, |
|
|
{ |
|
|
inlineData: { |
|
|
mimeType: "application/pdf", |
|
|
data: base64, |
|
|
}, |
|
|
}, |
|
|
]); |
|
|
|
|
|
const response = await result.response; |
|
|
const extractedData = JSON.parse(response.text()); |
|
|
|
|
|
return NextResponse.json(extractedData); |
|
|
} catch (error) { |
|
|
console.error("Error extracting data:", error); |
|
|
return NextResponse.json( |
|
|
{ |
|
|
error: |
|
|
"Failed to extract data, open a thread in discussions, could be be a rate limit issue.s", |
|
|
}, |
|
|
{ status: 500 } |
|
|
); |
|
|
} |
|
|
} |