@forge-ml/rag
Version:
A RAG (Retrieval-Augmented Generation) package for Forge ML
39 lines (38 loc) • 1.23 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const openai_1 = __importDefault(require("openai"));
class OpenAIEmbedder {
openai;
constructor({ apiKey }) {
this.openai = new openai_1.default({ 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 {
chunkId: chunk.forgeMetadata.chunkId,
embedding: await this.generateEmbedding(chunk.text),
};
}));
}
}
exports.default = OpenAIEmbedder;