Spaces:
Runtime error
Runtime error
| import type { NextApiRequest, NextApiResponse } from 'next'; | |
| import { storesDir } from '@/utils/file-handler'; | |
| import { makeChain } from '@/utils/make-chain'; | |
| import { XenovaTransformersEmbeddings } from '../../embed/hf'; | |
| import { HNSWLib } from 'langchain/vectorstores/hnswlib'; | |
| let vectorStore: HNSWLib; | |
| export default async function handler( | |
| req: NextApiRequest, | |
| res: NextApiResponse, | |
| ) { | |
| const { prompt, messages } = req.body; | |
| if (!prompt) { | |
| return res.status(400).json({ message: 'No question in the request' }); | |
| } | |
| // OpenAI recommends replacing newlines with spaces for best results | |
| const sanitizedQuestion = prompt.trim().replaceAll('\n', ' '); | |
| res.writeHead(200, { | |
| 'Content-Type': 'text/event-stream', | |
| 'Cache-Control': 'no-cache, no-transform', | |
| Connection: 'keep-alive', | |
| }); | |
| const sendData = (data: string) => { | |
| res.write(`${data}\n\n`); | |
| }; | |
| // load store | |
| if (!vectorStore) { | |
| vectorStore = await HNSWLib.load(storesDir, new XenovaTransformersEmbeddings()); | |
| } | |
| //create chain | |
| const chain = makeChain(vectorStore, (token: string) => { | |
| // skipping stremaing for now | |
| // sendData(JSON.stringify({ data: token })); | |
| }); | |
| try { | |
| //Ask a question | |
| const response = await chain.call({ | |
| question: sanitizedQuestion, | |
| chat_history: messages || [], | |
| }); | |
| console.log('response', response); | |
| sendData(response.text); | |
| } catch (error) { | |
| console.log('error', error); | |
| } finally { | |
| // sendData('[DONE]'); | |
| res.end(); | |
| } | |
| } | |