Hi everyone,
I am currently trying to create an app that have to send a PDF file (the file is sometimes scanned or not) to OpenAI API, then ask something like:
“Analyze this PDF file and return a JSON with the following structure:
{
‘user’: string,
‘issue’: string,
‘date’: string // format YYYY-MM-DD
}”
My current questions are:
-
What’s the best approach to implement this with the Assistants API?
- Should I use file_search or any other tool?
- Any specific setup for PDF handling?
-
Technical implementation:
- Using TypeScript with OpenAI SDK v4.80.0
- Need to handle the file upload correctly
- Want to ensure proper error handling
Has anyone successfully implemented something similar? Any best practices or pitfalls to avoid?
Code snippet of my current attempt:
const assistant = await openai.beta.assistants.create({
name: "PDF Analyzer",
model: "gpt-4o",
instructions: "You are an expert at analyzing PDF files and extracting specific information in JSON format.",
tools: [{ type: "file_search" }]
});
// Create thread and process file
const thread = await openai.beta.threads.create();
const file = await openai.files.create({
file: pdfContent,
purpose: 'assistants'
});
// Add message with JSON requirements
await openai.beta.threads.messages.create(thread.id, {
role: "user",
content: `Analyze the PDF and return a JSON with this structure:
{
"user": string,
"issue": string,
"date": string // YYYY-MM-DD
}
Ensure all fields are present and properly formatted.`,
file_ids: [file.id]
});