File size: 1,522 Bytes
e82c85b
 
 
 
 
f1b879c
e82c85b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
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();
  }
}