prev pVectorSearch2023-06-02 parent pVectorSearch
NextJS
$ npx create-next-app@latestvecsearch-service.push an existing repository from the command line
Vercel
https://vecsearch-service.vercel.app/.Create API
src, the place to put it is <project_root>/pages/api/foo.ts.
tsimport { NextApiRequest, NextApiResponse } from "next";
const handle = async (_req: NextApiRequest, res: NextApiResponse) => {
res.status(200).json({ data: "hello world" });
};
export default handle;
API that taps other APIs
import { NextApiRequest, NextApiResponse } from "next";
import axios from "axios";
const handle = async (_req: NextApiRequest, res: NextApiResponse) => {
const otherApiUrl =
"https://scrapbox.io/api/pages/nishio/pVectorSearch2023-06-05";
try {
const response = await axios.get(otherApiUrl, {});
res.status(200).json(response.data);
} catch (error) {
res
.status(500)
.json({ error: "An error occurred while calling the other API" });
}
};
export default handle;
- OK
Beating OpenAI embed.ts
import axios from "axios";
export const embed = async (textToEmbed: string) => {
const model = "text-embedding-ada-002";
const URL = "https://api.openai.com/v1/embeddings";
// Call OpenAI Embedding API
const openAIResponse = await axios.post(
URL,
{ input: textToEmbed, model: model },
{
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
"Content-Type": "application/json",
},
}
);
const openAIEmbedding = openAIResponse.data.data[0].embedding; // assuming the embedding is in this path
return openAIEmbedding;
};
embed.ts
import { NextApiRequest, NextApiResponse } from "next";
import { embed } from "../../utils/embed";
const handle = async (req: NextApiRequest, res: NextApiResponse) => {
const textToEmbed = req.body.text;
const openAIEmbedding = await embed(textToEmbed);
res.status(200).json(openAIEmbedding);
};
export default handle;
Striking the Qdrant search.ts
import { NextApiRequest, NextApiResponse } from "next";
import { embed } from "../../utils/embed";
import { QdrantClient } from "@qdrant/js-client-rest";
const handle = async (req: NextApiRequest, res: NextApiResponse) => {
const textToEmbed = req.body.text;
const openAIEmbedding = await embed(textToEmbed);
const COLLECTION_NAME = process.env.QDRANT_COLLECTION_NAME!;
const client = new QdrantClient({
url: process.env.QDRANT_ENDPOINT,
apiKey: process.env.QDRANT_API_KEY,
});
const grdantResult = await client.search(COLLECTION_NAME, {
vector: openAIEmbedding,
limit: 3,
});
res.status(200).json(grdantResult);
};
export default handle;
Take a search string from a form :
Error: Event handlers cannot be passed to Client Component props.
<button id="search" onClick={function} children=...>
^^^^^^^^^^
If you need interactivity, consider converting part of this to a Client Component.
Formatting the response
Note for next time
Page title image
/api/pages/:projectName/:pageTitle/iconScreen for administrator
GPT next: pVectorSearch2023-06-06
This page is auto-translated from /nishio/pVectorSearch2023-06-05 using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.