import { IEmbeddings } from "../../interfaces/IEmbedding"; import { OptionsEmbeddingService } from "../../types/embedding"; import { OpenAI } from 'openai' export class OpenAIEmbeddingService implements IEmbeddings { baseURL: string apiKey: string model: string constructor(options?: OptionsEmbeddingService) { this.apiKey = options?.apiKey || process.env.OPENAI_API_KEY || "" this.baseURL = options?.baseUrl || process.env.OPENAI_API_URL || "https://api.openai.com/v1" this.model = options?.model || process.env.OPENAI_EMBEDDING_MODEL || "text-embedding-ada-002" if (!this.apiKey) { throw new Error("OpenAI API key not found") } } async getEmbeddings(text: string | string[]): Promise { const openai = new OpenAI({ apiKey: this.apiKey, baseURL: this.baseURL, }) const result = await openai.embeddings.create({ model: this.model, input: text }) return result.data.map(item => item.embedding) } }