UNPKG

@forge-ml/rag

Version:

A RAG (Retrieval-Augmented Generation) package for Forge ML

35 lines (34 loc) 1.04 kB
import OpenAI from "openai"; class OpenAIEmbedder { openai; constructor({ apiKey }) { this.openai = new OpenAI({ apiKey }); } /** * Generate an embedding for a given text. * @param text The text to generate an embedding for. * @returns The embedding for the text. */ async generateEmbedding(text) { const embedding = await this.openai.embeddings.create({ model: "text-embedding-3-large", input: text, }); return embedding.data[0].embedding; } /** * Embed an array of chunks. * @param chunks The chunks to embed. * @returns The embeddings for the chunks. */ async embedChunks(chunks) { return Promise.all(chunks.map(async (chunk) => { return { documentId: chunk.forgeMetadata.documentId, chunkId: chunk.forgeMetadata.chunkId, embedding: await this.generateEmbedding(chunk.text), }; })); } } export default OpenAIEmbedder;