I am trying to build an AI Saas, using next.js, aws s3, neondb, and pineconedb that takes in a pdf and let’s you chat with openAI about the contents. I am currently writing a function that takes in the pdf and uses PDFLoader from Langchain to convert the pdf in text strings. When I test this function though, certain pdfs work and others don’t. I’ve noticed that simple pdfs that are from say google doc or word are easily processed and return an object with the contents. However, when I try to test a pdf that is a print to pdf from a webpage, I get an empty array . Here is an example: ‘https://www.congress.gov/118/bills/hr5009/BILLS-118hr5009pcs.pdf’
Here is my code so far, it spans across multiple files. I’ve also attached a snippet of the output I get from both types of pdfs. Please let me know if you need to see any other code and I will provide it:
pinecone.ts
import { Pinecone } from "@pinecone-database/pinecone";
`import { downloadFromS3 } from "./S3-server";
import { PDFLoader } from "langchain/document_loaders/fs/pdf";
import {
Document,
RecursiveCharacterTextSplitter,
} from "@pinecone-database/doc-splitter";
const pinecone = new Pinecone({
apiKey: `${process.env.PINECONE_API_KEY}`,
});
type PDFPage = {
pageContent: string;
metadata: {
loc: { pageNumber: number };
};
};
export async function loadS3IntoPincecone(fileKey: string) {
console.log("downloading s3 into file system");
const file_name = await downloadFromS3(fileKey);
if (!file_name) {
throw new Error("could not download file from s3");
}
const loader = new PDFLoader(file_name);
const pages = (await loader.load()) as PDFPage[];
return pages;
}`
route.ts
import { loadS3IntoPincecone } from "@/lib/pinecone";
import { NextResponse } from "next/server";
export async function POST(req: Request, res: Response) {
try {
const body = await req.json();
const { file_key, file_name } = body;
const pages = await loadS3IntoPincecone(file_key);
return NextResponse.json({ pages });
} catch (error) {
console.error(error);
return NextResponse.json(
{ error: "internal server error" },
{ status: 500 }
);
}
}
S3-server.ts
import AWS from "aws-sdk";
import fs from "fs";
export async function downloadFromS3(file_key: string) {
try {
AWS.config.update({
accessKeyId: process.env.NEXT_PUBLIC_S3_ACCESS_KEY_ID,
secretAccessKey: process.env.NEXT_PUBLIC_S3_SECRET_ACCESS_KEY,
});
const s3 = new AWS.S3({
params: {
Bucket: process.env.NEXT_PUBLIC_S3_BUCKET_NAME,
},
region: "us-east-2",
});
const params = {
Bucket: process.env.NEXT_PUBLIC_S3_BUCKET_NAME!,
Key: file_key,
};
const obj = await s3.getObject(params).promise();
const file_name = `C:/Users/camya/Documents/govguide_tmp/pdf-${Date.now()}.pdf`;
fs.writeFileSync(file_name, obj.Body as Buffer);
return file_name;
} catch (error) {
console.error(error);
return null;
}
}
s3.ts
import AWS from "aws-sdk";
export async function uploadtoS3(file: File) {
try {
AWS.config.update({
accessKeyId: process.env.NEXT_PUBLIC_S3_ACCESS_KEY_ID,
secretAccessKey: process.env.NEXT_PUBLIC_S3_SECRET_ACCESS_KEY,
});
const s3 = new AWS.S3({
params: {
Bucket: process.env.NEXT_PUBLIC_S3_BUCKET_NAME,
},
region: "us-east-2",
});
const file_key =
"uploads/" + Date.now().toString() + file.name.replace(" ", "-");
const params = {
Bucket: process.env.NEXT_PUBLIC_S3_BUCKET_NAME!,
Key: file_key,
Body: file,
};
const upload = s3
.putObject(params)
.on("httpUploadProgress", (evt) => {
console.log(
"uploading to s3...",
parseInt(((evt.loaded * 100) / evt.total).toString()) + "%"
);
})
.promise();
await upload.then((data) => {
console.log("successfully uploaded to s3!", file_key);
});
return Promise.resolve({
file_key,
file_name: file.name,
});
} catch (error) {}
}
export function getS3Url(file_key: string) {
const url = `https://${process.env.NEXT_PUBLIC_S3_BUCKET_NAME}.s3.us-east-2.amazonaws.com/${file_key}`;
return url;
}
Thank you in advance!
I tried to upload a pdf into my app and get an object that contains a text string output of the file. When I do so for certain ‘print to pdf’ webpages I get an empty object.
Mya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.