File size: 1,329 Bytes
a6ae084
 
 
 
 
f61b332
 
a6ae084
 
f61b332
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6ae084
f61b332
 
 
 
 
 
 
a6ae084
f61b332
 
 
 
 
 
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
import { NextApiRequest, NextApiResponse } from 'next'
import { client, generateHash } from 'gradio-chatbot'
import { QdrantClient } from '@qdrant/js-client-rest';

const qclient = new QdrantClient({
  url: process.env.QDRANT_URL,
  apiKey: process.env.QDRANT_API_KEY,
});

export async function search(text: string, limit = 5, score_threshold = 0.78) {
  const bot = await client('justest-embeddings-api.hf.space', {
    session_hash: generateHash(),
  });
  const response = await bot.predict(0, [text]) as any;
  const [embeddings] = response.data || [];
  if (embeddings.length) {
    console.log('start search embedding', text, limit, score_threshold);
    const searchResults = await qclient.search('mdn-docs', {
      vector: JSON.parse(embeddings),
      params: {
        hnsw_ef: 128,
        exact: true
      },
      score_threshold,
      limit,
    });
    return searchResults;
  }
}

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  const text = String(req.query.text);
  const limit = String(req.query.limit || 5);
  const searchResults = await search(text, parseInt(limit, 10))
  if (searchResults) {
    return res.status(200).json({
      data: searchResults,
      success: true,
    });
  }
  res.status(404).json({
    message: 'not found',
    success: false,
  });
}