Files
ts-playground/pages/api/splitandembed.ts
T
2023-07-24 08:42:32 -07:00

59 lines
1.3 KiB
TypeScript

// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from "next";
import {
Document,
MetadataMode,
SentenceSplitter,
VectorStoreIndex,
getNodesFromDocument,
serviceContextFromDefaults,
} from "llamaindex";
type Input = {
document: string;
chunkSize?: number;
chunkOverlap?: number;
};
type Output = {
error?: string;
payload?: {
nodesWithEmbedding: {
text: string;
embedding: number[];
}[];
};
};
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Output>
) {
if (req.method !== "POST") {
res.status(405).json({ error: "Method not allowed" });
return;
}
const { document, chunkSize, chunkOverlap }: Input = req.body;
const nodes = getNodesFromDocument(
new Document({ text: document }),
new SentenceSplitter(chunkSize, chunkOverlap)
);
const nodesWithEmbeddings = await VectorStoreIndex.getNodeEmbeddingResults(
nodes,
serviceContextFromDefaults(),
true
);
res.status(200).json({
payload: {
nodesWithEmbedding: nodesWithEmbeddings.map((nodeWithEmbedding) => ({
text: nodeWithEmbedding.node.getContent(MetadataMode.NONE),
embedding: nodeWithEmbedding.embedding,
})),
},
});
}