joschan21 / quill

Quill - A Modern SaaS-Platform Built With Next.js 13
1.91k stars 514 forks source link

PineconeIndex namespace Error #10

Closed iampapagray closed 1 year ago

iampapagray commented 1 year ago

While testing Pinecone within api/uploadthing/core.ts i get the error:

 TypeError: this.pineconeIndex.namespace is not a function
    at PineconeStore.addVectors (webpack-internal:///(rsc)/./node_modules/.pnpm/langchain@0.0.163_@pinecone-database+pinecone@1.1.0_pdf-parse@1.1.1/node_modules/langchain/dist/vectorstores/pinecone.js:113:46)
    at PineconeStore.addDocuments (webpack-internal:///(rsc)/./node_modules/.pnpm/langchain@0.0.163_@pinecone-database+pinecone@1.1.0_pdf-parse@1.1.1/node_modules/langchain/dist/vectorstores/pinecone.js:69:21)

core.ts :

import { db } from "@/db";
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";
import { createUploadthing, type FileRouter } from "uploadthing/next";

import { PDFLoader } from "langchain/document_loaders/fs/pdf";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { PineconeStore } from "langchain/vectorstores/pinecone";
import { getPineconeClient } from "@/lib/pinecone";

const f = createUploadthing();

const middleware = async () => {
  const { getUser } = getKindeServerSession();
  const user = getUser();

  if (!user || !user.id) throw new Error("Unauthorized");

  return { userId: user.id };
};

const onUploadComplete = async ({
  metadata,
  file,
}: {
  metadata: Awaited<ReturnType<typeof middleware>>;
  file: {
    key: string;
    name: string;
    url: string;
  };
}) => {
  const isFileExist = await db.file.findFirst({
    where: {
      key: file.key,
    },
  });

  if (isFileExist) return;

  const createdFile = await db.file.create({
    data: {
      key: file.key,
      name: file.name,
      userId: metadata.userId,
      url: `https://uploadthing-prod.s3.us-west-2.amazonaws.com/${file.key}`,
      uploadStatus: "PROCESSING",
    },
  });

  try {
    const response = await fetch(
      `https://uploadthing-prod.s3.us-west-2.amazonaws.com/${file.key}`
    );

    const blob = await response.blob();

    const loader = new PDFLoader(blob);

    const pageLevelDocs = await loader.load();

    const pagesAmt = pageLevelDocs.length;

    // vectorize and index entire document
    const pinecone = await getPineconeClient();
    const pineconeIndex = pinecone.Index("chatty-pdf");

    const embeddings = new OpenAIEmbeddings({
      openAIApiKey: process.env.OPENAI_API_KEY,
    });

    await PineconeStore.fromDocuments(pageLevelDocs, embeddings, {
      // @ts-ignore
      pineconeIndex,
      namespace: createdFile.id,
    });

    await db.file.update({
      data: {
        uploadStatus: "SUCCESS",
      },
      where: {
        id: createdFile.id,
      },
    });
  } catch (error) {
    console.log("Processing Err: ", error);
    await db.file.update({
      data: {
        uploadStatus: "FAILED",
      },
      where: {
        id: createdFile.id,
      },
    });
  }
};

export const ourFileRouter = {
  pdfUploader: f({ pdf: { maxFileSize: "4MB" } })
    .middleware(middleware)
    .onUploadComplete(onUploadComplete),
} satisfies FileRouter;

export type OurFileRouter = typeof ourFileRouter;

I can verify that the content of lib/pinecone.ts is correct (apikey and environment). How can i resolve this and continue learning?

iampapagray commented 1 year ago

I've been searching through Pinecone docs for a solution and i found out that Indexes in the [gcp-starter environment](https://docs.pinecone.io/docs/starter-environment) do not support namespaces.

Whiles creating my Index in Pinecone the only option available to me (Free Plan) is the Starter (which uses 'gcp-starter' environment).

This seems to be the reason for the error above.

Anyone know a way around this? since we will need the namespace (File ID) in order to search for indexes. How else can a unique id be passed to Pinecode?

anand-mukul commented 1 year ago

Follow this step on Pinecone:

Delete your Index, Delete Your Organization and, Create new Organization and Index . And, You're all Good! 👋

This will solve your problem: https://gist.github.com/anand-mukul/1a813bd686f7faff2e1793b6580fa539

iampapagray commented 1 year ago

Follow this step on Pinecone:

Delete your Index, Delete Your Organization and, Create new Organization and Index . And, You're all Good! 👋

This will solve your problem: https://gist.github.com/anand-mukul/1a813bd686f7faff2e1793b6580fa539

ALright. Thanks. Let me try that real quick.

Got a new environment. Thanks

iampapagray commented 1 year ago

@anand-mukul After getting the new environment, i am unable to test the app anymore. The dashboard page gets stuck on Loading. When i try uploading a file, i get stuck at Redirecting...

Even though the file is uploaded and the record saved in the DB. Pinecode also works correctly as the status of the file is updated to SUCCESS.

I added some logs to the TRPC routes and noticed that they are not been called. I'm not sure what caused this.

anand-mukul commented 1 year ago

Please share more details such as console image or error message also, verify the Pinecone Index and Environment Name.

iampapagray commented 1 year ago

@anand-mukul The pinecone details are correct and I can verify that the vectors are being stored on pinecone. There are also no console logs in the browser or the terminal.

I'll restart my laptop and retry and let you know if it still persists.

iampapagray commented 1 year ago

After restarting the computer everything seems to work as expected.

engmsaleh commented 1 year ago

Also, you need to comment out namespace from these

await PineconeStore.fromDocuments(
  pageLevelDocs,
  embeddings,
  {
    pineconeIndex,
    // namespace: createdFile.id,
  }
)