Spaces:
Sleeping
Sleeping
File size: 1,764 Bytes
3b780fb f24ad59 3b780fb f24ad59 3b780fb f24ad59 3b780fb f24ad59 3b780fb |
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 60 61 62 |
import { NextResponse, NextRequest } from "next/server"
import queryString from "query-string"
import { BasicSearchResult, ExtendedSearchResult } from "./types"
import { extend, search } from "."
import { parsePrompt } from "../../parsers/parsePrompt"
import { parseLatentSearchMode } from "../../parsers/parseLatentSearchMode"
import { parseBasicSearchResult } from "../../parsers/parseBasicSearchResults"
export type LatentSearchMode =
| "basic"
| "extended"
// we hide/wrap the micro-service under a unified AiTube API
export async function GET(req: NextRequest, res: NextResponse) {
const qs = queryString.parseUrl(req.url || "")
const query = (qs || {}).query
const mode = parseLatentSearchMode(query?.m)
if (mode === "basic") {
const prompt = parsePrompt(query?.p)
const basicSearchResults: BasicSearchResult[] = await search({
prompt,
nbResults: 4
})
console.log(`[api/v1/search] found ${basicSearchResults.length} basic search results`)
console.log(`[api/v1/search]`, basicSearchResults)
return NextResponse.json(basicSearchResults, {
status: 200,
statusText: "OK",
})
} else if (mode === "extended") {
const basicResults = parseBasicSearchResult(query?.e)
const extendedSearchResults: ExtendedSearchResult[] = await extend({
basicResults
})
console.log(`[api/v1/search] extended ${extendedSearchResults.length} search results`)
console.log(`[api/v1/search]`, extendedSearchResults)
return NextResponse.json(extendedSearchResults, {
status: 200,
statusText: "OK",
})
} else {
/*
return NextResponse.json([], {
status: 200,
statusText: "OK",
})
*/
throw new Error(`Please specify the mode.`)
}
}
|