Comments (2)
PATH::\src\app\api\message\route.ts (Also read inline comment)
import { db } from '@/db' import { openai } from '@/lib/openai' import { getPineconeClient } from '@/lib/pinecone' import { SendMessageValidator } from '@/lib/validators/SendMessageValidator' import { getKindeServerSession } from '@kinde-oss/kinde-auth-nextjs/server' import { OpenAIEmbeddings } from 'langchain/embeddings/openai' import { PineconeStore } from 'langchain/vectorstores/pinecone' import { NextRequest } from 'next/server' import { OpenAIStream, StreamingTextResponse } from 'ai' export const POST = async (req: NextRequest) => { // endpoint for asking a question to a pdf file const body = await req.json() const { getUser } = getKindeServerSession() const user = getUser() const { id: userId } = user if (!userId) return new Response('Unauthorized', { status: 401 }) const { fileId, message } = SendMessageValidator.parse(body) const file = await db.file.findFirst({ where: { id: fileId, userId, }, }) if (!file) return new Response('Not found', { status: 404 }) await db.message.create({ data: { text: message, isUserMessage: true, userId, fileId, }, }) // 1: vectorize message const embeddings = new OpenAIEmbeddings({ openAIApiKey: process.env.OPENAI_API_KEY, }) const pinecone = await getPineconeClient() //Must add this line and also check this in ..\src\app\api\uploadthing\core.ts const pineconeIndex = pinecone.Index('YourPineconeIndexName') //Replace it your pinecone index name const vectorStore = await PineconeStore.fromExistingIndex( embeddings, { //@ts-ignore pineconeIndex, namespace: file.id, } ) const results = await vectorStore.similaritySearch( message, 4 ) const prevMessages = await db.message.findMany({ where: { fileId, }, orderBy: { createdAt: 'asc', }, take: 6, }) const formattedPrevMessages = prevMessages.map((msg) => ({ role: msg.isUserMessage ? ('user' as const) : ('assistant' as const), content: msg.text, })) const response = await openai.chat.completions.create({ model: 'gpt-3.5-turbo', temperature: 0, stream: true, messages: [ { role: 'system', content: 'Use the following pieces of context (or previous conversaton if needed) to answer the users question in markdown format.', }, { role: 'user', content: `Use the following pieces of context (or previous conversaton if needed) to answer the users question in markdown format. \nIf you don't know the answer, just say that you don't know, don't try to make up an answer. \n----------------\n PREVIOUS CONVERSATION: ${formattedPrevMessages.map((message) => { if (message.role === 'user') return `User: ${message.content}\n` return `Assistant: ${message.content}\n` })} \n----------------\n CONTEXT: ${results.map((r) => r.pageContent).join('\n\n')} USER INPUT: ${message}`, }, ], }) const stream = OpenAIStream(response, { async onCompletion(completion) { await db.message.create({ data: { text: completion, isUserMessage: false, fileId, userId, }, }) }, }) return new StreamingTextResponse(stream) }
Also ADD THIS::\src\lib\pinecone.ts
import { PineconeClient } from '@pinecone-database/pinecone'
export const getPineconeClient = async () => {
const client = new PineconeClient()
await client.init({
apiKey: process.env.PINECONE_API_KEY!,
environment: 'asia-southeast1-gcp-free', //Replace it you Pinecone Environment, Check pinecone website
})
return client
}
from quill.
Ad-hoc solution
// @ts-ignore <-- add this
const stream = OpenAIStream(response, {
async onCompletion(completion) {
await db.message.create({
data: {
text: completion,
isUserMessage: false,
fileId,
userId
}
})
}
})
from quill.
Related Issues (20)
- tRPC error calling Stripe, missing API HOT 1
- Setting up your account... You will be redirected automatically Loading issue
- File not showing in Prisma studio HOT 1
- Throwing an Error sign even though UserAccount file is created and imported
- Billing info after upgrading returns undefined, and the Upgrade "Diamond" doesn't update on making stripe payment, It is updating in the stripe dashboard though, can anyone help?
- Skeleton not showing HOT 2
- File not showing proper output in Dashboard HOT 7
- .
- when i signin then only loader is working not signin successfully please anyone help me and and another error is Property 'email' does not exist on type 'Promise<KindeUser | null>'.ts(2339) HOT 5
- isLoading is not working it is giving error can anyone help HOT 10
- Issue when uploading PDF in dashboard. HOT 8
- The logged-in user is not showing in Prisma Studio. HOT 2
- ChatWrapper / getFileUploadStatus: Property status does not exist on type HOT 4
- Cannot find module langchain/dist/document_loaders/fs/pdf or its corresponding type declarations HOT 1
- Error [InsufficientQuotaError]: 429 HOT 1
- The logged-in user is not showing in Prisma Studio. im using Neon console HOT 1
- Property 'status' does not exist on type 'Query<{ status: UploadStatus; }, TRPCClientErrorLike<{ input: { fileId: string; }; output: { status: UploadStatus; }; transformer: false; errorShape: DefaultErrorShape; }>, { ...; }, any>'.ts(2339) HOT 3
- Image Source error in image-loader.js HOT 4
- Property 'id' does not exist on type 'KindeUser | null'.ts(2339) on src\app\api\message\route.ts HOT 5
- Object literal may only specify known properties, and 'environment' does not exist in type 'PineconeConfiguration'.ts(2353) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from quill.