Spaces:
Runtime error
Runtime error
File size: 1,503 Bytes
73a1dae |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import type { NextApiRequest, NextApiResponse } from 'next';
import {
readHNSWLibModelFromLocal,
storesDir,
vectorStoreToHNSWLibModel,
} from '@/utils/file-handler';
import fs from 'fs-extra';
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
import { HNSWLib } from 'langchain/vectorstores/hnswlib';
import { XenovaTransformersEmbeddings } from '../../embed/hf'
async function handleDocs(text: string) {
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 });
const docs = await textSplitter.createDocuments([text]);
console.log(docs);
const vectorStore = await HNSWLib.fromDocuments(docs, new XenovaTransformersEmbeddings());
console.log(vectorStore);
return vectorStore;
}
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
const { text } = JSON.parse(req.body);
// console.log(text);
if (!text) {
return res.status(400).json({ message: 'No question in the request' });
}
const exists = await fs.exists(storesDir);
console.log(exists);
if (exists) {
console.log('read from ' + storesDir);
const model = await readHNSWLibModelFromLocal();
return res.status(200).send({
...model,
});
}
const vectorStore = await handleDocs(text);
const model = await vectorStoreToHNSWLibModel(vectorStore);
res.status(200).send({
...model,
});
}
export const config = {
api: {
bodyParser: true, // Disallow body parsing, consume as stream
},
}; |