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/になるのかAPIを作る
srcがないときの置き場所は、<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を叩くAPI
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
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;
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;
フォームから検索文字列を取る :
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.
レスポンスを整形する
次回のためのメモ
ページタイトル画像
/api/pages/:projectName/:pageTitle/icon管理者用の画面
GPT next: pVectorSearch2023-06-06