Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File not showing proper output in Dashboard #78

Open
jwalishjoseph opened this issue Mar 2, 2024 · 7 comments
Open

File not showing proper output in Dashboard #78

jwalishjoseph opened this issue Mar 2, 2024 · 7 comments

Comments

@jwalishjoseph
Copy link

image
image

@achris-alonzo30
Copy link

I had the same problem @jwalishjoseph. The problem is that the TRPC is not working properly either update the code if you're using the old version or don't use TRPC

@jwalishjoseph
Copy link
Author

how to fix the code

@achris-alonzo30
Copy link

how to fix the code

I don't really use TRPC so I can't help you with that.

@jwalishjoseph
Copy link
Author

What alternative should I consider instead of using TRPC?

@achris-alonzo30
Copy link

What alternative should I consider instead of using TRPC?

I would recommend not using TRPC at all or just use tanstack query itself.

@achris-alonzo30
Copy link

What alternative should I consider instead of using TRPC?

I'm also trying to recreate it myself, so I can show you my own approach.

@Shubham-sharma8
Copy link

Try this
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'
import { getUserSubscriptionPlan } from '@/lib/stripe'
import { PLANS } from '@/config/stripe'

const f = createUploadthing()

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

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

const subscriptionPlan = await getUserSubscriptionPlan()

return { subscriptionPlan, userId: user.id }
}

const onUploadComplete = async ({
metadata,
file,
}: {
metadata: Awaited<ReturnType>
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://utfs.io/f/${file.key},
uploadStatus: 'PROCESSING',
},
})

try {
const response = await fetch(
https://utfs.io/f/${file.key}
)

const blob = await response.blob()

const loader = new PDFLoader(blob)

const pageLevelDocs = await loader.load()

const pagesAmt = pageLevelDocs.length

const { subscriptionPlan } = metadata
const { isSubscribed } = subscriptionPlan

const isProExceeded =
  pagesAmt >
  PLANS.find((plan) => plan.name === 'Pro')!.pagesPerPdf
const isFreeExceeded =
  pagesAmt >
  PLANS.find((plan) => plan.name === 'Free')!
    .pagesPerPdf

if (
  (isSubscribed && isProExceeded) ||
  (!isSubscribed && isFreeExceeded)
) {
  await db.file.update({
    data: {
      uploadStatus: 'FAILED',
    },
    where: {
      id: createdFile.id,
    },
  })
}

// vectorize and index entire document
const pinecone = await getPineconeClient()
const pineconeIndex = pinecone.Index('quill')

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

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

await db.file.update({
  data: {
    uploadStatus: 'SUCCESS',
  },
  where: {
    id: createdFile.id,
  },
})

} catch (err) {
await db.file.update({
data: {
uploadStatus: 'FAILED',
},
where: {
id: createdFile.id,
},
})
}
}

export const ourFileRouter = {
freePlanUploader: f({ pdf: { maxFileSize: '4MB' } })
.middleware(middleware)
.onUploadComplete(onUploadComplete),
proPlanUploader: f({ pdf: { maxFileSize: '16MB' } })
.middleware(middleware)
.onUploadComplete(onUploadComplete),
} satisfies FileRouter

export type OurFileRouter = typeof ourFileRouter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants