Spaces:
Runtime error
Runtime error
File size: 1,243 Bytes
73a1dae 8c64b1d 73a1dae 8c64b1d 5e14bd6 8c64b1d 73a1dae 5e14bd6 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 |
import type { NextApiRequest, NextApiResponse } from 'next';
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
import { TransformersEmbeddingFunction } from '../../embed/hf';
const {ChromaClient} = require('chromadb');
const client = new ChromaClient();
async function handleDocs(text: string) {
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 });
const docs = await textSplitter.createDocuments([text]);
const collection = await client.createCollection({name: "docs", embeddingFunction: TransformersEmbeddingFunction})
await collection.add({
ids: [...docs.map((v, k) => k)],
metadatas: [...docs.map(doc => doc.metadata)],
documents: [...docs.map(doc => doc.pageContent)],
});
return collection;
}
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 vectorStore = await handleDocs(text);
res.status(200).send({
model: vectorStore,
});
}
export const config = {
api: {
bodyParser: true, // Disallow body parsing, consume as stream
},
}; |