Efficient Retrieval of Multiple Documents Using PineconeDB

  1. Objective: Efficiently retrieve multiple documents from PineconeDB based on a PDF ID field in their metadata, prioritizing relevant documents.
  2. Approach:
  • Initialize PineconeDB.
  • Check if PineconeDB is properly initialized.
  • Set up filtering criteria based on PDF IDs.
  • Retrieve documents from PineconeDB matching the provided PDF IDs.
  1. Code Snippet:
    exports.resumeCompare = async (req, res) => {
    const { question, history, pdfId, userId } = req.body;

// Initialize PineconeDB
const pinecone = await initPinecone();

// Check if PineconeDB is properly initialized
if (!pinecone || !pinecone.Index) {
throw new Error(“Pinecone Index method not available”);
}

const index = pinecone.Index(process.env.PINECONE_INDEX_NAME);

// Set up filtering criteria based on PDF IDs
const pdf = pdfId.map((id) => id.toString());

const vectorStore = await PineconeStore.fromExistingIndex(
new OpenAIEmbeddings({}),
{
pineconeIndex: index,
textKey: “text”,
namespace: userId,
filter: { pdfId: { $in: pdf } },
topK: 5,
}
);

// Retrieve documents from PineconeDB
const sourceDocuments = await vectorStore.query();

// Handle the retrieved documents as needed

res.status(200).json({ sourceDocuments });
};