Spaces:
Sleeping
Sleeping
| 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, | |
| }); | |
| } | |