File size: 1,044 Bytes
73a1dae
 
5e14bd6
f1b879c
73a1dae
 
 
 
5e14bd6
 
 
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
import type { NextApiRequest, NextApiResponse } from 'next';
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
import { Chroma } from "langchain/vectorstores/chroma";
import XenovaTransformersEmbeddings from '../../embed/hf'

async function handleDocs(text: string) {
  const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 });
  const docs = await textSplitter.createDocuments([text]);
  const vectorStore = await Chroma.fromDocuments(docs, new XenovaTransformersEmbeddings(), {
    collectionName: 'docs'
  });
  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 vectorStore = await handleDocs(text);
  res.status(200).send({
    model: vectorStore,
  });
}

export const config = {
  api: {
    bodyParser: true, // Disallow body parsing, consume as stream
  },
};